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 javax.jms.BytesMessage; import javax.jms.JMSException; 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.Mockito; import org.mockito.MockitoAnnotations; import org.skyscreamer.jsonassert.JSONAssert; import org.skyscreamer.jsonassert.JSONCompareMode; import org.springframework.core.io.ClassPathResource; import org.springframework.jms.core.JmsTemplate; 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.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.ibm.mq.constants.CMQC; 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.constant.MqSpringMessageId; import com.pgf.mqspring.exception.MqSpringException; import lombok.extern.slf4j.Slf4j; @Slf4j @ExtendWith(SpringExtension.class) class MqReceiveServiceImplTest01 { @Mock AmazonS3 s3Client; @Mock ObjectMapper objectMapper; @Mock JmsTemplate jmsTemplate; @Mock MqSpringMessageSourceComponent messageSource; @Mock BytesMessage bm; @InjectMocks CharacterCodeConverterComponentImpl characterCodeConverter; @InjectMocks MqReceiveServiceImpl mqReceiveService; @BeforeEach public void beforeEacn() throws JMSException, IOException { MockitoAnnotations.openMocks(this); String body = "c3c9c84000000002000000b40000000000000000c3e2d8c3c2c4c3d600000000000000000000000000000000000001110002bf2000000002000001d4000000b4000000000000000000010000016800020000000200000000404040405c5c5c5c5c5c5c5c40404040404040404040404040404040c3c9f1f840404040c7c5f0f240404040404040404040404040404040c7c5f0f240404040404040404040404040404040000000000000000000000003000000000000011ef1f8f0f4d6404040f0f0f0f0c5404040d5404040e8404040d5404040d5404040d5404040d5404040ffffffff00000000d4c7c5f0f2f0f040d4c7c5f0f2f0f040d5404040000000c600000058000000000000011e000000000000000000000000000000000040404040404040400000000000f1f061f1f761f2f200000000000000000000000000004040404040404040404040404040404040404040404040404040404040404040404040404040404040400000000000f1f07af5f97af5f2ffffc100004040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; byte[] bytes = DatatypeConverter.parseHexBinary(body); bm.setStringProperty("JMS_IBM_Format", CMQC.MQFMT_CICS); bm.setIntProperty("JMS_IBM_Encoding", CMQC.MQENC_NATIVE); bm.setIntProperty("JMS_IBM_Character_Set", 943); bm.setJMSCorrelationID("AMQ!NEW_SESSION_CORRELID"); bm.writeBytes(DatatypeConverter.parseHexBinary(body)); bm.setStringProperty("JMSXUserID", "GETS"); Mockito.when(jmsTemplate.receiveSelected(Mockito.anyString(), Mockito.anyString())).thenReturn(bm); Mockito.when(bm.getBody(byte[].class)).thenReturn(bytes); S3Object s3Object = new S3Object(); s3Object.setObjectContent(new ClassPathResource( "com/pgf/mqspring/service/impl/MqReceiveServiceImplTest_01/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 receiveMessage01() { ObjectMapper mapper = new ObjectMapper(); ReflectionTestUtils.setField(mqReceiveService, "mapper", mapper); String response = mqReceiveService.receiveMessage("20220912093246881183"); log.info("json=" + response); String expect = "{\"mqmd\":{\"correlId\":null},\"mqcih\":{\"strucId\":\"CIH\",\"version\":\"2\",\"strucLength\":\"180\",\"format\":\"CSQCBDCO\",\"flags\":\"0\",\"returnCode\":\"0\",\"compCode\":\"0\",\"reason\":\"0\",\"uowControl\":\"273\",\"getWaitInterval\":\"180000\",\"linkType\":\"2\",\"outputDataLength\":\"468\",\"facilityKeepTime\":\"180\",\"adsDescriptor\":\"0\",\"conversationalTask\":\"0\",\"taskEndStatus\":\"65536\",\"facility\":\"0168000200000002\",\"function\":\"0\",\"abendCode\":\"\",\"autherticator\":\"********\",\"replyToFormat\":\"\",\"transactionId\":\"GE02\",\"facilityLike\":\"\",\"attentionId\":\"\",\"startCode\":\"\",\"cancelCode\":\"\",\"nextTransactionCode\":\"GE02\",\"cursorPositon\":\"0\",\"errorOffset\":\"0\"},\"receiveMessage\":{\"sendMapVector\":{\"vectorDescriptor\":\"1804\",\"vectorType\":\"O\",\"vectorVersion\":\"18085046173888752\",\"scEraseIndic\":\"E\",\"scEraseaupIndic\":\"N\",\"scFreekbIndic\":\"Y\",\"scAlarmIndic\":\"N\",\"scFrsetIndic\":\"N\",\"scLastIndic\":\"N\",\"scWaitIndic\":\"N\",\"scCursor\":\"-1\",\"scMsrData\":\"00000000\",\"smMapset\":\"MGE0200\",\"smMap\":\"MGE0200\",\"smDataIndic\":\"N\",\"smDataLen\":\"198\",\"smDataOffset\":\"88\",\"smAdsdLen\":\"0\",\"smAdsdOffset\":\"286\"},\"ads\":\"10/17/22,,                     ,10:59:52,,\"}}"; JSONAssert.assertEquals(expect, response, JSONCompareMode.LENIENT); } @Test void receiveMessage02() throws JMSException { Mockito.when(bm.getBody(Mockito.any())).thenThrow(new JMSException("receiveMessage02 to error")); Mockito.when(messageSource.getMessage(Mockito.any())).thenReturn("MQ受信エラー。"); try { mqReceiveService.receiveMessage(null); assertTrue(false); } catch (MqSpringException e) { assertEquals(MqSpringMessageId.SYS7006E,e.getMessageId()); } } @Test void receiveMessage03() throws JsonProcessingException { Mockito.when(objectMapper.writeValueAsString(Mockito.any())).thenThrow(new JsonProcessingException("receiveMessage03 to error"){}); Mockito.when(messageSource.getMessage(Mockito.any())).thenReturn("JSON変換エラー。"); try { mqReceiveService.receiveMessage(null); assertTrue(false); } catch (MqSpringException e) { assertEquals(MqSpringMessageId.SYS7080E,e.getMessageId()); } } @Test void receiveMessage04() { Mockito.when(jmsTemplate.receiveSelected(Mockito.anyString(), Mockito.anyString())).thenReturn(null); Mockito.when(messageSource.getMessage(Mockito.any())).thenReturn("MQ受信タイムアウト。"); try { mqReceiveService.receiveMessage(null); assertTrue(false); } catch (MqSpringException e) { assertEquals(MqSpringMessageId.SYS7008E,e.getMessageId()); } } @Test void receiveMessage05() throws JMSException { byte[] bytes = null; Mockito.when(bm.getBody(byte[].class)).thenReturn(bytes); Mockito.when(messageSource.getMessage(Mockito.any())).thenReturn("ホストアプリケーションで障害が発生しました。"); try { mqReceiveService.receiveMessage(null); assertTrue(false); } catch (MqSpringException e) { assertEquals(MqSpringMessageId.SYS7072E,e.getMessageId()); } } }