package org.onap.policy.apex.context.impl.schema.java;
import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import java.lang.reflect.Constructor;
import org.onap.policy.apex.context.ContextRuntimeException;
import org.onap.policy.apex.context.impl.schema.AbstractSchemaHelper;
+import org.onap.policy.apex.context.parameters.ContextParameterConstants;
+import org.onap.policy.apex.context.parameters.SchemaParameters;
import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
import org.onap.policy.apex.model.utilities.typeutils.TypeBuilder;
+import org.onap.policy.common.parameters.ParameterService;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
/**
- * This class implements translation to and from Apex distributed objects and Java objects when a
- * Java schema is used. It creates schema items as Java objects and marshals and unmarshals these
- * objects in various formats. All objects must be of the type of Java class defined in the schema.
+ * This class implements translation to and from Apex distributed objects and Java objects when a Java schema is used.
+ * It creates schema items as Java objects and marshals and unmarshals these objects in various formats. All objects
+ * must be of the type of Java class defined in the schema.
*
* @author Liam Fallon (liam.fallon@ericsson.com)
*/
/*
* (non-Javadoc)
*
- * @see
- * org.onap.policy.apex.context.impl.schema.AbstractSchemaHelper#init(org.onap.policy.apex.model
- * .basicmodel. concepts. AxKey,
- * org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema)
+ * @see org.onap.policy.apex.context.impl.schema.AbstractSchemaHelper#init(org.onap.policy.apex.model .basicmodel.
+ * concepts. AxKey, org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema)
*/
@Override
public void init(final AxKey userKey, final AxContextSchema schema) {
} catch (final IllegalArgumentException e) {
String resultSting = userKey.getId() + ": class/type " + schema.getSchema() + " for context schema \""
- + schema.getId() + "\" not found.";
+ + schema.getId() + "\" not found.";
if (JavaSchemaHelper.BUILT_IN_MAP.get(javatype) != null) {
resultSting += " Primitive types are not supported. Use the appropriate Java boxing type instead.";
} else {
}
if (getSchemaClass() == null) {
- final String returnString =
- getUserKey().getId() + ": could not create an instance, schema class for the schema is null";
+ final String returnString = getUserKey().getId()
+ + ": could not create an instance, schema class for the schema is null";
LOGGER.warn(returnString);
throw new ContextRuntimeException(returnString);
}
if (incomingObject instanceof JsonElement) {
- final String elementJsonString = new Gson().toJson((JsonElement) incomingObject);
- return new Gson().fromJson(elementJsonString, this.getSchemaClass());
+ final String elementJsonString = getGson().toJson((JsonElement) incomingObject);
+ return getGson().fromJson(elementJsonString, this.getSchemaClass());
}
if (getSchemaClass().isAssignableFrom(incomingObject.getClass())) {
}
final String returnString = getUserKey().getId() + ": the object \"" + incomingObject + "\" of type \""
- + incomingObject.getClass().getCanonicalName()
- + "\" is not an instance of JsonObject and is not assignable to \""
- + getSchemaClass().getCanonicalName() + "\"";
+ + incomingObject.getClass().getCanonicalName()
+ + "\" is not an instance of JsonObject and is not assignable to \""
+ + getSchemaClass().getCanonicalName() + "\"";
LOGGER.warn(returnString);
throw new ContextRuntimeException(returnString);
}
// Check the incoming object is of a correct class
if (getSchemaClass().isAssignableFrom(schemaObject.getClass())) {
// Use Gson to translate the object
- return new Gson().toJson(schemaObject);
+ return getGson().toJson(schemaObject);
} else {
final String returnString = getUserKey().getId() + ": object \"" + schemaObject.toString()
- + "\" of class \"" + schemaObject.getClass().getCanonicalName() + "\" not compatible with class \""
- + getSchemaClass().getCanonicalName() + "\"";
+ + "\" of class \"" + schemaObject.getClass().getCanonicalName()
+ + "\" not compatible with class \"" + getSchemaClass().getCanonicalName() + "\"";
LOGGER.warn(returnString);
throw new ContextRuntimeException(returnString);
}
@Override
public Object marshal2Object(final Object schemaObject) {
// Use Gson to marshal the schema object into a Json element to return
- return new Gson().toJsonTree(schemaObject, getSchemaClass());
+ return getGson().toJsonTree(schemaObject, getSchemaClass());
}
/**
return stringConstructor.newInstance(object.toString());
} catch (final Exception e) {
final String returnString = getUserKey().getId() + ": object \"" + object.toString() + "\" of class \""
- + object.getClass().getCanonicalName() + "\" not compatible with class \""
- + getSchemaClass().getCanonicalName() + "\"";
+ + object.getClass().getCanonicalName() + "\" not compatible with class \""
+ + getSchemaClass().getCanonicalName() + "\"";
LOGGER.warn(returnString, e);
throw new ContextRuntimeException(returnString);
}
}
+
+ /**
+ * Get a GSON instance that has the correct adaptation included.
+ *
+ * @return the GSON instance
+ */
+ private Gson getGson() {
+ GsonBuilder gsonBuilder = new GsonBuilder();
+
+ // Get the Java schema helper parameters from the parameter service
+ SchemaParameters schemaParameters = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
+
+ JavaSchemaHelperParameters javaSchemaHelperParmeters = (JavaSchemaHelperParameters) schemaParameters
+ .getSchemaHelperParameterMap().get("Java");
+
+ if (javaSchemaHelperParmeters == null) {
+ javaSchemaHelperParmeters = new JavaSchemaHelperParameters();
+ }
+
+ for (JavaSchemaHelperJsonAdapterParameters jsonAdapterEntry : javaSchemaHelperParmeters.getJsonAdapters()
+ .values()) {
+
+ Object adapterObject;
+ try {
+ adapterObject = jsonAdapterEntry.getAdaptorClazz().newInstance();
+ } catch (InstantiationException | IllegalAccessException e) {
+ final String returnString = getUserKey().getId() + ": instantiation of adapter class \""
+ + jsonAdapterEntry.getAdaptorClass() + "\" to decode and encode class \""
+ + jsonAdapterEntry.getAdaptedClass() + "\" failed: " + e.getMessage();
+ LOGGER.warn(returnString, e);
+ throw new ContextRuntimeException(returnString);
+ }
+
+ gsonBuilder.registerTypeAdapter(jsonAdapterEntry.getAdaptedClazz(), adapterObject);
+ }
+
+ return gsonBuilder.create();
+ }
+
}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2018 Ericsson. 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.context.impl.schema.java;
+
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonSerializer;
+
+import org.onap.policy.common.parameters.GroupValidationResult;
+import org.onap.policy.common.parameters.ParameterGroup;
+import org.onap.policy.common.parameters.ValidationStatus;
+import org.slf4j.ext.XLogger;
+import org.slf4j.ext.XLoggerFactory;
+
+//@formatter:off
+/**
+ * Event protocol parameters for JSON as an event protocol.
+ *
+ * <p>The parameters for this plugin are:
+ * <ol>
+ * <li>adaptedClass: The name of the class being adapted.
+ * <li>adapterClass: the JSON adapter class to use for the adapted class.
+ * </ol>
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+//@formatter:on
+public class JavaSchemaHelperJsonAdapterParameters implements ParameterGroup {
+ private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaSchemaHelperJsonAdapterParameters.class);
+
+ // Recurring string constants
+ private static final String ADAPTED_CLASS = "adaptedClass";
+ private static final String ADAPTOR_CLASS = "adaptorClass";
+
+ private String adaptedClass;
+ private String adaptorClass;
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getName() {
+ return getAdaptedClass();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void setName(String adaptedClass) {
+ setAdaptedClass(adaptedClass);
+ }
+
+ /**
+ * Gets the adapted class.
+ *
+ * @return the adapted class
+ */
+ public String getAdaptedClass() {
+ return adaptedClass;
+ }
+
+ /**
+ * Gets the adapted class.
+ *
+ * @return the adapted class
+ */
+ public Class<?> getAdaptedClazz() {
+ if (adaptedClass == null) {
+ return null;
+ }
+
+ try {
+ return Class.forName(adaptedClass);
+ } catch (final ClassNotFoundException e) {
+ LOGGER.warn("class \"" + adaptedClass + "\" not found: ", e);
+ return null;
+ }
+ }
+
+ /**
+ * Sets the adapted class.
+ *
+ * @param adaptedClass the new adapted class
+ */
+ public void setAdaptedClass(String adaptedClass) {
+ this.adaptedClass = adaptedClass;
+ }
+
+ /**
+ * Gets the adaptor class.
+ *
+ * @return the adaptor class
+ */
+ public String getAdaptorClass() {
+ return adaptorClass;
+ }
+
+ /**
+ * Gets the adaptor class.
+ *
+ * @return the adaptor class
+ */
+ public Class<?> getAdaptorClazz() {
+ if (adaptorClass == null) {
+ return null;
+ }
+
+ try {
+ return Class.forName(adaptorClass);
+ } catch (final ClassNotFoundException e) {
+ LOGGER.warn("class \"" + adaptorClass + "\" not found: ", e);
+ return null;
+ }
+ }
+
+ /**
+ * Sets the adaptor class.
+ *
+ * @param adaptorClass the new adaptor class
+ */
+ public void setAdaptorClass(String adaptorClass) {
+ this.adaptorClass = adaptorClass;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public GroupValidationResult validate() {
+ final GroupValidationResult result = new GroupValidationResult(this);
+
+ getClass(ADAPTED_CLASS, adaptedClass, result);
+
+ Class<?> adaptorClazz = getClass(ADAPTOR_CLASS, adaptorClass, result);
+ if (adaptorClazz != null) {
+ String errorMessage = null;
+
+ if (!JsonSerializer.class.isAssignableFrom(adaptorClazz)) {
+ errorMessage = "class is not a JsonSerializer";
+ }
+
+ if (!JsonDeserializer.class.isAssignableFrom(adaptorClazz)) {
+ if (errorMessage == null) {
+ errorMessage = "class is not a JsonDeserializer";
+ }
+ else {
+ errorMessage = "class is not a JsonSerializer or JsonDeserializer";
+ }
+ }
+
+ if (errorMessage != null) {
+ result.setResult(ADAPTOR_CLASS, ValidationStatus.INVALID, errorMessage);
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * Check a class exists.
+ *
+ * @param parameterName the parameter name of the class to check for existence
+ * @param classToCheck the class to check for existence
+ * @param result the result of the check
+ */
+ private Class<?> getClass(String parameterName, String classToCheck, final GroupValidationResult result) {
+ if (classToCheck == null || classToCheck.trim().length() == 0) {
+ result.setResult(parameterName, ValidationStatus.INVALID, "parameter is null or blank");
+ return null;
+ }
+
+ // Get the class for the event protocol
+ try {
+ return Class.forName(classToCheck);
+ } catch (final ClassNotFoundException e) {
+ result.setResult(parameterName, ValidationStatus.INVALID, "class not found: " + e.getMessage());
+ LOGGER.warn("class not found: ", e);
+ return null;
+ }
+ }
+}
package org.onap.policy.apex.context.impl.schema.java;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
import org.onap.policy.apex.context.parameters.SchemaHelperParameters;
+import org.onap.policy.common.parameters.GroupValidationResult;
/**
* The Schema helper parameter class for the Java schema helper is an empty parameter class that acts as a placeholder.
* @author Liam Fallon (liam.fallon@ericsson.com)
*/
public class JavaSchemaHelperParameters extends SchemaHelperParameters {
+ // Map of specific type adapters for this event
+ private Map<String, JavaSchemaHelperJsonAdapterParameters> jsonAdapters = new LinkedHashMap<>();
/**
- * The Constructor.
+ * Constructor for Java schema helper parameters.
*/
public JavaSchemaHelperParameters() {
+ this.setName("Java");
this.setSchemaHelperPluginClass(JavaSchemaHelper.class.getCanonicalName());
}
+
+ /**
+ * Get the JSON adapters.
+ *
+ * @return the JSON adapters
+ */
+ public Map<String, JavaSchemaHelperJsonAdapterParameters> getJsonAdapters() {
+ return jsonAdapters;
+ }
+
+ /**
+ * Set JSON adapters for the schema helper.
+ *
+ * @param jsonAdapters the JSON adapters
+ */
+ public void setJsonAdapters(Map<String, JavaSchemaHelperJsonAdapterParameters> jsonAdapters) {
+ this.jsonAdapters = jsonAdapters;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.onap.policy.apex.service.parameters.ApexParameterValidator#validate()
+ */
+ @Override
+ public GroupValidationResult validate() {
+ final GroupValidationResult result = new GroupValidationResult(this);
+
+ for (Entry<String, JavaSchemaHelperJsonAdapterParameters> typeAdapterEntry : jsonAdapters.entrySet()) {
+ result.setResult("jsonAdapters", typeAdapterEntry.getKey(), typeAdapterEntry.getValue().validate());
+ }
+ return result;
+ }
+
}
import org.onap.policy.common.parameters.GroupValidationResult;
import org.onap.policy.common.parameters.ParameterGroup;
+// @formatter:off
/**
- * Bean class to hold parameters for context handling in Apex. This class contains all the context
- * parameters for schema handling, distribution, locking, and persistence of context albums.
+ * Bean class to hold parameters for context handling in Apex. This class contains all the context parameters for schema
+ * handling, distribution, locking, and persistence of context albums.
*
* <p>The following parameters are defined:
* <ol>
- * <li>flushPeriod: Context is flushed to any persistor plugin that is defined periodically, and the
- * period for flushing is the flush period.
- * <li>distributorParameters: The parameters (a {@link DistributorParameters} instance) for the
- * distributor plugin that is being used for context album distribution
- * <li>schemaParameters: The parameters (a {@link SchemaParameters} instance) for the schema plugin
- * that is being used for context album schemas
- * <li>lockManagerParameters: The parameters (a {@link LockManagerParameters} instance) for the
- * locking mechanism plugin that is being used for context album locking
- * <li>persistorParameters: The parameters (a {@link PersistorParameters} instance) for the
- * persistence plugin that is being used for context album persistence
+ * <li>flushPeriod: Context is flushed to any persistor plugin that is defined periodically, and the period for flushing
+ * is the flush period.
+ * <li>distributorParameters: The parameters (a {@link DistributorParameters} instance) for the distributor plugin that
+ * is being used for context album distribution
+ * <li>schemaParameters: The parameters (a {@link SchemaParameters} instance) for the schema plugin that is being used
+ * for context album schemas
+ * <li>lockManagerParameters: The parameters (a {@link LockManagerParameters} instance) for the locking mechanism plugin
+ * that is being used for context album locking
+ * <li>persistorParameters: The parameters (a {@link PersistorParameters} instance) for the persistence plugin that is
+ * being used for context album persistence
* </ol>
- *
- * @author Liam Fallon (liam.fallon@ericsson.com)
*/
+// @formatter:on
public class ContextParameters implements ParameterGroup {
// @formatter:off
// Plugin Parameters
// @formatter:on
/**
- * Constructor to create a context parameters instance and register the instance with the
- * parameter service.
+ * Constructor to create a context parameters instance and register the instance with the parameter service.
*/
public ContextParameters() {
super();
public void setPersistorParameters(final PersistorParameters persistorParameters) {
this.persistorParameters = persistorParameters;
}
-
+
@Override
public String toString() {
return "ContextParameters [name=" + name + ", distributorParameters=" + distributorParameters
@Override
public GroupValidationResult validate() {
- return new GroupValidationResult(this);
+ GroupValidationResult result = new GroupValidationResult(this);
+
+ // @formatter:off
+ result.setResult("distributorParameters", distributorParameters.validate());
+ result.setResult("schemaParameters", schemaParameters.validate());
+ result.setResult("lockManagerParameters", lockManagerParameters.validate());
+ result.setResult("persistorParameters", persistorParameters.validate());
+ // @formatter:on
+
+ return result;
}
}
package org.onap.policy.apex.context.parameters;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.TreeMap;
import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
import org.onap.policy.common.parameters.ParameterGroup;
/**
- * Bean class holding schema parameters for schemas and their helpers. As more than one schema can
- * be used in Apex simultaneously, this class is used to hold the schemas that are defined in a
- * given Apex system and to get the schema helper plugin parameters {@link SchemaHelperParameters}
- * for each schema.
+ * Bean class holding schema parameters for schemas and their helpers. As more than one schema can be used in Apex
+ * simultaneously, this class is used to hold the schemas that are defined in a given Apex system and to get the schema
+ * helper plugin parameters {@link SchemaHelperParameters} for each schema.
*
* <p>The default {@code Java} schema is always defined and its parameters are held in a
* {@link JavaSchemaHelperParameters} instance.
private Map<String, SchemaHelperParameters> schemaHelperParameterMap;
/**
- * Constructor to create a distributor parameters instance and register the instance with the
- * parameter service.
+ * Constructor to create a distributor parameters instance and register the instance with the parameter service.
*/
public SchemaParameters() {
super();
public SchemaHelperParameters getSchemaHelperParameters(final String schemaFlavour) {
return schemaHelperParameterMap.get(schemaFlavour);
}
-
+
@Override
public String getName() {
return name;
@Override
public GroupValidationResult validate() {
- return new GroupValidationResult(this);
+ final GroupValidationResult result = new GroupValidationResult(this);
+
+ for (Entry<String, SchemaHelperParameters> schemaHelperEntry : schemaHelperParameterMap.entrySet()) {
+ result.setResult("schemaHelperParameterMap", schemaHelperEntry.getKey(),
+ schemaHelperEntry.getValue().validate());
+ }
+ return result;
}
}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2018 Ericsson. 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.context.impl.schema.java;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonSerializationContext;
+import com.google.gson.JsonSerializer;
+
+import java.lang.reflect.Type;
+
+/**
+ * This class serialises and deserialises various type of event protocol parameters to and from
+ * JSON.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class BadJsonAdapter
+ implements JsonSerializer<String>, JsonDeserializer<String> {
+
+ public BadJsonAdapter() throws IllegalAccessException {
+ throw new IllegalAccessException("Test for Bad Adapter");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public JsonElement serialize(final String src, final Type typeOfSrc,
+ final JsonSerializationContext context) {
+ return new Gson().toJsonTree(src, String.class);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String deserialize(final JsonElement json, final Type typeOfT,
+ final JsonDeserializationContext context) {
+
+ return new Gson().fromJson(json, String.class);
+ }
+}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2018 Ericsson. 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.context.impl.schema.java;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.junit.Test;
+
+/**
+ * Test Java schema helper parameters.
+ */
+public class JavaSchemaHelperParametersTest {
+
+ @Test
+ public void testJavaSchemaHelperParameters() {
+ JavaSchemaHelperParameters pars = new JavaSchemaHelperParameters();
+
+ assertEquals("org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelper",
+ pars.getSchemaHelperPluginClass());
+
+ assertEquals(0, pars.getJsonAdapters().size());
+
+ JavaSchemaHelperJsonAdapterParameters jsonPars = new JavaSchemaHelperJsonAdapterParameters();
+ pars.getJsonAdapters().put("JsonAdapter0", jsonPars);
+
+ assertFalse(pars.validate().isValid());
+
+ jsonPars.setAdaptedClass("AAA");
+ jsonPars.setAdaptorClass("AAA");
+ assertFalse(pars.validate().isValid());
+
+ jsonPars.setAdaptedClass("java.lang.String");
+ jsonPars.setAdaptorClass("AAA");
+ assertFalse(pars.validate().isValid());
+
+ jsonPars.setAdaptedClass("AAA");
+ jsonPars.setAdaptorClass("java.lang.String");
+ assertFalse(pars.validate().isValid());
+
+ jsonPars.setAdaptedClass("java.lang.String");
+ jsonPars.setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.TestJsonDeserializer");
+ assertFalse(pars.validate().isValid());
+
+ jsonPars.setAdaptedClass("java.lang.String");
+ jsonPars.setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.TestJsonSerializer");
+ assertFalse(pars.validate().isValid());
+
+ jsonPars.setAdaptedClass("java.lang.String");
+ jsonPars.setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.TestJsonAdapter");
+ assertTrue(pars.validate().isValid());
+
+ Map<String, JavaSchemaHelperJsonAdapterParameters> adapterMap = new LinkedHashMap<>();
+
+ pars.setJsonAdapters(adapterMap);
+ assertTrue(pars.validate().isValid());
+ }
+
+ @Test
+ public void testJavaSchemaHelperJsonAdapterParameters() {
+ JavaSchemaHelperJsonAdapterParameters pars = new JavaSchemaHelperJsonAdapterParameters();
+
+ assertNull(pars.getName());
+ assertNull(pars.getAdaptedClass());
+ assertNull(pars.getAdaptedClazz());
+ assertNull(pars.getAdaptorClass());
+ assertNull(pars.getAdaptorClazz());
+
+ pars.setName("Zooby");
+ assertEquals("Zooby", pars.getAdaptedClass());
+ assertEquals("Zooby", pars.getName());
+ assertNull(pars.getAdaptedClazz());
+
+ pars.setAdaptorClass("Zooby");
+ assertEquals("Zooby", pars.getAdaptorClass());
+ assertNull(pars.getAdaptorClazz());
+ }
+}
import com.google.gson.JsonPrimitive;
import java.math.BigDecimal;
+import java.time.Instant;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.apex.context.ContextRuntimeException;
import org.onap.policy.apex.context.SchemaHelper;
+import org.onap.policy.apex.context.parameters.ContextParameterConstants;
+import org.onap.policy.apex.context.parameters.SchemaParameters;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
-
+import org.onap.policy.common.parameters.ParameterService;
public class JavaSchemaHelperTest {
+ /**
+ * Initialize JSON adapters.
+ */
+ @BeforeClass
+ public static void registerParameters() {
+ JavaSchemaHelperParameters javaSchemaHelperPars = new JavaSchemaHelperParameters();
+
+ JavaSchemaHelperJsonAdapterParameters stringAdapterPars = new JavaSchemaHelperJsonAdapterParameters();
+ stringAdapterPars.setAdaptedClass("java.lang.String");
+ stringAdapterPars.setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.TestJsonAdapter");
+
+ javaSchemaHelperPars.getJsonAdapters().put("String", stringAdapterPars);
+
+ SchemaParameters schemaPars = new SchemaParameters();
+ schemaPars.getSchemaHelperParameterMap().put("Java", javaSchemaHelperPars);
+
+ ParameterService.register(schemaPars);
+ }
+
+ @AfterClass
+ public static void deregisterParameters() {
+ ParameterService.clear();
+ }
@Test
public void testJavaSchemaHelperInit() {
fail("test should throw an exception here");
} catch (ContextRuntimeException e) {
assertEquals("UserKey:0.0.1: class/type java.lang.Rubbish for context schema"
- + " \"SchemaKey:0.0.1\" not found. Check the class path of the JVM", e.getMessage());
+ + " \"SchemaKey:0.0.1\" not found. Check the class path of the JVM", e.getMessage());
}
AxContextSchema builtInJavaTypeSchema = new AxContextSchema(schemaKey, "Java", "short");
fail("test should throw an exception here");
} catch (ContextRuntimeException e) {
assertEquals("UserKey:0.0.1: class/type short for context schema "
- + "\"SchemaKey:0.0.1\" not found. Primitive types are not supported."
- + " Use the appropriate Java boxing type instead.", e.getMessage());
+ + "\"SchemaKey:0.0.1\" not found. Primitive types are not supported."
+ + " Use the appropriate Java boxing type instead.", e.getMessage());
}
}
fail("test should throw an exception here");
} catch (ContextRuntimeException e) {
assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null",
- e.getMessage());
+ e.getMessage());
}
try {
fail("test should throw an exception here");
} catch (ContextRuntimeException e) {
assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null",
- e.getMessage());
+ e.getMessage());
}
try {
fail("test should throw an exception here");
} catch (ContextRuntimeException e) {
assertEquals("NULL:0.0.0: could not create an instance, schema class for the schema is null",
- e.getMessage());
+ e.getMessage());
}
AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
fail("test should throw an exception here");
} catch (ContextRuntimeException e) {
assertEquals("UserKey:0.0.1: could not create an instance of class "
- + "\"java.lang.Integer\" using the default constructor \"Integer()\"", e.getMessage());
+ + "\"java.lang.Integer\" using the default constructor \"Integer()\"", e.getMessage());
}
try {
fail("test should throw an exception here");
} catch (ContextRuntimeException e) {
assertEquals("UserKey:0.0.1: the object \"1.23\" of type "
- + "\"java.lang.Float\" is not an instance of JsonObject and is not "
- + "assignable to \"java.lang.Integer\"", e.getMessage());
+ + "\"java.lang.Float\" is not an instance of JsonObject and is not "
+ + "assignable to \"java.lang.Integer\"", e.getMessage());
}
try {
fail("test should throw an exception here");
} catch (ContextRuntimeException e) {
assertEquals("UserKey:0.0.1: could not create an instance of class \"java.lang.Integer\" "
- + "using the string constructor \"Integer(String)\"", e.getMessage());
+ + "using the string constructor \"Integer(String)\"", e.getMessage());
}
JsonElement jsonIntElement = null;
doubleSchemaHelper.init(userKey, doubleSchema);
stringSchemaHelper.init(userKey, stringSchema);
- AxContextSchema myBaseClassSchema =
- new AxContextSchema(schemaKey, "Java", "org.onap.policy.apex.context.impl.schema.java.MyBaseClass");
+ AxContextSchema myBaseClassSchema = new AxContextSchema(schemaKey, "Java",
+ "org.onap.policy.apex.context.impl.schema.java.MyBaseClass");
SchemaHelper myBaseClassSchemaHelper = new JavaSchemaHelper();
myBaseClassSchemaHelper.init(userKey, myBaseClassSchema);
fail("test should throw an exception here");
} catch (ContextRuntimeException e) {
assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
- + "compatible with class \"java.lang.Byte\"", e.getMessage());
+ + "compatible with class \"java.lang.Byte\"", e.getMessage());
}
assertEquals(null, shortSchemaHelper.unmarshal(null));
fail("test should throw an exception here");
} catch (ContextRuntimeException e) {
assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
- + "compatible with class \"java.lang.Short\"", e.getMessage());
+ + "compatible with class \"java.lang.Short\"", e.getMessage());
}
assertEquals(null, intSchemaHelper.unmarshal(null));
fail("test should throw an exception here");
} catch (ContextRuntimeException e) {
assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
- + "compatible with class \"java.lang.Integer\"", e.getMessage());
+ + "compatible with class \"java.lang.Integer\"", e.getMessage());
}
assertEquals(null, longSchemaHelper.unmarshal(null));
fail("test should throw an exception here");
} catch (ContextRuntimeException e) {
assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
- + "compatible with class \"java.lang.Long\"", e.getMessage());
+ + "compatible with class \"java.lang.Long\"", e.getMessage());
}
assertEquals(null, floatSchemaHelper.unmarshal(null));
fail("test should throw an exception here");
} catch (ContextRuntimeException e) {
assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
- + "compatible with class \"java.lang.Float\"", e.getMessage());
+ + "compatible with class \"java.lang.Float\"", e.getMessage());
}
assertEquals(null, doubleSchemaHelper.unmarshal(null));
fail("test should throw an exception here");
} catch (ContextRuntimeException e) {
assertEquals("UserKey:0.0.1: object \"one two three\" of class \"java.lang.String\" not "
- + "compatible with class \"java.lang.Double\"", e.getMessage());
+ + "compatible with class \"java.lang.Double\"", e.getMessage());
}
assertEquals("123", stringSchemaHelper.unmarshal(123));
fail("test should throw an exception here");
} catch (ContextRuntimeException e) {
assertEquals("UserKey:0.0.1: object \"123.45\" of class \"java.lang.Double\" not "
- + "compatible with class \"java.lang.Integer\"", e.getMessage());
+ + "compatible with class \"java.lang.Integer\"", e.getMessage());
}
JsonPrimitive intJsonPrimitive = (JsonPrimitive) intSchemaHelper.marshal2Object(123);
assertEquals(123, intJsonPrimitive.getAsInt());
}
+
+ @Test
+ public void testJavaSchemaHelperAdapters() {
+ AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
+ AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
+
+ AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
+ SchemaHelper stringSchemaHelper = new JavaSchemaHelper();
+ stringSchemaHelper.init(userKey, stringSchema);
+
+ assertEquals("null", stringSchemaHelper.marshal2String(null));
+ assertEquals("\"Hello\"", stringSchemaHelper.marshal2String("Hello"));
+ try {
+ stringSchemaHelper.marshal2String(Instant.ofEpochMilli(1000));
+ fail("test should throw an exception here");
+ } catch (ContextRuntimeException e) {
+ assertEquals("UserKey:0.0.1: object \"1970-01-01T00:00:01Z\" of class \"java.time.Instant\" "
+ + "not compatible with class \"java.lang.String\"", e.getMessage());
+ }
+
+ JsonPrimitive stringJsonPrimitive = (JsonPrimitive) stringSchemaHelper.marshal2Object("Another String");
+ assertEquals("Another String", stringJsonPrimitive.getAsString());
+ }
+
+ @Test
+ public void testJavaSchemaHelperBadAdapter() {
+ AxArtifactKey schemaKey = new AxArtifactKey("SchemaKey", "0.0.1");
+ AxArtifactKey userKey = new AxArtifactKey("UserKey", "0.0.1");
+
+ SchemaParameters pars = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
+
+ JavaSchemaHelperParameters javaShPars = (JavaSchemaHelperParameters) pars.getSchemaHelperParameterMap()
+ .get("Java");
+ javaShPars.getJsonAdapters().get("String")
+ .setAdaptorClass("org.onap.policy.apex.context.impl.schema.java.BadJsonAdapter");
+
+ AxContextSchema stringSchema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
+ SchemaHelper stringSchemaHelper = new JavaSchemaHelper();
+ stringSchemaHelper.init(userKey, stringSchema);
+
+ try {
+ stringSchemaHelper.marshal2String("Hello");
+ fail("test should throw an exception");
+ } catch (ContextRuntimeException pre) {
+ assertEquals("UserKey:0.0.1: instantiation of adapter class "
+ + "\"org.onap.policy.apex.context.impl.schema.java.BadJsonAdapter\" "
+ + "to decode and encode class \"java.lang.String\" failed: Test for Bad Adapter",
+ pre.getMessage());
+ }
+ }
+
+ @Test
+ public void testJavaSchemaHelperDefaultAdapter() {
+ SchemaParameters pars = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
+
+ JavaSchemaHelperParameters javaShPars = (JavaSchemaHelperParameters) pars.getSchemaHelperParameterMap()
+ .get("Java");
+
+ pars.getSchemaHelperParameterMap().clear();
+
+ testJavaSchemaHelperAdapters();
+
+ pars.getSchemaHelperParameterMap().put("Java", javaShPars);
+ }
}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2018 Ericsson. 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.context.impl.schema.java;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonSerializationContext;
+import com.google.gson.JsonSerializer;
+
+import java.lang.reflect.Type;
+
+/**
+ * This class serialises and deserialises various type of event protocol parameters to and from JSON.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class TestJsonAdapter implements JsonSerializer<String>, JsonDeserializer<String> {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public JsonElement serialize(final String src, final Type typeOfSrc, final JsonSerializationContext context) {
+ return new Gson().toJsonTree(src, String.class);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) {
+
+ return new Gson().fromJson(json, String.class);
+ }
+}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2018 Ericsson. 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.context.impl.schema.java;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+
+import java.lang.reflect.Type;
+
+/**
+ * This class serialises and deserialises various type of event protocol parameters to and from JSON.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class TestJsonDeserializer implements JsonDeserializer<String> {
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) {
+
+ return new Gson().fromJson(json, String.class);
+ }
+}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2018 Ericsson. 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.context.impl.schema.java;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonSerializationContext;
+import com.google.gson.JsonSerializer;
+
+import java.lang.reflect.Type;
+
+import org.onap.policy.common.parameters.ParameterRuntimeException;
+import org.slf4j.ext.XLogger;
+import org.slf4j.ext.XLoggerFactory;
+
+/**
+ * This class serialises and deserialises various type of event protocol parameters to and from JSON.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class TestJsonSerializer implements JsonSerializer<String> {
+ private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestJsonSerializer.class);
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public JsonElement serialize(final String src, final Type typeOfSrc, final JsonSerializationContext context) {
+ final String returnMessage = "serialization of Apex event protocol parameters to Json is not supported";
+ LOGGER.error(returnMessage);
+ throw new ParameterRuntimeException(returnMessage);
+ }
+}
package org.onap.policy.apex.core.engine;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.TreeMap;
import org.onap.policy.apex.context.parameters.ContextParameters;
import org.onap.policy.common.parameters.ParameterGroup;
/**
- * This class holds the parameters for a single Apex engine. This parameter class holds parameters
- * for context schemas and context albums for the engine and a map of the logic flavour executors
- * defined for the engine and the parameters for each of those executors.
+ * This class holds the parameters for a single Apex engine. This parameter class holds parameters for context schemas
+ * and context albums for the engine and a map of the logic flavour executors defined for the engine and the parameters
+ * for each of those executors.
*
- * <p>The context parameters for the engine are held in a {@link ContextParameters} instance. This
- * instance holds the parameters for context schema handling that will be used by the engine as well
- * as the context album distribution, locking, and persistence parameters.
+ * <p>The context parameters for the engine are held in a {@link ContextParameters} instance. This instance holds the
+ * parameters for context schema handling that will be used by the engine as well as the context album distribution,
+ * locking, and persistence parameters.
*
- * <p>In Apex, an engine can be configured to use many logic flavours. The executors for each logic
- * flavour are identified by their name. Each logic flavour executor must have an instance of
- * {@link ExecutorParameters} defined for it, which specifies the executor plugins to use for that
- * logic flavour executor and specific parameters for those executor plugins.
+ * <p>In Apex, an engine can be configured to use many logic flavours. The executors for each logic flavour are
+ * identified by their name. Each logic flavour executor must have an instance of {@link ExecutorParameters} defined for
+ * it, which specifies the executor plugins to use for that logic flavour executor and specific parameters for those
+ * executor plugins.
*
* @author Liam Fallon (liam.fallon@ericsson.com)
*/
private Map<String, ExecutorParameters> executorParameterMap = new TreeMap<>();
/**
- * Constructor to create an engine parameters instance and register the instance with the
- * parameter service.
+ * Constructor to create an engine parameters instance and register the instance with the parameter service.
*/
public EngineParameters() {
super();
-
+
// Set the name for the parameters
this.name = EngineParameterConstants.MAIN_GROUP_NAME;
}
public void setExecutorParameterMap(final Map<String, ExecutorParameters> executorParameterMap) {
this.executorParameterMap = executorParameterMap;
}
-
+
@Override
public String getName() {
return name;
@Override
public GroupValidationResult validate() {
- return new GroupValidationResult(this);
+ final GroupValidationResult result = new GroupValidationResult(this);
+
+ result.setResult("contextParameters", contextParameters.validate());
+
+ for (Entry<String, ExecutorParameters> executorParEntry : executorParameterMap.entrySet()) {
+ result.setResult("executorParameterMap", executorParEntry.getKey(), executorParEntry.getValue().validate());
+ }
+ return result;
}
}
import org.onap.policy.common.parameters.ValidationStatus;
import org.onap.policy.common.utils.resources.ResourceUtils;
+// @formatter:off
/**
* This class holds the parameters for an Apex Engine Service with multiple engine threads running multiple engines.
*
* <li>periodicEventPeriod: The period in milliseconds at which the periodic event PERIOIC_EVENT will be generated by
* APEX, 0 means no periodic event generation, negative values are illegal.
* </ol>
- *
- * @author Liam Fallon (liam.fallon@ericsson.com)
*/
+// @formatter:on
public class EngineServiceParameters implements ParameterGroup {
private static final int MAX_PORT = 65535;
*/
public EngineServiceParameters() {
super();
-
+
// Set the name for the parameters
this.name = ApexParameterConstants.ENGINE_SERVICE_GROUP_NAME;
}
public GroupValidationResult validate() {
final GroupValidationResult result = new GroupValidationResult(this);
+ validateStringParameters(result);
+
+ validateNumericParameters(result);
+
+ if (policyModelFileName != null) {
+ validatePolicyModelFileName(result);
+ }
+ result.setResult("engineParameters", engineParameters.validate());
+
+ return result;
+ }
+
+ /**
+ * Validate string parameters.
+ *
+ * @param result the result of string parameter validation
+ */
+ private void validateStringParameters(final GroupValidationResult result) {
if (name == null || !name.matches(AxKey.NAME_REGEXP)) {
result.setResult("name", ValidationStatus.INVALID,
"name is invalid, it must match regular expression" + AxKey.NAME_REGEXP);
result.setResult("version", ValidationStatus.INVALID,
"version is invalid, it must match regular expression" + AxKey.VERSION_REGEXP);
}
+ }
+ /**
+ * Validate numeric parameters.
+ *
+ * @param result the result of numeric parameter validation
+ */
+ private void validateNumericParameters(final GroupValidationResult result) {
if (id < 0) {
result.setResult("id", ValidationStatus.INVALID,
"id not specified or specified value [" + id + "] invalid, must be specified as id >= 0");
+ "] invalid, must be specified as 1024 <= port <= 65535");
}
- if (policyModelFileName != null) {
- validatePolicyModelFileName(result);
- }
-
if (periodicEventPeriod < 0) {
result.setResult("periodicEventPeriod", ValidationStatus.INVALID, "periodicEventPeriod ["
+ periodicEventPeriod + "] invalid, must be specified in milliseconds as >=0");
}
-
- return result;
}
/**
* Validate the policy model file name parameter.
+ *
* @param result the variable in which to store the result of the validation
*/
private void validatePolicyModelFileName(final GroupValidationResult result) {
if (policyModelFileName.trim().length() == 0) {
- result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "\""
- + policyModelFileName + "\" invalid, must be specified as a non-empty string");
+ result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID,
+ "\"" + policyModelFileName + "\" invalid, must be specified as a non-empty string");
return;
}
-
+
// The file name can refer to a resource on the local file system or on the class
// path
final URL fileUrl = ResourceUtils.getUrl4Resource(policyModelFileName);