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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations