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>
<version>${jackson.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.onap.policy.common</groupId>
+ <artifactId>gson</artifactId>
+ <version>${policy.common.version}</version>
+ </dependency>
+
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
import org.kie.api.runtime.rule.QueryResults;
import org.kie.api.runtime.rule.QueryResultsRow;
import org.onap.policy.common.endpoints.event.comm.TopicSink;
+import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
+import org.onap.policy.common.gson.annotation.GsonJsonProperty;
import org.onap.policy.drools.controller.DroolsController;
import org.onap.policy.drools.core.PolicyContainer;
import org.onap.policy.drools.core.PolicySession;
* Policy Container, the access object to the policy-core layer.
*/
@JsonIgnore
+ @GsonJsonIgnore
protected final PolicyContainer policyContainer;
/**
}
@JsonIgnore
+ @GsonJsonIgnore
@Override
public PolicyContainer getContainer() {
return this.policyContainer;
}
@JsonProperty("sessions")
+ @GsonJsonProperty("sessions")
@Override
public List<String> getSessionNames() {
return getSessionNames(true);
}
@JsonProperty("sessionCoordinates")
+ @GsonJsonProperty("sessionCoordinates")
@Override
public List<String> getCanonicalSessionNames() {
return getSessionNames(false);
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
-
+import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
import org.onap.policy.drools.controller.DroolsController;
import org.onap.policy.drools.protocol.coders.EventProtocolCoder.CoderFilters;
import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomCoder;
* Formatter for JSON encoding/decoding.
*/
@JsonIgnore
+ @GsonJsonIgnore
public static final DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSxxx");
@JsonIgnore
+ @GsonJsonIgnore
public static final DateTimeFormatter zuluFormat = DateTimeFormatter.ISO_INSTANT;
/**
* decoder.
*/
@JsonIgnore
+ @GsonJsonIgnore
protected final Gson decoder = new GsonBuilder().disableHtmlEscaping()
.registerTypeAdapter(ZonedDateTime.class, new GsonUTCAdapter())
.registerTypeAdapter(Instant.class, new GsonInstantAdapter()).create();
* encoder.
*/
@JsonIgnore
+ @GsonJsonIgnore
protected final Gson encoder = new GsonBuilder().disableHtmlEscaping()
.registerTypeAdapter(ZonedDateTime.class, new GsonUTCAdapter())
.registerTypeAdapter(Instant.class, new GsonInstantAdapter()).create();
* @return the Gson decoder
*/
@JsonIgnore
+ @GsonJsonIgnore
protected Gson getDecoder() {
return this.decoder;
}
* @return the Gson encoder
*/
@JsonIgnore
+ @GsonJsonIgnore
protected Gson getEncoder() {
return this.encoder;
}
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.onap.policy.common.gson.annotation.GsonJsonAnyGetter;
+import org.onap.policy.common.gson.annotation.GsonJsonAnySetter;
+import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
+import org.onap.policy.common.gson.annotation.GsonJsonProperty;
/**
*
*/
@JsonProperty("name")
+ @GsonJsonProperty("name")
private String name;
/**
* Set of operations that can be applied to a controller: create, lock
*
*/
@JsonProperty("operation")
+ @GsonJsonProperty("operation")
private String operation;
/**
* Maven Related Information.
*
*/
@JsonProperty("drools")
+ @GsonJsonProperty("drools")
private DroolsConfiguration drools;
+
@JsonIgnore
+ @GsonJsonIgnore
private Map<String, Object> additionalProperties = new HashMap<>();
+
protected static final Object NOT_FOUND_VALUE = new Object();
/**
* The name
*/
@JsonProperty("name")
+ @GsonJsonProperty("name")
public String getName() {
return name;
}
* The name
*/
@JsonProperty("name")
+ @GsonJsonProperty("name")
public void setName(String name) {
this.name = name;
}
* The operation
*/
@JsonProperty("operation")
+ @GsonJsonProperty("operation")
public String getOperation() {
return operation;
}
* The operation
*/
@JsonProperty("operation")
+ @GsonJsonProperty("operation")
public void setOperation(String operation) {
this.operation = operation;
}
* The drools
*/
@JsonProperty("drools")
+ @GsonJsonProperty("drools")
public DroolsConfiguration getDrools() {
return drools;
}
* The drools
*/
@JsonProperty("drools")
+ @GsonJsonProperty("drools")
public void setDrools(DroolsConfiguration drools) {
this.drools = drools;
}
}
@JsonAnyGetter
+ @GsonJsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
+ @GsonJsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
* ============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.
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.onap.policy.common.gson.annotation.GsonJsonAnyGetter;
+import org.onap.policy.common.gson.annotation.GsonJsonAnySetter;
+import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
+import org.onap.policy.common.gson.annotation.GsonJsonProperty;
/**
* Maven Related Information.
*
*/
@JsonProperty("artifactId")
+ @GsonJsonProperty("artifactId")
private String artifactId;
/**
*
*/
@JsonProperty("groupId")
+ @GsonJsonProperty("groupId")
private String groupId;
/**
*
*/
@JsonProperty("version")
+ @GsonJsonProperty("version")
private String version;
+
@JsonIgnore
+ @GsonJsonIgnore
private Map<String, Object> additionalProperties = new HashMap<>();
+
protected static final Object NOT_FOUND_VALUE = new Object();
/**
* The artifactId
*/
@JsonProperty("artifactId")
+ @GsonJsonProperty("artifactId")
public String getArtifactId() {
return artifactId;
}
* The artifactId
*/
@JsonProperty("artifactId")
+ @GsonJsonProperty("artifactId")
public void setArtifactId(String artifactId) {
this.artifactId = artifactId;
}
* The groupId
*/
@JsonProperty("groupId")
+ @GsonJsonProperty("groupId")
public String getGroupId() {
return groupId;
}
* The groupId
*/
@JsonProperty("groupId")
+ @GsonJsonProperty("groupId")
public void setGroupId(String groupId) {
this.groupId = groupId;
}
* The version
*/
@JsonProperty("version")
+ @GsonJsonProperty("version")
public String getVersion() {
return version;
}
* The version
*/
@JsonProperty("version")
+ @GsonJsonProperty("version")
public void setVersion(String version) {
this.version = version;
}
}
@JsonAnyGetter
+ @GsonJsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
+ @GsonJsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
* ============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.
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.onap.policy.common.gson.annotation.GsonJsonAnyGetter;
+import org.onap.policy.common.gson.annotation.GsonJsonAnySetter;
+import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
+import org.onap.policy.common.gson.annotation.GsonJsonProperty;
/**
/** Unique Transaction ID. This is an UUID. (Required) */
@JsonProperty("requestID")
+ @GsonJsonProperty("requestID")
private String requestID;
/* Set of entities on which configuration can be performed: controller (Required) */
@JsonProperty("entity")
+ @GsonJsonProperty("entity")
private String entity;
/* Controller Information, only applicable when the entity is set to controller */
@JsonProperty("controllers")
+ @GsonJsonProperty("controllers")
private List<ControllerConfiguration> controllers = new ArrayList<>();
- @JsonIgnore private Map<String, Object> additionalProperties = new HashMap<>();
+ @JsonIgnore @GsonJsonIgnore private Map<String, Object> additionalProperties = new HashMap<>();
protected static final Object NOT_FOUND_VALUE = new Object();
/** No args constructor for use in serialization. */
* @return The requestID
*/
@JsonProperty("requestID")
+ @GsonJsonProperty("requestID")
public String getRequestID() {
return requestID;
}
* @param requestID The requestID
*/
@JsonProperty("requestID")
+ @GsonJsonProperty("requestID")
public void setRequestID(String requestID) {
this.requestID = requestID;
}
* @return The entity
*/
@JsonProperty("entity")
+ @GsonJsonProperty("entity")
public String getEntity() {
return entity;
}
* @param entity The entity
*/
@JsonProperty("entity")
+ @GsonJsonProperty("entity")
public void setEntity(String entity) {
this.entity = entity;
}
*
* @return The controller
*/
- @JsonProperty("controller")
+ @JsonProperty("controllers")
+ @GsonJsonProperty("controllers")
public List<ControllerConfiguration> getControllers() {
return controllers;
}
*
* @param controllers controllers
*/
- @JsonProperty("controller")
+ @JsonProperty("controllers")
+ @GsonJsonProperty("controllers")
public void setControllers(List<ControllerConfiguration> controllers) {
this.controllers = controllers;
}
}
@JsonAnyGetter
+ @GsonJsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
+ @GsonJsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
* ============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.
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
-
+import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
import org.onap.policy.drools.controller.DroolsController;
import org.onap.policy.drools.features.PolicyControllerFeatureAPI;
import org.onap.policy.drools.protocol.configuration.DroolsConfiguration;
* {@inheritDoc}.
*/
@JsonIgnore
+ @GsonJsonIgnore
@Override
public List<PolicyControllerFeatureAPI> getFeatureProviders() {
return getProviders();
* ============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.
import org.onap.policy.common.endpoints.http.server.HttpServletServer;
import org.onap.policy.common.endpoints.http.server.HttpServletServerFactory;
import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
+import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
+import org.onap.policy.common.gson.annotation.GsonJsonProperty;
import org.onap.policy.drools.controller.DroolsController;
import org.onap.policy.drools.core.PolicyContainer;
import org.onap.policy.drools.core.jmx.PdpJmxListener;
}
@JsonIgnore
+ @GsonJsonIgnore
@Override
public synchronized Properties getEnvironment() {
return this.environment;
}
@JsonIgnore
+ @GsonJsonIgnore
@Override
public List<PolicyController> getPolicyControllers() {
return getControllerFactory().inventory();
}
@JsonProperty("controllers")
+ @GsonJsonProperty("controllers")
@Override
public List<String> getPolicyControllerIds() {
final List<String> controllerNames = new ArrayList<>();
@Override
@JsonIgnore
+ @GsonJsonIgnore
public Properties getProperties() {
return this.properties;
}
}
@JsonIgnore
+ @GsonJsonIgnore
@Override
public List<PolicyEngineFeatureAPI> getFeatureProviders() {
return getEngineProviders();
* ============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.
import org.onap.policy.common.endpoints.event.comm.TopicListener;
import org.onap.policy.common.endpoints.event.comm.TopicSink;
import org.onap.policy.common.endpoints.event.comm.TopicSource;
+import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
import org.onap.policy.drools.controller.DroolsController;
import org.onap.policy.drools.controller.DroolsControllerFactory;
import org.onap.policy.drools.features.PolicyControllerFeatureAPI;
* Mapping topics to sinks.
*/
@JsonIgnore
+ @GsonJsonIgnore
private final HashMap<String, TopicSink> topic2Sinks = new HashMap<>();
/**
*/
@Override
@JsonIgnore
+ @GsonJsonIgnore
public Properties getProperties() {
return this.properties;
}
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;
/**
* Set up.
- *
+ *
* @throws IOException throws an IO exception
*/
@BeforeClass
controller.halt();
Assert.assertFalse(controller.isAlive());
+
+ new GsonTestUtils().compareGson(controller, MavenDroolsControllerTest.class);
}
private DroolsController createDroolsController(long courtesyStartTimeMs) throws InterruptedException {
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 {
Assert.assertFalse(controller.isAlive());
}
+ @Test
+ public void testSerialize() {
+ new GsonTestUtils().compareGson(new NullDroolsController(), NullDroolsControllerTest.class);
+ }
+
@Test
public void lock() {
DroolsController controller = new NullDroolsController();
* ============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.
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));
}
}
* ============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.
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";
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();
.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));
+ }
}
* ============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.
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;
// 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"));
//
// Controller Constructor gets
//
ControllerConfiguration controller2 = new ControllerConfiguration(
- controller.get("name"),
- controller.get("operation"),
+ controller.get("name"),
+ controller.get("operation"),
controller.get("drools"));
//
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+", "@");
+ }
}
--- /dev/null
+/*-
+ * ============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;
+ }
+}
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;
ipc.build(MY_NAME, properties);
}
+ @Test
+ public void testSerialize() {
+ assertEquals(controller, ipc.build(MY_NAME, properties));
+
+ new GsonTestUtils().compareGson(ipc, PolicyControllerFactoryTest.class);
+ }
+
@Test
public void testPatchStringDroolsConfiguration() {
// unknown controller
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;
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;
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;
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);
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();
* ============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.
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;
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;
*/
private static Logger logger = LoggerFactory.getLogger(PolicyEngineTest.class);
+ private static GsonTestUtils gson;
+
/**
* clean up working directory.
*/
/**
* 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 */
/* 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
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
Thread.sleep(10000L);
assertFalse(PolicyEngine.manager.isAlive());
}
-
}
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 {
verify(drools, never()).stop();
}
+ @Test
+ public void testSerialize() {
+ new GsonMgmtTestBuilder().addDroolsControllerMock().addTopicSinkMock().addTopicSourceMock().build()
+ .compareGson(apc, AggregatedPolicyControllerTest.class);
+ }
+
@Test
public void testGetName() {
assertEquals(AGG_NAME, apc.getName());
--- /dev/null
+{
+ "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"]
+}
--- /dev/null
+{
+ "alive": false,
+ "locked": false,
+ "groupId": "NO-GROUP-ID",
+ "artifactId": "NO-ARTIFACT-ID",
+ "version": "NO-VERSION",
+ "sessionNames": [],
+ "container": null,
+ "baseDomainNames": [],
+ "brained": false,
+ "canonicalSessionNames": [],
+ "recentSourceEvents": [],
+ "recentSinkEvents": []
+}
--- /dev/null
+{
+ "name": "name",
+ "operation": "operation",
+ "drools": {
+ "artifactId": "org.onap.artifact",
+ "groupId": "group",
+ "version": "1.0.0"
+ },
+ "foo": "bar",
+ "hello": "world"
+}
--- /dev/null
+{
+ "artifactId": "org.onap.artifact",
+ "groupId": "group",
+ "version": "1.0.0",
+ "foo": "bar",
+ "hello": "world"
+}
--- /dev/null
+{
+ "requestID": "${obj.requestID}",
+ "entity": "entity1",
+ "controllers": [
+ {
+ "name": "name",
+ "operation": "operation"
+ },
+ {
+ "name": "name2",
+ "operation": "operation2"
+ }
+ ]
+}
--- /dev/null
+{
+ "features": ["feature-a", "feature-b"]
+}
--- /dev/null
+{
+ "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" }
+ ]
+}
--- /dev/null
+{
+ "alive": true,
+ "locked": false,
+ "sources": [],
+ "sinks": [],
+ "httpServers": [
+ {
+ "name": "TELEMETRY",
+ "host": "localhost",
+ "port": 9698,
+ "user": null,
+ "alive": true,
+ "aaf": false
+ }
+ ],
+ "features": [],
+ "controllers": [
+ "foo"
+ ]
+}
--- /dev/null
+{
+ "alive": false,
+ "locked": false,
+ "sources": [],
+ "sinks": [],
+ "httpServers": [
+ {
+ "name": "TELEMETRY",
+ "host": "localhost",
+ "port": 9698,
+ "user": null,
+ "alive": false,
+ "aaf": false
+ }
+ ],
+ "features": [],
+ "controllers": []
+}
--- /dev/null
+{
+ "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" }
+ ]
+}