aboutsummaryrefslogtreecommitdiffstats
path: root/policy-management/src/test
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-02-15 12:24:14 -0500
committerJim Hahn <jrh3@att.com>2019-02-15 12:26:37 -0500
commitcb8557e068ec89473dcb4d01a68e7dfd0a4644c5 (patch)
tree4e5eaea52f9a5f5416e75f28f18b5ffdef0b86e6 /policy-management/src/test
parentb47140e63ebc5d878c66935698df99a987a6418e (diff)
Test gson in policy-management
Added jackson-like Gson annotations to policy-management classes. Also added tests to verify that the classes serialize the same with gson as with jackson. Removed some trailing spaces. Replaced tabs and adjusted spacing in json files. Removed trailing newlines. Updated license data per review comment. Fixed merge conflict. Added files that were inadvertently deleted. Change-Id: Ib546e70f9d9d83187a6a93ff5c634000c4d53da5 Issue-ID: POLICY-1431 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'policy-management/src/test')
-rw-r--r--policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java5
-rw-r--r--policy-management/src/test/java/org/onap/policy/drools/controller/internal/NullDroolsControllerTest.java6
-rw-r--r--policy-management/src/test/java/org/onap/policy/drools/protocol/configuration/ControllerConfigurationTest.java69
-rw-r--r--policy-management/src/test/java/org/onap/policy/drools/protocol/configuration/DroolsConfigurationTest.java24
-rw-r--r--policy-management/src/test/java/org/onap/policy/drools/protocol/configuration/PdpdConfigurationTest.java47
-rw-r--r--policy-management/src/test/java/org/onap/policy/drools/system/GsonMgmtTestBuilder.java162
-rw-r--r--policy-management/src/test/java/org/onap/policy/drools/system/PolicyControllerFactoryTest.java8
-rw-r--r--policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java20
-rw-r--r--policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java20
-rw-r--r--policy-management/src/test/java/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.java7
-rw-r--r--policy-management/src/test/resources/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.json14
-rw-r--r--policy-management/src/test/resources/org/onap/policy/drools/controller/internal/NullDroolsControllerTest.json14
-rw-r--r--policy-management/src/test/resources/org/onap/policy/drools/protocol/configuration/ControllerConfigurationTest.json11
-rw-r--r--policy-management/src/test/resources/org/onap/policy/drools/protocol/configuration/DroolsConfigurationTest.json7
-rw-r--r--policy-management/src/test/resources/org/onap/policy/drools/protocol/configuration/PdpdConfigurationTest.json14
-rw-r--r--policy-management/src/test/resources/org/onap/policy/drools/system/PolicyControllerFactoryTest.json3
-rw-r--r--policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineManagerTest.json24
-rw-r--r--policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestAdd.json20
-rw-r--r--policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestConfig.json18
-rw-r--r--policy-management/src/test/resources/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.json18
20 files changed, 465 insertions, 46 deletions
diff --git a/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java
index dce38f9b..4c262775 100644
--- a/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java
+++ b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java
@@ -26,6 +26,7 @@ import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.kie.api.builder.ReleaseId;
+import org.onap.policy.common.utils.gson.GsonTestUtils;
import org.onap.policy.drools.controller.DroolsController;
import org.onap.policy.drools.util.KieUtils;
@@ -43,7 +44,7 @@ public class MavenDroolsControllerTest {
/**
* Set up.
- *
+ *
* @throws IOException throws an IO exception
*/
@BeforeClass
@@ -77,6 +78,8 @@ public class MavenDroolsControllerTest {
controller.halt();
Assert.assertFalse(controller.isAlive());
+
+ new GsonTestUtils().compareGson(controller, MavenDroolsControllerTest.class);
}
private DroolsController createDroolsController(long courtesyStartTimeMs) throws InterruptedException {
diff --git a/policy-management/src/test/java/org/onap/policy/drools/controller/internal/NullDroolsControllerTest.java b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/NullDroolsControllerTest.java
index 8cb754c1..77da1c1f 100644
--- a/policy-management/src/test/java/org/onap/policy/drools/controller/internal/NullDroolsControllerTest.java
+++ b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/NullDroolsControllerTest.java
@@ -22,6 +22,7 @@ package org.onap.policy.drools.controller.internal;
import org.junit.Assert;
import org.junit.Test;
+import org.onap.policy.common.utils.gson.GsonTestUtils;
import org.onap.policy.drools.controller.DroolsController;
public class NullDroolsControllerTest {
@@ -40,6 +41,11 @@ public class NullDroolsControllerTest {
}
@Test
+ public void testSerialize() {
+ new GsonTestUtils().compareGson(new NullDroolsController(), NullDroolsControllerTest.class);
+ }
+
+ @Test
public void lock() {
DroolsController controller = new NullDroolsController();
controller.lock();
diff --git a/policy-management/src/test/java/org/onap/policy/drools/protocol/configuration/ControllerConfigurationTest.java b/policy-management/src/test/java/org/onap/policy/drools/protocol/configuration/ControllerConfigurationTest.java
index f3817d5c..a3d53eba 100644
--- a/policy-management/src/test/java/org/onap/policy/drools/protocol/configuration/ControllerConfigurationTest.java
+++ b/policy-management/src/test/java/org/onap/policy/drools/protocol/configuration/ControllerConfigurationTest.java
@@ -2,14 +2,14 @@
* ============LICENSE_START=======================================================
* Configuration Test
* ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * 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.
@@ -27,78 +27,97 @@ 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,
+
+ 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));
}
}
diff --git a/policy-management/src/test/java/org/onap/policy/drools/protocol/configuration/DroolsConfigurationTest.java b/policy-management/src/test/java/org/onap/policy/drools/protocol/configuration/DroolsConfigurationTest.java
index 7d9b6551..4ad4f880 100644
--- a/policy-management/src/test/java/org/onap/policy/drools/protocol/configuration/DroolsConfigurationTest.java
+++ b/policy-management/src/test/java/org/onap/policy/drools/protocol/configuration/DroolsConfigurationTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* Configuration Test
* ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * 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.
@@ -25,9 +25,9 @@ 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 DroolsConfigurationTest {
private static final String ARTIFACT_ID_STRING = "artifactId";
@@ -47,6 +47,9 @@ public class DroolsConfigurationTest {
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";
+
@Test
public void test() {
final Properties additionalProperties = new Properties();
@@ -94,4 +97,21 @@ public class DroolsConfigurationTest {
.append(additionalProperties).toHashCode();
assertEquals(droolsConfig2.hashCode(), hashCode);
}
+
+ @Test
+ public void testSerialize() {
+ final DroolsConfiguration droolsConfig = new DroolsConfiguration(ARTIFACT, GROUPID, VERSION);
+ droolsConfig.setAdditionalProperty(ADDITIONAL_PROPERTY_KEY, ADDITIONAL_PROPERTY_VALUE);
+ droolsConfig.setAdditionalProperty(ADDITIONAL_PROPERTY_KEY2, ADDITIONAL_PROPERTY_VALUE2);
+
+ GsonTestUtils gson = new GsonTestUtils();
+
+ // ensure jackson and gson give same result
+ gson.compareGson(droolsConfig, DroolsConfigurationTest.class);
+
+ // ensure we get the same value when decoding
+ DroolsConfiguration config2 = gson.gsonRoundTrip(droolsConfig, DroolsConfiguration.class);
+ assertEquals(droolsConfig, config2);
+ assertEquals(gson.gsonEncode(droolsConfig), gson.gsonEncode(config2));
+ }
}
diff --git a/policy-management/src/test/java/org/onap/policy/drools/protocol/configuration/PdpdConfigurationTest.java b/policy-management/src/test/java/org/onap/policy/drools/protocol/configuration/PdpdConfigurationTest.java
index b5f3c535..4ab42f44 100644
--- a/policy-management/src/test/java/org/onap/policy/drools/protocol/configuration/PdpdConfigurationTest.java
+++ b/policy-management/src/test/java/org/onap/policy/drools/protocol/configuration/PdpdConfigurationTest.java
@@ -2,14 +2,14 @@
* ============LICENSE_START=======================================================
* Configuration Test
* ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * 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.
@@ -24,11 +24,13 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import java.io.IOException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import java.util.UUID;
-
import org.junit.Test;
+import org.onap.policy.common.utils.gson.GsonTestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -82,8 +84,8 @@ public class PdpdConfigurationTest {
// Constructor with values test get calls
//
DroolsConfiguration drools2 = new DroolsConfiguration(
- drools.get("artifactId"),
- drools.get("groupId"),
+ drools.get("artifactId"),
+ drools.get("groupId"),
drools.get("version"));
//
@@ -139,8 +141,8 @@ public class PdpdConfigurationTest {
// Controller Constructor gets
//
ControllerConfiguration controller2 = new ControllerConfiguration(
- controller.get("name"),
- controller.get("operation"),
+ controller.get("name"),
+ controller.get("operation"),
controller.get("drools"));
//
@@ -251,4 +253,33 @@ public class PdpdConfigurationTest {
assertEquals(config.getEntity(), ENTITY);
}
+ @Test
+ public void testSerialize() throws IOException {
+ List<ControllerConfiguration> controllers = Arrays.asList(new ControllerConfiguration(NAME, OPERATION, null),
+ new ControllerConfiguration(NAME2, OPERATION2, null));
+ PdpdConfiguration pdpConfig = new PdpdConfiguration(REQUEST_ID, ENTITY, controllers);
+
+ GsonTestUtils gson = new GsonTestUtils();
+
+ // ensure jackson and gson give same result
+ gson.compareGson(pdpConfig, PdpdConfigurationTest.class);
+
+ // ensure we get the same value when decoding
+ PdpdConfiguration config2 = gson.gsonRoundTrip(pdpConfig, PdpdConfiguration.class);
+ assertEquals(stripIdent(pdpConfig.toString()), stripIdent(config2.toString()));
+ assertEquals(pdpConfig, config2);
+ assertEquals(gson.gsonEncode(pdpConfig), gson.gsonEncode(config2));
+ }
+
+ /**
+ * Object identifiers may change with each execution, so this method is used to strip
+ * the identifier from the text string so that the strings will still match across
+ * different runs.
+ *
+ * @param text text from which to strip the identifier
+ * @return the text, without the identifier
+ */
+ private String stripIdent(String text) {
+ return text.replaceAll("@\\w+", "@");
+ }
}
diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/GsonMgmtTestBuilder.java b/policy-management/src/test/java/org/onap/policy/drools/system/GsonMgmtTestBuilder.java
new file mode 100644
index 00000000..e6569bc5
--- /dev/null
+++ b/policy-management/src/test/java/org/onap/policy/drools/system/GsonMgmtTestBuilder.java
@@ -0,0 +1,162 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * policy-management
+ * ================================================================================
+ * Copyright (C) 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.system;
+
+import com.google.gson.Gson;
+import com.google.gson.TypeAdapter;
+import com.google.gson.TypeAdapterFactory;
+import com.google.gson.reflect.TypeToken;
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
+import org.onap.policy.common.endpoints.event.comm.TopicSink;
+import org.onap.policy.common.endpoints.event.comm.TopicSource;
+import org.onap.policy.common.endpoints.http.server.HttpServletServer;
+import org.onap.policy.common.utils.gson.GsonSerializer;
+import org.onap.policy.common.utils.gson.GsonTestUtilsBuilder;
+import org.onap.policy.drools.controller.DroolsController;
+
+/**
+ * Utilities used to test encoding and decoding of Policy objects.
+ */
+public class GsonMgmtTestBuilder extends GsonTestUtilsBuilder {
+
+ /**
+ * Adds support for serializing a topic source mock.
+ *
+ * @return the builder
+ */
+ public GsonMgmtTestBuilder addTopicSourceMock() {
+ TypeAdapterFactory sgson = new TypeAdapterFactory() {
+ @Override
+ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
+ Class<? super T> clazz = type.getRawType();
+
+ if (TopicSource.class.isAssignableFrom(clazz)) {
+ return new GsonSerializer<T>() {
+ @Override
+ public void write(JsonWriter out, T value) throws IOException {
+ TopicSource obj = (TopicSource) value;
+ out.beginObject().name("name").value(obj.getTopic()).endObject();
+ }
+ };
+ }
+
+ return null;
+ }
+ };
+
+ addMock(TopicSource.class, sgson);
+
+ return this;
+ }
+
+ /**
+ * Adds support for serializing a topic sink mock.
+ *
+ * @return the builder
+ */
+ public GsonMgmtTestBuilder addTopicSinkMock() {
+ TypeAdapterFactory sgson = new TypeAdapterFactory() {
+ @Override
+ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
+ Class<? super T> clazz = type.getRawType();
+
+ if (TopicSink.class.isAssignableFrom(clazz)) {
+ return new GsonSerializer<T>() {
+ @Override
+ public void write(JsonWriter out, T value) throws IOException {
+ TopicSink obj = (TopicSink) value;
+ out.beginObject().name("name").value(obj.getTopic()).endObject();
+ }
+ };
+ }
+
+ return null;
+ }
+ };
+
+ addMock(TopicSink.class, sgson);
+
+ return this;
+ }
+
+ /**
+ * Adds support for serializing a drools controller mock.
+ *
+ * @return the builder
+ */
+ public GsonMgmtTestBuilder addDroolsControllerMock() {
+ TypeAdapterFactory sgson = new TypeAdapterFactory() {
+ @Override
+ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
+ Class<? super T> clazz = type.getRawType();
+
+ if (DroolsController.class.isAssignableFrom(clazz)) {
+ return new GsonSerializer<T>() {
+ @Override
+ public void write(JsonWriter out, T value) throws IOException {
+ DroolsController obj = (DroolsController) value;
+ out.beginObject().name("group").value(obj.getGroupId()).name("artifact")
+ .value(obj.getArtifactId()).name("version").value(obj.getVersion())
+ .endObject();
+ }
+ };
+ }
+
+ return null;
+ }
+ };
+
+ addMock(DroolsController.class, sgson);
+
+ return this;
+ }
+
+ /**
+ * Adds support for serializing an http servlet mock.
+ *
+ * @return the builder
+ */
+ public GsonMgmtTestBuilder addHttpServletServerMock() {
+ TypeAdapterFactory sgson = new TypeAdapterFactory() {
+ @Override
+ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
+ Class<? super T> clazz = type.getRawType();
+
+ if (HttpServletServer.class.isAssignableFrom(clazz)) {
+ return new GsonSerializer<T>() {
+ @Override
+ public void write(JsonWriter out, T value) throws IOException {
+ HttpServletServer obj = (HttpServletServer) value;
+ out.beginObject().name("port").value(obj.getPort()).endObject();
+ }
+ };
+ }
+
+ return null;
+ }
+ };
+
+ addMock(HttpServletServer.class, sgson);
+
+ return this;
+ }
+}
diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyControllerFactoryTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyControllerFactoryTest.java
index f5fc8a5c..e56670bd 100644
--- a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyControllerFactoryTest.java
+++ b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyControllerFactoryTest.java
@@ -37,6 +37,7 @@ import java.util.List;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;
+import org.onap.policy.common.utils.gson.GsonTestUtils;
import org.onap.policy.drools.controller.DroolsController;
import org.onap.policy.drools.features.PolicyControllerFeatureAPI;
import org.onap.policy.drools.protocol.configuration.DroolsConfiguration;
@@ -126,6 +127,13 @@ public class PolicyControllerFactoryTest {
}
@Test
+ public void testSerialize() {
+ assertEquals(controller, ipc.build(MY_NAME, properties));
+
+ new GsonTestUtils().compareGson(ipc, PolicyControllerFactoryTest.class);
+ }
+
+ @Test
public void testPatchStringDroolsConfiguration() {
// unknown controller
assertThatIllegalArgumentException().isThrownBy(() -> ipc.patch(MY_NAME, config));
diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java
index 4f7e1b0a..4b8357a2 100644
--- a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java
+++ b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java
@@ -37,8 +37,6 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -53,6 +51,7 @@ import org.onap.policy.common.endpoints.event.comm.TopicSink;
import org.onap.policy.common.endpoints.event.comm.TopicSource;
import org.onap.policy.common.endpoints.http.server.HttpServletServer;
import org.onap.policy.common.endpoints.http.server.HttpServletServerFactory;
+import org.onap.policy.common.utils.gson.GsonTestUtils;
import org.onap.policy.drools.controller.DroolsController;
import org.onap.policy.drools.features.PolicyControllerFeatureAPI;
import org.onap.policy.drools.features.PolicyEngineFeatureAPI;
@@ -80,7 +79,8 @@ public class PolicyEngineManagerTest {
private static final Object MY_EVENT = new Object();
- private static final Gson encoder = new GsonBuilder().disableHtmlEscaping().create();
+ private static final GsonTestUtils gson = new GsonMgmtTestBuilder().addTopicSourceMock().addTopicSinkMock()
+ .addHttpServletServerMock().build();
private Properties properties;
private PolicyEngineFeatureAPI prov1;
@@ -186,25 +186,31 @@ public class PolicyEngineManagerTest {
when(controllerFactory.get(CONTROLLER2)).thenReturn(controller2);
// do NOT return controller3 or controller4
+ when(server1.getPort()).thenReturn(1001);
when(server1.waitedStart(anyLong())).thenReturn(true);
when(server1.stop()).thenReturn(true);
+ when(server2.getPort()).thenReturn(1002);
when(server2.waitedStart(anyLong())).thenReturn(true);
when(server2.stop()).thenReturn(true);
when(serverFactory.build(any())).thenReturn(servers);
+ when(source1.getTopic()).thenReturn("source1-topic");
when(source1.start()).thenReturn(true);
when(source1.stop()).thenReturn(true);
+ when(source2.getTopic()).thenReturn("source2-topic");
when(source2.start()).thenReturn(true);
when(source2.stop()).thenReturn(true);
+ when(sink1.getTopic()).thenReturn("sink1-topic");
when(sink1.start()).thenReturn(true);
when(sink1.stop()).thenReturn(true);
when(sink1.send(any())).thenReturn(true);
when(sink1.getTopicCommInfrastructure()).thenReturn(CommInfrastructure.NOOP);
+ when(sink2.getTopic()).thenReturn("sink2-topic");
when(sink2.start()).thenReturn(true);
when(sink2.stop()).thenReturn(true);
@@ -261,12 +267,18 @@ public class PolicyEngineManagerTest {
pdpConfig.getControllers().add(config4);
pdpConfig.setEntity(PdpdConfiguration.CONFIG_ENTITY_CONTROLLER);
- pdpConfigJson = encoder.toJson(pdpConfig);
+ pdpConfigJson = gson.gsonEncode(pdpConfig);
mgr = new PolicyEngineManagerImpl();
}
@Test
+ public void testSerialize() {
+ mgr.configure(properties);
+ gson.compareGson(mgr, PolicyEngineManagerTest.class);
+ }
+
+ @Test
public void testFactory() {
mgr = new PolicyEngineManager();
diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java
index dae409ee..a00d6db2 100644
--- a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java
+++ b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* policy-management
* ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * 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.
@@ -23,12 +23,12 @@ package org.onap.policy.drools.system;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
-
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
@@ -38,6 +38,7 @@ import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
import org.onap.policy.common.endpoints.event.comm.TopicSink;
import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSink;
import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
+import org.onap.policy.common.utils.gson.GsonTestUtils;
import org.onap.policy.drools.persistence.SystemPersistence;
import org.onap.policy.drools.properties.DroolsProperties;
import org.onap.policy.drools.protocol.coders.EventProtocolCoder;
@@ -98,6 +99,8 @@ public class PolicyEngineTest {
*/
private static Logger logger = LoggerFactory.getLogger(PolicyEngineTest.class);
+ private static GsonTestUtils gson;
+
/**
* clean up working directory.
*/
@@ -121,13 +124,15 @@ public class PolicyEngineTest {
/**
* Start up.
- *
+ *
* @throws IOException throws IO exception
*/
@BeforeClass
public static void startUp() throws IOException {
logger.info("enter");
+ gson = new GsonTestUtils();
+
cleanUpWorkingDir();
/* ensure presence of config directory */
@@ -151,14 +156,16 @@ public class PolicyEngineTest {
/* override default port */
engineProps.put(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
- + PolicyEngine.TELEMETRY_SERVER_DEFAULT_NAME + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
- "" + DEFAULT_TELEMETRY_PORT);
+ + PolicyEngine.TELEMETRY_SERVER_DEFAULT_NAME
+ + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "" + DEFAULT_TELEMETRY_PORT);
assertFalse(PolicyEngine.manager.isAlive());
PolicyEngine.manager.configure(engineProps);
assertFalse(PolicyEngine.manager.isAlive());
logger.info("policy-engine {} has configuration {}", PolicyEngine.manager, engineProps);
+
+ gson.compareGson(PolicyEngine.manager, new File(PolicyEngineTest.class.getSimpleName() + "Config.json"));
}
@Test
@@ -229,6 +236,8 @@ public class PolicyEngineTest {
PolicyEngine.manager.createPolicyController(TEST_CONTROLLER_NAME, controllerProperties);
assertTrue(PolicyController.factory.inventory().size() == 1);
+
+ gson.compareGson(PolicyEngine.manager, new File(PolicyEngineTest.class.getSimpleName() + "Add.json"));
}
@Test
@@ -292,5 +301,4 @@ public class PolicyEngineTest {
Thread.sleep(10000L);
assertFalse(PolicyEngine.manager.isAlive());
}
-
}
diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.java
index 83344486..43068810 100644
--- a/policy-management/src/test/java/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.java
+++ b/policy-management/src/test/java/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.java
@@ -49,6 +49,7 @@ import org.onap.policy.drools.controller.DroolsControllerFactory;
import org.onap.policy.drools.features.PolicyControllerFeatureAPI;
import org.onap.policy.drools.persistence.SystemPersistence;
import org.onap.policy.drools.protocol.configuration.DroolsConfiguration;
+import org.onap.policy.drools.system.GsonMgmtTestBuilder;
public class AggregatedPolicyControllerTest {
@@ -260,6 +261,12 @@ public class AggregatedPolicyControllerTest {
}
@Test
+ public void testSerialize() {
+ new GsonMgmtTestBuilder().addDroolsControllerMock().addTopicSinkMock().addTopicSourceMock().build()
+ .compareGson(apc, AggregatedPolicyControllerTest.class);
+ }
+
+ @Test
public void testGetName() {
assertEquals(AGG_NAME, apc.getName());
}
diff --git a/policy-management/src/test/resources/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.json b/policy-management/src/test/resources/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.json
new file mode 100644
index 00000000..5ff6c2af
--- /dev/null
+++ b/policy-management/src/test/resources/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.json
@@ -0,0 +1,14 @@
+{
+ "alive": false,
+ "locked": false,
+ "recentSourceEvents": [],
+ "recentSinkEvents": [],
+ "modelClassLoaderHash": ${obj.modelClassLoaderHash},
+ "groupId": "org.onap.policy.drools.test",
+ "artifactId": "echo",
+ "version": "${obj.version}",
+ "baseDomainNames": ["onap.policies.test"],
+ "brained": true,
+ "sessions": ["echo"],
+ "sessionCoordinates": ["org.onap.policy.drools.test:echo:${obj.version}:echo"]
+}
diff --git a/policy-management/src/test/resources/org/onap/policy/drools/controller/internal/NullDroolsControllerTest.json b/policy-management/src/test/resources/org/onap/policy/drools/controller/internal/NullDroolsControllerTest.json
new file mode 100644
index 00000000..58bd0d90
--- /dev/null
+++ b/policy-management/src/test/resources/org/onap/policy/drools/controller/internal/NullDroolsControllerTest.json
@@ -0,0 +1,14 @@
+{
+ "alive": false,
+ "locked": false,
+ "groupId": "NO-GROUP-ID",
+ "artifactId": "NO-ARTIFACT-ID",
+ "version": "NO-VERSION",
+ "sessionNames": [],
+ "container": null,
+ "baseDomainNames": [],
+ "brained": false,
+ "canonicalSessionNames": [],
+ "recentSourceEvents": [],
+ "recentSinkEvents": []
+}
diff --git a/policy-management/src/test/resources/org/onap/policy/drools/protocol/configuration/ControllerConfigurationTest.json b/policy-management/src/test/resources/org/onap/policy/drools/protocol/configuration/ControllerConfigurationTest.json
new file mode 100644
index 00000000..6b4196fd
--- /dev/null
+++ b/policy-management/src/test/resources/org/onap/policy/drools/protocol/configuration/ControllerConfigurationTest.json
@@ -0,0 +1,11 @@
+{
+ "name": "name",
+ "operation": "operation",
+ "drools": {
+ "artifactId": "org.onap.artifact",
+ "groupId": "group",
+ "version": "1.0.0"
+ },
+ "foo": "bar",
+ "hello": "world"
+}
diff --git a/policy-management/src/test/resources/org/onap/policy/drools/protocol/configuration/DroolsConfigurationTest.json b/policy-management/src/test/resources/org/onap/policy/drools/protocol/configuration/DroolsConfigurationTest.json
new file mode 100644
index 00000000..3869ea28
--- /dev/null
+++ b/policy-management/src/test/resources/org/onap/policy/drools/protocol/configuration/DroolsConfigurationTest.json
@@ -0,0 +1,7 @@
+{
+ "artifactId": "org.onap.artifact",
+ "groupId": "group",
+ "version": "1.0.0",
+ "foo": "bar",
+ "hello": "world"
+}
diff --git a/policy-management/src/test/resources/org/onap/policy/drools/protocol/configuration/PdpdConfigurationTest.json b/policy-management/src/test/resources/org/onap/policy/drools/protocol/configuration/PdpdConfigurationTest.json
new file mode 100644
index 00000000..f5495ffb
--- /dev/null
+++ b/policy-management/src/test/resources/org/onap/policy/drools/protocol/configuration/PdpdConfigurationTest.json
@@ -0,0 +1,14 @@
+{
+ "requestID": "${obj.requestID}",
+ "entity": "entity1",
+ "controllers": [
+ {
+ "name": "name",
+ "operation": "operation"
+ },
+ {
+ "name": "name2",
+ "operation": "operation2"
+ }
+ ]
+}
diff --git a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyControllerFactoryTest.json b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyControllerFactoryTest.json
new file mode 100644
index 00000000..0c17f35f
--- /dev/null
+++ b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyControllerFactoryTest.json
@@ -0,0 +1,3 @@
+{
+ "features": ["feature-a", "feature-b"]
+}
diff --git a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineManagerTest.json b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineManagerTest.json
new file mode 100644
index 00000000..db50e53c
--- /dev/null
+++ b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineManagerTest.json
@@ -0,0 +1,24 @@
+{
+ "alive": false,
+ "controllers": [
+ "controller-a",
+ "controller-b"
+ ],
+ "features": [
+ "feature-a",
+ "feature-b"
+ ],
+ "httpServers": [
+ { "port": 1001 },
+ { "port": 1002 }
+ ],
+ "locked": false,
+ "sinks": [
+ { "name": "sink1-topic" },
+ { "name": "sink2-topic" }
+ ],
+ "sources": [
+ { "name": "source1-topic" },
+ { "name": "source2-topic" }
+ ]
+}
diff --git a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestAdd.json b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestAdd.json
new file mode 100644
index 00000000..7a247d32
--- /dev/null
+++ b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestAdd.json
@@ -0,0 +1,20 @@
+{
+ "alive": true,
+ "locked": false,
+ "sources": [],
+ "sinks": [],
+ "httpServers": [
+ {
+ "name": "TELEMETRY",
+ "host": "localhost",
+ "port": 9698,
+ "user": null,
+ "alive": true,
+ "aaf": false
+ }
+ ],
+ "features": [],
+ "controllers": [
+ "foo"
+ ]
+}
diff --git a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestConfig.json b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestConfig.json
new file mode 100644
index 00000000..7f06f5a0
--- /dev/null
+++ b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestConfig.json
@@ -0,0 +1,18 @@
+{
+ "alive": false,
+ "locked": false,
+ "sources": [],
+ "sinks": [],
+ "httpServers": [
+ {
+ "name": "TELEMETRY",
+ "host": "localhost",
+ "port": 9698,
+ "user": null,
+ "alive": false,
+ "aaf": false
+ }
+ ],
+ "features": [],
+ "controllers": []
+}
diff --git a/policy-management/src/test/resources/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.json b/policy-management/src/test/resources/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.json
new file mode 100644
index 00000000..3557f21d
--- /dev/null
+++ b/policy-management/src/test/resources/org/onap/policy/drools/system/internal/AggregatedPolicyControllerTest.json
@@ -0,0 +1,18 @@
+{
+ "alive": false,
+ "drools": {
+ "artifact": "artifact-a",
+ "group": "group-a",
+ "version": "version-a"
+ },
+ "locked": false,
+ "name": "agg-name",
+ "topicSinks": [
+ { "name": "sink-a" },
+ { "name": "sink-b" }
+ ],
+ "topicSources": [
+ { "name": "source-a" },
+ { "name": "source-b" }
+ ]
+}