package com.pgf.mqspring.service.impl; import static org.junit.jupiter.api.Assertions.*; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.util.HashSet; import java.util.Objects; import java.util.Set; import javax.xml.bind.DatatypeConverter; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.util.ReflectionTestUtils; import com.pgf.mqspring.component.MqSpringMessageSourceComponent; import com.pgf.mqspring.component.TelegramInfoComponent; import com.pgf.mqspring.component.impl.CharacterCodeConverterComponentImpl; import com.pgf.mqspring.component.impl.TelegramInfoComponentImpl; import com.pgf.mqspring.model.ReplyResponseModel; import lombok.extern.slf4j.Slf4j; @Slf4j @ExtendWith(SpringExtension.class) class MqReceiveServiceImplTest02 { @Mock MqSpringMessageSourceComponent messageSource; @InjectMocks CharacterCodeConverterComponentImpl characterCodeConverter; @InjectMocks MqReceiveServiceImpl mqReceiveService; @BeforeEach public void beforeEacn() throws IOException { MockitoAnnotations.openMocks(this); String csvPath = "C:\\pleiades\\2022-12\\workspace_PGF\\mq-spring\\src\\test\\resources\\com\\pgf\\mqspring\\service\\impl\\MqReceiveServiceImplTest_02\\ibm_telegram_info.csv"; Path path = Paths.get(csvPath); try (MockedStatic mockedStatic = Mockito.mockStatic(Paths.class)) { mockedStatic.when(() -> Paths.get("/ibm_telegram_info.csv")).thenReturn(path).toString(); TelegramInfoComponent telegramInfoComponent = new TelegramInfoComponentImpl(messageSource, "", ""); ReflectionTestUtils.setField(characterCodeConverter, "telegramInfoComponent", telegramInfoComponent); ReflectionTestUtils.setField(mqReceiveService, "characterCodeConverter", characterCodeConverter); } } @Test void editReplyData01() throws InterruptedException { // バイト配列の受信電文のサンプルデータ String body = "c3c9c84000000002000000b40000000000000000c3e2d8c3c2c4c3d600000000000000000000000000000000000001110002bf2000000002000001d4000000b4000000000000000000010000016800020000000200000000404040405c5c5c5c5c5c5c5c40404040404040404040404040404040c3c9f1f840404040c7c5f0f240404040404040404040404040404040c7c5f0f240404040404040404040404040404040000000000000000000000003000000000000011ef1f8f0f4d6404040f0f0f0f0c5404040d5404040e8404040d5404040d5404040d5404040d5404040ffffffff00000000d4c7c5f0f2f0f040d4c7c5f0f2f0f040d5404040000000c600000058000000000000011e000000000000000000000000000000000040404040404040400000000000f1f061f1f761f2f200000000000000000000000000004040404040404040404040404040404040404040404040404040404040404040404040404040404040400000000000f1f07af5f97af5f2ffffc100004040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; byte[] bytes = DatatypeConverter.parseHexBinary(body); ReplyResponseModel response = mqReceiveService.editReplyData(bytes); assertEquals(true, checkReplyResponseModel(response)); } @Test void editReplyData02() { try { mqReceiveService.editReplyData(null); assertTrue(false); } catch (NullPointerException e) { assertTrue(true); } } @Test void editReplyData03() { byte[] bytes = { 'C', 'I', 'H', 2 }; try { mqReceiveService.editReplyData(bytes); assertTrue(false); } catch (ArrayIndexOutOfBoundsException e) { assertTrue(true); } } @Test void editReplyData04() { // バイト配列の受信電文のサンプルデータ String body = "c3c9c84000000002000000b40000000000000000c3e2d8c3c2c4c3d600000000000000000000000000000000000001110002bf2000000002000001d4000000b4000000000000000000010000016800020000000200000000404040405c5c5c5c5c5c5c5c40404040404040404040404040404040c3c9f1f840404040c7c5f0f240404040404040404040404040404040c7c5f0f240404040404040404040404040404040000000000000000000000003000000000000011ef1f8f0f4d6404040f0f0f0f0c5404040d5404040e8404040d5404040d5404040d5404040d5404040ffffffff00000000d4c7c5f0f2f0f040d4c7c5f0f2f0f040d5404040000000c600000058000000000000011e000000000000000000000000000000000040404040404040400000000000f1f061f1f761f2f200000000000000000000000000004040404040404040404040404040404040404040404040404040404040404040404040404040404040400000000000f1f07af5f97af5f2ffffc100004040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; byte[] bytes = DatatypeConverter.parseHexBinary(body); bytes = Arrays.copyOfRange(bytes, 0, 184); ReplyResponseModel response = mqReceiveService.editReplyData(bytes); assertEquals(true, checkReplyResponseModel(response)); } @Test void editReplyData05() { // バイト配列の受信電文のサンプルデータ String body = "c3c9c84000000002000000b40000000000000000c3e2d8c3c2c4c3d600000000000000000000000000000000000001110002bf2000000002000001d4000000b4000000000000000000010000016800020000000200000000404040405c5c5c5c5c5c5c5c40404040404040404040404040404040c3c9f1f840404040c7c5f0f240404040404040404040404040404040c7c5f0f240404040404040404040404040404040000000000000000000000003000000000000011ef1f8f0f4d6404040f0f0f0f0c5404040d5404040e8404040d5404040d5404040d5404040d5404040ffffffff00000000d4c7c5f0f2f0f040d4c7c5f0f2f0f040d5404040000000c600000058000000000000011e000000000000000000000000000000000040404040404040400000000000f1f061f1f761f2f200000000000000000000000000004040404040404040404040404040404040404040404040404040404040404040404040404040404040400000000000f1f07af5f97af5f2ffffc100004040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; byte[] bytes = DatatypeConverter.parseHexBinary(body); bytes = Arrays.copyOfRange(bytes, 0, 268); ReplyResponseModel response = mqReceiveService.editReplyData(bytes); assertEquals(true, checkReplyResponseModel(response)); } private boolean checkReplyResponseModel(ReplyResponseModel model) { if (!Objects.equals(model.getMqcih().getStrucId(), "CIH")) { log.error("StrucId=" + model.getMqcih().getStrucId()); return false; } Set versions = new HashSet(Arrays.asList(new String[] { "1", "2" })); if (!versions.contains(model.getMqcih().getVersion())) { log.error("Version=" + model.getMqcih().getVersion()); return false; } if (!Objects.equals(model.getMqcih().getStrucLength(), "180")) { log.error("StrucLength=" + model.getMqcih().getStrucLength()); return false; } if (Objects.nonNull(model.getReceiveMessage().getSendMapVector())) { Set vectorDescriptors = new HashSet(Arrays.asList(new String[] { "1802", "1804" })); if (!vectorDescriptors.contains(model.getReceiveMessage().getSendMapVector().getVectorDescriptor())) { log.error("VectorDescriptor=" + model.getReceiveMessage().getSendMapVector().getVectorDescriptor()); return false; } Set vectorTypes = new HashSet(Arrays.asList(new String[] { "I", "O" })); if (!vectorTypes.contains(model.getReceiveMessage().getSendMapVector().getVectorType())) { log.error("VectorType=" + model.getReceiveMessage().getSendMapVector().getVectorType()); return false; } Set sceraseIndicators = new HashSet(Arrays.asList(new String[] { "N", "E", "A", "D" })); if (!sceraseIndicators.contains(model.getReceiveMessage().getSendMapVector().getSceraseIndicator())) { log.error("SceraseIndicator=" + model.getReceiveMessage().getSendMapVector().getSceraseIndicator()); return false; } Set yNList = new HashSet(Arrays.asList(new String[] { "Y", "N" })); if (!sceraseIndicators.contains(model.getReceiveMessage().getSendMapVector().getSceraseaupIndicator())) { log.error( "SceraseaupIndicator=" + model.getReceiveMessage().getSendMapVector().getSceraseaupIndicator()); return false; } if (!yNList.contains(model.getReceiveMessage().getSendMapVector().getScfreekbIndicator())) { log.error("ScfreekbIndicator=" + model.getReceiveMessage().getSendMapVector().getScfreekbIndicator()); return false; } if (!yNList.contains(model.getReceiveMessage().getSendMapVector().getScalarmIndicator())) { log.error("ScalarmIndicator=" + model.getReceiveMessage().getSendMapVector().getScalarmIndicator()); return false; } if (!yNList.contains(model.getReceiveMessage().getSendMapVector().getScfrsetIndicator())) { log.error("ScfrsetIndicator=" + model.getReceiveMessage().getSendMapVector().getScfrsetIndicator()); return false; } if (!yNList.contains(model.getReceiveMessage().getSendMapVector().getSclastIndicator())) { log.error("SclastIndicator=" + model.getReceiveMessage().getSendMapVector().getSclastIndicator()); return false; } if (!yNList.contains(model.getReceiveMessage().getSendMapVector().getScwaitIndicator())) { log.error("ScwaitIndicator=" + model.getReceiveMessage().getSendMapVector().getScwaitIndicator()); return false; } Set scCursors = new HashSet(Arrays.asList(new String[] { "-1", "-2" })); if (!scCursors.contains(model.getReceiveMessage().getSendMapVector().getSccursor())) { log.error("Sccursor=" + model.getReceiveMessage().getSendMapVector().getSccursor()); return false; } Set smDataIndics = new HashSet(Arrays.asList(new String[] { "D", "M", "N" })); if (!smDataIndics.contains(model.getReceiveMessage().getSendMapVector().getSmdataIndicator())) { log.error("SmdataIndicator=" + model.getReceiveMessage().getSendMapVector().getSmdataIndicator()); return false; } } return true; } }