Search in sources :

Example 6 with HttpMethod

use of com.arbindo.mimock.entities.HttpMethod in project mimock by arbindo.

the class GenericMockRequestService method serveMockRequest.

public DomainModelForMock serveMockRequest(GenericRequestModel request) throws MatchingMockNotFoundException, IOException {
    log.log(Level.INFO, "Fetching matching mock from the DB");
    String route = request.getRoute();
    HttpMethod httpMethod = httpMethod(request.getHttpMethod());
    String queryParam = request.getQueryParam();
    Optional<Mock> resultFromDB = getResultFromDB(route, httpMethod, queryParam);
    if (resultFromDB.isEmpty()) {
        log.log(Level.ERROR, "No matching rows returned from DB");
        String errorMessage = "Matching mock does not exist";
        throw new MatchingMockNotFoundException(errorMessage);
    }
    log.log(Level.INFO, "Returning matching mock");
    Mock matchingMock = resultFromDB.get();
    if (shouldValidateRequestHeaders(matchingMock, request)) {
        validateRequestHeaders(request, matchingMock);
    }
    if (shouldValidateRequestBody(matchingMock, request)) {
        validateRequestBody(matchingMock.getRequestBodiesForMock().getRequestBody(), request.getRequestBody());
    }
    return domainModelMapper.mappedModel(matchingMock);
}
Also used : HttpMethod(com.arbindo.mimock.entities.HttpMethod) DomainModelForMock(com.arbindo.mimock.generic.model.DomainModelForMock) Mock(com.arbindo.mimock.entities.Mock)

Example 7 with HttpMethod

use of com.arbindo.mimock.entities.HttpMethod in project mimock by arbindo.

the class HttpMethodsRepositoryTest method shouldReturnHttpMethodForValidMethod.

@ParameterizedTest
@ValueSource(strings = { "GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH" })
void shouldReturnHttpMethodForValidMethod(String method) {
    // Act
    HttpMethod httpMethod = httpMethodsRepository.findByMethod(method);
    // Assert
    assertNotNull(httpMethod);
    assertEquals(method, httpMethod.getMethod());
}
Also used : HttpMethod(com.arbindo.mimock.entities.HttpMethod) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with HttpMethod

use of com.arbindo.mimock.entities.HttpMethod in project mimock by arbindo.

the class HttpMethodsRepositoryTest method shouldReturnNullForInvalidMethodForSqlInjectionStrings.

@ParameterizedTest
@ValueSource(strings = { "get OR 1=1", "GET; DROP TABLE mocks;" })
void shouldReturnNullForInvalidMethodForSqlInjectionStrings(String method) {
    // Act
    HttpMethod httpMethod = httpMethodsRepository.findByMethod(method);
    // Assert
    assertNull(httpMethod);
}
Also used : HttpMethod(com.arbindo.mimock.entities.HttpMethod) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with HttpMethod

use of com.arbindo.mimock.entities.HttpMethod in project mimock by arbindo.

the class HttpMethodsRepositoryTest method shouldReturnNullForInvalidMethodWhenEmptyOrNull.

@ParameterizedTest
@EmptySource
@NullSource
void shouldReturnNullForInvalidMethodWhenEmptyOrNull(String method) {
    // Act
    HttpMethod httpMethod = httpMethodsRepository.findByMethod(method);
    // Assert
    assertNull(httpMethod);
}
Also used : HttpMethod(com.arbindo.mimock.entities.HttpMethod) EmptySource(org.junit.jupiter.params.provider.EmptySource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) NullSource(org.junit.jupiter.params.provider.NullSource)

Example 10 with HttpMethod

use of com.arbindo.mimock.entities.HttpMethod in project mimock by arbindo.

the class GetStaticRecordsControllerTest method listAllSupportedHttpMethods.

@Test
void listAllSupportedHttpMethods() throws Exception {
    String route = UrlConfig.STATIC_RECORDS_PATH + UrlConfig.HTTP_METHODS_STATIC_RECORDS;
    List<HttpMethod> httpMethodsFromDB = new ArrayList<>();
    HttpMethod getMethod = HttpMethod.builder().method("GET").build();
    HttpMethod postMethod = HttpMethod.builder().method("POST").build();
    httpMethodsFromDB.add(getMethod);
    httpMethodsFromDB.add(postMethod);
    lenient().when(mockGetStaticRecordsService.listAllSupportedHttpMethods()).thenReturn(httpMethodsFromDB);
    MvcResult result = mockMvc.perform(get(route).contentType(MediaType.APPLICATION_JSON_VALUE)).andExpect(status().isOk()).andReturn();
    String response = result.getResponse().getContentAsString();
    assertNotNull(response);
    ObjectMapper objectMapper = new ObjectMapper();
    List<HttpMethod> httpMethods = Arrays.asList(objectMapper.readValue(response, HttpMethod[].class));
    assertNotNull(httpMethods);
    assertFalse(httpMethods.isEmpty());
    assertEquals("GET", httpMethods.get(0).getMethod());
    assertEquals("POST", httpMethods.get(1).getMethod());
}
Also used : ArrayList(java.util.ArrayList) MvcResult(org.springframework.test.web.servlet.MvcResult) HttpMethod(com.arbindo.mimock.entities.HttpMethod) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Aggregations

HttpMethod (com.arbindo.mimock.entities.HttpMethod)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 ValueSource (org.junit.jupiter.params.provider.ValueSource)8 Mock (com.arbindo.mimock.entities.Mock)2 DomainModelForMock (com.arbindo.mimock.generic.model.DomainModelForMock)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 EmptySource (org.junit.jupiter.params.provider.EmptySource)2 NullSource (org.junit.jupiter.params.provider.NullSource)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 MvcResult (org.springframework.test.web.servlet.MvcResult)1