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