package com.pgf.mqspring.service.impl; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; import java.io.IOException; 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.MockitoAnnotations; import org.springframework.core.io.ClassPathResource; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.util.ReflectionTestUtils; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.S3Object; 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 AmazonS3 s3Client; @Mock MqSpringMessageSourceComponent messageSource; @InjectMocks CharacterCodeConverterComponentImpl characterCodeConverter; @InjectMocks MqReceiveServiceImpl mqReceiveService; @BeforeEach public void beforeEacn() throws IOException { MockitoAnnotations.openMocks(this); S3Object s3Object = new S3Object(); s3Object.setObjectContent(new ClassPathResource( "com/pgf/mqspring/service/impl/MqReceiveServiceImplTest_02/ibm_telegram_info.csv") .getInputStream()); when(s3Client.getObject(anyString(), anyString())).thenReturn(s3Object); TelegramInfoComponent telegramInfoComponent = new TelegramInfoComponentImpl(s3Client, 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 scEraseIndics = new HashSet(Arrays.asList(new String[] {"N", "E", "A", "D"})); if (!scEraseIndics.contains(model.getReceiveMessage().getSendMapVector().getScEraseIndic())) { log.error("ScEraseIndic=" + model.getReceiveMessage().getSendMapVector().getScEraseIndic()); return false; } Set yNList = new HashSet(Arrays.asList(new String[] {"Y", "N"})); if (!scEraseIndics.contains(model.getReceiveMessage().getSendMapVector().getScEraseaupIndic())) { log.error("ScEraseaupIndic=" + model.getReceiveMessage().getSendMapVector().getScEraseaupIndic()); return false; } if (!yNList.contains(model.getReceiveMessage().getSendMapVector().getScFreekbIndic())) { log.error("ScFreekbIndic=" + model.getReceiveMessage().getSendMapVector().getScFreekbIndic()); return false; } if (!yNList.contains(model.getReceiveMessage().getSendMapVector().getScAlarmIndic())) { log.error("ScAlarmIndic=" + model.getReceiveMessage().getSendMapVector().getScAlarmIndic()); return false; } if (!yNList.contains(model.getReceiveMessage().getSendMapVector().getScFrsetIndic())) { log.error("ScFrsetIndic=" + model.getReceiveMessage().getSendMapVector().getScFrsetIndic()); return false; } if (!yNList.contains(model.getReceiveMessage().getSendMapVector().getScLastIndic())) { log.error("ScLastIndic=" + model.getReceiveMessage().getSendMapVector().getScLastIndic()); return false; } if (!yNList.contains(model.getReceiveMessage().getSendMapVector().getScWaitIndic())) { log.error("ScWaitIndic=" + model.getReceiveMessage().getSendMapVector().getScWaitIndic()); 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().getSmDataIndic())) { log.error("SmDataIndic=" + model.getReceiveMessage().getSendMapVector().getSmDataIndic()); return false; } } return true; }}