组件接口职责优化

This commit is contained in:
lxp
2026-10-12 11:56:02 +08:00
parent 13bdd2538d
commit 9e3bc846e8
9 changed files with 402 additions and 196 deletions
@@ -203,6 +203,72 @@ class AdminApiTest {
assertThat(storage.objectExists(created.path("urdfMainObjectKey").asText())).isTrue();
}
@Test
void replacingBothModelsIsAtomicWhenOneUploadedFileIsMissing() throws Exception {
JsonNode created = create(uploaded());
long id = created.path("id").asLong();
JsonNode session = sign(Map.of("componentId", id));
String step = session.path("step").path("keyPrefix").asText() + "replacement.STEP";
String prefix = session.path("urdf").path("keyPrefix").asText();
String main = prefix + "robot.urdf";
String mesh = prefix + "meshes/new.stl";
Map<String,Object> form = update(1);
form.put("uploadSessionToken", session.path("uploadSessionToken").asText());
form.put("stepObjectKey", step);
form.put("urdfObjectKeys", List.of(main, mesh));
storage.simulateDirectUpload(step, "application/octet-stream", new byte[]{1,2});
storage.simulateDirectUpload(main, "application/xml", new byte[]{3});
expectUpdateError(id, form, 422, "COMPONENT_UPLOAD_OBJECT_MISSING");
JsonNode afterFailure = data(mvc.perform(get("/admin-api/components/{id}", id))
.andExpect(status().isOk()).andReturn());
assertThat(afterFailure).isEqualTo(created);
storage.simulateDirectUpload(mesh, "application/octet-stream", new byte[]{4,5,6});
JsonNode changed = data(mvc.perform(put("/admin-api/components/{id}", id)
.contentType(MediaType.APPLICATION_JSON).content(json.writeValueAsString(form)))
.andExpect(status().isOk()).andReturn());
assertThat(changed.path("componentCode")).isEqualTo(created.path("componentCode"));
assertThat(changed.path("version").asLong()).isEqualTo(2);
assertThat(changed.path("stepObjectKey").asText()).isEqualTo(step);
assertThat(changed.path("stepUrl").asText()).endsWith("replacement.STEP");
assertThat(changed.path("urdfPrefix").asText()).isEqualTo(prefix);
assertThat(changed.path("urdfMainObjectKey").asText()).isEqualTo(main);
assertThat(changed.path("urdfFiles").get(1).path("relativePath").asText()).isEqualTo("meshes/new.stl");
assertThat(changed.path("urdfFiles").get(1).path("sizeBytes").asLong()).isEqualTo(3);
assertThat(storage.objectExists(created.path("stepObjectKey").asText())).isTrue();
assertThat(storage.objectExists(created.path("urdfMainObjectKey").asText())).isTrue();
}
@Test
void nullResourceFieldsPreserveModelsAndExistingValidationOrderIsRetained() throws Exception {
JsonNode created = create(uploaded());
long id = created.path("id").asLong();
JsonNode session = sign(Map.of("componentId", id));
Map<String,Object> form = update(1);
form.put("stepObjectKey", null);
form.put("urdfObjectKeys", null);
form.put("uploadSessionToken", session.path("uploadSessionToken").asText());
expectUpdateError(id, form, 422, "INVALID_COMPONENT_UPLOAD_OBJECT");
form.put("componentCode", "CW-" + "F".repeat(32));
expectUpdateError(id, form, 422, "COMPONENT_CODE_IMMUTABLE");
form.remove("componentCode");
form.remove("uploadSessionToken");
JsonNode changed = data(mvc.perform(put("/admin-api/components/{id}", id)
.contentType(MediaType.APPLICATION_JSON).content(json.writeValueAsString(form)))
.andExpect(status().isOk()).andReturn());
assertThat(changed.path("version").asLong()).isEqualTo(2);
assertThat(changed.path("stepObjectKey")).isEqualTo(created.path("stepObjectKey"));
assertThat(changed.path("urdfPrefix")).isEqualTo(created.path("urdfPrefix"));
assertThat(changed.path("urdfMainObjectKey")).isEqualTo(created.path("urdfMainObjectKey"));
assertThat(changed.path("urdfFiles")).isEqualTo(created.path("urdfFiles"));
form.put("uploadSessionToken", "invalid-upload-token");
form.put("stepObjectKey", session.path("step").path("keyPrefix").asText() + "missing.step");
expectUpdateError(id, form, 409, "COMPONENT_REVISION_CONFLICT");
}
@Test
void newAndExistingSessionsCannotBeUsedForAnotherComponent() throws Exception {
JsonNode first = create(uploaded());