CADI AAF Integration and merging the code
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / controller / PolicyControllerTest.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  * 
37  */
38 package org.onap.portalapp.portal.controller;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41
42 import org.json.simple.JSONObject;
43 import org.junit.Assert;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.InjectMocks;
48 import org.mockito.Matchers;
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.framework.MockitoTestSuite;
54 import org.onap.portalapp.portal.scheduler.SchedulerProperties;
55 import org.onap.portalapp.portal.scheduler.SchedulerRestInterface;
56 import org.onap.portalapp.portal.scheduleraux.RestObject;
57 import org.onap.portalapp.portal.scheduleraux.SchedulerAuxResponseWrapper;
58 import org.onap.portalapp.portal.scheduleraux.SchedulerAuxRestInterfaceFactory;
59 import org.onap.portalapp.portal.scheduleraux.SchedulerAuxRestInterfaceIfc;
60 import org.onap.portalapp.portal.scheduleraux.SchedulerAuxUtil;
61 import org.onap.portalsdk.core.web.support.UserUtils;
62 import org.powermock.api.mockito.PowerMockito;
63 import org.powermock.core.classloader.annotations.PrepareForTest;
64 import org.powermock.modules.junit4.PowerMockRunner;
65 import org.springframework.http.HttpStatus;
66 import org.springframework.http.ResponseEntity;
67
68
69 @RunWith(PowerMockRunner.class)
70 @PrepareForTest({ UserUtils.class, SchedulerProperties.class, SchedulerAuxRestInterfaceFactory.class,SchedulerAuxUtil.class })
71 public class PolicyControllerTest {
72         @Mock
73         SchedulerRestInterface schedulerRestInterface;
74
75         @InjectMocks
76         SchedulerAuxController policyController = new SchedulerAuxController();
77
78         @Before
79         public void setup() {
80                 MockitoAnnotations.initMocks(this);
81         }
82
83         MockEPUser mockUser = new MockEPUser();
84         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
85
86         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
87         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
88         NullPointerException nullPointerException = new NullPointerException();
89
90         @Before
91         public void setUp() {
92
93                 PowerMockito.mockStatic(SchedulerProperties.class);
94
95                 Mockito.when(SchedulerProperties.getProperty(SchedulerProperties.SCHEDULERAUX_GET_CONFIG_VAL)).thenReturn("/api/getConfig");
96                 Mockito.when(SchedulerProperties.getProperty(SchedulerProperties.SCHEDULERAUX_CLIENT_MECHID_VAL))
97                                 .thenReturn("m06814@controller.dcae.ecomp.att.com");
98                 Mockito.when(SchedulerProperties.getProperty(SchedulerProperties.SCHEDULERAUX_CLIENT_PASSWORD_VAL))
99                                 .thenReturn("OBF:1ffu1qvu1t2z1l161fuk1i801nz91ro41xf71xfv1rqi1nx51i7y1fuq1kxw1t371qxw1fh0");
100                 Mockito.when(SchedulerProperties.getProperty(SchedulerProperties.SCHEDULERAUX_USERNAME_VAL)).thenReturn("testpdp");
101                 Mockito.when(SchedulerProperties.getProperty(SchedulerProperties.SCHEDULERAUX_PASSWORD_VAL))
102                                 .thenReturn("OBF:1igd1kft1l1a1sw61svs1kxs1kcl1idt");
103                 Mockito.when(SchedulerProperties.getProperty(SchedulerProperties.SCHEDULERAUX_ENVIRONMENT_VAL)).thenReturn("TEST");
104                 Mockito.when(SchedulerProperties.getProperty(SchedulerProperties.SCHEDULERAUX_SERVER_URL_VAL))
105                                 .thenReturn("https://policypdp-conexus-e2e.ecomp.cci.att.com:8081/pdp");
106
107         }
108
109         @SuppressWarnings({ "unchecked" })
110         @Test
111         public void getPolicyInfoTest1() throws Exception {
112
113                 JSONObject jsonObject = Mockito.mock(JSONObject.class);
114                 PowerMockito.mockStatic(SchedulerAuxRestInterfaceFactory.class);
115                 PowerMockito.mockStatic(SchedulerProperties.class);
116                 SchedulerAuxRestInterfaceIfc policyRestInterface = Mockito.mock(SchedulerAuxRestInterfaceIfc.class);
117                 PowerMockito.mockStatic(SchedulerAuxUtil.class);
118                 // RestObject restObj=Mockito.mock(RestObject.class);
119                 SchedulerAuxResponseWrapper policyWrapper = Mockito.mock(SchedulerAuxResponseWrapper.class);
120                 PowerMockito.when(SchedulerAuxUtil.wrapResponse(Matchers.any(RestObject.class))).thenReturn(policyWrapper);
121                 Mockito.when(policyWrapper.getResponse()).thenReturn("Success");
122                 Mockito.when(policyWrapper.getStatus()).thenReturn(200);
123
124                 PowerMockito.when(SchedulerAuxRestInterfaceFactory.getInstance()).thenReturn(policyRestInterface);
125                 Mockito.doNothing().when(policyRestInterface).Post(Matchers.anyString(), Matchers.anyObject(),
126                                 Matchers.anyString(), Matchers.anyString(), Matchers.anyObject());
127
128                 ResponseEntity<String> responsePolicy = policyController.getPolicyInfo(mockedRequest);
129                 Assert.assertEquals(responsePolicy.getStatusCode(), HttpStatus.OK);
130         }
131
132         @SuppressWarnings("unchecked")
133         @Test
134         public void getPolicyInfoTestexpected() throws Exception {
135
136                 JSONObject jsonObject = Mockito.mock(JSONObject.class);
137                 PowerMockito.mockStatic(SchedulerAuxRestInterfaceFactory.class);
138                 PowerMockito.mockStatic(SchedulerProperties.class);
139                 Mockito.when(SchedulerProperties.getProperty(SchedulerProperties.SCHEDULERAUX_GET_CONFIG_VAL)).thenThrow(nullPointerException);
140                 ResponseEntity<String> responsePolicy = policyController.getPolicyInfo(mockedRequest);
141                 Assert.assertEquals(responsePolicy.getStatusCode(), HttpStatus.INTERNAL_SERVER_ERROR);
142
143         }
144         
145         @SuppressWarnings("unchecked")
146         @Test
147         public void getPolicyInfoTestException() throws Exception {
148
149                 JSONObject jsonObject = Mockito.mock(JSONObject.class);
150                 PowerMockito.mockStatic(SchedulerAuxRestInterfaceFactory.class);
151                 PowerMockito.mockStatic(SchedulerProperties.class);
152                 SchedulerAuxRestInterfaceIfc policyRestInterface = Mockito.mock(SchedulerAuxRestInterfaceIfc.class);
153                 PowerMockito.mockStatic(SchedulerAuxUtil.class);
154                 // RestObject restObj=Mockito.mock(RestObject.class);
155                 SchedulerAuxResponseWrapper policyWrapper = Mockito.mock(SchedulerAuxResponseWrapper.class);
156                 PowerMockito.when(SchedulerAuxUtil.wrapResponse(Matchers.any(RestObject.class))).thenReturn(policyWrapper);
157                 Mockito.when(policyWrapper.getResponse()).thenReturn("Success");
158                 Mockito.when(policyWrapper.getStatus()).thenReturn(200);
159
160                 PowerMockito.when(SchedulerAuxRestInterfaceFactory.getInstance()).thenReturn(policyRestInterface);
161                 /*Mockito.doNothing().when(policyRestInterface).Post(Matchers.anyString(), Matchers.anyObject(),
162                                 Matchers.anyString(), Matchers.anyString(), Matchers.anyObject());*/
163                 Mockito.doThrow(new NullPointerException()).when(policyRestInterface).Post(Matchers.anyString(), Matchers.anyObject(),
164                                 Matchers.anyString(), Matchers.anyString(), Matchers.anyObject());
165
166
167                 ResponseEntity<String> responsePolicy = policyController.getPolicyInfo(mockedRequest);
168                 Assert.assertEquals(responsePolicy.getStatusCode(), HttpStatus.INTERNAL_SERVER_ERROR);
169         }
170
171         @SuppressWarnings("unchecked")
172         @Test
173         public void getPolicyInfoTestException1() throws Exception {
174
175                 JSONObject jsonObject = Mockito.mock(JSONObject.class);
176                 PowerMockito.mockStatic(SchedulerAuxRestInterfaceFactory.class);
177                 PowerMockito.mockStatic(SchedulerProperties.class);
178                 SchedulerAuxRestInterfaceIfc policyRestInterface = Mockito.mock(SchedulerAuxRestInterfaceIfc.class);
179                 PowerMockito.mockStatic(SchedulerAuxUtil.class);
180                 // RestObject restObj=Mockito.mock(RestObject.class);
181                 SchedulerAuxResponseWrapper policyWrapper = Mockito.mock(SchedulerAuxResponseWrapper.class);
182                 PowerMockito.when(SchedulerAuxUtil.wrapResponse(Matchers.any(RestObject.class))).thenReturn(policyWrapper);
183                 Mockito.when(policyWrapper.getResponse()).thenReturn("Bad Request");
184                 Mockito.when(policyWrapper.getStatus()).thenReturn(400);
185
186                 PowerMockito.when(SchedulerAuxRestInterfaceFactory.getInstance()).thenReturn(policyRestInterface);
187                 Mockito.doNothing().when(policyRestInterface).Post(Matchers.anyString(), Matchers.anyObject(),
188                                 Matchers.anyString(), Matchers.anyString(), Matchers.anyObject());
189                 /*Mockito.doThrow(new NullPointerException()).when(policyRestInterface).Post(Matchers.anyString(), Matchers.anyObject(),
190                                 Matchers.anyString(), Matchers.anyString(), Matchers.anyObject());*/
191
192
193                 ResponseEntity<String> responsePolicy = policyController.getPolicyInfo(mockedRequest);
194                 Assert.assertEquals(responsePolicy.getStatusCode(), HttpStatus.BAD_REQUEST);
195         }
196
197 }