1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/*-
* ============LICENSE_START=======================================================
* Configuration Test
* ================================================================================
* Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.policy.drools.protocol.configuration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Properties;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.junit.Test;
import org.onap.policy.common.utils.gson.GsonTestUtils;
public class ControllerConfigurationTest {
private static final String NAME = "name";
private static final String OPERATION = "operation";
private static final String NAME2 = "name2";
private static final String OPERATION2 = "operation2";
private static final String ARTIFACT = "org.onap.artifact";
private static final String GROUPID = "group";
private static final String VERSION = "1.0.0";
private static final String ARTIFACT2 = "org.onap.artifact2";
private static final String GROUPID2 = "group2";
private static final String VERSION2 = "1.0.1";
private static final String ADDITIONAL_PROPERTY_KEY = "foo";
private static final String ADDITIONAL_PROPERTY_VALUE = "bar";
private static final String ADDITIONAL_PROPERTY_KEY2 = "hello";
private static final String ADDITIONAL_PROPERTY_VALUE2 = "world";
private static final DroolsConfiguration DROOLS_CONFIG = new DroolsConfiguration(ARTIFACT, GROUPID, VERSION);
private static final DroolsConfiguration DROOLS_CONFIG2 = new DroolsConfiguration(ARTIFACT2, GROUPID2, VERSION2);
private static final String DROOLS_STRING = "drools";
/**
* Test.
*/
@Test
public void test() {
Properties additionalProperties = new Properties();
additionalProperties.put(ADDITIONAL_PROPERTY_KEY, ADDITIONAL_PROPERTY_VALUE);
ControllerConfiguration controllerConfig = new ControllerConfiguration(NAME, OPERATION, DROOLS_CONFIG);
assertTrue(controllerConfig.equals(controllerConfig));
assertFalse(controllerConfig.equals(new Object()));
ControllerConfiguration controllerConfig2 = new ControllerConfiguration();
controllerConfig2.setName(NAME2);
controllerConfig2.setOperation(OPERATION2);
controllerConfig2.setDrools(DROOLS_CONFIG2);
assertEquals(controllerConfig2.getName(), NAME2);
assertEquals(controllerConfig2.getOperation(), OPERATION2);
assertEquals(controllerConfig2.getDrools(), DROOLS_CONFIG2);
assertEquals(controllerConfig2, controllerConfig2.withName(NAME2));
assertEquals(controllerConfig2, controllerConfig2.withOperation(OPERATION2));
assertEquals(controllerConfig2, controllerConfig2.withDrools(DROOLS_CONFIG2));
controllerConfig2.setAdditionalProperty(ADDITIONAL_PROPERTY_KEY, ADDITIONAL_PROPERTY_VALUE);
assertEquals(controllerConfig2.getAdditionalProperties(), additionalProperties);
assertEquals(controllerConfig2, controllerConfig2.withAdditionalProperty(ADDITIONAL_PROPERTY_KEY,
ADDITIONAL_PROPERTY_VALUE));
assertTrue(controllerConfig2.declaredProperty(NAME, NAME2));
assertTrue(controllerConfig2.declaredProperty(OPERATION, OPERATION2));
assertTrue(controllerConfig2.declaredProperty(DROOLS_STRING, DROOLS_CONFIG2));
assertFalse(controllerConfig2.declaredProperty("dummy", NAME));
assertEquals(controllerConfig2.declaredPropertyOrNotFound(NAME, NAME2), NAME2);
assertEquals(controllerConfig2.declaredPropertyOrNotFound(OPERATION, OPERATION2), OPERATION2);
assertEquals(controllerConfig2.declaredPropertyOrNotFound(DROOLS_STRING, DROOLS_CONFIG2), DROOLS_CONFIG2);
assertEquals(controllerConfig2.declaredPropertyOrNotFound("dummy", NAME), NAME);
int hashCode = new HashCodeBuilder().append(NAME2).append(OPERATION2).append(DROOLS_CONFIG2)
.append(additionalProperties).toHashCode();
assertEquals(controllerConfig2.hashCode(), hashCode);
}
@Test
public void testSerialize() {
ControllerConfiguration controllerConfig = new ControllerConfiguration(NAME, OPERATION, DROOLS_CONFIG);
controllerConfig.setAdditionalProperty(ADDITIONAL_PROPERTY_KEY, ADDITIONAL_PROPERTY_VALUE);
controllerConfig.setAdditionalProperty(ADDITIONAL_PROPERTY_KEY2, ADDITIONAL_PROPERTY_VALUE2);
GsonTestUtils gson = new GsonTestUtils();
// ensure jackson and gson give same result
gson.compareGson(controllerConfig, ControllerConfigurationTest.class);
// ensure we get the same value when decoding
ControllerConfiguration config2 = gson.gsonRoundTrip(controllerConfig, ControllerConfiguration.class);
assertEquals(controllerConfig, config2);
assertEquals(gson.gsonEncode(controllerConfig), gson.gsonEncode(config2));
}
}
|