Search in sources :

Example 1 with GenericResponseWrapper

use of com.arbindo.mimock.manage.mimocks.models.v1.GenericResponseWrapper in project mimock by neel1996.

the class MockManagementControllerTest method shouldReturnHttpOk_UpdateMockByIdApi_ReturnsValidMockData.

@Test
void shouldReturnHttpOk_UpdateMockByIdApi_ReturnsValidMockData() throws Exception {
    // Arrange
    MockMultipartFile file = getMockMultipartFile();
    MockRequest mockRequest = createMockRequestWithFile(file);
    Mock mock = generateMock(mockRequest);
    String route = UrlConfig.MOCKS_PATH + "/" + mock.getId();
    lenient().when(mockManagementService.updateMock(anyString(), any(ProcessedMockRequest.class))).thenReturn(mock);
    GenericResponseWrapper<Mock> genericResponseWrapper = getGenericResponseWrapper(HttpStatus.OK, Messages.UPDATE_RESOURCE_SUCCESS, mock);
    String expectedResponseBody = convertObjectToJsonString(genericResponseWrapper);
    String expectedContentType = "application/json";
    // Act
    MockMultipartHttpServletRequestBuilder builder = MockMvcRequestBuilders.multipart(route);
    builder.with(request -> {
        request.setMethod("PUT");
        return request;
    });
    MvcResult result = mockMvc.perform(builder.file(file).param("name", mockRequest.getName()).param("route", mockRequest.getRoute()).param("httpMethod", mockRequest.getHttpMethod()).param("requestHeader", "{\"x-auth-token\": \"123e4567-e89b-12d3-a456-426614174000\"}").param("shouldDoExactHeaderMatching", "false").param("requestBody", "{\"name\": \"blog\", \"auto_init\": true, \"private\": true, \"gitignore_template\": \"nanoc\"}").param("responseHeaders", "{\"Content-Type\": \"application/json\"}").param("responseContentType", mockRequest.getResponseContentType()).param("statusCode", String.valueOf(mockRequest.getStatusCode())).param("expectedTextResponse", mockRequest.getExpectedTextResponse()).param("description", mockRequest.getDescription())).andExpect(status().isOk()).andExpect(content().contentType(expectedContentType)).andReturn();
    // Assert
    assertEquals(expectedResponseBody, result.getResponse().getContentAsString());
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) ProcessedMockRequest(com.arbindo.mimock.manage.mimocks.models.v1.ProcessedMockRequest) MockMultipartHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder) MockRequest(com.arbindo.mimock.manage.mimocks.models.v1.MockRequest) ProcessedMockRequest(com.arbindo.mimock.manage.mimocks.models.v1.ProcessedMockRequest) JsonMapper.convertObjectToJsonString(com.arbindo.mimock.helpers.general.JsonMapper.convertObjectToJsonString) RandomDataGenerator.generateRandomAlphabeticString(com.arbindo.mimock.helpers.general.RandomDataGenerator.generateRandomAlphabeticString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MvcResult(org.springframework.test.web.servlet.MvcResult) Mock(com.arbindo.mimock.entities.Mock) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 2 with GenericResponseWrapper

use of com.arbindo.mimock.manage.mimocks.models.v1.GenericResponseWrapper in project mimock by neel1996.

the class MockManagementControllerTest method shouldReturnHttpCreated_CreateMockApi_ReturnsValidMockData.

@Test
void shouldReturnHttpCreated_CreateMockApi_ReturnsValidMockData() throws Exception {
    // Arrange
    MockMultipartFile file = getMockMultipartFile();
    MockRequest mockRequest = createMockRequestWithFile(file);
    Mock createdMock = generateMock(mockRequest);
    String route = UrlConfig.MOCKS_PATH;
    lenient().when(mockManagementService.createMock(any(ProcessedMockRequest.class))).thenReturn(createdMock);
    final String location = "http://localhost" + route + "/" + createdMock.getId();
    GenericResponseWrapper<Mock> genericResponseWrapper = getGenericResponseWrapper(HttpStatus.CREATED, Messages.createResourceSuccess(location), createdMock);
    String expectedResponseBody = convertObjectToJsonString(genericResponseWrapper);
    String expectedContentType = "application/json";
    // Act
    MvcResult result = mockMvc.perform(multipart(route).file(file).param("name", mockRequest.getName()).param("route", mockRequest.getRoute()).param("httpMethod", mockRequest.getHttpMethod()).param("responseContentType", mockRequest.getResponseContentType()).param("statusCode", String.valueOf(mockRequest.getStatusCode())).param("expectedTextResponse", mockRequest.getExpectedTextResponse()).param("description", mockRequest.getDescription())).andExpect(status().isCreated()).andExpect(content().contentType(expectedContentType)).andReturn();
    // Assert
    assertEquals(expectedResponseBody, result.getResponse().getContentAsString());
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) ProcessedMockRequest(com.arbindo.mimock.manage.mimocks.models.v1.ProcessedMockRequest) MockRequest(com.arbindo.mimock.manage.mimocks.models.v1.MockRequest) ProcessedMockRequest(com.arbindo.mimock.manage.mimocks.models.v1.ProcessedMockRequest) JsonMapper.convertObjectToJsonString(com.arbindo.mimock.helpers.general.JsonMapper.convertObjectToJsonString) RandomDataGenerator.generateRandomAlphabeticString(com.arbindo.mimock.helpers.general.RandomDataGenerator.generateRandomAlphabeticString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MvcResult(org.springframework.test.web.servlet.MvcResult) Mock(com.arbindo.mimock.entities.Mock) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 3 with GenericResponseWrapper

use of com.arbindo.mimock.manage.mimocks.models.v1.GenericResponseWrapper in project mimock by neel1996.

the class MockManagementControllerTest method shouldReturnHttpBadRequest_CreateMockApi_ReturnsNullMockData.

@Test
void shouldReturnHttpBadRequest_CreateMockApi_ReturnsNullMockData() throws Exception {
    // Arrange
    MockMultipartFile file = getMockMultipartFile();
    MockRequest mockRequest = createMockRequestWithFile(file);
    String route = UrlConfig.MOCKS_PATH;
    lenient().when(mockManagementService.createMock(any(ProcessedMockRequest.class))).thenReturn(null);
    GenericResponseWrapper<Mock> genericResponseWrapper = getGenericResponseWrapper(HttpStatus.BAD_REQUEST, Messages.CREATE_RESOURCE_FAILED, null);
    String expectedResponseBody = convertObjectToJsonString(genericResponseWrapper);
    String expectedContentType = "application/json";
    // Act
    MvcResult result = mockMvc.perform(multipart(route).file(file).param("name", mockRequest.getName()).param("route", mockRequest.getRoute()).param("httpMethod", mockRequest.getHttpMethod()).param("responseContentType", mockRequest.getResponseContentType()).param("statusCode", String.valueOf(mockRequest.getStatusCode())).param("expectedTextResponse", mockRequest.getExpectedTextResponse()).param("description", mockRequest.getDescription())).andExpect(status().isBadRequest()).andExpect(content().contentType(expectedContentType)).andReturn();
    // Assert
    assertEquals(expectedResponseBody, result.getResponse().getContentAsString());
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) ProcessedMockRequest(com.arbindo.mimock.manage.mimocks.models.v1.ProcessedMockRequest) MockRequest(com.arbindo.mimock.manage.mimocks.models.v1.MockRequest) ProcessedMockRequest(com.arbindo.mimock.manage.mimocks.models.v1.ProcessedMockRequest) JsonMapper.convertObjectToJsonString(com.arbindo.mimock.helpers.general.JsonMapper.convertObjectToJsonString) RandomDataGenerator.generateRandomAlphabeticString(com.arbindo.mimock.helpers.general.RandomDataGenerator.generateRandomAlphabeticString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MvcResult(org.springframework.test.web.servlet.MvcResult) Mock(com.arbindo.mimock.entities.Mock) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 4 with GenericResponseWrapper

use of com.arbindo.mimock.manage.mimocks.models.v1.GenericResponseWrapper in project mimock by neel1996.

the class MockManagementControllerTest method shouldReturnHttpBadRequest_UpdateMockByIdApi_ReturnsNullMockData.

@Test
void shouldReturnHttpBadRequest_UpdateMockByIdApi_ReturnsNullMockData() throws Exception {
    // Arrange
    MockMultipartFile file = getMockMultipartFile();
    MockRequest mockRequest = createMockRequestWithFile(file);
    Mock mock = generateMock(mockRequest);
    String route = UrlConfig.MOCKS_PATH + "/" + mock.getId();
    lenient().when(mockManagementService.updateMock(anyString(), any(ProcessedMockRequest.class))).thenReturn(null);
    GenericResponseWrapper<Mock> genericResponseWrapper = getGenericResponseWrapper(HttpStatus.BAD_REQUEST, Messages.UPDATE_RESOURCE_FAILED, null);
    String expectedResponseBody = convertObjectToJsonString(genericResponseWrapper);
    String expectedContentType = "application/json";
    // Act
    MockMultipartHttpServletRequestBuilder builder = MockMvcRequestBuilders.multipart(route);
    builder.with(request -> {
        request.setMethod("PUT");
        return request;
    });
    MvcResult result = mockMvc.perform(builder.file(file).param("name", mockRequest.getName()).param("route", mockRequest.getRoute()).param("httpMethod", mockRequest.getHttpMethod()).param("responseContentType", mockRequest.getResponseContentType()).param("statusCode", String.valueOf(mockRequest.getStatusCode())).param("expectedTextResponse", mockRequest.getExpectedTextResponse()).param("description", mockRequest.getDescription())).andExpect(status().isBadRequest()).andExpect(content().contentType(expectedContentType)).andReturn();
    // Assert
    assertEquals(expectedResponseBody, result.getResponse().getContentAsString());
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) ProcessedMockRequest(com.arbindo.mimock.manage.mimocks.models.v1.ProcessedMockRequest) MockMultipartHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder) MockRequest(com.arbindo.mimock.manage.mimocks.models.v1.MockRequest) ProcessedMockRequest(com.arbindo.mimock.manage.mimocks.models.v1.ProcessedMockRequest) JsonMapper.convertObjectToJsonString(com.arbindo.mimock.helpers.general.JsonMapper.convertObjectToJsonString) RandomDataGenerator.generateRandomAlphabeticString(com.arbindo.mimock.helpers.general.RandomDataGenerator.generateRandomAlphabeticString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MvcResult(org.springframework.test.web.servlet.MvcResult) Mock(com.arbindo.mimock.entities.Mock) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Aggregations

Mock (com.arbindo.mimock.entities.Mock)4 JsonMapper.convertObjectToJsonString (com.arbindo.mimock.helpers.general.JsonMapper.convertObjectToJsonString)4 RandomDataGenerator.generateRandomAlphabeticString (com.arbindo.mimock.helpers.general.RandomDataGenerator.generateRandomAlphabeticString)4 MockRequest (com.arbindo.mimock.manage.mimocks.models.v1.MockRequest)4 ProcessedMockRequest (com.arbindo.mimock.manage.mimocks.models.v1.ProcessedMockRequest)4 Test (org.junit.jupiter.api.Test)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)4 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)4 MvcResult (org.springframework.test.web.servlet.MvcResult)4 MockMultipartHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder)2