Search in sources :

Example 6 with MockResponse

use of com.sequenceiq.mock.spi.MockResponse in project cloudbreak by hortonworks.

the class ResponseModifierServiceTest method testEvaluateResponseSamePathFirstWithZeroTimes.

@Test
public // matter the order of the responses with the same path. Can add the response with the same code.
void testEvaluateResponseSamePathFirstWithZeroTimes() {
    MockResponse mockResponse1 = createMockResponse(0, 400, "/path1", "get", "message1");
    MockResponse mockResponse3 = createMockResponse(1, 200, "/path1", "get", "message2");
    underTest.addResponse(mockResponse1);
    underTest.addResponse(mockResponse3);
    String assertPath = "get_/path1";
    assertException(assertPath, "400 message1", HttpStatus.BAD_REQUEST);
    assertException(assertPath, "400 message1", HttpStatus.BAD_REQUEST);
    assertException(assertPath, "400 message1", HttpStatus.BAD_REQUEST);
    List<MockResponse> actualResponses = underTest.getResponse(assertPath);
    assertEquals(actualResponses.get(1).getPath(), "/path1");
    assertEquals(actualResponses.get(1).getHttpMethod(), "get");
    assertEquals(actualResponses.get(1).getMessage(), "message2");
    assertEquals(actualResponses.get(1).getTimes(), 1);
    assertEquals(actualResponses.get(1).getStatusCode(), 200);
}
Also used : MockResponse(com.sequenceiq.mock.spi.MockResponse) Test(org.junit.jupiter.api.Test)

Example 7 with MockResponse

use of com.sequenceiq.mock.spi.MockResponse in project cloudbreak by hortonworks.

the class ResponseModifierService method evaluateResponse.

/**
 * Evaluate the response by URI an http method if it appears in the pre-defined response list. The order of the responses matter with the same path and
 * method. (Check the unit tests)
 * Evaluate the times:
 * 0: will be permanently in the list, the next ones with the same path and method will be never evaluate
 * 1...n: evaluate n times. If reach the n-th, the response will be removed from the list of path.
 * <p>
 * Evaluate the response or exception
 * Only if the the status code is 200 return with the response.
 * If the status code is null, throw a NotFound status.
 *
 * @param path            the http method and URI separated by `_`. e.g: get_/path
 * @param defaultResponse supplier for the default response if the response cannot be found in the pre-defined list
 * @param <T>             Type of the default value
 * @return return with the default values or throw an exception based on the pre-defined status code
 * @throws Throwable if any errors occurred in the supplier.
 */
public <T> T evaluateResponse(String path, Class<?> returnType, CheckedSupplier<T, Throwable> defaultResponse) throws Throwable {
    List<MockResponse> mockResponses = responses.get(path);
    if (CollectionUtils.isEmpty(mockResponses)) {
        LOGGER.debug("Cannot find mock response, call the default. Path: {}", path);
        return defaultResponse.get();
    }
    MockResponse mockResponse = mockResponses.get(0);
    handleTimes(path, mockResponse);
    return parseStatusCode(returnType, mockResponse);
}
Also used : MockResponse(com.sequenceiq.mock.spi.MockResponse)

Example 8 with MockResponse

use of com.sequenceiq.mock.spi.MockResponse in project cloudbreak by hortonworks.

the class ResponseModifierServiceTest method testEvaluateResponseWhenTransformResponsePackage.

@Test
public void testEvaluateResponseWhenTransformResponsePackage() throws Throwable {
    MockResponse mockResponse1 = createMockResponse(0, 200, "/path1", "get", null, new ApiCommand().id(Integer.valueOf(1)));
    mockResponse1.setClss("com.cloudera.api.swagger.model.ApiCommand");
    underTest.addResponse(mockResponse1);
    ApiCommand actualPost = underTest.evaluateResponse("get_/path1", ApiCommand.class, () -> null);
    assertEquals(actualPost.getId(), Integer.valueOf(1));
}
Also used : MockResponse(com.sequenceiq.mock.spi.MockResponse) ApiCommand(com.sequenceiq.mock.swagger.model.ApiCommand) Test(org.junit.jupiter.api.Test)

Example 9 with MockResponse

use of com.sequenceiq.mock.spi.MockResponse in project cloudbreak by hortonworks.

the class ResponseModifierServiceTest method testEvaluateResponseWhenResponseMapButTheReturnTypePublicKey.

@Test
public void testEvaluateResponseWhenResponseMapButTheReturnTypePublicKey() throws Throwable {
    MockResponse mockResponse1 = createMockResponse(0, 200, "/path1", "get", null, Map.of("publicKeyId", "id", "publicKey", "hash"), Map.class);
    underTest.addResponse(mockResponse1);
    PublicKey actualPost = underTest.evaluateResponse("get_/path1", PublicKey.class, () -> null);
    assertEquals(actualPost.getPublicKey(), "hash");
    assertEquals(actualPost.getPublicKeyId(), "id");
}
Also used : MockResponse(com.sequenceiq.mock.spi.MockResponse) PublicKey(com.sequenceiq.mock.spi.controller.PublicKey) Test(org.junit.jupiter.api.Test)

Example 10 with MockResponse

use of com.sequenceiq.mock.spi.MockResponse in project cloudbreak by hortonworks.

the class ResponseModifierServiceTest method testEvaluateResponseSamePathDifferentMethodsDifferentCodes.

@Test
public // matter the order of the responses with the same path.
void testEvaluateResponseSamePathDifferentMethodsDifferentCodes() {
    MockResponse mockResponse1 = createMockResponse(1, 400, "/path1", "get", "message1");
    MockResponse mockResponse2 = createMockResponse(1, 500, "/path1", "post", "message2");
    MockResponse mockResponse3 = createMockResponse(1, 404, "/path1", "delete", "message3");
    underTest.addResponse(mockResponse1);
    underTest.addResponse(mockResponse2);
    underTest.addResponse(mockResponse3);
    assertException("get_/path1", "400 message1", HttpStatus.BAD_REQUEST);
    assertException("post_/path1", "500 message2", HttpStatus.INTERNAL_SERVER_ERROR);
    assertException("delete_/path1", "404 message3", HttpStatus.NOT_FOUND);
    assertNull(underTest.getResponse("get_/path1"));
    assertNull(underTest.getResponse("post_/path1"));
    assertNull(underTest.getResponse("delete_/path1"));
}
Also used : MockResponse(com.sequenceiq.mock.spi.MockResponse) Test(org.junit.jupiter.api.Test)

Aggregations

MockResponse (com.sequenceiq.mock.spi.MockResponse)13 Test (org.junit.jupiter.api.Test)11 PublicKey (com.sequenceiq.mock.spi.controller.PublicKey)2 ApiCommand (com.sequenceiq.mock.swagger.model.ApiCommand)1