Search in sources :

Example 1 with DomainModelForMock

use of com.arbindo.mimock.generic.model.DomainModelForMock in project mimock by arbindo.

the class GenericMockRequestControllerTest method shouldReturnHttpOK_WhenMatchingMockExists_WithQueryParamsAndTextualResponseAndStatus200.

@Test
void shouldReturnHttpOK_WhenMatchingMockExists_WithQueryParamsAndTextualResponseAndStatus200() throws Exception {
    String route = "/api/testmock/testroute?version=2.0&active=true";
    String expectedContentType = "application/json";
    String expectedResponseBody = "{'message': 'Hello World!'}";
    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
    mockHttpServletRequest.setQueryString("version=2.0&active=true");
    DomainModelForMock expectedMock = DomainModelForMock.builder().responseContentType(expectedContentType).statusCode(200).responseBody(expectedResponseBody).build();
    lenient().when(genericMockRequestService.serveMockRequest(any())).thenReturn(expectedMock);
    Optional<DomainModelForMock> response = genericMockRequestController.serveRequest(route, mockHttpServletRequest);
    assertTrue(response.isPresent());
    DomainModelForMock actualResponse = response.get();
    assertEquals(expectedResponseBody, actualResponse.getResponseBody().toString());
    assertEquals(200, actualResponse.getStatusCode());
    assertEquals(expectedContentType, actualResponse.getResponseContentType());
}
Also used : DomainModelForMock(com.arbindo.mimock.generic.model.DomainModelForMock) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with DomainModelForMock

use of com.arbindo.mimock.generic.model.DomainModelForMock in project mimock by arbindo.

the class GenericMockRequestControllerTest method shouldReturnHttpBadRequest_WhenMatchingMockExists_WithQueryParamsAndTextualResponseAndStatus400.

@Test
void shouldReturnHttpBadRequest_WhenMatchingMockExists_WithQueryParamsAndTextualResponseAndStatus400() throws Exception {
    String route = "/api/testmock/testroute?version=2.0&active=true";
    String expectedContentType = "application/json";
    String expectedResponseBody = "{'error': 'Required field missing'}";
    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
    mockHttpServletRequest.setQueryString("version=2.0&active=true");
    DomainModelForMock expectedMock = DomainModelForMock.builder().responseContentType(expectedContentType).statusCode(400).responseBody(expectedResponseBody).build();
    lenient().when(genericMockRequestService.serveMockRequest(any())).thenReturn(expectedMock);
    Optional<DomainModelForMock> response = genericMockRequestController.serveRequest(route, mockHttpServletRequest);
    assertTrue(response.isPresent());
    DomainModelForMock actualResponse = response.get();
    assertEquals(expectedResponseBody, actualResponse.getResponseBody().toString());
    assertEquals(400, actualResponse.getStatusCode());
    assertEquals(expectedContentType, actualResponse.getResponseContentType());
}
Also used : DomainModelForMock(com.arbindo.mimock.generic.model.DomainModelForMock) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with DomainModelForMock

use of com.arbindo.mimock.generic.model.DomainModelForMock in project mimock by arbindo.

the class GenericMockRequestServiceTest method shouldReturnMockData_WhenMatchingMockExistsWithoutQueryParams.

@Test
void shouldReturnMockData_WhenMatchingMockExistsWithoutQueryParams() throws Exception {
    String method = "GET";
    String expectedRoute = "/api/testmock/testroute";
    Map<String, Object> requestHeaders = RandomDataGenerator.getRequestHeaders();
    String requestBody = "{\"name\": \"blog\", \"auto_init\": true, \"private\": true, \"gitignore_template\": \"nanoc\"}";
    GenericRequestModel genericRequestModel = GenericRequestModel.builder().httpMethod(method).route(expectedRoute).requestHeaders(requestHeaders).requestBody(JsonMapper.convertJSONStringToMap(requestBody)).build();
    HttpMethod httpMethod = HttpMethod.builder().method(method).id(httpMethodsDBHelper.getHttpMethodByMethod(method).getId()).build();
    ResponseContentType responseContentType = responseContentTypeDBHelper.findOneByContentType("application/json");
    RequestHeader requestHeader = RequestHeader.builder().requestHeader(requestHeaders).matchExact(true).build();
    RequestBodiesForMock requestBodiesForMock = RequestBodiesForMock.builder().requestBody(JsonMapper.convertJSONStringToMap(requestBody)).build();
    Mock expectedMock = Mock.builder().route(genericRequestModel.getRoute()).httpMethod(httpMethod).responseContentType(responseContentType).requestHeaders(requestHeader).requestBodiesForMock(requestBodiesForMock).statusCode(200).build();
    lenient().when(mockHttpMethodsRepository.findByMethod(method)).thenReturn(httpMethod);
    lenient().when(mockRepository.findOneByRouteAndHttpMethod(genericRequestModel.getRoute(), httpMethod)).thenReturn(Optional.of(expectedMock));
    DomainModelForMock actualMock = genericMockRequestService.serveMockRequest(genericRequestModel);
    verify(mockHttpMethodsRepository, times(1)).findByMethod(method);
    verify(mockRepository, times(1)).findOneByRouteAndHttpMethod(genericRequestModel.getRoute(), httpMethod);
    assertEquals(expectedRoute, actualMock.getRoute());
    assertEquals(200, actualMock.getStatusCode());
    assertEquals(responseContentType.getContentType(), actualMock.getResponseContentType());
}
Also used : DomainModelForMock(com.arbindo.mimock.generic.model.DomainModelForMock) DomainModelForMock(com.arbindo.mimock.generic.model.DomainModelForMock) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with DomainModelForMock

use of com.arbindo.mimock.generic.model.DomainModelForMock in project mimock by arbindo.

the class DomainModelMapperTest method shouldReturnModelWithTextResponseFactory.

@Test
void shouldReturnModelWithTextResponseFactory() {
    ResponseContentType responseContentType = ResponseContentType.builder().contentType("application/json").build();
    TextualResponse textualResponse = TextualResponse.builder().responseBody("{'message': 'Hello World!'}").build();
    Mock testMock = Mock.builder().responseContentType(responseContentType).statusCode(200).textualResponse(textualResponse).binaryResponse(null).build();
    DomainModelForMock mappedModel = domainModelMapper.mappedModel(testMock);
    assertEquals("{'message': 'Hello World!'}", mappedModel.getResponseBody());
    assertEquals(200, mappedModel.getStatusCode());
    assertEquals("application/json", mappedModel.getResponseContentType());
    assertEquals(TypeOfResponse.TEXTUAL_RESPONSE, mappedModel.getTypeOfResponse());
}
Also used : DomainModelForMock(com.arbindo.mimock.generic.model.DomainModelForMock) TextualResponse(com.arbindo.mimock.entities.TextualResponse) ResponseContentType(com.arbindo.mimock.entities.ResponseContentType) DomainModelForMock(com.arbindo.mimock.generic.model.DomainModelForMock) Mock(com.arbindo.mimock.entities.Mock) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with DomainModelForMock

use of com.arbindo.mimock.generic.model.DomainModelForMock in project mimock by arbindo.

the class DomainModelMapperTest method shouldReturnModelWithTextResponseFactoryWhenResponseHeaderIsNotNull.

@Test
void shouldReturnModelWithTextResponseFactoryWhenResponseHeaderIsNotNull() {
    ResponseContentType responseContentType = ResponseContentType.builder().contentType("application/json").build();
    TextualResponse textualResponse = TextualResponse.builder().responseBody("{'message': 'Hello World!'}").build();
    Mock testMock = Mock.builder().responseContentType(responseContentType).statusCode(200).textualResponse(textualResponse).responseHeaders(MocksGenerator.generateResponseHeader()).binaryResponse(null).build();
    DomainModelForMock mappedModel = domainModelMapper.mappedModel(testMock);
    assertEquals("{'message': 'Hello World!'}", mappedModel.getResponseBody());
    assertEquals(200, mappedModel.getStatusCode());
    assertEquals("application/json", mappedModel.getResponseContentType());
    assertEquals(TypeOfResponse.TEXTUAL_RESPONSE, mappedModel.getTypeOfResponse());
    assertNotNull(mappedModel.getResponseHeaders());
}
Also used : DomainModelForMock(com.arbindo.mimock.generic.model.DomainModelForMock) TextualResponse(com.arbindo.mimock.entities.TextualResponse) ResponseContentType(com.arbindo.mimock.entities.ResponseContentType) DomainModelForMock(com.arbindo.mimock.generic.model.DomainModelForMock) Mock(com.arbindo.mimock.entities.Mock) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

DomainModelForMock (com.arbindo.mimock.generic.model.DomainModelForMock)37 Test (org.junit.jupiter.api.Test)33 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)33 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)12 Mock (com.arbindo.mimock.entities.Mock)10 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8 ResponseContentType (com.arbindo.mimock.entities.ResponseContentType)6 TextualResponse (com.arbindo.mimock.entities.TextualResponse)6 IOException (java.io.IOException)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 BinaryResponse (com.arbindo.mimock.entities.BinaryResponse)2 HttpMethod (com.arbindo.mimock.entities.HttpMethod)2