package com.zy.common.service; import com.zy.asrs.entity.LocDetl; import com.zy.asrs.entity.LocMast; import com.zy.asrs.entity.RowLastno; import com.zy.asrs.entity.RowLastnoType; import com.zy.asrs.entity.result.FindLocNoAttributeVo; import com.zy.asrs.service.BasCrnDepthRuleService; import com.zy.asrs.service.LocDetlService; import com.zy.asrs.service.LocMastService; import com.zy.common.model.CrnDepthRuleProfile; import com.zy.common.entity.Parameter; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.util.ReflectionTestUtils; import java.lang.reflect.Constructor; import java.util.Arrays; import java.util.LinkedHashMap; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class CommonServiceDoubleExtensionSameGoodsTest { @Mock private BasCrnDepthRuleService basCrnDepthRuleService; @Mock private LocMastService locMastService; @Mock private LocDetlService locDetlService; private CommonService commonService; private Parameter originalParameter; @BeforeEach void setUp() { commonService = new CommonService(); ReflectionTestUtils.setField(commonService, "basCrnDepthRuleService", basCrnDepthRuleService); ReflectionTestUtils.setField(commonService, "locMastService", locMastService); ReflectionTestUtils.setField(commonService, "locDetlService", locDetlService); originalParameter = (Parameter) ReflectionTestUtils.getField(Parameter.class, "instance"); ReflectionTestUtils.setField(Parameter.class, "instance", buildParameter()); } @org.junit.jupiter.api.AfterEach void tearDown() { ReflectionTestUtils.setField(Parameter.class, "instance", originalParameter); } @Test void doubleExtension_shouldReturnShallowLocWhenSameStandby1DeepLocHasEmptyPartner() { RowLastno rowLastno = buildRowLastno(); CrnDepthRuleProfile profile = buildDoubleExtensionProfile(); when(basCrnDepthRuleService.resolveProfile(rowLastno, 1, 2)).thenReturn(profile); LocMast deepLoc = buildLoc("D-001", 1, 1, 1, "F", 1); LocMast shallowLoc = buildLoc("S-001", 2, 1, 1, "O", 1); LocDetl deepDetl = buildDetl("D-001", "PO-001"); when(locMastService.selectList(any())).thenReturn(Arrays.asList(deepLoc)); when(locDetlService.selectList(any())).thenReturn(Arrays.asList(deepDetl)); when(locMastService.selectOne(any())).thenReturn(shallowLoc); LocMast result = ReflectionTestUtils.invokeMethod(commonService, "findConfiguredEmptyLocForCrn", rowLastno, buildRowLastnoType(), 1, 2, null, buildAttributeVo("PO-001"), false); assertEquals("S-001", result.getLocNo()); assertEquals(Integer.valueOf(2), result.getRow1()); assertEquals("O", result.getLocSts()); } @Test void doubleExtension_shouldFallBackWhenSameStandby1DeepLocHasNoEmptyShallowPartner() { RowLastno rowLastno = buildRowLastno(); CrnDepthRuleProfile profile = buildDoubleExtensionProfile(); when(basCrnDepthRuleService.resolveProfile(rowLastno, 1, 2)).thenReturn(profile); LocMast sameGoodsDeepLoc = buildLoc("D-001", 1, 1, 1, "F", 1); LocMast genericShallowLoc = buildLoc("S-002", 2, 2, 1, "O", 1); LocMast genericDeepFallbackLoc = buildLoc("D-002", 1, 2, 1, "F", 1); LocDetl deepDetl = buildDetl("D-001", "PO-001"); when(locMastService.selectList(any())).thenReturn(Arrays.asList(sameGoodsDeepLoc), Arrays.asList(genericShallowLoc)); when(locDetlService.selectList(any())).thenReturn(Arrays.asList(deepDetl)); when(locMastService.selectOne(any())).thenReturn(null, null, genericDeepFallbackLoc); LocMast result = ReflectionTestUtils.invokeMethod(commonService, "findConfiguredEmptyLocForCrn", rowLastno, buildRowLastnoType(), 1, 2, null, buildAttributeVo("PO-001"), false); assertEquals("S-002", result.getLocNo()); assertEquals(Integer.valueOf(2), result.getRow1()); assertEquals("O", result.getLocSts()); } @Test void singleExtension_shouldNotApplySameGoodsPrecheck() { RowLastno rowLastno = buildRowLastno(); CrnDepthRuleProfile profile = buildSingleExtensionProfile(); when(basCrnDepthRuleService.resolveProfile(rowLastno, 1, 2)).thenReturn(profile); LocMast openLoc = buildLoc("S-010", 2, 1, 1, "O", 1); when(locMastService.selectList(any())).thenReturn(Arrays.asList(openLoc)); LocMast result = ReflectionTestUtils.invokeMethod(commonService, "findConfiguredEmptyLocForCrn", rowLastno, buildRowLastnoType(), 1, 2, null, buildAttributeVo("PO-001"), false); assertEquals("S-010", result.getLocNo()); verifyNoInteractions(locDetlService); } private RowLastno buildRowLastno() { RowLastno rowLastno = new RowLastno(); rowLastno.setWhsType(1); rowLastno.setCurrentRow(2); return rowLastno; } private RowLastnoType buildRowLastnoType() { RowLastnoType rowLastnoType = new RowLastnoType(); rowLastnoType.setType(1); return rowLastnoType; } private FindLocNoAttributeVo buildAttributeVo(String standby1) { FindLocNoAttributeVo attributeVo = new FindLocNoAttributeVo(); attributeVo.setStandby1(standby1); return attributeVo; } private CrnDepthRuleProfile buildDoubleExtensionProfile() { CrnDepthRuleProfile profile = new CrnDepthRuleProfile(); profile.setLayoutType(2); profile.setSearchRows(Arrays.asList(2, 1)); profile.setShallowRows(Arrays.asList(2)); profile.setDeepRows(Arrays.asList(1)); LinkedHashMap shallowToDeep = new LinkedHashMap(); shallowToDeep.put(2, 1); LinkedHashMap deepToShallow = new LinkedHashMap(); deepToShallow.put(1, 2); profile.setShallowToDeepRow(shallowToDeep); profile.setDeepToShallowRow(deepToShallow); return profile; } private CrnDepthRuleProfile buildSingleExtensionProfile() { CrnDepthRuleProfile profile = new CrnDepthRuleProfile(); profile.setLayoutType(1); profile.setSearchRows(Arrays.asList(2)); profile.setShallowRows(Arrays.asList(2)); profile.setDeepRows(null); profile.setShallowToDeepRow(new LinkedHashMap()); profile.setDeepToShallowRow(new LinkedHashMap()); return profile; } private LocMast buildLoc(String locNo, Integer row, Integer bay, Integer lev, String status, Integer crnNo) { LocMast locMast = new LocMast(); locMast.setLocNo(locNo); locMast.setRow1(row); locMast.setBay1(bay); locMast.setLev1(lev); locMast.setLocSts(status); locMast.setCrnNo(crnNo); return locMast; } private LocDetl buildDetl(String locNo, String standby1) { LocDetl locDetl = new LocDetl(); locDetl.setLocNo(locNo); locDetl.setStandby1(standby1); return locDetl; } private Parameter buildParameter() { try { Constructor constructor = Parameter.class.getDeclaredConstructor(); constructor.setAccessible(true); Parameter parameter = constructor.newInstance(); parameter.setHighFreqFrontBayCount("0"); return parameter; } catch (Exception e) { throw new IllegalStateException("Failed to initialize Parameter test instance", e); } } }