Search in sources :

Example 1 with ExceptionResponse

use of com.sequenceiq.cloudbreak.common.exception.ExceptionResponse in project cloudbreak by hortonworks.

the class ConstraintViolationExceptionMapperTest method testToResponseWhenHasTwoValidationError.

@Test
public void testToResponseWhenHasTwoValidationError() {
    ConstraintViolation<Object> constraintViolation1 = mock(ConstraintViolation.class);
    ConstraintViolation<Object> constraintViolation2 = mock(ConstraintViolation.class);
    when(constraintViolation1.getMessage()).thenReturn("something validation error occurred");
    when(constraintViolation1.getPropertyPath()).thenReturn(PathImpl.createPathFromString("path.smgth"));
    when(constraintViolation2.getMessage()).thenReturn("other validation error happened");
    ConstraintViolationException exception = new ConstraintViolationException("Error message", Set.of(constraintViolation1, constraintViolation2));
    Response response = underTest.toResponse(exception);
    ExceptionResponse entity = (ExceptionResponse) response.getEntity();
    Assertions.assertTrue(entity.getMessage().contains("More than one validation errors happened: \n"));
    Assertions.assertTrue(entity.getMessage().contains("something validation error occurred"));
    Assertions.assertTrue(entity.getMessage().contains("other validation error happened"));
    List<String> result = ((List<ValidationResultResponse>) entity.getPayload()).stream().map(v -> v.getField() + ":" + v.getResult()).collect(Collectors.toList());
    Assertions.assertTrue(result.contains("path.smgth:something validation error occurred"));
    Assertions.assertTrue(result.contains(":other validation error happened"));
}
Also used : Response(javax.ws.rs.core.Response) ExceptionResponse(com.sequenceiq.cloudbreak.common.exception.ExceptionResponse) InjectMocks(org.mockito.InjectMocks) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Set(java.util.Set) JsonUtil(com.sequenceiq.cloudbreak.common.json.JsonUtil) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) PathImpl(org.hibernate.validator.internal.engine.path.PathImpl) Test(org.junit.jupiter.api.Test) List(java.util.List) ConstraintViolationException(javax.validation.ConstraintViolationException) Response(javax.ws.rs.core.Response) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions(org.junit.jupiter.api.Assertions) ExceptionResponse(com.sequenceiq.cloudbreak.common.exception.ExceptionResponse) ConstraintViolation(javax.validation.ConstraintViolation) Mockito.mock(org.mockito.Mockito.mock) ExceptionResponse(com.sequenceiq.cloudbreak.common.exception.ExceptionResponse) ConstraintViolationException(javax.validation.ConstraintViolationException) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 2 with ExceptionResponse

use of com.sequenceiq.cloudbreak.common.exception.ExceptionResponse in project cloudbreak by hortonworks.

the class FreeIpaServiceTest method internalDescribeFreeipaNotFoundTest.

@Test
void internalDescribeFreeipaNotFoundTest() {
    ExceptionResponse exceptionResponse = new ExceptionResponse("Freeipa not found");
    final Response response = mock(Response.class);
    Mockito.when(response.readEntity(Mockito.any(Class.class))).thenReturn(exceptionResponse);
    NotFoundException notFoundException = mock(NotFoundException.class);
    when(notFoundException.getResponse()).thenReturn(response);
    when(freeIpaV1Endpoint.describeInternal(eq(ENVCRN), eq("1111"))).thenThrow(notFoundException);
    Optional<DescribeFreeIpaResponse> describeFreeIpaResponse = underTest.internalDescribe(ENVCRN, "1111");
    assertThat(describeFreeIpaResponse.isEmpty()).isEqualTo(true);
}
Also used : ExceptionResponse(com.sequenceiq.cloudbreak.common.exception.ExceptionResponse) DescribeFreeIpaResponse(com.sequenceiq.freeipa.api.v1.freeipa.stack.model.describe.DescribeFreeIpaResponse) Response(javax.ws.rs.core.Response) DescribeFreeIpaResponse(com.sequenceiq.freeipa.api.v1.freeipa.stack.model.describe.DescribeFreeIpaResponse) ExceptionResponse(com.sequenceiq.cloudbreak.common.exception.ExceptionResponse) NotFoundException(javax.ws.rs.NotFoundException) Test(org.junit.jupiter.api.Test)

Example 3 with ExceptionResponse

use of com.sequenceiq.cloudbreak.common.exception.ExceptionResponse in project cloudbreak by hortonworks.

the class ConstraintViolationExceptionMapperTest method testToResponseWhenHasOneValidationError.

@Test
public void testToResponseWhenHasOneValidationError() {
    ConstraintViolation<Object> constraintViolation = mock(ConstraintViolation.class);
    when(constraintViolation.getMessage()).thenReturn("something validation error occurred");
    when(constraintViolation.getPropertyPath()).thenReturn(PathImpl.createPathFromString("path.smgth"));
    ConstraintViolationException exception = new ConstraintViolationException("Error message", Set.of(constraintViolation));
    Response response = underTest.toResponse(exception);
    ExceptionResponse entity = (ExceptionResponse) response.getEntity();
    String actual = JsonUtil.writeValueAsStringSilentSafe(entity);
    String expected = "{\"message\":\"something validation error occurred\"," + "\"payload\":[{\"field\":\"path.smgth\",\"result\":\"something validation error occurred\"}]}";
    Assertions.assertEquals(expected, actual);
}
Also used : Response(javax.ws.rs.core.Response) ExceptionResponse(com.sequenceiq.cloudbreak.common.exception.ExceptionResponse) ExceptionResponse(com.sequenceiq.cloudbreak.common.exception.ExceptionResponse) ConstraintViolationException(javax.validation.ConstraintViolationException) Test(org.junit.jupiter.api.Test)

Aggregations

ExceptionResponse (com.sequenceiq.cloudbreak.common.exception.ExceptionResponse)3 Response (javax.ws.rs.core.Response)3 Test (org.junit.jupiter.api.Test)3 ConstraintViolationException (javax.validation.ConstraintViolationException)2 JsonUtil (com.sequenceiq.cloudbreak.common.json.JsonUtil)1 DescribeFreeIpaResponse (com.sequenceiq.freeipa.api.v1.freeipa.stack.model.describe.DescribeFreeIpaResponse)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 ConstraintViolation (javax.validation.ConstraintViolation)1 NotFoundException (javax.ws.rs.NotFoundException)1 PathImpl (org.hibernate.validator.internal.engine.path.PathImpl)1 Assertions (org.junit.jupiter.api.Assertions)1 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)1 InjectMocks (org.mockito.InjectMocks)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.when (org.mockito.Mockito.when)1 MockitoExtension (org.mockito.junit.jupiter.MockitoExtension)1