ed7db3b8abf70c3684239a6b0520bf696c2ff8a0
[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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.xacml.test.std.pap;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.hamcrest.CoreMatchers.not;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertThat;
28 import java.io.IOException;
29 import java.net.ConnectException;
30 import java.net.URI;
31 import java.net.URISyntaxException;
32 import java.util.Properties;
33 import org.junit.Test;
34 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
35 import com.att.research.xacml.api.pap.PAPException;
36
37
38 public class StdPDPPolicyTest {
39   @Test
40   public void testPolicy() throws URISyntaxException, IOException, PAPException {
41     // Set up test data
42     String value = "testVal";
43     URI uri = new URI("http://localhost/");
44     int[] array = {1, 1};
45
46     // Test constructors
47     StdPDPPolicy policy = new StdPDPPolicy(value, true);
48     assertNotNull(policy);
49     StdPDPPolicy policy2 = new StdPDPPolicy(value, true, value);
50     assertNotNull(policy2);
51     StdPDPPolicy policy4 = new StdPDPPolicy();
52     assertNotNull(policy4);
53     StdPDPPolicy policy5 = new StdPDPPolicy(value, true, value, uri, false, value, value, "1");
54     assertNotNull(policy5);
55     StdPDPPolicy policy6 = new StdPDPPolicy(value, true, value, uri, false);
56     assertNotNull(policy6);
57     StdPDPPolicy policy8 = new StdPDPPolicy(value, true, value, uri, false);
58     assertNotNull(policy8);
59
60     // Test set and get
61     policy.setId(value);
62     assertEquals(value, policy.getId());
63     policy.setName(value);
64     assertEquals(value, policy.getName());
65     policy.setPolicyId(value);
66     assertEquals(value, policy.getPolicyId());
67     policy.setDescription(value);
68     assertEquals(value, policy.getDescription());
69     policy.setVersion("1");
70     assertEquals("1", policy.getVersion());
71     assertEquals(1, policy.getVersionInts()[0]);
72     assertEquals(true, policy.isRoot());
73     policy.setValid(true);
74     assertEquals(true, policy.isValid());
75     assertEquals(uri, policy5.getLocation());
76     policy.setRoot(false);
77     assertEquals(false, policy.isRoot());
78     policy.setLocation(uri);
79     assertEquals(uri, policy.getLocation());
80
81     // Test equals combinations
82     assertThat(policy.hashCode(), is(not(0)));
83     assertEquals(true, policy.equals(policy));
84     assertEquals(false, policy.equals(null));
85     assertEquals(false, policy.equals(value));
86     assertEquals(true, policy6.equals(policy8));
87     policy6.setId("1");
88     assertEquals(false, policy6.equals(policy8));
89     policy6.setId(null);
90     assertEquals(false, policy6.equals(policy8));
91     policy8.setId(null);
92     assertEquals(true, policy6.equals(policy8));
93     policy8.setPolicyId(value);
94     assertEquals(false, policy6.equals(policy8));
95     policy6.setPolicyId("1");
96     policy8.setPolicyId("2");
97     assertEquals(false, policy6.equals(policy8));
98     policy8.setPolicyId("1");
99     policy6.setVersion("1");
100     policy8.setVersion("2");
101     assertEquals(false, policy6.equals(policy8));
102
103     // Test toString
104     assertThat(policy.toString().length(), is(not(0)));
105     assertEquals("1.1", StdPDPPolicy.versionArrayToString(array));
106     assertEquals("", StdPDPPolicy.versionArrayToString(null));
107     assertEquals(0, StdPDPPolicy.versionStringToArray(null).length);
108   }
109
110   @Test(expected = ConnectException.class)
111   public void negTestStream() throws URISyntaxException, IOException, PAPException {
112     // Set up test data
113     String value = "testVal";
114     URI uri = new URI("http://localhost/");
115     StdPDPPolicy policy = new StdPDPPolicy(value, true, value, uri, false, value, value, "1");
116
117     // Negative test stream
118     policy.getStream();
119   }
120
121   @Test(expected = ConnectException.class)
122   public void negTestConstructor1() throws URISyntaxException, IOException {
123     // Set up test data
124     String value = "testVal";
125     URI uri = new URI("http://localhost/");
126
127     // Test constructor
128     StdPDPPolicy policy = new StdPDPPolicy(value, true, value, uri);
129     assertNotNull(policy);
130   }
131
132   @Test(expected = ConnectException.class)
133   public void negTestConstructor2() throws URISyntaxException, IOException {
134     // Set up test data
135     String value = "testVal";
136     URI uri = new URI("http://localhost/");
137     Properties props = new Properties();
138
139     // Test constructor
140     StdPDPPolicy policy = new StdPDPPolicy(value, true, uri, props);
141     assertNotNull(policy);
142   }
143 }