Catalog alignment
[sdc.git] / catalog-fe / src / test / java / org / openecomp / sdc / fe / impl / AuditTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Samsung. 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
21 package org.openecomp.sdc.fe.impl;
22
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.openecomp.sdc.common.api.Constants;
28 import org.slf4j.Logger;
29
30 import javax.servlet.http.HttpServletRequest;
31 import java.util.HashMap;
32 import java.util.Map;
33
34 import static org.mockito.ArgumentMatchers.eq;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.when;
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class AuditTest {
40
41     private static final int STATUS = 12345;
42     private static final String[] PARAMETERS = {"abc", "def", "ghi", "jkl", "mno", "http"};
43     private static final String EXPECTED = "EVENT = ARTIFACT_UPLOAD  USER_ID=abc USER_NAME=def ghi ACCESS_IP=jkl ACCESS_TYPE=mno RURL=http SC=12345";
44     private static final String INTERNAL_ERROR = "Internal Error";
45
46     @Mock
47     private Logger log;
48
49     @Mock
50     private HttpRequestInfo info;
51
52     @Mock
53     private HttpServletRequest request;
54
55     @Test
56     public void testErrorWithEmptyHttpRequestInfo() {
57         // when
58         Audit.error(log, (HttpRequestInfo) null, 0);
59
60         // then
61         verify(log).error(eq(INTERNAL_ERROR));
62     }
63
64     @Test
65     public void testErrorWithHttpRequestInfo() {
66         // given
67         Map<String, String> headers = new HashMap<>();
68         headers.put(Constants.USER_ID_HEADER, PARAMETERS[0]);
69         headers.put(Constants.FIRST_NAME_HEADER, PARAMETERS[1]);
70         headers.put(Constants.LAST_NAME_HEADER, PARAMETERS[2]);
71         headers.put(Constants.ORIGIN_HEADER, PARAMETERS[3]);
72         headers.put(Constants.ACCESS_HEADER, PARAMETERS[4]);
73
74         when(info.getHeaders()).thenReturn(headers);
75         when(info.getRequestURL()).thenReturn(PARAMETERS[5]);
76
77         // when
78         Audit.error(log, info, STATUS);
79
80         // then
81         verify(log).error(eq(EXPECTED));
82     }
83
84     @Test
85     public void testErrorWithEmptyHttpServletRequest() {
86         // when
87         Audit.error(log, (HttpServletRequest) null, 0);
88
89         // then
90         verify(log).error(eq(INTERNAL_ERROR));
91     }
92
93     @Test
94     public void testErrorWithHttpServletRequest() {
95         // given
96         when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn(PARAMETERS[0]);
97         when(request.getHeader(Constants.FIRST_NAME_HEADER)).thenReturn(PARAMETERS[1]);
98         when(request.getHeader(Constants.LAST_NAME_HEADER)).thenReturn(PARAMETERS[2]);
99         when(request.getHeader(Constants.ORIGIN_HEADER)).thenReturn(PARAMETERS[3]);
100         when(request.getHeader(Constants.ACCESS_HEADER)).thenReturn(PARAMETERS[4]);
101         when(request.getRequestURL()).thenReturn(new StringBuffer(PARAMETERS[5]));
102
103         // when
104         Audit.error(log, request, STATUS);
105
106         // then
107         verify(log).error(eq(EXPECTED));
108     }
109
110     @Test
111     public void testInfoWithEmptyHttpRequestInfo() {
112         // when
113         Audit.info(log, null, 0);
114
115         // then
116         verify(log).info(eq(INTERNAL_ERROR));
117     }
118
119     @Test
120     public void testInfoWithHttpRequestInfo() {
121         // given
122         Map<String, String> headers = new HashMap<>();
123         headers.put(Constants.USER_ID_HEADER, PARAMETERS[0]);
124         headers.put(Constants.FIRST_NAME_HEADER, PARAMETERS[1]);
125         headers.put(Constants.LAST_NAME_HEADER, PARAMETERS[2]);
126         headers.put(Constants.ORIGIN_HEADER, PARAMETERS[3]);
127         headers.put(Constants.ACCESS_HEADER, PARAMETERS[4]);
128
129         when(info.getHeaders()).thenReturn(headers);
130         when(info.getRequestURL()).thenReturn(PARAMETERS[5]);
131
132         // when
133         Audit.info(log, info, STATUS);
134
135         // then
136         verify(log).info(eq(EXPECTED));
137     }
138 }