组件新增状态字段,禁用状态不参与拼装

This commit is contained in:
lxp
2026-10-20 18:07:55 +08:00
parent 5d94bc7871
commit 7eef6e3994
21 changed files with 265 additions and 16 deletions
@@ -79,6 +79,7 @@ class DemandFlowApiTest {
@Autowired private com.linkerbot.cadworld.service.demand.DemandAttachmentStore attachments;
@MockitoBean private DemandModelClient model;
@MockitoSpyBean private DemandMatchService matches;
@MockitoSpyBean private com.linkerbot.cadworld.service.matching.RobotMatchingService matching;
@Autowired @org.springframework.beans.factory.annotation.Qualifier("requestMappingHandlerMapping")
private RequestMappingHandlerMapping mappings;
@@ -95,6 +96,7 @@ class DemandFlowApiTest {
CREATE TABLE IF NOT EXISTS component (
id BIGINT AUTO_INCREMENT PRIMARY KEY,component_code VARCHAR(64) NOT NULL UNIQUE,
component_name VARCHAR(128) NOT NULL DEFAULT '',category VARCHAR(64) NOT NULL DEFAULT '',
status VARCHAR(16) NOT NULL DEFAULT 'ENABLED' CHECK (status IN ('ENABLED','DISABLED')),
description CLOB NOT NULL DEFAULT '',material VARCHAR(128) NOT NULL DEFAULT '',
mass_kg DECIMAL(18,6) NOT NULL DEFAULT 0,degrees_of_freedom INT NOT NULL DEFAULT 0,
rotation_axis CHAR(1) DEFAULT NULL CHECK (rotation_axis IN ('X','Y','Z')),
@@ -668,6 +670,58 @@ class DemandFlowApiTest {
assertThat(refreshed.path("alternatives").findValuesAsText("componentCode")).doesNotContain("CW-ARM");
}
@Test void disablingComponentsOnlyAffectsNewMessagesAndPreservesHistoricalResults() throws Exception {
components(true, 20);
Session session = create();
JsonNode completed = submit(session, "m1", TEXT, List.of());
String analysisId = completed.path("analysisId").asText();
JsonNode original = completed.path("match");
assertThat(original.path("components").findValuesAsText("componentCode")).containsExactly("CW-TORSO", "CW-ARM");
assertThat(original.path("alternatives").findValuesAsText("componentCode")).contains("CW-ARM-ALT");
jdbc.update("UPDATE component SET status='DISABLED',version=version+1 WHERE category='ARM_R'");
JsonNode historical = data(mvc.perform(get("/api/robot-analyses/" + analysisId)
.header("Authorization", "Bearer " + session.token())).andExpect(status().isOk()).andReturn());
assertThat(historical.path("match")).isEqualTo(original);
mvc.perform(get("/api/components/CW-ARM")).andExpect(status().isOk())
.andExpect(jsonPath("$.data.componentCode").value("CW-ARM"))
.andExpect(jsonPath("$.data.id").doesNotExist());
JsonNode partial = submit(session, "m2", TEXT, List.of()).path("match");
assertThat(partial.path("status").asText()).isEqualTo("partial");
assertThat(partial.path("components").findValuesAsText("componentCode")).containsExactly("CW-TORSO");
assertThat(partial.path("alternatives").findValuesAsText("componentCode")).doesNotContain("CW-ARM", "CW-ARM-ALT");
jdbc.update("UPDATE component SET status='ENABLED',version=version+1 WHERE component_code='CW-ARM-ALT'");
JsonNode restored = submit(session, "m3", TEXT, List.of()).path("match");
assertThat(restored.path("status").asText()).isEqualTo("complete");
assertThat(restored.path("components").findValuesAsText("componentCode")).containsExactly("CW-TORSO", "CW-ARM-ALT");
assertThat(restored.path("alternatives").findValuesAsText("componentCode")).doesNotContain("CW-ARM");
jdbc.update("UPDATE component SET status='DISABLED',version=version+1");
JsonNode empty = submit(session, "m4", TEXT, List.of()).path("match");
assertThat(empty.path("status").asText()).isEqualTo("no_match");
assertThat(empty.path("components")).isEmpty();
assertThat(empty.path("alternatives")).isEmpty();
}
@Test void disablingComponentBeforeResourceLookupFailsWithoutSavingMatch() throws Exception {
components(true, 20);
doAnswer(invocation -> {
Object response = invocation.callRealMethod();
// 保持版本不变,独立验证资源回查阶段的状态校验。
jdbc.update("UPDATE component SET status='DISABLED' WHERE component_code='CW-ARM'");
return response;
}).when(matching).matchConfirmed(org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.anySet(),
org.mockito.ArgumentMatchers.anyList(), org.mockito.ArgumentMatchers.anySet());
Session session = create();
MvcResult pending = message(session, "m-disabled-during-match", TEXT, List.of());
pending.getAsyncResult(5000);
mvc.perform(asyncDispatch(pending)).andExpect(status().isOk());
assertThat(pending.getResponse().getContentAsString()).contains("event:analysis.failed", "COMPONENT_REVISION_CONFLICT")
.doesNotContain("event:analysis.completed");
assertThat(jdbc.queryForObject("SELECT COUNT(*) FROM robot_demand_match", Integer.class)).isZero();
}
@Test void prefersRequirementsOnlyAfterHardRequirementsAndDoesNotRequireDegradationForPreferences() throws Exception {
components(true, 20);
jdbc.update("UPDATE component SET mass_kg=8,torque_nm=40 WHERE component_code='CW-ARM-ALT'");
@@ -6,6 +6,7 @@ import ch.qos.logback.core.read.ListAppender;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkerbot.cadworld.common.web.RequestIdFilter;
import com.linkerbot.cadworld.common.exception.BizException;
import com.linkerbot.cadworld.config.MatchingProperties;
import com.linkerbot.cadworld.dto.matching.RobotMatchRequest;
import com.linkerbot.cadworld.dto.matching.RobotMatchResponse;
@@ -36,11 +37,13 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@@ -77,6 +80,7 @@ class MatchingApiTest {
id BIGINT AUTO_INCREMENT PRIMARY KEY,
component_code VARCHAR(64) NOT NULL DEFAULT '' UNIQUE,
component_name VARCHAR(128) NOT NULL DEFAULT '', category VARCHAR(64) NOT NULL DEFAULT '',
status VARCHAR(16) NOT NULL DEFAULT 'ENABLED' CHECK (status IN ('ENABLED','DISABLED')),
description CLOB NOT NULL DEFAULT '', material VARCHAR(128) NOT NULL DEFAULT '',
mass_kg DECIMAL(18,6) NOT NULL DEFAULT 0, degrees_of_freedom INT NOT NULL DEFAULT 0,
rotation_axis CHAR(1) DEFAULT NULL CHECK (rotation_axis IN ('X','Y','Z')),
@@ -963,6 +967,42 @@ class MatchingApiTest {
assertThat(component.get("urdfInfo").isContainerNode()).isTrue();
}
@Test
void disabledComponentsAreExcludedFromNewMatchesAndAlternativesButRemainReadable() throws Exception {
save("CW-A", "TORSO_0", 0, "1", "0", "");
save("CW-B", "TORSO_0", 0, "1", "0", "");
save("CW-C", "TORSO_0", 0, "1", "0", "");
JsonNode original = match(request("TORSO_0"));
assertThat(part(original, "TORSO_0").path("componentCode").asText()).isEqualTo("CW-A");
assertThat(alternatives(original, "TORSO_0").findValuesAsText("componentCode")).contains("CW-B", "CW-C");
jdbc.update("UPDATE component SET status='DISABLED',version=version+1 WHERE component_code IN ('CW-A','CW-C')");
JsonNode refreshed = match(request("TORSO_0"));
assertThat(part(refreshed, "TORSO_0").path("componentCode").asText()).isEqualTo("CW-B");
assertThat(refreshed.path("alternatives").findValuesAsText("componentCode")).doesNotContain("CW-A", "CW-C");
mvc.perform(get("/api/components/CW-A")).andExpect(status().isOk())
.andExpect(jsonPath("$.data.componentCode").value("CW-A"))
.andExpect(jsonPath("$.data.id").doesNotExist());
jdbc.update("UPDATE component SET status='DISABLED' WHERE component_code='CW-B'");
JsonNode empty = match(request("TORSO_0"));
assertThat(empty.path("status").asText()).isEqualTo("NO_MATCH");
assertThat(empty.path("parts")).isEmpty();
assertThat(empty.path("alternatives")).isEmpty();
jdbc.update("UPDATE component SET status='ENABLED',version=version+1 WHERE component_code='CW-A'");
assertThat(part(match(request("TORSO_0")), "TORSO_0").path("componentCode").asText()).isEqualTo("CW-A");
}
@Test
void disablingCandidateBeforeResultLoadReturnsRevisionConflict() throws Exception {
save("CW-A", "TORSO_0", 0, "1", "0", "");
Map<String, ?> enabled = ReflectionTestUtils.invokeMethod(components, "load", List.of("CW-A"));
assertThat(enabled).containsKey("CW-A");
jdbc.update("UPDATE component SET status='DISABLED' WHERE component_code='CW-A'");
assertThatThrownBy(() -> ReflectionTestUtils.invokeMethod(components, "load", List.of("CW-A")))
.isInstanceOf(BizException.class).extracting("code").isEqualTo("COMPONENT_REVISION_CONFLICT");
}
private JsonNode metadata(String root, String... ports) {
List<Map<String, Object>> frames = new ArrayList<>();
for (int index = 0; index < ports.length; index++) {
@@ -146,6 +146,20 @@ class ProjectApiTest {
assertThat(detail.path("snapshot")).isEqualTo(created.path("snapshot"));
}
@Test
void disabledComponentDoesNotChangeSavedProjectSnapshot() throws Exception {
// 项目只依赖独立快照,此表仅模拟组件状态变化,不参与项目查询。
jdbc.execute("CREATE TABLE IF NOT EXISTS component (component_code VARCHAR(64) PRIMARY KEY,status VARCHAR(16) NOT NULL)");
jdbc.update("DELETE FROM component");
jdbc.update("INSERT INTO component (component_code,status) VALUES ('C-TORSO','ENABLED')");
JsonNode created = create(source(false));
jdbc.update("UPDATE component SET status='DISABLED' WHERE component_code='C-TORSO'");
JsonNode detail = data(mvc.perform(get("/api/projects/" + created.path("projectCode").asText())
.header("Authorization", bearer(token))).andExpect(status().isOk()).andReturn());
assertThat(detail.path("snapshot")).isEqualTo(created.path("snapshot"));
assertThat(detail.findValues("id")).isEmpty();
}
@Test
void privateOperationsRequireLoginAndOwnership() throws Exception {
String code = create(source(false)).path("projectCode").asText();