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