2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.xacml.test.std.pap;
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;
31 import com.att.research.xacml.api.pap.PAPException;
33 import java.io.IOException;
34 import java.net.ConnectException;
36 import java.net.URISyntaxException;
37 import java.util.Properties;
39 import org.junit.Test;
40 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
41 import org.onap.policy.xacml.std.pap.StdPDPPolicyParams;
43 public class StdPDPPolicyTest {
45 public void testPolicy() throws URISyntaxException, IOException, PAPException {
47 String value = "testVal";
48 URI uri = new URI("http://localhost/");
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);
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);
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());
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));
95 assertEquals(false, policy6.equals(policy8));
97 assertEquals(false, policy6.equals(policy8));
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));
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);
117 @Test(expected = ConnectException.class)
118 public void negTestStream() throws URISyntaxException, IOException, PAPException {
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());
125 // Negative test stream
129 @Test(expected = ConnectException.class)
130 public void negTestConstructor1() throws URISyntaxException, IOException {
132 String value = "testVal";
133 URI uri = new URI("http://localhost:54287");
136 StdPDPPolicy policy = new StdPDPPolicy(value, true, value, uri);
137 assertNotNull(policy);
140 @Test(expected = ConnectException.class)
141 public void negTestConstructor2() throws URISyntaxException, IOException {
143 String value = "testVal";
144 URI uri = new URI("http://localhost:54287");
145 Properties props = new Properties();
148 StdPDPPolicy policy = new StdPDPPolicy(value, true, uri, props);
149 assertNotNull(policy);