Replace cambria with DmaaP client
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / common / publishing / MessageRouterHttpStatusMapperTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * VES Collector
4  * ================================================================================
5  * Copyright (C) 2021 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.onap.dcae.common.publishing;
21
22 import org.junit.jupiter.api.Test;
23 import org.junit.jupiter.params.ParameterizedTest;
24 import org.junit.jupiter.params.provider.EnumSource;
25 import org.onap.dcae.common.model.BackwardsCompatibilityException;
26 import org.onap.dcae.common.model.InternalException;
27 import org.onap.dcae.common.model.PayloadToLargeException;
28 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishResponse;
29 import org.springframework.http.HttpStatus;
30
31 import static org.junit.jupiter.api.Assertions.assertSame;
32 import static org.junit.jupiter.api.Assertions.assertThrows;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
35 import static org.onap.dcae.ApplicationSettings.responseCompatibility;
36 import static org.onap.dcae.common.publishing.MessageRouterHttpStatusMapper.getHttpStatus;
37
38 class MessageRouterHttpStatusMapperTest {
39
40     public static final String BACKWARDS_COMPATIBILITY = "v7.2";
41     public static final String BACKWARDS_COMPATIBILITY_NONE = "NONE";
42
43     @Test
44     void ves_shouldResponse202() {
45         //given
46         responseCompatibility = BACKWARDS_COMPATIBILITY;
47         MessageRouterPublishResponse messageRouterPublishResponse = mock(MessageRouterPublishResponse.class);
48         when(messageRouterPublishResponse.successful()).thenReturn(true);
49
50         //when
51         HttpStatus httpStatusResponse = getHttpStatus(messageRouterPublishResponse);
52
53         //then
54         assertSame(HttpStatus.ACCEPTED, httpStatusResponse);
55     }
56
57     @ParameterizedTest
58     @EnumSource(
59             value = HttpStatus.class,
60             names = {"NOT_FOUND", "REQUEST_TIMEOUT", "TOO_MANY_REQUESTS", "INTERNAL_SERVER_ERROR", "BAD_GATEWAY",
61                     "SERVICE_UNAVAILABLE", "GATEWAY_TIMEOUT","PAYLOAD_TOO_LARGE"}
62     )
63     void ves_shouldMapErrorsToBackwardsCompatibility(HttpStatus httpStatus) {
64         //given
65         responseCompatibility = BACKWARDS_COMPATIBILITY;
66         MessageRouterPublishResponse messageRouterPublishResponse = mock(MessageRouterPublishResponse.class);
67         when(messageRouterPublishResponse.failReason()).thenReturn(httpStatus.toString());
68
69         //when
70         //then
71         assertThrows(BackwardsCompatibilityException.class,()->getHttpStatus(messageRouterPublishResponse));
72     }
73
74     @Test
75     void ves_shouldResponse200WhenBackwardsCompatibilityIsNone() {
76         //given
77         responseCompatibility = BACKWARDS_COMPATIBILITY_NONE;
78         MessageRouterPublishResponse messageRouterPublishResponse = mock(MessageRouterPublishResponse.class);
79         when(messageRouterPublishResponse.successful()).thenReturn(true);
80
81         //when
82         HttpStatus httpStatusResponse = getHttpStatus(messageRouterPublishResponse);
83
84         //then
85         assertSame(HttpStatus.OK, httpStatusResponse);
86     }
87
88     @Test
89     void ves_shouldHandleError413WhenBackwardsCompatibilityIsNone() {
90         //given
91         responseCompatibility = BACKWARDS_COMPATIBILITY_NONE;
92         MessageRouterPublishResponse messageRouterPublishResponse = mock(MessageRouterPublishResponse.class);
93         when(messageRouterPublishResponse.failReason()).thenReturn(HttpStatus.PAYLOAD_TOO_LARGE.toString());
94
95         //when
96         //then
97         assertThrows(PayloadToLargeException.class,()->getHttpStatus(messageRouterPublishResponse));
98     }
99
100     @ParameterizedTest
101     @EnumSource(
102             value = HttpStatus.class,
103             names = {"NOT_FOUND", "REQUEST_TIMEOUT", "TOO_MANY_REQUESTS", "INTERNAL_SERVER_ERROR", "BAD_GATEWAY",
104                     "SERVICE_UNAVAILABLE", "GATEWAY_TIMEOUT"}
105     )
106     void ves_shouldMapErrorsTo503WhenBackwardsCompatibilityIsNone(HttpStatus httpStatus) {
107         //given
108         responseCompatibility = BACKWARDS_COMPATIBILITY_NONE;
109         MessageRouterPublishResponse messageRouterPublishResponse = mock(MessageRouterPublishResponse.class);
110         when(messageRouterPublishResponse.failReason()).thenReturn(httpStatus.toString());
111
112         //when
113         //then
114         assertThrows(InternalException.class,()->getHttpStatus(messageRouterPublishResponse));
115     }
116 }