Format ONAP-XACML and add JUnit
[policy/engine.git] / ONAP-XACML / src / test / java / org / onap / policy / xacml / test / std / pap / StdPDPPIPConfigTest.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.assertj.core.api.Assertions.assertThatExceptionOfType;
26 import static org.hamcrest.CoreMatchers.is;
27 import static org.hamcrest.CoreMatchers.not;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertThat;
31
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.Properties;
35
36 import org.junit.Test;
37 import org.onap.policy.xacml.std.pap.StdPDPPIPConfig;
38
39 public class StdPDPPIPConfigTest {
40     @Test
41     public void testConfig() {
42         // Setup test data
43         String id = "testID";
44         String value = "testVal";
45         Properties props = new Properties();
46         props.setProperty(id + ".classname", value);
47         props.setProperty(id + ".dontcare", "blah");
48         props.setProperty("foo", "bar");
49         Map<String, String> map = new HashMap<String, String>();
50         map.put(id, value);
51
52         // Test constructors
53         StdPDPPIPConfig config = new StdPDPPIPConfig();
54         assertNotNull(config);
55         StdPDPPIPConfig config2 = new StdPDPPIPConfig(id);
56         assertNotNull(config2);
57         StdPDPPIPConfig config3 = new StdPDPPIPConfig(id, value, value);
58         assertNotNull(config3);
59         StdPDPPIPConfig config4 = new StdPDPPIPConfig(id, props);
60         assertNotNull(config4);
61         StdPDPPIPConfig config5 = new StdPDPPIPConfig(id, props);
62         assertNotNull(config5);
63
64         // Test set and get
65         config.setId(value);
66         assertEquals(value, config.getId());
67         config.setName(value);
68         assertEquals(value, config.getName());
69         config.setDescription(value);
70         assertEquals(value, config.getDescription());
71         config.setClassname(value);
72         assertEquals(value, config.getClassname());
73         config.setValues(map);
74         assertEquals(map, config.getConfiguration());
75         assertEquals(true, config.isConfigured());
76         config.setConfig(map);
77         assertEquals(map, config.getConfig());
78
79         // Test equals combinations
80         assertEquals(true, config4.equals(config5));
81         assertEquals(true, config4.equals(config4));
82         assertEquals(false, config4.equals(null));
83         assertEquals(false, config4.equals(value));
84         config4.setClassname(null);
85         config5.setClassname(value);
86         assertEquals(false, config4.equals(config5));
87         config4.setClassname(id);
88         assertEquals(false, config4.equals(config5));
89         config4.setClassname(value);
90         config5.setConfig(map);
91         assertEquals(false, config4.equals(config5));
92         config4.setConfig(null);
93         assertEquals(false, config4.equals(config5));
94         config4.setConfig(map);
95         config5.setDescription(value);
96         assertEquals(false, config4.equals(config5));
97         config4.setDescription(id);
98         assertEquals(false, config4.equals(config5));
99         config4.setDescription(value);
100         config4.setId(null);
101         assertEquals(false, config4.equals(config5));
102         config4.setId(value);
103         assertEquals(false, config4.equals(config5));
104         config4.setId(id);
105         config5.setName(value);
106         assertEquals(false, config4.equals(config5));
107         config4.setName(id);
108         assertEquals(false, config4.equals(config5));
109         assertThat(config.hashCode(), is(not(0)));
110
111         // Test toString
112         assertThat(config.toString().length(), is(not(0)));
113     }
114
115     @Test
116     public void testBadProperties() {
117         Properties props = new Properties();
118         props.setProperty("foo", "bar");
119         assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> {
120             new StdPDPPIPConfig("myid", props);
121         });
122     }
123 }