2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.openecomp.sdc.fe.impl;
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;
30 import javax.servlet.http.HttpServletRequest;
31 import java.util.HashMap;
34 import static org.mockito.ArgumentMatchers.eq;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.when;
38 @RunWith(MockitoJUnitRunner.class)
39 public class AuditTest {
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";
50 private HttpRequestInfo info;
53 private HttpServletRequest request;
56 public void testErrorWithEmptyHttpRequestInfo() {
58 Audit.error(log, (HttpRequestInfo) null, 0);
61 verify(log).error(eq(INTERNAL_ERROR));
65 public void testErrorWithHttpRequestInfo() {
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]);
74 when(info.getHeaders()).thenReturn(headers);
75 when(info.getRequestURL()).thenReturn(PARAMETERS[5]);
78 Audit.error(log, info, STATUS);
81 verify(log).error(eq(EXPECTED));
85 public void testErrorWithEmptyHttpServletRequest() {
87 Audit.error(log, (HttpServletRequest) null, 0);
90 verify(log).error(eq(INTERNAL_ERROR));
94 public void testErrorWithHttpServletRequest() {
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]));
104 Audit.error(log, request, STATUS);
107 verify(log).error(eq(EXPECTED));
111 public void testInfoWithEmptyHttpRequestInfo() {
113 Audit.info(log, null, 0);
116 verify(log).info(eq(INTERNAL_ERROR));
120 public void testInfoWithHttpRequestInfo() {
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]);
129 when(info.getHeaders()).thenReturn(headers);
130 when(info.getRequestURL()).thenReturn(PARAMETERS[5]);
133 Audit.info(log, info, STATUS);
136 verify(log).info(eq(EXPECTED));