7aae84ebd7207aab88ae85de50b1c3b20b84bcc9
[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.BeforeEach;
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 org.mockito.junit.jupiter.MockitoSettings;
29 import org.mockito.quality.Strictness;
30 import javax.ws.rs.container.ContainerRequestContext;
31 import javax.ws.rs.container.ContainerResponseContext;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.MultivaluedMap;
34 import java.io.IOException;
35 import static org.junit.Assert.assertEquals;
36 import static org.junit.Assert.assertNull;
37 import static org.mockito.Mockito.when;
38
39 @ExtendWith(MockitoExtension.class)
40 public class ResponseHeaderManipulationTest {
41
42     @InjectMocks
43     private ResponseHeaderManipulation responseHeaderManipulation;
44
45     @Mock
46     private ContainerRequestContext mockRequestContext;
47
48     @Mock
49     private ContainerResponseContext mockResponseContext;
50
51     @Mock
52     private MultivaluedMap<String, Object> mockResponseHeaders;
53
54     @BeforeEach
55     public void setUp() {
56         when(mockResponseContext.getHeaders()).thenReturn(mockResponseHeaders);
57     }
58
59     @Test
60     public void testFilterWithContentType() throws IOException {
61         when(mockResponseContext.getHeaderString("Content-Type")).thenReturn(MediaType.APPLICATION_JSON);
62         responseHeaderManipulation.filter(mockRequestContext, mockResponseContext);
63
64         assertEquals("application/json",mockResponseContext.getHeaderString("Content-Type"));
65     }
66
67     @Test
68     public void testFilterWithNoContentType() throws IOException {
69         // Arrange: Simulate Accept header with "*/*" and no Content-Type set
70         when(mockRequestContext.getHeaderString("Accept")).thenReturn("*/*");
71
72         responseHeaderManipulation.filter(mockRequestContext, mockResponseContext);
73
74         assertEquals("*/*",mockRequestContext.getHeaderString("Accept"));
75     }
76
77     @Test
78     public void testFilterWithNullAcceptHeader() throws IOException {
79         // Arrange: Simulate null Accept header
80         when(mockRequestContext.getHeaderString("Accept")).thenReturn(null);
81         responseHeaderManipulation.filter(mockRequestContext, mockResponseContext);
82
83         assertNull(mockRequestContext.getHeaderString("Accept"));
84     }
85
86     @Test
87     public void testFilterWithEmptyAcceptHeader() throws IOException {
88         // Arrange: Simulate empty Accept header and no Content-Type set
89         when(mockRequestContext.getHeaderString("Accept")).thenReturn("");
90         when(mockResponseContext.getHeaderString("Content-Type")).thenReturn(null);
91
92         responseHeaderManipulation.filter(mockRequestContext, mockResponseContext);
93         assertEquals("", mockRequestContext.getHeaderString("Accept"));
94     }
95
96 }