7810fe8270e9aa55dfee17bae788701862cfd5e7
[aai/schema-service.git] /
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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.aai.schemaservice.interceptors.post;
21
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
32 import static org.junit.jupiter.api.Assertions.assertEquals;
33 import static org.mockito.Mockito.*;
34
35 @ExtendWith(MockitoExtension.class)
36 public class InvalidResponseStatusTest {
37
38     @InjectMocks
39     private InvalidResponseStatus invalidResponseStatus;
40
41     @Mock
42     private ContainerRequestContext mockRequestContext;
43
44     @Mock
45     private ContainerResponseContext mockResponseContext;
46
47
48     @Test
49     public void testFilter_ResponseStatus405_ShouldChangeTo400() throws IOException {
50         when(mockResponseContext.getStatus()).thenReturn(405);
51
52         invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
53
54         verify(mockResponseContext).setStatus(400);
55
56         verify(mockResponseContext).setEntity(anyString());
57     }
58
59     @Test
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);
64
65         invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
66
67         verify(mockResponseContext).setStatus(400);
68
69         verify(mockResponseContext).setEntity(anyString());
70         assertEquals("application/json",mockResponseContext.getHeaderString("Content-Type"));
71     }
72
73     @Test
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);
78
79         invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
80
81         verify(mockResponseContext).setEntity(anyString());
82         Assertions.assertNull(mockResponseContext.getHeaderString("Content-Type"));
83     }
84
85     @Test
86     public void testFilter_ResponseStatus406_ContentTypeXml_ShouldSetXmlMessage() throws IOException {
87         when(mockResponseContext.getStatus()).thenReturn(406);
88         when(mockResponseContext.getHeaderString("Content-Type")).thenReturn("application/xml");
89
90         invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
91
92         verify(mockResponseContext).setStatus(406);
93
94         verify(mockResponseContext).setEntity(anyString());
95         assertEquals("application/xml",mockResponseContext.getHeaderString("Content-Type"));
96     }
97
98     @Test
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");
103
104         invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
105
106         verify(mockResponseContext).setStatus(406);
107
108         verify(mockResponseContext).setEntity(anyString());
109         assertEquals("application/json",mockResponseContext.getHeaderString("Content-Type"));
110     }
111
112     @Test
113     public void testFilter_ResponseStatus406_ContentTypeUnsupported_ShouldReturnErrorMessage() throws IOException {
114         when(mockResponseContext.getStatus()).thenReturn(406);
115         when(mockResponseContext.getHeaderString("Content-Type")).thenReturn("application/unsupported");
116
117         invalidResponseStatus.filter(mockRequestContext, mockResponseContext);
118
119         verify(mockResponseContext).setStatus(406);
120
121         verify(mockResponseContext).setEntity(anyString());
122         assertEquals("application/unsupported",mockResponseContext.getHeaderString("Content-Type"));
123
124     }
125 }