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