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