5f8319cf41e193c2f5bcd76a8e1cc08849f341cd
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / logging / aop / EPEELFLoggerAdviceTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.portal.logging.aop;
39
40 import static org.junit.Assert.assertNotEquals;
41
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.mockito.InjectMocks;
49 import org.mockito.Mock;
50 import org.mockito.Mockito;
51 import org.mockito.MockitoAnnotations;
52 import org.onap.portalapp.portal.core.MockEPUser;
53 import org.onap.portalapp.portal.domain.EPUser;
54 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
55 import org.onap.portalapp.portal.utils.EcompPortalUtils;
56 import org.onap.portalapp.util.EPUserUtils;
57 import org.onap.portalsdk.core.util.SystemProperties;
58 import org.onap.portalsdk.core.util.SystemProperties.SecurityEventTypeEnum;
59 import org.onap.portalsdk.core.web.support.AppUtils;
60 import org.powermock.api.mockito.PowerMockito;
61 import org.powermock.core.classloader.annotations.PrepareForTest;
62 import org.powermock.modules.junit4.PowerMockRunner;
63 import org.springframework.mock.web.MockHttpServletRequest;
64 import org.springframework.mock.web.MockHttpSession;
65
66 @RunWith(PowerMockRunner.class)
67 @PrepareForTest({ EcompPortalUtils.class, SystemProperties.class,
68                 EPCommonSystemProperties.class })
69 public class EPEELFLoggerAdviceTest {
70         
71         @Before
72         public void setup() {
73                 MockitoAnnotations.initMocks(this);
74         }
75         
76         @Mock
77         SecurityEventTypeEnum securityEventTypeEnum;
78         
79         @InjectMocks
80         EPEELFLoggerAdvice epEELFLoggerAdvice = new EPEELFLoggerAdvice();
81
82         @Test
83         @SuppressWarnings("static-access")
84         public void getCurrentDateTimeUTCTest(){
85                 String actual = epEELFLoggerAdvice.getCurrentDateTimeUTC();
86                 assertNotEquals("", actual);
87         }
88         
89         MockEPUser mockUser = new MockEPUser();
90         
91         @Test
92         public void loadServletRequestBasedDefaultsTest(){
93                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
94                 PowerMockito.mockStatic(EcompPortalUtils.class);
95                 PowerMockito.mockStatic(EPUserUtils.class);
96                 PowerMockito.mockStatic(AppUtils.class);
97                 PowerMockito.mockStatic(SystemProperties.class);
98                 EPUser user = mockUser.mockEPUser();
99                 MockHttpSession session = new MockHttpSession();
100                 session.setAttribute("user_attribute_name", user);
101                 MockHttpServletRequest request = new MockHttpServletRequest();
102                 request.setServletPath("http:test.com");
103                 request.setSession(session);
104                 request.addHeader("user-agent", "Mozilla/5.0");
105                 Mockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user_attribute_name");
106                 epEELFLoggerAdvice.loadServletRequestBasedDefaults(request, securityEventTypeEnum);
107         }
108         
109         @Test
110         public void afterTest(){
111                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
112                 PowerMockito.mockStatic(EcompPortalUtils.class);
113                 PowerMockito.mockStatic(EPUserUtils.class);
114                 PowerMockito.mockStatic(AppUtils.class);
115                 PowerMockito.mockStatic(SystemProperties.class);
116                 EPUser user = mockUser.mockEPUser();
117                 MockHttpSession session = new MockHttpSession();
118                 session.setAttribute("user_attribute_name", user);
119                 MockHttpServletRequest request = new MockHttpServletRequest();
120                 request.setServletPath("http:test.com");
121                 request.setSession(session);
122                 request.addHeader("user-agent", "Mozilla/5.0");
123                 Mockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user_attribute_name");
124                 List<Object> args = new ArrayList<>();
125                 args.add("testClassName");
126                 args.add("testMethodName");
127                 Object[] passOnArgs = args.toArray(new Object[args.size()]);
128                 List<Object> args2 = new ArrayList<>();
129                 args2.add(request);
130                 Object[] httpArgs = args2.toArray(new Object[args2.size()]);
131                 epEELFLoggerAdvice.after(securityEventTypeEnum, "200", "200",  httpArgs, null, passOnArgs);
132         }
133         
134         @Test
135         public void BeforeTest(){
136                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
137                 PowerMockito.mockStatic(EcompPortalUtils.class);
138                 PowerMockito.mockStatic(EPUserUtils.class);
139                 PowerMockito.mockStatic(AppUtils.class);
140                 PowerMockito.mockStatic(SystemProperties.class);
141                 EPUser user = mockUser.mockEPUser();
142                 MockHttpSession session = new MockHttpSession();
143                 session.setAttribute("user_attribute_name", user);
144                 MockHttpServletRequest request = new MockHttpServletRequest();
145                 request.setServletPath("http:test.com");
146                 request.setSession(session);
147                 request.addHeader("user-agent", "Mozilla/5.0");
148                 Mockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user_attribute_name");
149                 List<Object> args = new ArrayList<>();
150                 args.add("testClassName");
151                 args.add("testMethodName");
152                 Object[] passOnArgs = args.toArray(new Object[args.size()]);
153                 List<Object> args2 = new ArrayList<>();
154                 args2.add(request);
155                 Object[] httpArgs = args2.toArray(new Object[args2.size()]);
156                 epEELFLoggerAdvice.before(securityEventTypeEnum, httpArgs, passOnArgs);
157         }
158 }