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