package com.millicom.microservice.service.mock; import com.fasterxml.jackson.databind.ObjectMapper; import com.millicom.microservice.service.service.common.GenerateErrorServiceImp; import com.millicom.microservice.service.service.messaging.ErrorMapper; import com.readinessit.library.logging.application.services.LoggerService; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.TestMethodOrder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.servlet.MockMvc; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc @TestMethodOrder(MethodOrderer.OrderAnnotation.class) class MsTemplateResourceTest extends com.millicom.microservice.service.helper.BaseTestAPI { @Autowired LoggerService loggerService; @Autowired private ObjectMapper objectMapper; @Autowired private MockMvc mockMvc; @Autowired private ErrorMapper errorMapper; @Autowired private GenerateErrorServiceImp generateErrorServiceImp; @Value("classpath:mockresponses/errorResponse.json") private org.springframework.core.io.Resource errorResponse; @Value("classpath:mockresponses/successResponse.json") private org.springframework.core.io.Resource successResponse; @Value("classpath:mockresponses/requestOk.json") private org.springframework.core.io.Resource requestOk; @Value("classpath:mockresponses/requestFail.json") private org.springframework.core.io.Resource requestFail; @Value("${service.createorder.patch}") private String patchService; /* @Test @Order(1) void testSucessfull() throws Exception { ExternalSystemsMock.externalSystemsResponse(externalSystemsMock, "REST", "POST", "/api/v1/createOrder", resourceToString(successResponse), 200, isNull()); MvcResult requestResult = mockMvc.perform(post(patchService).content(resourceToString(requestOk)).contentType(MediaType.APPLICATION_JSON_VALUE) .accept(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().is2xxSuccessful()).andReturn(); assertEquals(200, requestResult.getResponse().getStatus()); } @Test @Order(2) void testbadRequest() throws Exception { MvcResult requestResult = mockMvc.perform(post(patchService).content(anyString()).contentType(MediaType.APPLICATION_JSON_VALUE) .accept(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isBadRequest()).andReturn(); assertEquals(400, requestResult.getResponse().getStatus()); } @Test @Order(3) void testUnsuported() throws Exception { MvcResult requestResult = mockMvc.perform(post(patchService).content(anyString()).contentType(MediaType.APPLICATION_XML) .accept(MediaType.APPLICATION_XML_VALUE)) .andExpect(status().isUnsupportedMediaType()).andReturn(); assertEquals(415, requestResult.getResponse().getStatus()); } @Test @Order(4) void testValidacionFallida() throws Exception { MvcResult requestResult = mockMvc.perform(post(patchService).content(resourceToString(requestFail)).contentType(MediaType.APPLICATION_JSON_VALUE) .accept(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isBadRequest()).andReturn(); assertEquals(400, requestResult.getResponse().getStatus()); } @Test @Order(5) void testUnathorized() throws Exception { Thread.sleep(2000); ExternalSystemsMock.externalSystemsResponse(externalSystemsMock, "REST", "POST", "/api/v1/createOrder", anyString(), 401, isNull()); MvcResult requestResult = mockMvc.perform(post(patchService).content(resourceToString(requestOk)).contentType(MediaType.APPLICATION_JSON_VALUE) .accept(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isUnauthorized()).andReturn(); assertEquals(401, requestResult.getResponse().getStatus()); } @Test @Order(6) void testFailServer() throws Exception { Thread.sleep(2000); ExternalSystemsMock.externalSystemsResponse(externalSystemsMock, "REST", "POST", "/api/v1/createOrder", resourceToString(errorResponse), 400, isNull()); MvcResult requestResult = mockMvc.perform(post(patchService).content(resourceToString(requestOk)).contentType(MediaType.APPLICATION_JSON_VALUE) .accept(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isBadRequest()).andReturn(); assertEquals(400, requestResult.getResponse().getStatus()); ErrorResponse response = objectMapper.readValue(requestResult.getResponse().getContentAsString(), ErrorResponse.class); } @Test @Order(7) void validateErrorMapper(){ ErrorStructure errorStructure=errorMapper.validate(null); assertEquals(errorStructure.getHttpStatus(), HttpStatus.BAD_GATEWAY); MessageResponse response= new MessageResponse(); response.setResponse(new ObjetoNoSerializable()); String json= errorMapper.obtenerJsonDeMessageResponse(response); assertNull(json); } @Test @Order(8) void validateGenerateErrorServiceImp () throws IOException { ErrorStructure errorStructure=generateErrorServiceImp.generarErrorNuevo(HttpStatus.CHECKPOINT,null); assertEquals(errorStructure.getHttpStatus(), HttpStatus.INTERNAL_SERVER_ERROR); assertEquals(errorStructure.getErrors().getFirst().getMessage(),"Error Interno del Adaptador"); ErrorResponse errorResponse1= generateErrorServiceImp.obtenerResponse("200",""); assertNull(errorResponse1.getErrors()); //List listFull= generateErrorServiceImp.obtenerResponse("200",resourceToString(errorResponse)); // assertEquals(listFull.getFirst().getMessage(),""); PortabilidadAplicacion.main(new String[]{}); // Verificar si el servicio de registro se utiliza correctamente //verify(loggerService); } @Test @Order(1) void testFailConexion() throws Exception { ExternalSystemsMock.externalSystemsResponse(externalSystemsMock, "REST", "POST", "/api/v1/createOrder", resourceToString(successResponse), 502, isNull()); MvcResult requestResult = mockMvc.perform(post(patchService).content(resourceToString(requestOk)).contentType(MediaType.APPLICATION_JSON_VALUE) .accept(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().is5xxServerError()).andReturn(); assertEquals(502, requestResult.getResponse().getStatus()); } */ }