45704437fbf1adaae3b3a14aa61e3b9537ca89ed
[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.pre;
21
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.onap.aai.schemaservice.interceptors.AAIHeaderProperties;
27 import javax.ws.rs.container.ContainerRequestContext;
28 import java.io.IOException;
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30 import static org.mockito.Mockito.*;
31
32 class HttpHeaderInterceptorTest {
33
34     private HttpHeaderInterceptor httpHeaderInterceptor;
35
36     @Mock
37     private ContainerRequestContext mockRequestContext;
38
39     @BeforeEach
40     void setUp() {
41         MockitoAnnotations.openMocks(this);
42         httpHeaderInterceptor = new HttpHeaderInterceptor();
43     }
44
45     @Test
46     void testFilter_WithPostMethodAndPatchOverride_ShouldSetMethodToPatch() throws IOException {
47         // Setup: Mock the method to POST and the HTTP_METHOD_OVERRIDE header to PATCH
48         when(mockRequestContext.getHeaderString(AAIHeaderProperties.HTTP_METHOD_OVERRIDE)).thenReturn("PATCH");
49         when(mockRequestContext.getMethod()).thenReturn("POST");
50
51         httpHeaderInterceptor.filter(mockRequestContext);
52
53         verify(mockRequestContext).setMethod("PATCH");
54     }
55
56     @Test
57     void testFilter_WithPostMethodAndNoOverride_ShouldNotChangeMethod() throws IOException {
58         // Setup: Mock the method to POST and no HTTP_METHOD_OVERRIDE header (null or empty)
59         when(mockRequestContext.getMethod()).thenReturn("POST");
60         when(mockRequestContext.getHeaderString(AAIHeaderProperties.HTTP_METHOD_OVERRIDE)).thenReturn(null);
61
62         httpHeaderInterceptor.filter(mockRequestContext);
63
64         verify(mockRequestContext, never()).setMethod(anyString());
65         assertEquals("POST", mockRequestContext.getMethod());
66     }
67
68     @Test
69     void testFilter_WithPatchMethodAndNoOverride_ShouldNotChangeMethod() throws IOException {
70         // Setup: Mock the method to PATCH and no override header
71         when(mockRequestContext.getMethod()).thenReturn("PATCH");
72         when(mockRequestContext.getHeaderString(AAIHeaderProperties.HTTP_METHOD_OVERRIDE)).thenReturn(null);
73
74         httpHeaderInterceptor.filter(mockRequestContext);
75
76         verify(mockRequestContext, never()).setMethod(anyString());
77         assertEquals("PATCH", mockRequestContext.getMethod());
78
79     }
80 }