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