0c701cd61a86a08cabfe6ecc9363615fe8f95e9e
[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.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;
37 import java.net.URI;
38 import java.util.Arrays;
39 import java.util.List;
40 import static org.junit.jupiter.api.Assertions.*;
41 import static org.mockito.Mockito.*;
42
43 class RequestTransactionLoggingTest {
44
45     @InjectMocks
46     RequestTransactionLogging requestTransactionLogging;
47
48     @Mock
49     ContainerRequestContext containerRequestContext;
50
51     @Mock
52     UriInfo uriInfoMock;
53
54     @Mock
55     private MultivaluedMap<String, String> mockHeaders;
56
57     @BeforeEach
58     void setUp() {
59         // Initialize mocks
60         MockitoAnnotations.openMocks(this);
61     }
62
63     @Test
64     public void testFilterMethod() throws UnsupportedEncodingException {
65
66         String contentType = "application/json";
67         String expectedTxId = "12345";
68         String request ="request_id";
69         String aaiRequestTs="aaiRequestId";
70         String payload = "{\"key\":\"value\"}";
71
72         List<MediaType> acceptHeaderValues = Arrays.asList(MediaType.APPLICATION_JSON_TYPE);
73
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);
79
80         when(containerRequestContext.getHeaders()).thenReturn(mockHeaders);
81
82         when(mockHeaders.containsKey("Content-Type")).thenReturn(false);
83         when(mockHeaders.containsKey("Accept")).thenReturn(true);
84
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");
89
90         InputStream inputStream = new ByteArrayInputStream(payload.getBytes("UTF-8"));
91         when(containerRequestContext.getEntityStream()).thenReturn(inputStream);
92
93         requestTransactionLogging.filter(containerRequestContext);
94
95         // Assertions for headers
96         assertNotNull(mockHeaders, "Headers should not be null.");
97         assertNull(mockHeaders.getFirst("Content-Type"), "Content-Type should initially be null.");
98     }
99
100     @Test
101     public void testFilterMethodWithIOException() throws IOException {
102
103         String contentType = "application/json";
104         String expectedTxId = "12345";
105         String request = "request_id";
106         String aaiRequestTs = "aaiRequestId";
107         String payload = "{\"key\":\"value\"}";
108
109         List<MediaType> acceptHeaderValues = Arrays.asList(MediaType.APPLICATION_JSON_TYPE);
110
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);
117
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");
122
123         doNothing().when(mockHeaders).putSingle(anyString(), anyString());
124         when(mockHeaders.containsKey("Content-Type")).thenReturn(false);
125         when(mockHeaders.containsKey("Accept")).thenReturn(true);
126
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");
131
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);
136
137         assertThrows(ContainerException.class, () -> {
138             requestTransactionLogging.filter(containerRequestContext);
139         });
140     }
141 }