package com.pgf.mqspring; import static org.junit.jupiter.api.Assertions.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.http.MediaType; import org.springframework.jms.core.JmsTemplate; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import com.amazonaws.services.s3.AmazonS3; import com.fasterxml.jackson.databind.ObjectMapper; import com.pgf.mqspring.component.MqSpringMessageSourceComponent; import com.pgf.mqspring.component.TelegramInfoComponent; import com.pgf.mqspring.model.MqcihRequestModel; import com.pgf.mqspring.model.MqmdRequestModel; import com.pgf.mqspring.model.ReceiveMapVectorRequestModel; import com.pgf.mqspring.model.ReplyRequestModel; import com.pgf.mqspring.model.SendMessageRequestModel; import com.pgf.mqspring.service.MqReceiveService; import com.pgf.mqspring.service.MqSendService; @SpringBootTest(properties = { "key.source.url=", "key.issuer=", "ibm.mq.queueName.send=", "ibm.mq.queueName.receive=" }) @AutoConfigureMockMvc class MqSpringApplicationTest02 { @Autowired private MockMvc mockMvc; @MockBean TelegramInfoComponent telegramInfoComponent; @InjectMocks MqSpringApplication mqSpringApplication; @Autowired WebApplicationContext webApplicationContext; @MockBean AmazonS3 s3Client; @Mock JmsTemplate jmsTemplate; @MockBean MqSendService service; @MockBean MqReceiveService mqReceiveService; @Autowired ConfigurableApplicationContext context; @MockBean MqSpringMessageSourceComponent messageSource; @MockBean Thread thread; @BeforeEach public void beforeEacn() { MockitoAnnotations.openMocks(this); mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test void replyRequest01() throws Exception { Mockito.when(service.getMessageId()).thenReturn("20220912093246881184"); Mockito.when(mqReceiveService.receiveMessage(Mockito.anyString())).thenReturn("{\"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\",\"sceraseIndicator\":\"E\",\"sceraseaupIndicator\":\"N\",\"scfreekbIndicator\":\"Y\",\"scalarmIndicator\":\"N\",\"scfrsetIndicator\":\"N\",\"sclastIndicator\":\"N\",\"scwaitIndicator\":\"N\",\"sccursor\":\"-1\",\"scmsrData\":\"00000000\",\"smmapset\":\"MGE0200\",\"smmap\":\"MGE0200\",\"smdataIndicator\":\"N\",\"smdataLen\":\"198\",\"smdataOffset\":\"88\",\"smadsdLen\":\"0\",\"smadsdOffset\":\"286\"},\"ads\":\"10/17/22,,                     ,10:59:52,,\"}}"); ReplyRequestModel itemRequest = getReplyRequestModel(); ObjectMapper objectMapper = new ObjectMapper(); ReflectionTestUtils.setField(mqSpringApplication, "jmsTemplate", jmsTemplate); ReflectionTestUtils.setField(mqSpringApplication, "context", context); mockMvc.perform( post("/pgf/mq-rest/v1/apl/cda/reply") .content(objectMapper.writeValueAsString(itemRequest)) .contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().is(200)); } @Test void replyRequest02() throws Exception { ReplyRequestModel itemRequest = getReplyRequestModel(); ObjectMapper objectMapper = new ObjectMapper(); Logger logger = LogManager.getLogger(); ReflectionTestUtils.setField(mqSpringApplication, "logger", logger); ReflectionTestUtils.setField(mqSpringApplication, "isProvidingService", false); Mockito.when(messageSource.getMessage(Mockito.any())).thenReturn("サービスを停止中です"); mockMvc.perform( post("/pgf/mq-rest/v1/apl/cda/reply") .content(objectMapper.writeValueAsString(itemRequest)) .contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().is(503)); assertTrue(true); } @Test void replyRequest03() throws Exception { Mockito.when(service.getMessageId()).thenReturn(null); ReplyRequestModel itemRequest = getReplyRequestModel(); ObjectMapper objectMapper = new ObjectMapper(); Logger logger = LogManager.getLogger(); ReflectionTestUtils.setField(mqSpringApplication, "jmsTemplate", jmsTemplate); ReflectionTestUtils.setField(mqSpringApplication, "context", context); ReflectionTestUtils.setField(mqSpringApplication, "logger", logger); ReflectionTestUtils.setField(mqSpringApplication, "isProvidingService", true); Mockito.when(messageSource.getMessage(Mockito.any())).thenReturn("メッセージID取得エラー"); mockMvc.perform( post("/pgf/mq-rest/v1/apl/cda/reply") .content(objectMapper.writeValueAsString(itemRequest)) .contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().is(500)); assertTrue(true); } @Test void replyRequest04() throws Exception { Mockito.when(service.getMessageId()).thenReturn(""); ReplyRequestModel itemRequest = getReplyRequestModel(); ObjectMapper objectMapper = new ObjectMapper(); Logger logger = LogManager.getLogger(); ReflectionTestUtils.setField(mqSpringApplication, "jmsTemplate", jmsTemplate); ReflectionTestUtils.setField(mqSpringApplication, "context", context); ReflectionTestUtils.setField(mqSpringApplication, "logger", logger); ReflectionTestUtils.setField(mqSpringApplication, "isProvidingService", true); Mockito.when(messageSource.getMessage(Mockito.any())).thenReturn("メッセージID取得エラー"); mockMvc.perform( post("/pgf/mq-rest/v1/apl/cda/reply") .content(objectMapper.writeValueAsString(itemRequest)) .contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().is(500)); assertTrue(true); } //thread処理がサポートされていないので、例外をスロー出来ないためテストケースから除外する。 // @Test // void replyRequest04() throws Exception { // } private ReplyRequestModel getReplyRequestModel() { MqmdRequestModel mqmd = new MqmdRequestModel(); mqmd.setFormat("MQCICS"); mqmd.setCorrelId("AMQ!NEW_SESSION_CORRELID"); mqmd.setPersistence("0"); mqmd.setExpiry(""); MqcihRequestModel mqcih = new MqcihRequestModel(); mqcih.setUowControl("273"); mqcih.setGetWaitInterval("180000"); mqcih.setLinkType("2"); mqcih.setFacilityKeepTime("180"); mqcih.setConversationalTask("0"); mqcih.setFacility("0138000100000001"); mqcih.setAutherticator("GETS3333"); mqcih.setTransactionId("EM04"); ReceiveMapVectorRequestModel receiveMapVector = new ReceiveMapVectorRequestModel(); receiveMapVector.setVectorDescriptor("1802"); receiveMapVector.setVectorType("I"); receiveMapVector.setVectorVersion("00000000"); receiveMapVector.setRmTransmitSendAreas("Y"); receiveMapVector.setRmMapset("MEM0400"); receiveMapVector.setRmMap("MEM0400"); receiveMapVector.setRmAid("DFHENTER"); receiveMapVector.setRmCposn(""); SendMessageRequestModel sendMessage = new SendMessageRequestModel(); sendMessage.setReceiveMapVector(receiveMapVector); sendMessage.setAds( "EM04,,,,,,8974864291,20250430,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"); ReplyRequestModel replyRequestModel = new ReplyRequestModel(); replyRequestModel.setMqmd(mqmd); replyRequestModel.setTimeout("60000"); replyRequestModel.setMqcih(mqcih); replyRequestModel.setSendMessage(sendMessage); return replyRequestModel; } }