Unit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-XACML / src / test / java / org / onap / policy / xacml / test / std / pap / StdPDPPolicyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-XACML
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
23 package org.onap.policy.xacml.test.std.pap;
24
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.hamcrest.CoreMatchers.not;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertThat;
30 import java.io.IOException;
31 import java.net.ConnectException;
32 import java.net.URI;
33 import java.net.URISyntaxException;
34 import java.util.Properties;
35 import org.junit.Test;
36 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
37 import com.att.research.xacml.api.pap.PAPException;
38 import org.onap.policy.xacml.std.pap.StdPDPPolicyParams;
39
40 public class StdPDPPolicyTest {
41     @Test
42     public void testPolicy() throws URISyntaxException, IOException, PAPException {
43         // Set up test data
44         String value = "testVal";
45         URI uri = new URI("http://localhost/");
46         int[] array = {1, 1};
47
48         // Test constructors
49         StdPDPPolicy policy = new StdPDPPolicy(value, true);
50         assertNotNull(policy);
51         StdPDPPolicy policy2 = new StdPDPPolicy(value, true, value);
52         assertNotNull(policy2);
53         StdPDPPolicy policy4 = new StdPDPPolicy();
54         assertNotNull(policy4);
55
56         StdPDPPolicy policy5 = new StdPDPPolicy(
57                 StdPDPPolicyParams.builder().id(value).isRoot(true).name(value).location(uri)
58                         .isValid(false).policyId(value).description(value).version("1").build());
59         assertNotNull(policy5);
60         StdPDPPolicy policy6 = new StdPDPPolicy(value, true, value, uri, false);
61         assertNotNull(policy6);
62         StdPDPPolicy policy8 = new StdPDPPolicy(value, true, value, uri, false);
63         assertNotNull(policy8);
64
65         // Test set and get
66         policy.setId(value);
67         assertEquals(value, policy.getId());
68         policy.setName(value);
69         assertEquals(value, policy.getName());
70         policy.setPolicyId(value);
71         assertEquals(value, policy.getPolicyId());
72         policy.setDescription(value);
73         assertEquals(value, policy.getDescription());
74         policy.setVersion("1");
75         assertEquals("1", policy.getVersion());
76         assertEquals(1, policy.getVersionInts()[0]);
77         assertEquals(true, policy.isRoot());
78         policy.setValid(true);
79         assertEquals(true, policy.isValid());
80         assertEquals(uri, policy5.getLocation());
81         policy.setRoot(false);
82         assertEquals(false, policy.isRoot());
83         policy.setLocation(uri);
84         assertEquals(uri, policy.getLocation());
85
86         // Test equals combinations
87         assertThat(policy.hashCode(), is(not(0)));
88         assertEquals(true, policy.equals(policy));
89         assertEquals(false, policy.equals(null));
90         assertEquals(false, policy.equals(value));
91         assertEquals(true, policy6.equals(policy8));
92         policy6.setId("1");
93         assertEquals(false, policy6.equals(policy8));
94         policy6.setId(null);
95         assertEquals(false, policy6.equals(policy8));
96         policy8.setId(null);
97         assertEquals(true, policy6.equals(policy8));
98         policy8.setPolicyId(value);
99         assertEquals(false, policy6.equals(policy8));
100         policy6.setPolicyId("1");
101         policy8.setPolicyId("2");
102         assertEquals(false, policy6.equals(policy8));
103         policy8.setPolicyId("1");
104         policy6.setVersion("1");
105         policy8.setVersion("2");
106         assertEquals(false, policy6.equals(policy8));
107
108         // Test toString
109         assertThat(policy.toString().length(), is(not(0)));
110         assertEquals("1.1", StdPDPPolicy.versionArrayToString(array));
111         assertEquals("", StdPDPPolicy.versionArrayToString(null));
112         assertEquals(0, StdPDPPolicy.versionStringToArray(null).length);
113     }
114
115     @Test(expected = ConnectException.class)
116     public void negTestStream() throws URISyntaxException, IOException, PAPException {
117         // Set up test data
118         String value = "testVal";
119         URI uri = new URI("http://localhost:54287");
120         StdPDPPolicy policy = new StdPDPPolicy(
121                 StdPDPPolicyParams.builder().id(value).isRoot(true).name(value).location(uri)
122                         .isValid(false).policyId(value).description(value).version("1").build());
123
124         // Negative test stream
125         policy.getStream();
126     }
127
128     @Test(expected = ConnectException.class)
129     public void negTestConstructor1() throws URISyntaxException, IOException {
130         // Set up test data
131         String value = "testVal";
132         URI uri = new URI("http://localhost:54287");
133
134         // Test constructor
135         StdPDPPolicy policy = new StdPDPPolicy(value, true, value, uri);
136         assertNotNull(policy);
137     }
138
139     @Test(expected = ConnectException.class)
140     public void negTestConstructor2() throws URISyntaxException, IOException {
141         // Set up test data
142         String value = "testVal";
143         URI uri = new URI("http://localhost:54287");
144         Properties props = new Properties();
145
146         // Test constructor
147         StdPDPPolicy policy = new StdPDPPolicy(value, true, uri, props);
148         assertNotNull(policy);
149     }
150 }