项目整机拼装与资源文件上传
This commit is contained in:
+118
@@ -371,6 +371,124 @@ class AdminWorkflowTest {
|
||||
oss.setEnabled(true);
|
||||
}
|
||||
|
||||
private JsonNode resourceSignature(String code, String type, List<Map<String, Object>> files) throws Exception {
|
||||
MvcResult result = mvc.perform(signed(body(post("/admin-api/projects/" + code + "/resources/" + type + "/signature"),
|
||||
Map.of("files", files)))).andReturn();
|
||||
assertThat(result.getResponse().getStatus()).isEqualTo(200);
|
||||
assertThat(result.getResponse().getHeader("Cache-Control")).isEqualTo("no-store");
|
||||
return json.readTree(result.getResponse().getContentAsString()).path("data");
|
||||
}
|
||||
|
||||
private void storeResourceFiles(JsonNode signature, List<Map<String, Object>> files) {
|
||||
for (int index = 0; index < files.size(); index++) {
|
||||
long size = ((Number) files.get(index).get("sizeBytes")).longValue();
|
||||
ossObjects.objects.put(signature.path("files").get(index).path("objectKey").asText(),
|
||||
new OssObjectStore.Metadata(size, "resource-etag-" + index));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void fourResourceTypesUploadReplaceAndClearIndependentlyWhileKeepingOssObjects() throws Exception {
|
||||
String code = createProject();
|
||||
String endpoint = "/admin-api/projects/" + code;
|
||||
JsonNode empty = request(signed(get(endpoint)), 200).path("data").path("project");
|
||||
assertThat(empty.path("modelGlbUrl").isNull()).isTrue();
|
||||
assertThat(empty.path("modelPartsFiles").isEmpty()).isTrue();
|
||||
assertThat(empty.path("walkingResourcesFiles").isEmpty()).isTrue();
|
||||
enableOss();
|
||||
Map<String, List<Map<String, Object>>> resources = new LinkedHashMap<>();
|
||||
resources.put("model-glb", List.of(Map.of("relativePath", "整机.GLb", "sizeBytes", 52428801L)));
|
||||
resources.put("model-parts", List.of(Map.of("relativePath", "左/a.glb", "sizeBytes", 1L),
|
||||
Map.of("relativePath", "右/a.glb", "sizeBytes", 2L)));
|
||||
resources.put("assembly-json", List.of(Map.of("relativePath", "steps.JSON", "sizeBytes", 8L)));
|
||||
resources.put("walking-resources", List.of(Map.of("relativePath", "策略/a.bin", "sizeBytes", 0L)));
|
||||
for (Map.Entry<String, List<Map<String, Object>>> resource : resources.entrySet()) {
|
||||
JsonNode signature = resourceSignature(code, resource.getKey(), resource.getValue());
|
||||
storeResourceFiles(signature, resource.getValue());
|
||||
request(signed(body(put(endpoint + "/resources/" + resource.getKey()),
|
||||
Map.of("batchCode", signature.path("batchCode").asText(), "files", resource.getValue()))), 200);
|
||||
}
|
||||
JsonNode bound = request(signed(get(endpoint)), 200).path("data").path("project");
|
||||
assertThat(bound.path("modelGlbUrl").asText()).endsWith("/%E6%95%B4%E6%9C%BA.GLb");
|
||||
assertThat(bound.path("modelPartsFiles").size()).isEqualTo(2);
|
||||
assertThat(bound.path("modelPartsFiles").get(0).path("url").asText()).endsWith("/%E5%B7%A6/a.glb");
|
||||
assertThat(bound.path("walkingResourcesFiles").get(0).path("relativePath").asText()).isEqualTo("策略/a.bin");
|
||||
assertThat(bound.path("assemblyJsonUrl").asText()).endsWith("/steps.JSON");
|
||||
JsonNode listing = request(signed(get("/admin-api/projects")), 200).path("data").path("items").get(0).path("project");
|
||||
assertThat(listing.has("modelPartsFiles")).isFalse();
|
||||
assertThat(listing.has("walkingResourcesFiles")).isFalse();
|
||||
assertThat(jdbc.queryForObject("SELECT model_glb_object_key FROM project WHERE project_code=?", String.class, code))
|
||||
.startsWith("/project-resources/" + code + "/model-glb/");
|
||||
JsonNode replacement = resourceSignature(code, "model-glb", resources.get("model-glb"));
|
||||
storeResourceFiles(replacement, resources.get("model-glb"));
|
||||
JsonNode updated = request(signed(body(put(endpoint + "/resources/model-glb"),
|
||||
Map.of("batchCode", replacement.path("batchCode").asText(), "files", resources.get("model-glb")))), 200).path("data");
|
||||
assertThat(updated.path("modelGlbUrl")).isNotEqualTo(bound.path("modelGlbUrl"));
|
||||
assertThat(updated.path("modelPartsFiles")).isEqualTo(bound.path("modelPartsFiles"));
|
||||
int objectCount = ossObjects.objects.size();
|
||||
oss.setEnabled(false);
|
||||
for (String type : resources.keySet()) {
|
||||
request(signed(delete(endpoint + "/resources/" + type)), 200);
|
||||
request(signed(delete(endpoint + "/resources/" + type)), 200);
|
||||
}
|
||||
JsonNode cleared = request(signed(get(endpoint)), 200).path("data").path("project");
|
||||
assertThat(cleared.path("modelGlbUrl").isNull()).isTrue();
|
||||
assertThat(cleared.path("assemblyJsonUrl").isNull()).isTrue();
|
||||
assertThat(cleared.path("modelPartsFiles").isEmpty()).isTrue();
|
||||
assertThat(cleared.path("walkingResourcesFiles").isEmpty()).isTrue();
|
||||
assertThat(ossObjects.objects).hasSize(objectCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceConfirmationFailuresPreserveBindingAndCannotUseAnotherProjectsObjects() throws Exception {
|
||||
String code = createProject();
|
||||
String endpoint = "/admin-api/projects/" + code + "/resources/model-glb";
|
||||
List<Map<String, Object>> files = List.of(Map.of("relativePath", "a.glb", "sizeBytes", 10L));
|
||||
request(body(post(endpoint + "/signature"), Map.of("files", files)), 401);
|
||||
String ordinaryToken = tokens.issue(ordinary.getId(), ordinary.getSessionVersion());
|
||||
request(body(post(endpoint + "/signature"), Map.of("files", files))
|
||||
.header("Authorization", "Bearer " + ordinaryToken), 403);
|
||||
request(signed(body(post(endpoint + "/signature"), Map.of("files", files))), 503);
|
||||
enableOss();
|
||||
JsonNode signature = resourceSignature(code, "model-glb", files);
|
||||
Map<String, Object> confirmation = Map.of("batchCode", signature.path("batchCode").asText(), "files", files);
|
||||
assertThat(request(signed(body(put(endpoint), confirmation)), 400).path("code").asText()).isEqualTo("RESOURCE_FILE_MISSING");
|
||||
storeResourceFiles(signature, files);
|
||||
JsonNode bound = request(signed(body(put(endpoint), confirmation)), 200).path("data");
|
||||
JsonNode next = resourceSignature(code, "model-glb", files);
|
||||
String nextKey = next.path("files").get(0).path("objectKey").asText();
|
||||
ossObjects.objects.put(nextKey, new OssObjectStore.Metadata(11L, "wrong-size"));
|
||||
assertThat(request(signed(body(put(endpoint), Map.of("batchCode", next.path("batchCode").asText(), "files", files))), 400)
|
||||
.path("code").asText()).isEqualTo("RESOURCE_FILE_SIZE_MISMATCH");
|
||||
assertThat(request(signed(get("/admin-api/projects/" + code)), 200).path("data").path("project").path("modelGlbUrl"))
|
||||
.isEqualTo(bound.path("modelGlbUrl"));
|
||||
String another = createProject();
|
||||
request(signed(body(put("/admin-api/projects/" + another + "/resources/model-glb"), confirmation)), 400);
|
||||
request(signed(body(post(endpoint + "/signature"), Map.of("files", List.of(Map.of("relativePath", "a.txt", "sizeBytes", 1))))), 400);
|
||||
request(signed(body(post(endpoint + "/signature"), Map.of("files", List.of()))), 400);
|
||||
request(signed(body(post(endpoint + "/signature"), Map.of("files", List.of(Map.of("relativePath", "../a.glb", "sizeBytes", 1))))), 400);
|
||||
request(signed(body(post(endpoint + "/signature"), Map.of("files", List.of(Map.of("relativePath", "a.glb", "sizeBytes", 5368709121L))))), 413);
|
||||
request(signed(body(post("/admin-api/projects/" + code + "/resources/unknown/signature"), Map.of("files", files))), 400);
|
||||
request(signed(delete("/admin-api/projects/" + code)), 200);
|
||||
request(signed(body(put(endpoint), confirmation)), 404);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceManifestsLargerThanOrdinaryJsonBodyLimitAreAccepted() throws Exception {
|
||||
String code = createProject();
|
||||
enableOss();
|
||||
List<Map<String, Object>> files = new ArrayList<>();
|
||||
for (int index = 0; index < 1200; index++)
|
||||
files.add(Map.of("relativePath", "x".repeat(220) + index + ".glb", "sizeBytes", 300000000L));
|
||||
assertThat(json.writeValueAsBytes(Map.of("files", files)).length).isGreaterThan(262144);
|
||||
JsonNode signature = resourceSignature(code, "model-parts", files);
|
||||
assertThat(signature.path("files").size()).isEqualTo(1200);
|
||||
storeResourceFiles(signature, files);
|
||||
JsonNode bound = request(signed(body(put("/admin-api/projects/" + code + "/resources/model-parts"),
|
||||
Map.of("batchCode", signature.path("batchCode").asText(), "files", files))), 200).path("data");
|
||||
assertThat(bound.path("modelPartsFiles").size()).isEqualTo(1200);
|
||||
}
|
||||
|
||||
private List<Map<String, Object>> urdfFiles() {
|
||||
return List.of(
|
||||
Map.of("relativePath", "urdf/机械臂.URDF", "sizeBytes", 12),
|
||||
|
||||
Reference in New Issue
Block a user