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