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.glassfish.jersey.server.ContainerException;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.onap.aai.schemaservice.interceptors.AAIHeaderProperties;
29 import javax.ws.rs.container.ContainerRequestContext;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.MultivaluedMap;
32 import javax.ws.rs.core.UriInfo;
33 import java.io.ByteArrayInputStream;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.UnsupportedEncodingException;
38 import java.util.Arrays;
39 import java.util.List;
40 import static org.junit.jupiter.api.Assertions.*;
41 import static org.mockito.Mockito.*;
43 class RequestTransactionLoggingTest {
46 RequestTransactionLogging requestTransactionLogging;
49 ContainerRequestContext containerRequestContext;
55 private MultivaluedMap<String, String> mockHeaders;
60 MockitoAnnotations.openMocks(this);
64 public void testFilterMethod() throws UnsupportedEncodingException {
66 String contentType = "application/json";
67 String expectedTxId = "12345";
68 String request ="request_id";
69 String aaiRequestTs="aaiRequestId";
70 String payload = "{\"key\":\"value\"}";
72 List<MediaType> acceptHeaderValues = Arrays.asList(MediaType.APPLICATION_JSON_TYPE);
74 when(containerRequestContext.getHeaderString("Content-Type")).thenReturn(contentType);
75 when(containerRequestContext.getAcceptableMediaTypes()).thenReturn(acceptHeaderValues);
76 when(containerRequestContext.getProperty(AAIHeaderProperties.AAI_TX_ID)).thenReturn(expectedTxId);
77 when(containerRequestContext.getProperty(AAIHeaderProperties.AAI_REQUEST)).thenReturn(request);
78 when(containerRequestContext.getProperty(AAIHeaderProperties.AAI_REQUEST_TS)).thenReturn(aaiRequestTs);
80 when(containerRequestContext.getHeaders()).thenReturn(mockHeaders);
82 when(mockHeaders.containsKey("Content-Type")).thenReturn(false);
83 when(mockHeaders.containsKey("Accept")).thenReturn(true);
85 // Mocking UriInfo methods
86 when(containerRequestContext.getUriInfo()).thenReturn(uriInfoMock);
87 when(uriInfoMock.getRequestUri()).thenReturn(URI.create("http://localhost/some/other/path"));
88 when(uriInfoMock.getPath()).thenReturn("/some/other/path");
90 InputStream inputStream = new ByteArrayInputStream(payload.getBytes("UTF-8"));
91 when(containerRequestContext.getEntityStream()).thenReturn(inputStream);
93 requestTransactionLogging.filter(containerRequestContext);
95 // Assertions for headers
96 assertNotNull(mockHeaders, "Headers should not be null.");
97 assertNull(mockHeaders.getFirst("Content-Type"), "Content-Type should initially be null.");
101 public void testFilterMethodWithIOException() throws IOException {
103 String contentType = "application/json";
104 String expectedTxId = "12345";
105 String request = "request_id";
106 String aaiRequestTs = "aaiRequestId";
107 String payload = "{\"key\":\"value\"}";
109 List<MediaType> acceptHeaderValues = Arrays.asList(MediaType.APPLICATION_JSON_TYPE);
111 // Mocking containerRequestContext methods
112 when(containerRequestContext.getHeaderString("Content-Type")).thenReturn(contentType);
113 when(containerRequestContext.getAcceptableMediaTypes()).thenReturn(acceptHeaderValues);
114 when(containerRequestContext.getProperty(AAIHeaderProperties.AAI_TX_ID)).thenReturn(expectedTxId);
115 when(containerRequestContext.getProperty(AAIHeaderProperties.AAI_REQUEST)).thenReturn(request);
116 when(containerRequestContext.getProperty(AAIHeaderProperties.AAI_REQUEST_TS)).thenReturn(aaiRequestTs);
118 // Mocking the getHeaders() method to return a non-null headers object
119 when(containerRequestContext.getHeaders()).thenReturn(mockHeaders);
120 when(mockHeaders.getFirst("Content-Type")).thenReturn(null); // This is to simulate the header being absent
121 when(mockHeaders.getFirst("Accept")).thenReturn("application/xml");
123 doNothing().when(mockHeaders).putSingle(anyString(), anyString());
124 when(mockHeaders.containsKey("Content-Type")).thenReturn(false);
125 when(mockHeaders.containsKey("Accept")).thenReturn(true);
127 // Mocking UriInfo methods
128 when(containerRequestContext.getUriInfo()).thenReturn(uriInfoMock);
129 when(uriInfoMock.getRequestUri()).thenReturn(URI.create("http://localhost/some/other/path"));
130 when(uriInfoMock.getPath()).thenReturn("/some/other/path");
132 // Mocking getEntityStream to throw an IOException
133 InputStream inputStream = mock(InputStream.class);
134 when(inputStream.available()).thenThrow(new IOException("Test IOException"));
135 when(containerRequestContext.getEntityStream()).thenReturn(inputStream);
137 assertThrows(ContainerException.class, () -> {
138 requestTransactionLogging.filter(containerRequestContext);