2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.common.publishing;
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;
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;
38 class MessageRouterHttpStatusMapperTest {
40 public static final String BACKWARDS_COMPATIBILITY = "v7.2";
41 public static final String BACKWARDS_COMPATIBILITY_NONE = "NONE";
44 void ves_shouldResponse202() {
46 responseCompatibility = BACKWARDS_COMPATIBILITY;
47 MessageRouterPublishResponse messageRouterPublishResponse = mock(MessageRouterPublishResponse.class);
48 when(messageRouterPublishResponse.successful()).thenReturn(true);
51 HttpStatus httpStatusResponse = getHttpStatus(messageRouterPublishResponse);
54 assertSame(HttpStatus.ACCEPTED, httpStatusResponse);
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"}
63 void ves_shouldMapErrorsToBackwardsCompatibility(HttpStatus httpStatus) {
65 responseCompatibility = BACKWARDS_COMPATIBILITY;
66 MessageRouterPublishResponse messageRouterPublishResponse = mock(MessageRouterPublishResponse.class);
67 when(messageRouterPublishResponse.failReason()).thenReturn(httpStatus.toString());
71 assertThrows(BackwardsCompatibilityException.class,()->getHttpStatus(messageRouterPublishResponse));
75 void ves_shouldResponse200WhenBackwardsCompatibilityIsNone() {
77 responseCompatibility = BACKWARDS_COMPATIBILITY_NONE;
78 MessageRouterPublishResponse messageRouterPublishResponse = mock(MessageRouterPublishResponse.class);
79 when(messageRouterPublishResponse.successful()).thenReturn(true);
82 HttpStatus httpStatusResponse = getHttpStatus(messageRouterPublishResponse);
85 assertSame(HttpStatus.OK, httpStatusResponse);
89 void ves_shouldHandleError413WhenBackwardsCompatibilityIsNone() {
91 responseCompatibility = BACKWARDS_COMPATIBILITY_NONE;
92 MessageRouterPublishResponse messageRouterPublishResponse = mock(MessageRouterPublishResponse.class);
93 when(messageRouterPublishResponse.failReason()).thenReturn(HttpStatus.PAYLOAD_TOO_LARGE.toString());
97 assertThrows(PayloadToLargeException.class,()->getHttpStatus(messageRouterPublishResponse));
102 value = HttpStatus.class,
103 names = {"NOT_FOUND", "REQUEST_TIMEOUT", "TOO_MANY_REQUESTS", "INTERNAL_SERVER_ERROR", "BAD_GATEWAY",
104 "SERVICE_UNAVAILABLE", "GATEWAY_TIMEOUT"}
106 void ves_shouldMapErrorsTo503WhenBackwardsCompatibilityIsNone(HttpStatus httpStatus) {
108 responseCompatibility = BACKWARDS_COMPATIBILITY_NONE;
109 MessageRouterPublishResponse messageRouterPublishResponse = mock(MessageRouterPublishResponse.class);
110 when(messageRouterPublishResponse.failReason()).thenReturn(httpStatus.toString());
114 assertThrows(InternalException.class,()->getHttpStatus(messageRouterPublishResponse));