8634cd3720d246a11daffa34d7965f09292dd356
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdcrests.errors;
21
22 import static org.junit.Assert.assertEquals;
23
24 import java.util.HashSet;
25 import java.util.Set;
26 import javax.validation.ConstraintViolation;
27 import javax.validation.ConstraintViolationException;
28 import javax.ws.rs.core.Response;
29 import org.codehaus.jackson.map.JsonMappingException;
30 import org.hibernate.validator.internal.engine.path.PathImpl;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.openecomp.sdc.common.errors.CoreException;
37 import org.openecomp.sdc.common.errors.ErrorCategory;
38 import org.openecomp.sdc.common.errors.ErrorCode;
39 import org.openecomp.sdc.common.errors.ErrorCode.ErrorCodeBuilder;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class DefaultExceptionMapperTest {
43
44     private static final String TEST_MESSAGE = "Test message";
45
46     @Mock
47     private ConstraintViolation<String> constraintViolation;
48     private PathImpl path = PathImpl.createRootPath();
49
50     @Test
51     public void shouldMapCoreExceptionToResponse() {
52         DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper();
53         ErrorCode errorCode = new ErrorCodeBuilder().withId("VSP_NOT_FOUND").withCategory(ErrorCategory.APPLICATION).build();
54         CoreException exception = new CoreException(errorCode);
55         Response response = defaultExceptionMapper.toResponse(exception);
56         assertEquals(response.getStatus(), 404);
57     }
58
59     @Test
60     public void shouldMapConstraintViolationExceptionToResponse() {
61         Mockito.when(constraintViolation.getPropertyPath()).thenReturn(path);
62         DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper();
63         Set<ConstraintViolation<String>> violations = new HashSet<>();
64         violations.add(constraintViolation);
65         ConstraintViolationException exception = new ConstraintViolationException(TEST_MESSAGE, violations);
66         Response response = defaultExceptionMapper.toResponse(exception);
67         assertEquals(response.getStatus(), 417);
68     }
69
70     @Test
71     public void shouldMapJsonMappingExceptionToResponse() {
72         DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper();
73         JsonMappingException exception = new JsonMappingException(TEST_MESSAGE);
74         Response response = defaultExceptionMapper.toResponse(exception);
75         assertEquals(response.getStatus(), 417);
76     }
77
78     @Test
79     public void shouldMapOtherExceptionToResponse() {
80         DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper();
81         Exception exception = new Exception(TEST_MESSAGE);
82         Response response = defaultExceptionMapper.toResponse(exception);
83         assertEquals(response.getStatus(), 500);
84     }
85 }