Update license; improve coverage; add docs dir
[portal.git] / ecomp-portal-BE-common / src / test / java / org / openecomp / portalapp / portal / controller / PolicyControllerTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 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.openecomp.portalapp.portal.controller;
39
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42 import javax.ws.rs.BadRequestException;
43
44 import org.json.simple.JSONObject;
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.openecomp.portalapp.portal.core.MockEPUser;
54 import org.openecomp.portalapp.portal.framework.MockitoTestSuite;
55 import org.openecomp.portalapp.portal.scheduler.SchedulerRestInterface;
56 import org.openecomp.portalapp.portal.scheduler.policy.PolicyProperties;
57 import org.openecomp.portalapp.portal.scheduler.policy.PolicyResponseWrapper;
58 import org.openecomp.portalapp.portal.scheduler.policy.PolicyRestInterfaceFactory;
59 import org.openecomp.portalapp.portal.scheduler.policy.PolicyRestInterfaceIfc;
60 import org.openecomp.portalapp.portal.scheduler.policy.PolicyUtil;
61 import org.openecomp.portalapp.portal.scheduler.policy.RestObject;
62 import org.openecomp.portalsdk.core.util.SystemProperties;
63 import org.openecomp.portalsdk.core.web.support.UserUtils;
64 import org.powermock.api.mockito.PowerMockito;
65 import org.powermock.core.classloader.annotations.PrepareForTest;
66 import org.powermock.modules.junit4.PowerMockRunner;
67 import org.springframework.http.HttpStatus;
68 import org.springframework.http.ResponseEntity;
69
70 import junit.framework.Assert;
71
72 @RunWith(PowerMockRunner.class)
73 @PrepareForTest({ UserUtils.class, SystemProperties.class, PolicyProperties.class, PolicyRestInterfaceFactory.class,
74                 PolicyUtil.class })
75 public class PolicyControllerTest {
76
77         @Mock
78         SchedulerRestInterface schedulerRestInterface;
79
80         @InjectMocks
81         PolicyController policyController = new PolicyController();
82
83         @Before
84         public void setup() {
85                 MockitoAnnotations.initMocks(this);
86         }
87
88         MockEPUser mockUser = new MockEPUser();
89         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
90
91         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
92         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
93         NullPointerException nullPointerException = new NullPointerException();
94
95         @Before
96         public void setUp() {
97
98                 PowerMockito.mockStatic(SystemProperties.class);
99                 PowerMockito.mockStatic(PolicyProperties.class);
100                 Mockito.when(SystemProperties.getProperty(PolicyProperties.POLICY_GET_CONFIG_VAL)).thenReturn("/api/getConfig");
101                 Mockito.when(SystemProperties.getProperty(PolicyProperties.POLICY_CLIENT_MECHID_VAL))
102                                 .thenReturn("m06814@controller.dcae.ecomp.att.com");
103                 Mockito.when(SystemProperties.getProperty(PolicyProperties.POLICY_CLIENT_PASSWORD_VAL))
104                                 .thenReturn("OBF:1ffu1qvu1t2z1l161fuk1i801nz91ro41xf71xfv1rqi1nx51i7y1fuq1kxw1t371qxw1fh0");
105                 Mockito.when(SystemProperties.getProperty(PolicyProperties.POLICY_USERNAME_VAL)).thenReturn("testpdp");
106                 Mockito.when(SystemProperties.getProperty(PolicyProperties.POLICY_PASSWORD_VAL))
107                                 .thenReturn("OBF:1igd1kft1l1a1sw61svs1kxs1kcl1idt");
108                 Mockito.when(SystemProperties.getProperty(PolicyProperties.POLICY_ENVIRONMENT_VAL)).thenReturn("TEST");
109                 Mockito.when(SystemProperties.getProperty(PolicyProperties.POLICY_SERVER_URL_VAL))
110                                 .thenReturn("https://policypdp-conexus-e2e.ecomp.cci.att.com:8081/pdp");
111
112         }
113
114         @SuppressWarnings({ "unchecked", "deprecation" })
115         @Test
116         public void getPolicyInfoTest1() throws Exception {
117
118                 JSONObject jsonObject = Mockito.mock(JSONObject.class);
119                 PowerMockito.mockStatic(PolicyRestInterfaceFactory.class);
120                 PowerMockito.mockStatic(SystemProperties.class);
121                 PolicyRestInterfaceIfc policyRestInterface = Mockito.mock(PolicyRestInterfaceIfc.class);
122                 PowerMockito.mockStatic(PolicyUtil.class);
123                 // RestObject restObj=Mockito.mock(RestObject.class);
124                 PolicyResponseWrapper policyWrapper = Mockito.mock(PolicyResponseWrapper.class);
125                 PowerMockito.when(PolicyUtil.wrapResponse(Matchers.any(RestObject.class))).thenReturn(policyWrapper);
126                 Mockito.when(policyWrapper.getResponse()).thenReturn("Success");
127                 Mockito.when(policyWrapper.getStatus()).thenReturn(200);
128
129                 PowerMockito.when(PolicyRestInterfaceFactory.getInstance()).thenReturn(policyRestInterface);
130                 Mockito.doNothing().when(policyRestInterface).Post(Matchers.anyString(), Matchers.anyObject(),
131                                 Matchers.anyString(), Matchers.anyString(), Matchers.anyObject());
132
133                 ResponseEntity<String> responsePolicy = policyController.getPolicyInfo(mockedRequest, jsonObject);
134                 Assert.assertEquals(responsePolicy.getStatusCode(), HttpStatus.OK);
135         }
136
137         @SuppressWarnings("unchecked")
138         @Test(expected = Exception.class)
139         public void getPolicyInfoTestexpected() throws Exception {
140
141                 JSONObject jsonObject = Mockito.mock(JSONObject.class);
142                 PowerMockito.mockStatic(PolicyRestInterfaceFactory.class);
143                 PowerMockito.mockStatic(SystemProperties.class);
144                 PolicyRestInterfaceIfc policyRestInterface = Mockito.mock(PolicyRestInterfaceIfc.class);
145                 PowerMockito.mockStatic(PolicyUtil.class);
146                 // RestObject restObj=Mockito.mock(RestObject.class);
147                 PolicyResponseWrapper policyWrapper = Mockito.mock(PolicyResponseWrapper.class);
148                 PowerMockito.when(PolicyUtil.wrapResponse(Matchers.any(RestObject.class))).thenReturn(policyWrapper);
149                 Mockito.when(policyWrapper.getResponse()).thenThrow(new BadRequestException());
150                 Mockito.when(policyWrapper.getStatus()).thenThrow(new BadRequestException());
151
152                 PowerMockito.when(PolicyRestInterfaceFactory.getInstance()).thenReturn(policyRestInterface);
153                 Mockito.doNothing().when(policyRestInterface).Post(Matchers.anyString(), Matchers.anyObject(),
154                                 Matchers.anyString(), Matchers.anyString(), Matchers.anyObject());
155
156                 policyController.getPolicyInfo(mockedRequest, jsonObject);
157         }
158
159 }