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;
32 import static org.junit.jupiter.api.Assertions.assertEquals;
33 import static org.mockito.Mockito.*;
35 @ExtendWith(MockitoExtension.class)
36 public class InvalidResponseStatusTest {
39 private InvalidResponseStatus invalidResponseStatus;
42 private ContainerRequestContext mockRequestContext;
45 private ContainerResponseContext mockResponseContext;
49 public void testFilter_ResponseStatus405_ShouldChangeTo400() throws IOException {
50 when(mockResponseContext.getStatus()).thenReturn(405);
52 invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
54 verify(mockResponseContext).setStatus(400);
56 verify(mockResponseContext).setEntity(anyString());
60 public void testFilter_ResponseStatus405_ShouldHandleContentType() throws IOException {
61 String contentType = "application/json";
62 when(mockResponseContext.getStatus()).thenReturn(405);
63 when(mockResponseContext.getHeaderString("Content-Type")).thenReturn(contentType);
65 invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
67 verify(mockResponseContext).setStatus(400);
69 verify(mockResponseContext).setEntity(anyString());
70 assertEquals("application/json",mockResponseContext.getHeaderString("Content-Type"));
74 public void testFilter_ContentTypeSetToNull_ShouldSetXmlContentType() throws IOException {
75 // Setup: Simulate 405 status and no Content-Type header
76 when(mockResponseContext.getStatus()).thenReturn(405);
77 when(mockResponseContext.getHeaderString("Content-Type")).thenReturn(null);
79 invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
81 verify(mockResponseContext).setEntity(anyString());
82 Assertions.assertNull(mockResponseContext.getHeaderString("Content-Type"));
86 public void testFilter_ResponseStatus406_ContentTypeXml_ShouldSetXmlMessage() throws IOException {
87 when(mockResponseContext.getStatus()).thenReturn(406);
88 when(mockResponseContext.getHeaderString("Content-Type")).thenReturn("application/xml");
90 invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
92 verify(mockResponseContext).setStatus(406);
94 verify(mockResponseContext).setEntity(anyString());
95 assertEquals("application/xml",mockResponseContext.getHeaderString("Content-Type"));
99 public void testFilter_ResponseStatus406_ContentTypeJson_ShouldSetJsonMessage() throws IOException {
100 // Setup: Simulate a 406 status and Content-Type header as "application/json"
101 when(mockResponseContext.getStatus()).thenReturn(406);
102 when(mockResponseContext.getHeaderString("Content-Type")).thenReturn("application/json");
104 invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
106 verify(mockResponseContext).setStatus(406);
108 verify(mockResponseContext).setEntity(anyString());
109 assertEquals("application/json",mockResponseContext.getHeaderString("Content-Type"));
113 public void testFilter_ResponseStatus406_ContentTypeUnsupported_ShouldReturnErrorMessage() throws IOException {
114 when(mockResponseContext.getStatus()).thenReturn(406);
115 when(mockResponseContext.getHeaderString("Content-Type")).thenReturn("application/unsupported");
117 invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
119 verify(mockResponseContext).setStatus(406);
121 verify(mockResponseContext).setEntity(anyString());
122 assertEquals("application/unsupported",mockResponseContext.getHeaderString("Content-Type"));