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