2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2025 Deutsche Telekom. 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.aai.schemaservice.interceptors.post;
22 import org.junit.jupiter.api.Assertions;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.ExtendWith;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import javax.ws.rs.container.ContainerRequestContext;
29 import javax.ws.rs.container.ContainerResponseContext;
30 import java.io.IOException;
31 import static org.junit.Assert.assertEquals;
32 import static org.mockito.Mockito.*;
34 @ExtendWith(MockitoExtension.class)
35 public class InvalidResponseStatusTest {
38 private InvalidResponseStatus invalidResponseStatus;
41 private ContainerRequestContext mockRequestContext;
44 private ContainerResponseContext mockResponseContext;
48 public void testFilter_ResponseStatus405_ShouldChangeTo400() throws IOException {
49 when(mockResponseContext.getStatus()).thenReturn(405);
51 invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
53 verify(mockResponseContext).setStatus(400);
55 verify(mockResponseContext).setEntity(anyString());
59 public void testFilter_ResponseStatus405_ShouldHandleContentType() throws IOException {
60 String contentType = "application/json";
61 when(mockResponseContext.getStatus()).thenReturn(405);
62 when(mockResponseContext.getHeaderString("Content-Type")).thenReturn(contentType);
64 invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
66 verify(mockResponseContext).setStatus(400);
68 verify(mockResponseContext).setEntity(anyString());
69 assertEquals("application/json",mockResponseContext.getHeaderString("Content-Type"));
73 public void testFilter_ContentTypeSetToNull_ShouldSetXmlContentType() throws IOException {
74 // Setup: Simulate 405 status and no Content-Type header
75 when(mockResponseContext.getStatus()).thenReturn(405);
76 when(mockResponseContext.getHeaderString("Content-Type")).thenReturn(null);
78 invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
80 verify(mockResponseContext).setEntity(anyString());
81 Assertions.assertNull(mockResponseContext.getHeaderString("Content-Type"));
85 public void testFilter_ResponseStatus406_ContentTypeXml_ShouldSetXmlMessage() throws IOException {
86 when(mockResponseContext.getStatus()).thenReturn(406);
87 when(mockResponseContext.getHeaderString("Content-Type")).thenReturn("application/xml");
89 invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
91 verify(mockResponseContext).setStatus(406);
93 verify(mockResponseContext).setEntity(anyString());
94 assertEquals("application/xml",mockResponseContext.getHeaderString("Content-Type"));
98 public void testFilter_ResponseStatus406_ContentTypeJson_ShouldSetJsonMessage() throws IOException {
99 // Setup: Simulate a 406 status and Content-Type header as "application/json"
100 when(mockResponseContext.getStatus()).thenReturn(406);
101 when(mockResponseContext.getHeaderString("Content-Type")).thenReturn("application/json");
103 invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
105 verify(mockResponseContext).setStatus(406);
107 verify(mockResponseContext).setEntity(anyString());
108 assertEquals("application/json",mockResponseContext.getHeaderString("Content-Type"));
112 public void testFilter_ResponseStatus406_ContentTypeUnsupported_ShouldReturnErrorMessage() throws IOException {
113 when(mockResponseContext.getStatus()).thenReturn(406);
114 when(mockResponseContext.getHeaderString("Content-Type")).thenReturn("application/unsupported");
116 invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
118 verify(mockResponseContext).setStatus(406);
120 verify(mockResponseContext).setEntity(anyString());
121 assertEquals("application/unsupported",mockResponseContext.getHeaderString("Content-Type"));