2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.schemaservice.interceptors.pre;
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.*;
32 class HttpHeaderInterceptorTest {
34 private HttpHeaderInterceptor httpHeaderInterceptor;
37 private ContainerRequestContext mockRequestContext;
41 MockitoAnnotations.openMocks(this);
42 httpHeaderInterceptor = new HttpHeaderInterceptor();
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");
51 httpHeaderInterceptor.filter(mockRequestContext);
53 verify(mockRequestContext).setMethod("PATCH");
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);
62 httpHeaderInterceptor.filter(mockRequestContext);
64 verify(mockRequestContext, never()).setMethod(anyString());
65 assertEquals("POST", mockRequestContext.getMethod());
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);
74 httpHeaderInterceptor.filter(mockRequestContext);
76 verify(mockRequestContext, never()).setMethod(anyString());
77 assertEquals("PATCH", mockRequestContext.getMethod());