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