Format ONAP-XACML and add JUnit
[policy/engine.git] / ONAP-XACML / src / test / java / org / onap / policy / xacml / test / std / pap / StdPDPTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-XACML
4  * ================================================================================
5  * Copyright (C) 2017-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 import static org.junit.Assert.assertTrue;
31
32 import com.att.research.xacml.api.pap.PDPPIPConfig;
33 import com.att.research.xacml.api.pap.PDPPolicy;
34
35 import java.util.HashSet;
36 import java.util.Properties;
37 import java.util.Set;
38
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.common.logging.flexlogger.FlexLogger;
42 import org.onap.policy.common.logging.flexlogger.Logger;
43 import org.onap.policy.xacml.std.pap.StdPDP;
44 import org.onap.policy.xacml.std.pap.StdPDPStatus;
45
46 public class StdPDPTest {
47
48     private static Logger logger = FlexLogger.getLogger(StdPDPTest.class);
49
50     private StdPDP stdPDP;
51
52     @Before
53     public void setUp() {
54
55         try {
56             stdPDP = new StdPDP();
57         } catch (Exception e) {
58             logger.error(e);
59         }
60     }
61
62     @Test
63     public void testGetId() {
64         try {
65             stdPDP.setId("testId");
66             assertTrue(stdPDP.getId() != null);
67         } catch (Exception e) {
68             logger.error(e);
69         }
70     }
71
72     @Test
73     public void testGetName() {
74         try {
75             stdPDP.setName("abc");
76             assertTrue(stdPDP.getName() != null);
77         } catch (Exception e) {
78             logger.error(e);
79         }
80     }
81
82     @Test
83     public void testGetDescription() {
84         try {
85             stdPDP.setDescription("somee here");
86             assertTrue(stdPDP.getDescription() != null);
87         } catch (Exception e) {
88             logger.error(e);
89         }
90     }
91
92     @Test
93     public void testGetStatus() {
94         try {
95             stdPDP.setStatus(new StdPDPStatus());
96             assertTrue(stdPDP.getStatus() != null);
97         } catch (Exception e) {
98             logger.error(e);
99         }
100     }
101
102     @Test
103     public void testGetPipConfigs() {
104         try {
105             assertTrue(stdPDP.getPipConfigs() != null);
106         } catch (Exception e) {
107             logger.error(e);
108         }
109     }
110
111     @Test
112     public void testGetJmxPort() {
113         try {
114             stdPDP.setJmxPort(123);
115             assertTrue(stdPDP.getJmxPort() != null);
116         } catch (Exception e) {
117             logger.error(e);
118         }
119     }
120
121     @Test
122     public void testPdp() {
123         // Set up test data
124         String id = "testID";
125         String value = "testVal";
126         Properties props = new Properties();
127         props.setProperty(id + ".name", value);
128         props.setProperty(id + ".description", value);
129         props.setProperty(id + ".jmxport", "0");
130         props.setProperty(id + ".foo", "0");
131         props.setProperty("foo" + ".jmxport", "0");
132
133         // Test constructors
134         StdPDP pdp = new StdPDP(id, 0);
135         assertNotNull(pdp);
136         StdPDP pdp2 = new StdPDP(id, value, 0);
137         assertNotNull(pdp2);
138         StdPDP pdp3 = new StdPDP(id, value, value, 0);
139         assertNotNull(pdp3);
140         StdPDP pdp4 = new StdPDP(id, props);
141         assertNotNull(pdp4);
142         StdPDP pdp5 = new StdPDP(id, props);
143         assertNotNull(pdp5);
144         StdPDP pdp6 = new StdPDP(id, value, value, null);
145         assertNotNull(pdp6);
146
147         // Test set and get
148         Set<PDPPIPConfig> pipConfigs = new HashSet<PDPPIPConfig>();
149         Set<PDPPolicy> policies = new HashSet<PDPPolicy>();
150         pdp.setPipConfigs(pipConfigs);
151         assertEquals(pipConfigs, pdp.getPipConfigs());
152         pdp.setPolicies(policies);
153         assertEquals(policies, pdp.getPolicies());
154
155         // Test equals combinations
156         assertEquals(true, pdp4.equals(pdp5));
157         assertEquals(true, pdp4.equals(pdp4));
158         assertEquals(false, pdp4.equals(null));
159         assertEquals(false, pdp4.equals(value));
160         pdp4.setId(null);
161         assertEquals(false, pdp4.equals(value));
162         pdp5.setId(id);
163         assertEquals(false, pdp4.equals(pdp5));
164         pdp4.setId(value);
165         assertEquals(false, pdp4.equals(pdp5));
166         assertThat(pdp.hashCode(), is(not(0)));
167
168         // Test compare
169         assertEquals(-1, pdp4.compareTo(null));
170         assertEquals(0, pdp4.compareTo(pdp4));
171         pdp5.setName(null);
172         assertEquals(-1, pdp4.compareTo(pdp5));
173         pdp4.setName(null);
174         pdp5.setName(value);
175         assertEquals(1, pdp4.compareTo(pdp5));
176
177         // Test toString
178         assertThat(pdp.toString().length(), is(not(0)));
179     }
180 }