Remove powermock to increase coverage
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / admin / RESTfulPAPEngineTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file 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  * ============LICENSE_END=========================================================
21  */
22 package org.onap.policy.admin;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.net.HttpURLConnection;
31 import java.net.URLConnection;
32 import javax.servlet.http.HttpServletResponse;
33 import org.junit.AfterClass;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.mockito.Mockito;
40 import org.onap.policy.rest.adapter.PolicyRestAdapter;
41 import org.onap.policy.xacml.api.pap.OnapPDP;
42 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
43 import com.att.research.xacml.api.pap.PAPException;
44 import com.att.research.xacml.api.pap.PDPPolicy;
45 import com.att.research.xacml.util.XACMLProperties;
46
47 public class RESTfulPAPEngineTest {
48     @Rule
49     public ExpectedException thrown = ExpectedException.none();
50
51     private RESTfulPAPEngine engine = null;
52     private String name = "testName";
53     private String id = "testID";
54     private String description = "testDescription";
55     private String policyType = "testType";
56     private String policyContent = "testContent";
57     private int jmxport = 0;
58     OnapPDPGroup group = Mockito.mock(OnapPDPGroup.class);
59     OnapPDPGroup newGroup = Mockito.mock(OnapPDPGroup.class);
60     OnapPDP pdp = Mockito.mock(OnapPDP.class);
61     InputStream policy;
62
63     @BeforeClass
64     public static void setUpBeforeClass() {
65         XACMLProperties.reloadProperties();
66     }
67
68     @AfterClass
69     public static void tearDownAfterClass() {
70         XACMLProperties.reloadProperties();
71     }
72
73     @Before
74     public void runConstructor() throws Exception {
75         // Mock connection
76         HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
77         Mockito.when(connection.getResponseCode()).thenReturn(HttpServletResponse.SC_NO_CONTENT);
78
79         // Set the system property temporarily
80         String systemKey = "xacml.properties";
81         String oldProperty = System.getProperty(systemKey);
82         System.setProperty(systemKey, "src/test/resources/xacml.admin.properties");
83
84         // Test constructor
85         String urlName = "localhost:1234";
86         engine = new RESTfulPAPEngine(urlName) {
87             @Override
88             protected URLConnection makeConnection(String fullURL) throws IOException {
89                 return connection;
90             }
91         };
92
93         // Initialize policy
94         policy = new ByteArrayInputStream(policyContent.getBytes("UTF-8"));
95
96         // Restore the original system property
97         if (oldProperty != null) {
98             System.setProperty(systemKey, oldProperty);
99         } else {
100             System.clearProperty(systemKey);
101         }
102     }
103
104     @Test
105     public void testGroups() throws Exception {
106         engine.setDefaultGroup(group);
107         assertEquals(engine.getDefaultGroup(), null);
108         engine.newGroup(name, description);
109         engine.removeGroup(group, newGroup);
110         assertEquals(engine.getPDPGroup(pdp), null);
111         assertEquals(engine.getPDPGroup(id), null);
112         assertEquals(engine.getPDP(id), null);
113         assertEquals(engine.getStatus(pdp), null);
114
115         thrown.expect(NullPointerException.class);
116         engine.getOnapPDPGroups();
117         fail("Expecting an exception.");
118     }
119
120     @Test
121     public void testUpdateGroup() throws PAPException {
122         thrown.expect(PAPException.class);
123         engine.updateGroup(group);
124         fail("Expecting an exception.");
125     }
126
127     @Test
128     public void testPDP() throws PAPException {
129         assertEquals(engine.getGroup(name), null);
130         engine.movePDP(pdp, newGroup);
131
132         thrown.expect(PAPException.class);
133         engine.newPDP(id, newGroup, name, description, jmxport);
134         fail("Expecting an exception.");
135     }
136
137     @Test
138     public void testUpdatePDP() throws PAPException {
139         thrown.expect(NullPointerException.class);
140         engine.updatePDP(pdp);
141         fail("Expecting an exception.");
142     }
143
144     @Test
145     public void testRemovePDP() throws PAPException {
146         thrown.expect(NullPointerException.class);
147         engine.removePDP(pdp);
148         fail("Expecting an exception.");
149     }
150
151     @Test
152     public void testValidatePolicy() throws PAPException {
153         PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
154
155         thrown.expect(PAPException.class);
156         engine.validatePolicyRequest(policyAdapter, policyType);
157         fail("Expecting an exception.");
158     }
159
160     @Test
161     public void testPublishPolicy() throws PAPException {
162         thrown.expect(PAPException.class);
163         engine.publishPolicy(id, name, false, policy, newGroup);
164         fail("Expecting an exception.");
165     }
166
167     @Test
168     public void testCopy() throws PAPException {
169         engine.copyFile(id, newGroup, policy);
170         PDPPolicy pdpPolicy = Mockito.mock(PDPPolicy.class);
171
172         thrown.expect(PAPException.class);
173         engine.copyPolicy(pdpPolicy, newGroup);
174         fail("Expecting an exception.");
175     }
176 }