From aece3940d349329efe6d220961f6f2a487f90565 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Wed, 19 Sep 2018 17:23:48 +0100 Subject: Refactor Editor REST servlet Issue-ID: POLICY-1034 Change-Id: Ifa08c7a31b4fe19eb62c2035b67b6f5cad313b5f Signed-off-by: liamfallon --- .../client/editor/rest/ApexEditorRestResource.java | 2038 -------------------- .../policy/apex/client/editor/rest/RestUtils.java | 184 -- .../apex/client/editor/rest/bean/BeanBase.java | 70 - .../client/editor/rest/bean/BeanContextAlbum.java | 111 -- .../client/editor/rest/bean/BeanContextSchema.java | 101 - .../apex/client/editor/rest/bean/BeanEvent.java | 138 -- .../apex/client/editor/rest/bean/BeanField.java | 63 - .../apex/client/editor/rest/bean/BeanKeyRef.java | 61 - .../apex/client/editor/rest/bean/BeanLogic.java | 60 - .../apex/client/editor/rest/bean/BeanModel.java | 82 - .../apex/client/editor/rest/bean/BeanPolicy.java | 114 -- .../apex/client/editor/rest/bean/BeanState.java | 136 -- .../client/editor/rest/bean/BeanStateOutput.java | 62 - .../client/editor/rest/bean/BeanStateTaskRef.java | 71 - .../apex/client/editor/rest/bean/BeanTask.java | 135 -- .../client/editor/rest/bean/BeanTaskParameter.java | 62 - .../apex/client/editor/rest/bean/package-info.java | 27 - .../rest/handling/ApexEditorRestResource.java | 818 ++++++++ .../editor/rest/handling/ContextAlbumHandler.java | 204 ++ .../editor/rest/handling/ContextSchemaHandler.java | 191 ++ .../client/editor/rest/handling/EventHandler.java | 264 +++ .../editor/rest/handling/KeyInfoHandler.java | 72 + .../client/editor/rest/handling/ModelHandler.java | 446 +++++ .../client/editor/rest/handling/PolicyHandler.java | 578 ++++++ .../client/editor/rest/handling/RestCommand.java | 64 + .../editor/rest/handling/RestCommandHandler.java | 102 + .../editor/rest/handling/RestCommandType.java | 56 + .../client/editor/rest/handling/RestSession.java | 125 ++ .../editor/rest/handling/RestSessionHandler.java | 110 ++ .../client/editor/rest/handling/RestUtils.java | 184 ++ .../client/editor/rest/handling/TaskHandler.java | 460 +++++ .../client/editor/rest/handling/bean/BeanBase.java | 70 + .../rest/handling/bean/BeanContextAlbum.java | 111 ++ .../rest/handling/bean/BeanContextSchema.java | 101 + .../editor/rest/handling/bean/BeanEvent.java | 138 ++ .../editor/rest/handling/bean/BeanField.java | 63 + .../editor/rest/handling/bean/BeanKeyRef.java | 61 + .../editor/rest/handling/bean/BeanLogic.java | 60 + .../editor/rest/handling/bean/BeanModel.java | 82 + .../editor/rest/handling/bean/BeanPolicy.java | 114 ++ .../editor/rest/handling/bean/BeanState.java | 136 ++ .../editor/rest/handling/bean/BeanStateOutput.java | 62 + .../rest/handling/bean/BeanStateTaskRef.java | 71 + .../client/editor/rest/handling/bean/BeanTask.java | 135 ++ .../rest/handling/bean/BeanTaskParameter.java | 62 + .../editor/rest/handling/bean/package-info.java | 27 + .../resources/templates/PeriodicEventTemplate.json | 11 + .../editor/rest/TestApexEditorRestResource.java | 1515 --------------- .../apex/client/editor/rest/bean/BeanFake.java | 49 - .../apex/client/editor/rest/bean/TestBeans.java | 88 - .../rest/handling/TestApexEditorRestResource.java | 1506 +++++++++++++++ .../client/editor/rest/handling/bean/BeanFake.java | 49 + .../editor/rest/handling/bean/TestBeans.java | 88 + 53 files changed, 6621 insertions(+), 5167 deletions(-) delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/ApexEditorRestResource.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/RestUtils.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanBase.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanContextAlbum.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanContextSchema.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanEvent.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanField.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanKeyRef.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanLogic.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanModel.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanPolicy.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanState.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanStateOutput.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanStateTaskRef.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanTask.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanTaskParameter.java delete mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/package-info.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ApexEditorRestResource.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ContextAlbumHandler.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ContextSchemaHandler.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/EventHandler.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/KeyInfoHandler.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ModelHandler.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/PolicyHandler.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestCommand.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestCommandHandler.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestCommandType.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestSession.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestSessionHandler.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestUtils.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/TaskHandler.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanBase.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanContextAlbum.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanContextSchema.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanEvent.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanField.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanKeyRef.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanLogic.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanModel.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanPolicy.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanState.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanStateOutput.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanStateTaskRef.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanTask.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanTaskParameter.java create mode 100644 client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/package-info.java create mode 100644 client/client-editor/src/main/resources/templates/PeriodicEventTemplate.json delete mode 100644 client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/TestApexEditorRestResource.java delete mode 100644 client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/bean/BeanFake.java delete mode 100644 client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/bean/TestBeans.java create mode 100644 client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/TestApexEditorRestResource.java create mode 100644 client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanFake.java create mode 100644 client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/bean/TestBeans.java (limited to 'client/client-editor/src') diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/ApexEditorRestResource.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/ApexEditorRestResource.java deleted file mode 100644 index fed1a50ce..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/ApexEditorRestResource.java +++ /dev/null @@ -1,2038 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonObject; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.TreeMap; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; - -import org.onap.policy.apex.client.editor.rest.bean.BeanContextAlbum; -import org.onap.policy.apex.client.editor.rest.bean.BeanContextSchema; -import org.onap.policy.apex.client.editor.rest.bean.BeanEvent; -import org.onap.policy.apex.client.editor.rest.bean.BeanField; -import org.onap.policy.apex.client.editor.rest.bean.BeanKeyRef; -import org.onap.policy.apex.client.editor.rest.bean.BeanLogic; -import org.onap.policy.apex.client.editor.rest.bean.BeanModel; -import org.onap.policy.apex.client.editor.rest.bean.BeanPolicy; -import org.onap.policy.apex.client.editor.rest.bean.BeanState; -import org.onap.policy.apex.client.editor.rest.bean.BeanStateOutput; -import org.onap.policy.apex.client.editor.rest.bean.BeanStateTaskRef; -import org.onap.policy.apex.client.editor.rest.bean.BeanTask; -import org.onap.policy.apex.client.editor.rest.bean.BeanTaskParameter; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; -import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; -import org.onap.policy.apex.model.modelapi.ApexApiResult; -import org.onap.policy.apex.model.modelapi.ApexApiResult.Result; -import org.onap.policy.apex.model.modelapi.ApexModel; -import org.onap.policy.apex.model.modelapi.ApexModelFactory; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicy; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.apex.model.policymodel.concepts.AxTask; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * The class represents the root resource exposed at the base URL
The url to access this resource would be in the - * form {@code /rest//....}
For example: a PUT request to the following URL - * {@code http://localhost:8080/apex/rest/109/ContextSchema/Update}, with a JSON string payload containing the new - * {@code Schema} in the body, can be explained as:
  • The server or servlet is running at the base URL - * {@code http://localhost:8080/apex}
  • This resource {@code ApexRestEditorResource} is used because the path - * {@code rest/109} matches the {@code Path} filter specification for this Resource ({@code @Path("rest/{session}")}), - * where the {@code int} path parameter {@code session} is assigned the {@code int} value {@code 109}
  • The path - * {@code ContextSchema/Update} redirects this call to the method {@link #updateContextSchema(String)}, which should be - * a {@link javax.ws.rs.PUT}, with a single String in the body/payload which gets mapped to the single String parameter - * for the method.
  • So, in summary, the REST request updates a {@code ContextSchema} as specified in the payload for - * {@code session} number {@code 109}
- * - * Note: An allocated {@code Session} identifier must be included in (almost) all requests. Models for different - * {@code Session} identifiers are completely isolated from one another. - * - * Note: To create a new {@code Session}, and have a new session ID allocated use {@link javax.ws.rs.GET} request - * to {@code /rest/-1/Session/Create} (for example: {@code http://localhost:8080/apex/rest/-1/Session/Create} ) - * - */ -@Path("editor/{session}") -@Produces( - { MediaType.APPLICATION_JSON }) -@Consumes( - { MediaType.APPLICATION_JSON }) - -public class ApexEditorRestResource { - // Get a reference to the logger - private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorRestResource.class); - - // Recurring string constants - private static final String IN_TASK = "\" in task "; - private static final String POLICY_STATE_CREATED_OTHER_ERROR = - "\". The policy and state were created, but there was an error adding the"; - private static final String IN_STATE = "\" in state \""; - private static final String POLICY_CREATED_STATE_ERROR = - "\". The policy was created, but there was an error adding the state."; - private static final String FOR_POLICY = "\" for policy \""; - private static final String NOT_OK = ": Not OK"; - private static final String TASK_PARTIALLY_DEFINED = " The task has only been partially defined."; - private static final String POLICY_PARTIALLY_DEFINED = " The policy has only been partially defined."; - private static final String POLICY_WAS_CREATED = "\". The policy was created, "; - private static final String VERSION = "version"; - private static final String POLICY_KEY = "policyKey"; - private static final String DESCRIPTION = "description"; - private static final String APEX_KEY_INFO = "apexKeyInfo"; - - // The next session will have this number, stating at 0 - private static int nextSession = 0; - - // All REST editor sessions being handled by the server - private static final Map SESSIONMODELMAP = new TreeMap<>(); - - // The ID of this session. This gets injected from the URL. - @PathParam("session") - private int sessionId = -1; - - // The Apex model for the session - private ApexModel sessionApexModel = null; - - /** - * This method sets the Apex model for the current editor session. Don't forget to call {@link #commitChanges()} - * when finished! This makes requests atomic. - * - * @return the result of finding the session Apex model and setting it - */ - private ApexApiResult initialiseSessionForChanges() { - if (sessionId < 0) { - return new ApexApiResult(Result.FAILED, "Session ID \"" + sessionId + "\" is negative"); - } - - if (!SESSIONMODELMAP.containsKey(sessionId)) { - return new ApexApiResult(Result.FAILED, "A session with session ID \"" + sessionId + "\" does not exist"); - } - - if (sessionApexModel == null) { - sessionApexModel = SESSIONMODELMAP.get(sessionId).clone(); - } - return new ApexApiResult(); - } - - /** - * This method sets the Apex model for the current editor session. Don't make any changes to the model. - * - * @return the result of finding the session Apex model and setting it - */ - private ApexApiResult initialiseSessionForReadOnly() { - if (sessionId < 0) { - return new ApexApiResult(Result.FAILED, "Session ID \"" + sessionId + "\" is negative"); - } - - if (!SESSIONMODELMAP.containsKey(sessionId)) { - return new ApexApiResult(Result.FAILED, "A session with session ID \"" + sessionId + "\" does not exist"); - } - - if (sessionApexModel == null) { - sessionApexModel = SESSIONMODELMAP.get(sessionId); - } - return new ApexApiResult(); - } - - /** - * This method commits changes to the Apex model for the current editor session. This should only be called once, at - * the end of a successful change to the model for this session - * - * @return the result of committing the session Apex model - */ - private ApexApiResult commitChanges() { - - if (sessionApexModel == null) { - return new ApexApiResult(Result.FAILED, "Cannot commit a changes for Session ID \"" + sessionId - + "\", because it has not been initialised / started"); - } - - SESSIONMODELMAP.put(sessionId, sessionApexModel); - - return new ApexApiResult(); - } - - /** - * Creates a new session. Always call this method with sessionID -1, whereby a new sessionID will be allocated. If - * successful the new sessionID will be available in the first message in the result. - * - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}. This includes the session id - * for this session. - */ - @GET - @Path("Session/Create") - public ApexApiResult createSession() { - ApexApiResult ret = null; - LOGGER.entry(); - try { - if (sessionId != -1) { - ret = new ApexApiResult(Result.FAILED, "Session ID must be set to -1 to create sessions: " + sessionId); - return ret; - } - - final int newSessionId = nextSession; - - if (SESSIONMODELMAP.containsKey(newSessionId)) { - ret = new ApexApiResult(Result.FAILED, "Session already exists for session: " + newSessionId); - return ret; - } - - SESSIONMODELMAP.put(newSessionId, new ApexModelFactory().createApexModel(null, true)); - incrementNextSession(); - - ret = new ApexApiResult(Result.SUCCESS, Integer.toString(newSessionId)); - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - String result = "Session/Create" + (ret != null && ret.isOk() ? ": OK" : NOT_OK); - LOGGER.exit(result); - } - } - - /** - * Load the model from a JSON string for this session. - * - * @param jsonString the JSON string to be parsed. The returned value(s) will be similar to {@link AxPolicyModel}, - * with merged {@linkplain AxKeyInfo} for the root object. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @PUT - @Path("/Model/Load") - public ApexApiResult loadFromString(final String jsonString) { - ApexApiResult ret = null; - LOGGER.entry(jsonString); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - ret = sessionApexModel.loadFromString(jsonString); - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - String result = "Model/Load" + (ret != null && ret.isOk() ? ": OK" : NOT_OK); - LOGGER.exit(result); - } - } - - /** - * Analyse the model and return analysis results. If successful the analysis results will be available in the - * messages in the result. - * - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @GET - @Path("Model/Analyse") - public ApexApiResult analyse() { - ApexApiResult ret = null; - LOGGER.entry(); - try { - ret = initialiseSessionForReadOnly(); - if (ret.isNok()) { - return ret; - } - - ret = sessionApexModel.analyse(); - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Model/Analyse" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Validate the model and return validation results. If successful the validation results will be available in the - * messages in the result. - * - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @GET - @Path("Model/Validate") - public ApexApiResult validate() { - ApexApiResult ret = null; - LOGGER.entry(); - try { - ret = initialiseSessionForReadOnly(); - if (ret.isNok()) { - return ret; - } - - ret = sessionApexModel.validate(); - - ret = addKeyInfo2Messages(ret); - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Model/Validate" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Creates the new model model for this session. - * - * @param jsonString the JSON string to be parsed containing the new model. See {@linkplain BeanModel} - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @POST - @Path("Model/Create") - public ApexApiResult createModel(final String jsonString) { - ApexApiResult ret = null; - LOGGER.entry(jsonString); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - final BeanModel jsonbean = RestUtils.getJsonParameters(jsonString, BeanModel.class); - ret = sessionApexModel.createModel(jsonbean.getName(), jsonbean.getVersion(), jsonbean.getUuid(), - jsonbean.getDescription()); - - if (ret.isOk()) { - ret = addKeyInfo2Messages(ret); - } - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Model/Create" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Update the model for this session. - * - * @param jsonString the JSON string to be parsed containing the updated model. See {@linkplain BeanModel} - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @PUT - @Path("Model/Update") - public ApexApiResult updateModel(final String jsonString) { - ApexApiResult ret = null; - LOGGER.entry(jsonString); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - final BeanModel jsonbean = RestUtils.getJsonParameters(jsonString, BeanModel.class); - ret = sessionApexModel.updateModel(jsonbean.getName(), jsonbean.getVersion(), jsonbean.getUuid(), - jsonbean.getDescription()); - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Model/Update" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Gets the key for the model for this session. If successful the model key will be available in the first message - * in the result. See {@linkplain AxKey} - * - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @GET - @Path("Model/GetKey") - public ApexApiResult getModelKey() { - ApexApiResult ret = null; - LOGGER.entry(); - try { - ret = initialiseSessionForReadOnly(); - if (ret.isNok()) { - return ret; - } - - ret = sessionApexModel.getModelKey(); - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Model/GetKey" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Retrieve the model for this session. If successful the model will be available in the first message in the - * result. The returned value will be similar to a {@link AxPolicyModel}, with merged {@linkplain AxKeyInfo} for the - * root object. - * - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @GET - @Path("Model/Get") - public ApexApiResult listModel() { - ApexApiResult ret = null; - LOGGER.entry(); - try { - ret = initialiseSessionForReadOnly(); - if (ret.isNok()) { - return ret; - } - - ret = sessionApexModel.listModel(); - if (ret.isNok()) { - return ret; - } - - ret = addKeyInfo2Messages(ret); - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Model/Get" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Download the model for this session as a String. - * - * @return the model represented as a JSON string. See {@linkplain AxPolicyModel} - */ - @GET - @Path("Model/Download") - public String downloadModel() { - ApexApiResult ret = null; - LOGGER.entry(); - try { - - ret = initialiseSessionForReadOnly(); - if (ret.isNok()) { - throw new IllegalStateException("Cannot download file: " + ret.getMessage()); - } - - ret = sessionApexModel.listModel(); - if (ret.isNok()) { - throw new IllegalStateException("Cannot download file: " + ret.getMessage()); - } - - return ret.getMessage(); - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - String result = "Model/Download" + (ret != null && ret.isOk() ? ": OK" : NOT_OK); - LOGGER.exit(result); - } - } - - /** - * Delete the model for this session. - * - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @DELETE - @Path("Model/Delete") - public ApexApiResult deleteModel() { - ApexApiResult ret = null; - LOGGER.entry(); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - ret = sessionApexModel.deleteModel(); - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Model/Delete" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * List key information with the given key names/versions. If successful the result(s) will be available in the - * result messages. See {@linkplain AxKeyInfo} - * - * @param name the name to search for. If null or empty, then all names will be queried - * @param version the version to search for. If null then all versions will be searched for. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @GET - @Path("KeyInformation/Get") - public ApexApiResult listKeyInformation(@QueryParam("name") final String name, - @QueryParam(VERSION) final String version) { - ApexApiResult ret = null; - String name1 = name; - String version1 = version; - LOGGER.entry(name1, version1); - try { - ret = initialiseSessionForReadOnly(); - if (ret.isNok()) { - return ret; - } - - if (name1 == null || "".equals(name1)) { - name1 = null; - } - if (version1 == null || "".equals(version1)) { - version1 = null; - } - - ret = sessionApexModel.listKeyInformation(name1, version1); - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("KeyInformation/Get" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Creates a context schema with the information in the JSON string passed. - * - * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextSchema} - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @POST - @Path("ContextSchema/Create") - public ApexApiResult createContextSchema(final String jsonString) { - ApexApiResult ret = null; - LOGGER.entry(jsonString); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - final BeanContextSchema jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class); - ret = sessionApexModel.createContextSchema(jsonbean.getName(), jsonbean.getVersion(), - jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(), jsonbean.getUuid(), - jsonbean.getDescription()); - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("ContextSchema/Create" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Update a context schema with the information in the JSON string passed. - * - * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextSchema} - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @PUT - @Path("ContextSchema/Update") - public ApexApiResult updateContextSchema(final String jsonString) { - ApexApiResult ret = null; - LOGGER.entry(jsonString); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - final BeanContextSchema jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class); - - ret = sessionApexModel.updateContextSchema(jsonbean.getName(), jsonbean.getVersion(), - jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(), jsonbean.getUuid(), - jsonbean.getDescription()); - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("ContextSchema/Update" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * List context schemas with the given key names/versions. If successful the result(s) will be available in the - * result messages. The returned value(s) will be similar to {@link AxContextSchema}, with merged - * {@linkplain AxKeyInfo} for the root object. - * - * @param name the name to search for. If null or empty, then all names will be queried - * @param version the version to search for. If null then all versions will be searched for. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @GET - @Path("ContextSchema/Get") - public ApexApiResult listContextSchemas(@QueryParam("name") final String name, - @QueryParam(VERSION) final String version) { - ApexApiResult ret = null; - String name1 = name; - String version1 = version; - LOGGER.entry(name1, version1); - try { - ret = initialiseSessionForReadOnly(); - if (ret.isNok()) { - return ret; - } - - if (name1 == null || "".equals(name1)) { - name1 = null; - } - if (version1 == null || "".equals(version1)) { - version1 = null; - } - - ret = sessionApexModel.listContextSchemas(name1, version1); - if (ret.isNok()) { - return ret; - } - - ret = addKeyInfo2Messages(ret); - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("ContextSchema/Get" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Delete context schemas with the given key names/versions. - * - * @param name the name to search for. If null or empty, then all names will be queried - * @param version the version to search for. If null then all versions will be searched for. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @DELETE - @Path("ContextSchema/Delete") - public ApexApiResult deleteContextSchema(@QueryParam("name") final String name, - @QueryParam(VERSION) final String version) { - ApexApiResult ret = null; - String name1 = name; - String version1 = version; - LOGGER.entry(name1, version1); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - if (name1 == null || "".equals(name1)) { - name1 = null; - } - if (version1 == null || "".equals(version1)) { - version1 = null; - } - - ret = sessionApexModel.deleteContextSchema(name1, version1); - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("ContextSchema/Delete" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Validate context schemas with the given key names/versions. The result(s) will be available in the result - * messages. - * - * @param name the name to search for. If null or empty, then all names will be queried - * @param version the version to search for. If null then all versions will be searched for. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @GET - @Path("Validate/ContextSchema") - public ApexApiResult validateContextSchemas(@QueryParam("name") final String name, - @QueryParam(VERSION) final String version) { - ApexApiResult ret = null; - String name1 = name; - String version1 = version; - LOGGER.entry(name1, version1); - try { - if (name1 == null || "".equals(name1)) { - name1 = null; - } - if (version1 == null || "".equals(version1)) { - version1 = null; - } - - ret = initialiseSessionForReadOnly(); - if (ret.isNok()) { - return ret; - } - - ret = sessionApexModel.validateContextSchemas(name1, version1); - if (ret.isNok()) { - return ret; - } - - ret = addKeyInfo2Messages(ret); - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Validate/ContextSchema" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Creates a context album with the information in the JSON string passed. - * - * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextAlbum} - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @POST - @Path("ContextAlbum/Create") - public ApexApiResult createContextAlbum(final String jsonString) { - ApexApiResult ret = null; - LOGGER.entry(jsonString); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class); - - ret = sessionApexModel.createContextAlbum(jsonbean.getName(), jsonbean.getVersion(), jsonbean.getScope(), - Boolean.toString(jsonbean.getWriteable()), jsonbean.getItemSchema().getName(), - jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(), jsonbean.getDescription()); - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("ContextAlbum/Create" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Update a context album with the information in the JSON string passed. - * - * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextAlbum} - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @PUT - @Path("ContextAlbum/Update") - public ApexApiResult updateContextAlbum(final String jsonString) { - ApexApiResult ret = null; - LOGGER.entry(jsonString); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class); - - ret = sessionApexModel.updateContextAlbum(jsonbean.getName(), jsonbean.getVersion(), jsonbean.getScope(), - Boolean.toString(jsonbean.getWriteable()), jsonbean.getItemSchema().getName(), - jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(), jsonbean.getDescription()); - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("ContextAlbum/Update" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * List context albums with the given key names/versions. If successful the result(s) will be available in the - * result messages. The returned value(s) will be similar to {@link AxContextAlbum}, with merged - * {@linkplain AxKeyInfo} for the root object. - * - * @param name the name to search for. If null or empty, then all names will be queried - * @param version the version to search for. If null then all versions will be searched for. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @GET - @Path("ContextAlbum/Get") - public ApexApiResult listContextAlbums(@QueryParam("name") final String name, - @QueryParam(VERSION) final String version) { - ApexApiResult ret = null; - String name1 = name; - String version1 = version; - LOGGER.entry(name1, version1); - try { - ret = initialiseSessionForReadOnly(); - if (ret.isNok()) { - return ret; - } - - if (name1 == null || "".equals(name1)) { - name1 = null; - } - if (version1 == null || "".equals(version1)) { - version1 = null; - } - - ret = sessionApexModel.listContextAlbum(name1, version1); - if (ret.isNok()) { - return ret; - } - - ret = addKeyInfo2Messages(ret); - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("ContextAlbum/Get" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Delete context albums with the given key names/versions. - * - * @param name the name to search for. If null or empty, then all names will be queried - * @param version the version to search for. If null then all versions will be searched for. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @DELETE - @Path("ContextAlbum/Delete") - public ApexApiResult deleteContextAlbum(@QueryParam("name") final String name, - @QueryParam(VERSION) final String version) { - ApexApiResult ret = null; - String name1 = name; - String version1 = version; - LOGGER.entry(name1, version1); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - if (name1 == null || "".equals(name1)) { - name1 = null; - } - if (version1 == null || "".equals(version1)) { - version1 = null; - } - - ret = sessionApexModel.deleteContextAlbum(name1, version1); - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("ContextAlbum/Delete" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Validate context albums with the given key names/versions. The result(s) will be available in the result - * messages. - * - * @param name the name to search for. If null or empty, then all names will be queried - * @param version the version to search for. If null then all versions will be searched for. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @GET - @Path("Validate/ContextAlbum") - public ApexApiResult validateContextAlbums(@QueryParam("name") final String name, - @QueryParam(VERSION) final String version) { - ApexApiResult ret = null; - String name1 = name; - String version1 = version; - LOGGER.entry(name1, version1); - try { - if (name1 == null || "".equals(name1)) { - name1 = null; - } - if (version1 == null || "".equals(version1)) { - version1 = null; - } - - ret = initialiseSessionForReadOnly(); - if (ret.isNok()) { - return ret; - } - - ret = sessionApexModel.listContextAlbum(name1, version1); - if (ret.isNok()) { - return ret; - } - - ret = addKeyInfo2Messages(ret); - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Validate/ContextAlbum" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Creates an event with the information in the JSON string passed. - * - * @param jsonString the JSON string to be parsed. See {@linkplain BeanEvent} - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @POST - @Path("Event/Create") - public ApexApiResult createEvent(final String jsonString) { - ApexApiResult ret = null; - LOGGER.entry(jsonString); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - final BeanEvent jsonbean = RestUtils.getJsonParameters(jsonString, BeanEvent.class); - - ret = sessionApexModel.createEvent(jsonbean.getName(), jsonbean.getVersion(), jsonbean.getNameSpace(), - jsonbean.getSource(), jsonbean.getTarget(), jsonbean.getUuid(), jsonbean.getDescription()); - if (ret.isNok()) { - return ret; - } - if (jsonbean.getParameters() != null) { - for (final Entry p : jsonbean.getParameters().entrySet()) { - if (p.getValue() == null) { - ret = new ApexApiResult(Result.FAILED, "Null event parameter information for parameter \"" - + p.getKey() + "\" in event " + jsonbean.getName() + ":" + jsonbean.getVersion() - + ". The event was created, but there was an error adding the event parameters." - + " The event has only been partially defined."); - return ret; - } - final ApexApiResult rettmp = sessionApexModel.createEventPar(jsonbean.getName(), - jsonbean.getVersion(), p.getKey(), p.getValue().getName(), - p.getValue().getVersion(), p.getValue().getOptional()); - if (rettmp.isNok()) { - rettmp.addMessage("Failed to add event parameter information for parameter \"" + p.getKey() - + "\" in event " + jsonbean.getName() + ":" + jsonbean.getVersion() - + ". The event was created, but there was an error adding the event parameters." - + " The event has only been partially defined."); - ret = rettmp; - return ret; - } - } - } - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Event/Create" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Update an event with the information in the JSON string passed. - * - * @param jsonString the JSON string to be parsed. See {@linkplain BeanEvent} - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @PUT - @Path("Event/Update") - public ApexApiResult updateEvent(final String jsonString) { - ApexApiResult ret = null; - LOGGER.entry(jsonString); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - final BeanEvent jsonbean = RestUtils.getJsonParameters(jsonString, BeanEvent.class); - - if (jsonbean.getName() == null || "".equals(jsonbean.getName()) || jsonbean.getVersion() == null - || "".equals(jsonbean.getVersion())) { - ret = new ApexApiResult(Result.FAILED, "Null/Empty event name/version (\"" + jsonbean.getName() + ":" - + jsonbean.getVersion() + "\" passed to UpdateEvent"); - return ret; - } - - ret = sessionApexModel.deleteEvent(jsonbean.getName(), jsonbean.getVersion()); - if (ret.isNok()) { - return ret; - } - - ret = createEvent(jsonString); - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Event/Update" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * List events with the given key names/versions. If successful the result(s) will be available in the result - * messages. The returned value(s) will be similar to {@link AxEvent}, with merged {@linkplain AxKeyInfo} for the - * root object. - * - * @param name the name to search for. If null or empty, then all names will be queried - * @param version the version to search for. If null then all versions will be searched for. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @GET - @Path("Event/Get") - public ApexApiResult listEvent(@QueryParam("name") final String name, @QueryParam(VERSION) final String version) { - ApexApiResult ret = null; - String name1 = name; - String version1 = version; - LOGGER.entry(name1, version1); - try { - ret = initialiseSessionForReadOnly(); - if (ret.isNok()) { - return ret; - } - - if (name1 == null || "".equals(name1)) { - name1 = null; - } - if (version1 == null || "".equals(version1)) { - version1 = null; - } - - ret = sessionApexModel.listEvent(name1, version1); - if (ret.isNok()) { - return ret; - } - - ret = addKeyInfo2Messages(ret); - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Event/Get" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Delete events with the given key names/versions. - * - * @param name the name to search for. If null or empty, then all names will be queried - * @param version the version to search for. If null then all versions will be searched for. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @DELETE - @Path("Event/Delete") - public ApexApiResult deleteEvent(@QueryParam("name") final String name, - @QueryParam(VERSION) final String version) { - ApexApiResult ret = null; - String name1 = name; - String version1 = version; - LOGGER.entry(name1, version1); - try { - if (name1 == null || "".equals(name1)) { - name1 = null; - } - if (version1 == null || "".equals(version1)) { - version1 = null; - } - - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - ret = sessionApexModel.deleteEvent(name1, version1); - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Event/Delete" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Validate events with the given key names/versions. The result(s) will be available in the result messages. - * - * @param name the name to search for. If null or empty, then all names will be queried - * @param version the version to search for. If null then all versions will be searched for. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @GET - @Path("Validate/Event") - public ApexApiResult validateEvent(@QueryParam("name") final String name, - @QueryParam(VERSION) final String version) { - ApexApiResult ret = null; - String name1 = name; - String version1 = version; - LOGGER.entry(name1, version1); - try { - ret = initialiseSessionForReadOnly(); - if (ret.isNok()) { - return ret; - } - - if (name1 == null || "".equals(name1)) { - name1 = null; - } - if (version1 == null || "".equals(version1)) { - version1 = null; - } - - ret = sessionApexModel.listEvent(name1, version1); - if (ret.isNok()) { - return ret; - } - - ret = addKeyInfo2Messages(ret); - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Validate/Event" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Creates a task with the information in the JSON string passed. - * - * @param jsonString the JSON string to be parsed. See {@linkplain BeanTask} - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @POST - @Path("Task/Create") - public ApexApiResult createTask(final String jsonString) { - ApexApiResult ret = null; - ApexApiResult tempres = null; - LOGGER.entry(jsonString); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - final BeanTask jsonbean = RestUtils.getJsonParameters(jsonString, BeanTask.class); - - ret = sessionApexModel.createTask(jsonbean.getName(), jsonbean.getVersion(), jsonbean.getUuid(), - jsonbean.getDescription()); - if (ret.isNok()) { - return ret; - } - if (jsonbean.getInputFields() != null) { - for (final Entry fin : jsonbean.getInputFields().entrySet()) { - if (fin.getValue() == null) { - ret = new ApexApiResult(Result.FAILED, "Null task input field information for field \"" - + fin.getKey() + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion() - + ". The task was created, but there was an error adding the input fields." - + TASK_PARTIALLY_DEFINED); - return ret; - } - if (fin.getKey() == null || !fin.getKey().equals(fin.getValue().getLocalName())) { - ret = new ApexApiResult(Result.FAILED, "Invalid task input field information for field \"" - + fin.getKey() + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion() - + ". The localName of the field (\"" + fin.getValue().getLocalName() - + "\") is not the same as the field name. " - + "The task was created, but there was an error adding the input fields." - + TASK_PARTIALLY_DEFINED); - return ret; - } - tempres = sessionApexModel.createTaskInputField(jsonbean.getName(), jsonbean.getVersion(), - fin.getKey(), fin.getValue().getName(), fin.getValue().getVersion(), - fin.getValue().getOptional()); - if (tempres.isNok()) { - tempres.addMessage("Failed to add task input field information for field \"" + fin.getKey() - + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion() - + ". The task was created, but there was an error adding the input fields." - + TASK_PARTIALLY_DEFINED); - ret = tempres; - return ret; - } - } - } - if (jsonbean.getOutputFields() != null) { - for (final Entry fout : jsonbean.getOutputFields().entrySet()) { - if (fout.getValue() == null) { - ret = new ApexApiResult(Result.FAILED, "Null task output field information for field \"" - + fout.getKey() + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion() - + ". The task was created, but there was an error adding the output fields." - + TASK_PARTIALLY_DEFINED); - return ret; - } - if (fout.getKey() == null || !fout.getKey().equals(fout.getValue().getLocalName())) { - ret = new ApexApiResult(Result.FAILED, "Invalid task output field information for field \"" - + fout.getKey() + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion() - + ". The localName of the field (\"" + fout.getValue().getLocalName() - + "\") is not the same as the field name. " - + "The task was created, but there was an error adding the output fields." - + TASK_PARTIALLY_DEFINED); - return ret; - } - tempres = sessionApexModel.createTaskOutputField(jsonbean.getName(), jsonbean.getVersion(), - fout.getKey(), fout.getValue().getName(), fout.getValue().getVersion(), - fout.getValue().getOptional()); - if (tempres.isNok()) { - tempres.addMessage("Failed to add task output field information for field \"" + fout.getKey() - + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion() - + ". The task was created, but there was an error adding the output fields." - + TASK_PARTIALLY_DEFINED); - ret = tempres; - return ret; - } - } - } - if (jsonbean.getTaskLogic() != null) { - final BeanLogic logic = jsonbean.getTaskLogic(); - tempres = sessionApexModel.createTaskLogic(jsonbean.getName(), jsonbean.getVersion(), - logic.getLogicFlavour(), logic.getLogic()); - if (tempres.isNok()) { - tempres.addMessage("Failed to add task logic in task " + jsonbean.getName() + ":" - + jsonbean.getVersion() - + ". The task was created, but there was an error adding the logic." - + TASK_PARTIALLY_DEFINED); - ret = tempres; - return ret; - } - } - if (jsonbean.getParameters() != null) { - for (final Entry param : jsonbean.getParameters().entrySet()) { - if (param.getKey() == null || param.getValue() == null - || !param.getKey().equals(param.getValue().getParameterName())) { - ret = new ApexApiResult(Result.FAILED, - "Null or invalid task parameter information for parameter \"" + param.getKey() - + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion() - + ". The task was created, " - + "but there was an error adding the parameters." - + TASK_PARTIALLY_DEFINED); - return ret; - } - tempres = sessionApexModel.createTaskParameter(jsonbean.getName(), jsonbean.getVersion(), - param.getValue().getParameterName(), param.getValue().getDefaultValue()); - if (tempres.isNok()) { - tempres.addMessage("Failed to add task parameter \"" + param.getKey() + IN_TASK - + jsonbean.getName() + ":" + jsonbean.getVersion() - + ". The task was created, but there was an error adding the parameters." - + TASK_PARTIALLY_DEFINED); - ret = tempres; - return ret; - } - } - } - if (jsonbean.getContexts() != null) { - for (final BeanKeyRef contextalbum : jsonbean.getContexts()) { - if (contextalbum.getName() == null || contextalbum.getVersion() == null) { - ret = new ApexApiResult(Result.FAILED, - "Null or invalid context album reference information in task " - + jsonbean.getName() + ":" + jsonbean.getVersion() - + ". The task was created, but there was an error adding the" - + " context album reference. " - + "The task has only been partially defined."); - return ret; - } - tempres = sessionApexModel.createTaskContextRef(jsonbean.getName(), jsonbean.getVersion(), - contextalbum.getName(), contextalbum.getVersion()); - if (tempres.isNok()) { - ret = new ApexApiResult(Result.FAILED, - "Failed to add context album reference information in task " - + jsonbean.getName() + ":" + jsonbean.getVersion() - + ". The task was created, but there was an error adding the" - + " context album reference. " - + "The task has only been partially defined."); - return ret; - } - } - } - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Task/Create" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Update a task with the information in the JSON string passed. - * - * @param jsonString the JSON string to be parsed. See {@linkplain BeanTask} - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @PUT - @Path("Task/Update") - public ApexApiResult updateTask(final String jsonString) { - ApexApiResult ret = null; - LOGGER.entry(jsonString); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - final BeanTask jsonbean = RestUtils.getJsonParameters(jsonString, BeanTask.class); - - if (jsonbean.getName() == null || "".equals(jsonbean.getName()) || jsonbean.getVersion() == null - || "".equals(jsonbean.getVersion())) { - ret = new ApexApiResult(Result.FAILED, "Null/Empty task name/version (\"" + jsonbean.getName() + ":" - + jsonbean.getVersion() + "\" passed to UpdateTask"); - return ret; - } - - ret = sessionApexModel.deleteTask(jsonbean.getName(), jsonbean.getVersion()); - if (ret.isNok()) { - return ret; - } - - ret = createTask(jsonString); - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Task/Update" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * List tasks with the given key names/versions. If successful the result(s) will be available in the result - * messages. The returned value(s) will be similar to {@link AxTask}, with merged {@linkplain AxKeyInfo} for the - * root object. - * - * @param name the name to search for. If null or empty, then all names will be queried - * @param version the version to search for. If null then all versions will be searched for. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @GET - @Path("Task/Get") - public ApexApiResult listTask(@QueryParam("name") final String name, @QueryParam(VERSION) final String version) { - ApexApiResult ret = null; - String name1 = name; - String version1 = version; - LOGGER.entry(name1, version1); - try { - ret = initialiseSessionForReadOnly(); - if (ret.isNok()) { - return ret; - } - - if (name1 == null || "".equals(name1)) { - name1 = null; - } - if (version1 == null || "".equals(version1)) { - version1 = null; - } - - ret = sessionApexModel.listTask(name1, version1); - if (ret.isNok()) { - return ret; - } - - ret = addKeyInfo2Messages(ret); - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Task/Get" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Delete tasks with the given key names/versions. - * - * @param name the name to search for. If null or empty, then all names will be queried - * @param version the version to search for. If null then all versions will be searched for. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @DELETE - @Path("Task/Delete") - public ApexApiResult deleteTask(@QueryParam("name") final String name, - @QueryParam(VERSION) final String version) { - ApexApiResult ret = null; - String name1 = name; - String version1 = version; - LOGGER.entry(name1, version1); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - if (name1 == null || "".equals(name1)) { - name1 = null; - } - if (version1 == null || "".equals(version1)) { - version1 = null; - } - - // all input/output fields, parameters, logic, context references is "owned"/contained - // in the task, so - // deleting the task removes all of these - ret = sessionApexModel.deleteTask(name1, version1); - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Task/Delete" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Validate tasks with the given key names/versions. The result(s) will be available in the result messages. - * - * @param name the name to search for. If null or empty, then all names will be queried - * @param version the version to search for. If null then all versions will be searched for. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @GET - @Path("Validate/Task") - public ApexApiResult validateTask(@QueryParam("name") final String name, - @QueryParam(VERSION) final String version) { - ApexApiResult ret = null; - String name1 = name; - String version1 = version; - LOGGER.entry(name1, version1); - try { - ret = initialiseSessionForReadOnly(); - if (ret.isNok()) { - return ret; - } - - if (name1 == null || "".equals(name1)) { - name1 = null; - } - if (version1 == null || "".equals(version1)) { - version1 = null; - } - - ret = sessionApexModel.listTask(name1, version1); - if (ret.isNok()) { - return ret; - } - - ret = addKeyInfo2Messages(ret); - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Validate/Task" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - // CHECKSTYLE:OFF: MethodLength - /** - * Creates a policy with the information in the JSON string passed. - * - * @param jsonString the JSON string to be parsed See {@linkplain BeanPolicy} - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @POST - @Path("Policy/Create") - public ApexApiResult createPolicy(final String jsonString) { - - ApexApiResult ret = null; - ApexApiResult tempres = null; - LOGGER.entry(jsonString); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - final BeanPolicy jsonbean = RestUtils.getJsonParameters(jsonString, BeanPolicy.class); - final String policyname = jsonbean.getName(); - final String policyversion = jsonbean.getVersion(); - - ret = sessionApexModel.createPolicy(policyname, policyversion, jsonbean.getTemplate(), - jsonbean.getFirstState(), jsonbean.getUuid(), jsonbean.getDescription()); - if (ret.isNok()) { - return ret; - } - - if (jsonbean.getStates() == null || jsonbean.getStates().isEmpty()) { - ret = new ApexApiResult(Result.FAILED, - "Null or empty state map; no states defined for policy \"" + policyname + ":" - + policyversion - + "\". The policy was created, but there was an error adding states." - + POLICY_PARTIALLY_DEFINED); - return ret; - } - - final Map statemap = jsonbean.getStates(); - // need to create all the states first, before populating them - for (final Map.Entry e : statemap.entrySet()) { - final String statename = e.getKey(); - final BeanState state = e.getValue(); - if (state == null) { - ret = new ApexApiResult(Result.FAILED, - "Null or invalid state information for state \"" + statename + FOR_POLICY - + policyname + ":" + policyversion + POLICY_CREATED_STATE_ERROR - + POLICY_PARTIALLY_DEFINED); - return ret; - } - if (state.getTrigger() == null) { - ret = new ApexApiResult(Result.FAILED, - "Null or invalid state trigger for state \"" + statename + FOR_POLICY + policyname - + ":" + policyversion + POLICY_CREATED_STATE_ERROR - + POLICY_PARTIALLY_DEFINED); - return ret; - } - if (state.getDefaultTask() == null) { - ret = new ApexApiResult(Result.FAILED, - "Null or invalid default task for state \"" + statename + FOR_POLICY + policyname - + ":" + policyversion + POLICY_CREATED_STATE_ERROR - + POLICY_PARTIALLY_DEFINED); - return ret; - } - tempres = sessionApexModel.createPolicyState(policyname, policyversion, statename, - state.getTrigger().getName(), state.getTrigger().getVersion(), - state.getDefaultTask().getName(), state.getDefaultTask().getVersion()); - if (tempres.isNok()) { - ret = tempres; - return ret; - } - } - - for (final Map.Entry e : statemap.entrySet()) { - final String statename = e.getKey(); - final BeanState state = e.getValue(); - - final BeanLogic tsl = state.getTaskSelectionLogic(); - if (tsl != null) { - tempres = sessionApexModel.createPolicyStateTaskSelectionLogic(policyname, policyversion, statename, - tsl.getLogicFlavour(), tsl.getLogic()); - if (tempres.isNok()) { - tempres.addMessage("Failed to add task selection logic for state \"" + statename + "\" for" - + " policy \"" + policyname + ":" + policyversion - + POLICY_WAS_CREATED - + "but there was an error adding the task selection logic " - + "for the state. The policy has only been partially defined."); - ret = tempres; - return ret; - } - } - - final BeanKeyRef[] contexts = state.getContexts(); - if (contexts != null) { - for (final BeanKeyRef c : contexts) { - if (c == null) { - ret = new ApexApiResult(Result.FAILED, "Null or invalid context reference \"" + c + "\" for" - + " state \"" + statename + FOR_POLICY + policyname + ":" + policyversion - + "\". The policy was created, but there was an error adding the context " - + "reference for the state. The policy has only been partially defined."); - return ret; - } - tempres = sessionApexModel.createPolicyStateContextRef(policyname, policyversion, statename, - c.getName(), c.getVersion()); - if (tempres.isNok()) { - tempres.addMessage("Failed to add context reference \"" + c + "\" for state \"" + statename - + FOR_POLICY + policyname + ":" + policyversion - + POLICY_WAS_CREATED - + "but there was an error adding the context reference " - + "for the state. The policy has only been partially defined."); - ret = tempres; - return ret; - } - } - } - - final Map finalizers = state.getFinalizers(); - if (finalizers != null) { - for (final Map.Entry f : finalizers.entrySet()) { - final String finalizername = f.getKey(); - final BeanLogic finalizer = f.getValue(); - if (finalizername == null || finalizer == null) { - ret = new ApexApiResult(Result.FAILED, - "Null or invalid finalizer information for finalizer " + "named \"" - + finalizername + IN_STATE + statename + FOR_POLICY - + policyname + ":" + policyversion - + POLICY_STATE_CREATED_OTHER_ERROR - + " finalizer. The policy has only " - + "been partially defined."); - return ret; - } - tempres = sessionApexModel.createPolicyStateFinalizerLogic(policyname, policyversion, statename, - finalizername, finalizer.getLogicFlavour(), finalizer.getLogic()); - if (tempres.isNok()) { - tempres.addMessage("Failed to add finalizer information for finalizer named \"" - + finalizername + "\" in" + " state \"" + statename + FOR_POLICY - + policyname + ":" + policyversion + POLICY_STATE_CREATED_OTHER_ERROR - + " finalizer. The policy has only been partially defined."); - ret = tempres; - return ret; - } - } - } - final Map outputs = state.getStateOutputs(); - if (outputs == null || outputs.isEmpty()) { - ret = new ApexApiResult(Result.FAILED, "No state outputs have been defined in state \"" + statename - + FOR_POLICY + policyname + ":" + policyversion - + "\". The policy and state were created, but there was an error adding state" - + " outputs. The policy has only been partially defined."); - return ret; - } - for (final Map.Entry o : outputs.entrySet()) { - final String outputname = o.getKey(); - final BeanStateOutput output = o.getValue(); - if (outputname == null || output == null || output.getEvent() == null) { - ret = new ApexApiResult(Result.FAILED, - "Null or invalid output information for output named \"" + outputname + IN_STATE - + statename + FOR_POLICY + policyname + ":" + policyversion - + POLICY_STATE_CREATED_OTHER_ERROR - + " output. The policy has only been partially defined."); - return ret; - } - tempres = sessionApexModel.createPolicyStateOutput(policyname, policyversion, statename, outputname, - output.getEvent().getName(), output.getEvent().getVersion(), output.getNextState()); - if (tempres.isNok()) { - tempres.addMessage("Failed to add output information for output named \"" + outputname - + IN_STATE + statename + FOR_POLICY + policyname + ":" + policyversion - + "\". The policy and state were created, " - + "but there was an error adding the output." + POLICY_PARTIALLY_DEFINED); - ret = tempres; - return ret; - } - } - - final Map taskmap = state.getTasks(); - if (taskmap == null || taskmap.isEmpty()) { - ret = new ApexApiResult(Result.FAILED, "No tasks have been defined in state \"" + statename - + FOR_POLICY + policyname + ":" + policyversion - + "\". The policy and state were created, but there was an error adding tasks." - + POLICY_PARTIALLY_DEFINED); - return ret; - } - for (final Map.Entry t : taskmap.entrySet()) { - final String tasklocalname = t.getKey(); - final BeanStateTaskRef taskref = t.getValue(); - if (tasklocalname == null || taskref == null || taskref.getTask() == null) { - ret = new ApexApiResult(Result.FAILED, "Null or invalid task information for task named \"" - + tasklocalname + IN_STATE + statename + "\" for for policy \"" + policyname - + ":" + policyversion - + "\". The policy and state were created, but there was an error adding the " - + "task. The policy has only been partially defined."); - return ret; - } - tempres = sessionApexModel.createPolicyStateTaskRef(policyname, policyversion, statename, - tasklocalname, taskref.getTask().getName(), taskref.getTask().getVersion(), - taskref.getOutputType(), taskref.getOutputName()); - if (tempres.isNok()) { - tempres.addMessage("Failed to add task reference \"" + t + "\" for state \"" + statename - + FOR_POLICY + policyname + ":" + policyversion + POLICY_WAS_CREATED - + "but there was an error adding the task reference for" - + " the state. The policy has only been partially defined."); - ret = tempres; - return ret; - } - } - - } - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Policy/Create" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - // CHECKSTYLE:ON: MethodLength - - /** - * Update a policy with the information in the JSON string passed. - * - * @param firstStatePeriodic indicates if periodic event should be created and added to model - * @param jsonString the JSON string to be parsed. See {@linkplain BeanPolicy} - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @PUT - @Path("Policy/Update") - public ApexApiResult updatePolicy(@QueryParam("firstStatePeriodic") final boolean firstStatePeriodic, - final String jsonString) { - ApexApiResult ret = null; - LOGGER.entry(jsonString); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - final BeanPolicy jsonbean = RestUtils.getJsonParameters(jsonString, BeanPolicy.class); - - if (jsonbean.getName() == null || "".equals(jsonbean.getName()) || jsonbean.getVersion() == null - || "".equals(jsonbean.getVersion())) { - ret = new ApexApiResult(Result.FAILED, "Null/Empty Policy name/version (\"" + jsonbean.getName() + ":" - + jsonbean.getVersion() + "\" passed to UpdatePolicy"); - return ret; - } - - ret = sessionApexModel.deletePolicy(jsonbean.getName(), jsonbean.getVersion()); - if (ret.isNok()) { - return ret; - } - if (firstStatePeriodic) { - final ApexApiResult existingPeriodicEvent = sessionApexModel.listEvent("PeriodicEvent", null); - if (existingPeriodicEvent.isNok()) { - final String periodicEventString = "{\"name\":\"PeriodicEvent\",\"version\":\"0.0.1\"," - + "\"uuid\":\"44236da1-3d47-4988-8033-b6fee9d6a0f4\"," - + "\"description\":\"Generated description for concept referred to by key " - + "'PeriodicEvent:0.0.1'\",\"source\":\"System\",\"target\":\"Apex\"," - + "\"nameSpace\":\"org.onap.policy.apex.domains.aadm.events\",\"parameters\":{}}"; - ret = createEvent(periodicEventString); - if (ret.isNok()) { - return ret; - } - } - } - ret = createPolicy(jsonString); - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Policy/Update" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * List policies with the given key names/versions. If successful the result(s) will be available in the result - * messages. The returned value(s) will be similar to {@link AxPolicy}, with merged {@linkplain AxKey Info} for the - * root object. - * - * @param name the name to search for. If null or empty, then all names will be queried - * @param version the version to search for. If null then all versions will be searched for. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @GET - @Path("Policy/Get") - public ApexApiResult listPolicy(@QueryParam("name") final String name, - @QueryParam(VERSION) final String version) { - ApexApiResult ret = null; - String name1 = name; - String version1 = version; - LOGGER.entry(name1, version1); - try { - ret = initialiseSessionForReadOnly(); - if (ret.isNok()) { - return ret; - } - - if (name1 == null || "".equals(name1)) { - name1 = null; - } - if (version1 == null || "".equals(version1)) { - version1 = null; - } - - ret = sessionApexModel.listPolicy(name1, version1); - - if (ret.isNok()) { - return ret; - } - - ret = addKeyInfo2Messages(ret); - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - LOGGER.exit("Policy/Get" + (ret != null && ret.isOk() ? ": OK" : NOT_OK)); - } - } - - /** - * Delete policies with the given key names/versions. - * - * @param name the name to search for. If null or empty, then all names will be queried - * @param version the version to search for. If null then all versions will be searched for. - * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any - * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} - */ - @DELETE - @Path("Policy/Delete") - public ApexApiResult deletePolicy(@QueryParam("name") final String name, - @QueryParam(VERSION) final String version) { - ApexApiResult ret = null; - String name1 = name; - String version1 = version; - LOGGER.entry(name1, version1); - try { - ret = initialiseSessionForChanges(); - if (ret.isNok()) { - return ret; - } - - if (name1 == null || "".equals(name1)) { - name1 = null; - } - if (version1 == null || "".equals(version1)) { - version1 = null; - } - - // all input/output fields, parameters, logic, context references is "owned"/contained - // in the task, so - // deleting the task removes all of these - ret = sessionApexModel.deletePolicy(name1, version1); - if (ret.isOk()) { - commitChanges(); - } - return ret; - } catch (final Exception e) { - LOGGER.catching(e); - throw e; - } finally { - String result = "Policy/Delete" + (ret != null && ret.isOk() ? ": OK" : NOT_OK); - LOGGER.exit(result); - } - } - - /** - * The json strings representing the objects listed, stored in result.messages[], does not contain the - * AxKeyInformation for that object. This utility method retrieves the AxKeyInfo for each object and adds it to the - * json for the object. - * - * @param result The list result, containing json representations of objects stored in its "messages" array - * @return The list result, containing json augmented representations of objects stored in its "messages" array - */ - private ApexApiResult addKeyInfo2Messages(final ApexApiResult result) { - if (result.isNok()) { - return result; - } - - final ApexApiResult ret = new ApexApiResult(result.getResult()); - ret.setMessages(result.getMessages()); - - final List messages = result.getMessages(); - final List augmessages = new ArrayList<>(messages.size()); - final GsonBuilder gb = new GsonBuilder(); - gb.serializeNulls().enableComplexMapKeySerialization(); - final Gson gson = gb.create(); - for (final String message : messages) { - try { - final JsonObject jsonObject = gson.fromJson(message, JsonObject.class); - JsonObject objecttochange = jsonObject; - String name = null; - if (jsonObject != null && jsonObject.get("key") != null && jsonObject.get("key").isJsonObject() - && jsonObject.getAsJsonObject("key").get("name") != null) { - name = jsonObject.getAsJsonObject("key").get("name").getAsString(); - } else if (jsonObject != null && jsonObject.get(POLICY_KEY) != null - && jsonObject.get(POLICY_KEY).isJsonObject() - && jsonObject.getAsJsonObject(POLICY_KEY).get("name") != null) { - name = jsonObject.getAsJsonObject(POLICY_KEY).get("name").getAsString(); - } - String version = null; - if (jsonObject != null && jsonObject.get("key") != null && jsonObject.get("key").isJsonObject() - && jsonObject.getAsJsonObject("key").get(VERSION) != null) { - version = jsonObject.getAsJsonObject("key").get(VERSION).getAsString(); - } else if (jsonObject != null && jsonObject.get(POLICY_KEY) != null - && jsonObject.get(POLICY_KEY).isJsonObject() - && jsonObject.getAsJsonObject(POLICY_KEY).get(VERSION) != null) { - version = jsonObject.getAsJsonObject(POLICY_KEY).get(VERSION).getAsString(); - } - - if (name == null && version == null && jsonObject.entrySet() != null - && !jsonObject.entrySet().isEmpty()) { - objecttochange = (JsonObject) jsonObject.entrySet().iterator().next().getValue(); - if (objecttochange != null && objecttochange.get("key") != null - && objecttochange.get("key").isJsonObject() - && objecttochange.getAsJsonObject("key").get("name") != null) { - name = objecttochange.getAsJsonObject("key").get("name").getAsString(); - } else if (objecttochange != null && objecttochange.get(POLICY_KEY) != null - && objecttochange.get(POLICY_KEY).isJsonObject() - && objecttochange.getAsJsonObject(POLICY_KEY).get("name") != null) { - name = objecttochange.getAsJsonObject(POLICY_KEY).get("name").getAsString(); - } - if (objecttochange != null && objecttochange.get("key") != null - && objecttochange.get("key").isJsonObject() - && objecttochange.getAsJsonObject("key").get(VERSION) != null) { - version = objecttochange.getAsJsonObject("key").get(VERSION).getAsString(); - } else if (objecttochange != null && objecttochange.get(POLICY_KEY) != null - && objecttochange.get(POLICY_KEY).isJsonObject() - && objecttochange.getAsJsonObject(POLICY_KEY).get(VERSION) != null) { - version = objecttochange.getAsJsonObject(POLICY_KEY).get(VERSION).getAsString(); - } - } - - String uuid = null; - String desc = null; - - if (name != null && version != null) { - final ApexApiResult keyInfoResult = sessionApexModel.listKeyInformation(name, version); - final List keyInfoMessages = keyInfoResult.getMessages(); - if (keyInfoResult.isOk() && keyInfoMessages != null && !keyInfoMessages.isEmpty()) { - final String keyInfoJson = keyInfoMessages.get(0); - final JsonObject keyInfoJsonObject = gson.fromJson(keyInfoJson, JsonObject.class); - if (keyInfoJsonObject != null && keyInfoJsonObject.get(APEX_KEY_INFO) != null - && keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject().get("UUID") != null) { - uuid = keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject().get("UUID").getAsString(); - } - if (keyInfoJsonObject != null && keyInfoJsonObject.get(APEX_KEY_INFO) != null - && keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject() - .get(DESCRIPTION) != null) { - desc = keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject().get(DESCRIPTION) - .getAsString(); - } - } - } - objecttochange.addProperty("uuid", uuid); - objecttochange.addProperty(DESCRIPTION, desc); - augmessages.add(gson.toJson(jsonObject)); - } catch (final Exception e) { - LOGGER.debug("error adding key information", e); - augmessages.add(message); - } - } - ret.setMessages(augmessages); - - if (messages.size() != augmessages.size()) { - ret.setResult(Result.OTHER_ERROR); - ret.addMessage("Failed to add KeyInfo to all results. Results are not complete"); - } - - return ret; - } - - /* - * This method is used only for testing and is used to cause an exception on calls from unit test to test exception - * handling. - */ - protected static int createCorruptSession() { - final ApexEditorRestResource apexEditorRestResource = new ApexEditorRestResource(); - final ApexApiResult result = apexEditorRestResource.createSession(); - final int corruptSessionId = Integer.parseInt(result.getMessages().get(0)); - - SESSIONMODELMAP.put(corruptSessionId, null); - - return corruptSessionId; - } - - /** - * Increment the session number. - */ - private static void incrementNextSession() { - nextSession++; - } - -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/RestUtils.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/RestUtils.java deleted file mode 100644 index 2888b0376..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/RestUtils.java +++ /dev/null @@ -1,184 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest; - -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; - -import java.io.StringReader; -import java.util.Map; -import java.util.Map.Entry; -import java.util.TreeMap; - -import javax.ws.rs.core.MediaType; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBElement; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; -import javax.xml.transform.stream.StreamSource; - -import org.eclipse.persistence.jaxb.MarshallerProperties; -import org.onap.policy.apex.client.editor.rest.bean.BeanBase; -import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; - -/** - * Utilities for handling RESTful communication for Apex. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public abstract class RestUtils { - // Regular expressions for checking input types - private static final String XML_INPUT_TYPE_REGEXP = "^\\s*<\\?xml.*>\\s*"; //starts with - /** - * starts with some kind of bracket [ or ( or {, then has something, then has bracket. - */ - private static final String JSON_INPUT_TYPE_REGEXP = "^\\s*[\\(\\{\\[][\\s+\\S]*[\\)\\}\\]]"; - - /** - * Constructor, block inheritance. - */ - private RestUtils() {} - - /** - * HTTP POST requests can't send nulls so we interpret blanks as nulls. - * - * @param parameter the parameter to convert from blank to null - * @return null if the parameter us blank, otherwise the original parameter - */ - private static String blank2null(final String parameter) { - return (parameter.length() == 0 ? null : parameter); - } - - /** - * HTTP POST requests can't send nulls so we interpret blanks as nulls. - * - * @param val the val - * @return null if the parameter us blank, otherwise the original parameter - */ - private static JsonElement blank2null(final JsonElement val) { - if (val == null) { - return JsonNull.INSTANCE; - } - if (val.isJsonPrimitive() && ((JsonPrimitive) val).isString()) { - final String v = ((JsonPrimitive) val).getAsString(); - if (v == null || "".equals(v)) { - return JsonNull.INSTANCE; - } - } - if (val.isJsonArray()) { - final JsonArray arr = val.getAsJsonArray(); - for (int i = 0; i < arr.size(); i++) { - arr.set(i, blank2null(arr.get(i))); - } - } - if (val.isJsonObject()) { - final JsonObject o = val.getAsJsonObject(); - for (final Entry e : o.entrySet()) { - e.setValue(blank2null(e.getValue())); - } - } - return val; - } - - /** - * Apex HTTP PUT requests send simple single level JSON strings, this method reads those strings - * into a map. - * - * @param jsonString the incoming JSON string - * @return a map of the JSON strings - */ - public static Map getJsonParameters(final String jsonString) { - final GsonBuilder gb = new GsonBuilder(); - gb.serializeNulls().enableComplexMapKeySerialization(); - final JsonObject jsonObject = gb.create().fromJson(jsonString, JsonObject.class); - - final Map jsonMap = new TreeMap<>(); - for (final Entry jsonEntry : jsonObject.entrySet()) { - jsonMap.put(jsonEntry.getKey(), (jsonEntry.getValue() == JsonNull.INSTANCE ? null - : blank2null(jsonEntry.getValue().getAsString()))); - } - return jsonMap; - } - - /** - * Apex HTTP PUT requests send simple single level JSON strings, this method reads those strings - * into a map. - * - * @param the generic type - * @param jsonString the incoming JSON string - * @param clz the clz - * @return a map of the JSON strings - */ - public static C getJsonParameters(final String jsonString, final Class clz) { - final GsonBuilder gb = new GsonBuilder(); - gb.serializeNulls().enableComplexMapKeySerialization(); - final JsonObject jsonObject = gb.create().fromJson(jsonString, JsonObject.class); - - for (final Entry jsonEntry : jsonObject.entrySet()) { - final JsonElement val = jsonEntry.getValue(); - jsonEntry.setValue(blank2null(val)); - } - return gb.create().fromJson(jsonObject, clz); - } - - /** - * Gets the concept from JSON. - * - * @param the generic type - * @param jsonString the json string - * @param clz the clz - * @return the concept from JSON - * @throws JAXBException the JAXB exception - */ - public static C getConceptFromJson(final String jsonString, final Class clz) - throws JAXBException { - Unmarshaller unmarshaller = null; - final JAXBContext jaxbContext = JAXBContext.newInstance(clz); - unmarshaller = jaxbContext.createUnmarshaller(); - if (jsonString.matches(JSON_INPUT_TYPE_REGEXP)) { - unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON); - unmarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); - } else if (jsonString.matches(XML_INPUT_TYPE_REGEXP)) { - unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML); - } else { - return null; - } - final StreamSource source = new StreamSource(new StringReader(jsonString)); - final JAXBElement rootElement = unmarshaller.unmarshal(source, clz); - return rootElement.getValue(); - } - - /** - * Gets the JSO nfrom concept. - * - * @param object the object - * @return the JSO nfrom concept - */ - public static String getJsonfromConcept(final Object object) { - final GsonBuilder gb = new GsonBuilder(); - gb.serializeNulls().enableComplexMapKeySerialization(); - return gb.create().toJson(object); - } -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanBase.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanBase.java deleted file mode 100644 index c3f249fb4..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanBase.java +++ /dev/null @@ -1,70 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; - -/** - * The base class for Beans. - */ -public abstract class BeanBase { - // Recurring string constants - private static final String PROBLEM_RETRIEVING_FIELD_PREFIX = "Problem retrieving field called ('"; - private static final String JSON_BEAN_SUFFIX = "') from JSON bean "; - - /** - * Gets a named field from the bean. - * - * @param field the field name - * @return the value for the field - */ - public String get(final String field) { - // CHECKSTYLE:OFF: MagicNumber - // use getter preferably - for (final Method method : this.getClass().getMethods()) { - if ((method.getName().startsWith("get")) && (method.getName().length() == (field.length() + 3))) { - if (method.getName().toLowerCase().endsWith(field.toLowerCase())) { - try { - return (String) method.invoke(this); - } catch (final Exception e) { - throw new IllegalArgumentException( - PROBLEM_RETRIEVING_FIELD_PREFIX + field + JSON_BEAN_SUFFIX + this, e); - } - } - } - } - // Use field approach - if (field != null) { - try { - final Field f = this.getClass().getDeclaredField(field); - if (f != null) { - f.setAccessible(true); - return (String) (f.get(this)); - } - } catch (final Exception e) { - throw new IllegalArgumentException( - PROBLEM_RETRIEVING_FIELD_PREFIX + field + JSON_BEAN_SUFFIX + this, e); - } - } - throw new IllegalArgumentException(PROBLEM_RETRIEVING_FIELD_PREFIX + field + JSON_BEAN_SUFFIX + this); - } -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanContextAlbum.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanContextAlbum.java deleted file mode 100644 index 1cc6c7c35..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanContextAlbum.java +++ /dev/null @@ -1,111 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import javax.xml.bind.annotation.XmlType; - -/** - * The ContextAlbum Bean. - */ -@XmlType -public class BeanContextAlbum extends BeanBase { - private String name = null; - private String version = null; - private String scope = null; - private String uuid = null; - private String description = null; - private BeanKeyRef itemSchema = null; - private boolean writeable; - - /** - * Gets the name. - * - * @return the name - */ - public String getName() { - return name; - } - - /** - * Gets the version. - * - * @return the version - */ - public String getVersion() { - return version; - } - - /** - * Gets the scope. - * - * @return the scope - */ - public String getScope() { - return scope; - } - - /** - * Gets the uuid. - * - * @return the uuid - */ - public String getUuid() { - return uuid; - } - - /** - * Gets the description. - * - * @return the description - */ - public String getDescription() { - return description; - } - - /** - * Gets the item schema. - * - * @return the item schema - */ - public BeanKeyRef getItemSchema() { - return itemSchema; - } - - /** - * Gets the writeable. - * - * @return the writeable - */ - public boolean getWriteable() { - return writeable; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "ContextAlbum [name=" + name + ", version=" + version + ", scope=" + scope + ", uuid=" + uuid - + ", description=" + description + ", itemSchema=" + itemSchema + ", writeable=" + writeable + "]"; - } -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanContextSchema.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanContextSchema.java deleted file mode 100644 index 95b296fef..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanContextSchema.java +++ /dev/null @@ -1,101 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import javax.xml.bind.annotation.XmlType; - -/** - * The ContextSchema Bean. - */ -@XmlType -public class BeanContextSchema extends BeanBase { - private String name = null; - private String version = null; - private String schemaFlavour = null; - private String schemaDefinition = null; - private String uuid = null; - private String description = null; - - /** - * Gets the name. - * - * @return the name - */ - public String getName() { - return name; - } - - /** - * Gets the version. - * - * @return the version - */ - public String getVersion() { - return version; - } - - /** - * Gets the uuid. - * - * @return the uuid - */ - public String getUuid() { - return uuid; - } - - /** - * Gets the description. - * - * @return the description - */ - public String getDescription() { - return description; - } - - /** - * Gets the schema flavour. - * - * @return the schema flavour - */ - public String getSchemaFlavour() { - return schemaFlavour; - } - - /** - * Gets the schema definition. - * - * @return the schema definition - */ - public String getSchemaDefinition() { - return schemaDefinition; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "ContextSchema [name=" + name + ", version=" + version + ", schemaFlavour=" + schemaFlavour - + ", schemaDefinition=" + schemaDefinition + ", uuid=" + uuid + ", description=" + description + "]"; - } -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanEvent.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanEvent.java deleted file mode 100644 index c867beab1..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanEvent.java +++ /dev/null @@ -1,138 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import java.util.Map; - -import javax.xml.bind.annotation.XmlType; - -/** - * The Event Bean. - */ -@XmlType -public class BeanEvent extends BeanBase { - private String name = null; - private String version = null; - private String nameSpace = null; - private String source = null; - private String target = null; - private String uuid = null; - private String description = null; - private Map parameters = null; - - /** - * Gets the name. - * - * @return the name - */ - public String getName() { - return name; - } - - /** - * Gets the version. - * - * @return the version - */ - public String getVersion() { - return version; - } - - /** - * Gets the name space. - * - * @return the name space - */ - public String getNameSpace() { - return nameSpace; - } - - /** - * Gets the source. - * - * @return the source - */ - public String getSource() { - return source; - } - - /** - * Gets the target. - * - * @return the target - */ - public String getTarget() { - return target; - } - - /** - * Gets the uuid. - * - * @return the uuid - */ - public String getUuid() { - return uuid; - } - - /** - * Gets the description. - * - * @return the description - */ - public String getDescription() { - return description; - } - - /** - * Gets the parameters. - * - * @return the parameters - */ - public Map getParameters() { - return parameters; - } - - /** - * Gets the parameter. - * - * @param ps the parameter string - * @return the parameter - */ - public BeanField getParameter(final String ps) { - if (parameters != null) { - return parameters.get(ps); - } - return null; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "Event [name=" + name + ", version=" + version + ", nameSpace=" + nameSpace + ", source=" + source - + ", target=" + target + ", uuid=" + uuid + ", description=" + description + ", parameters=" - + getParameters() + "]"; - } - -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanField.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanField.java deleted file mode 100644 index d606c4d8d..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanField.java +++ /dev/null @@ -1,63 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import javax.xml.bind.annotation.XmlType; - -/** - * The Field Bean. - */ -@XmlType -public class BeanField extends BeanKeyRef { - - private boolean optional = true; - private String localName = null; - - /** - * Gets the local name for this field. - * - * @return the local name for this field. - */ - public String getLocalName() { - return localName; - } - - /** - * Gets the optional flag. - * - * @return the optional flag - */ - public boolean getOptional() { - return optional; - } - - /* - * (non-Javadoc) - * - * @see org.onap.policy.apex.client.editor.rest.bean.Bean_KeyRef#toString() - */ - @Override - public String toString() { - return "Field [localName=" + getLocalName() + ", name=" + getName() + ", version=" + getVersion() - + ", optional=" + getOptional() + "]"; - } - -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanKeyRef.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanKeyRef.java deleted file mode 100644 index dfe8ac6d2..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanKeyRef.java +++ /dev/null @@ -1,61 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import javax.xml.bind.annotation.XmlType; - -/** - * The Key Reference Bean. - */ -@XmlType -public class BeanKeyRef extends BeanBase { - private String name = null; - private String version = null; - - /** - * Gets the name. - * - * @return the name - */ - public String getName() { - return name; - } - - /** - * Gets the version. - * - * @return the version - */ - public String getVersion() { - return version; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "KeyRef [name=" + name + ", version=" + version + "]"; - } - -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanLogic.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanLogic.java deleted file mode 100644 index ef8e3c726..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanLogic.java +++ /dev/null @@ -1,60 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import javax.xml.bind.annotation.XmlType; - -/** - * The Logic Bean. - */ -@XmlType -public class BeanLogic extends BeanBase { - private String logic = null; - private String logicFlavour = null; - - /** - * Gets the logic flavour. - * - * @return the logic flavour - */ - public String getLogicFlavour() { - return logicFlavour; - } - - /** - * Gets the logic. - * - * @return the logic - */ - public String getLogic() { - return logic; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "Logic [logicFlavour=" + logicFlavour + ", logic=" + logic + "]"; - } -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanModel.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanModel.java deleted file mode 100644 index 0d69be971..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanModel.java +++ /dev/null @@ -1,82 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import javax.xml.bind.annotation.XmlType; - -/** - * The Model Bean. - */ -@XmlType -public class BeanModel extends BeanBase { - - private String name = null; - private String version = null; - private String uuid = null; - private String description = null; - - /** - * Gets the name. - * - * @return the name - */ - public String getName() { - return name; - } - - /** - * Gets the version. - * - * @return the version - */ - public String getVersion() { - return version; - } - - /** - * Gets the uuid. - * - * @return the uuid - */ - public String getUuid() { - return uuid; - } - - /** - * Gets the description. - * - * @return the description - */ - public String getDescription() { - return description; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "Model [name=" + name + ", version=" + version + ", uuid=" + uuid + ", description=" + description + "]"; - } - -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanPolicy.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanPolicy.java deleted file mode 100644 index fad64f6ac..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanPolicy.java +++ /dev/null @@ -1,114 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import java.util.Map; - -import javax.xml.bind.annotation.XmlType; - -/** - * The Policy Bean. - */ -@XmlType -public class BeanPolicy extends BeanBase { - private String name = null; - private String version = null; - private String uuid = null; - private String description = null; - private String firstState = null; - private String template = null; - private Map states = null; - - /** - * Gets the name. - * - * @return the name - */ - public String getName() { - return name; - } - - /** - * Gets the version. - * - * @return the version - */ - public String getVersion() { - return version; - } - - /** - * Gets the uuid. - * - * @return the uuid - */ - public String getUuid() { - return uuid; - } - - /** - * Gets the description. - * - * @return the description - */ - public String getDescription() { - return description; - } - - /** - * Gets the first state. - * - * @return the first state - */ - public String getFirstState() { - return firstState; - } - - /** - * Gets the template. - * - * @return the template - */ - public String getTemplate() { - return template; - } - - /** - * Gets the states. - * - * @return the states - */ - public Map getStates() { - return states; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "Policy [name=" + name + ", version=" + version + ", uuid=" + uuid + ", description=" + description - + ", firstState=" + firstState + ", template=" + template + ", states=" + states + "]"; - } - -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanState.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanState.java deleted file mode 100644 index f404d79a4..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanState.java +++ /dev/null @@ -1,136 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import java.util.Arrays; -import java.util.Map; - -import javax.xml.bind.annotation.XmlType; - -/** - * The State Bean. - */ -@XmlType -public class BeanState extends BeanBase { - - private String name = null; - private BeanKeyRef trigger = null; - private BeanKeyRef defaultTask = null; - private BeanKeyRef[] contexts = null; - private BeanLogic taskSelectionLogic = null; - private Map tasks = null; - private Map finalizers = null; - private Map stateOutputs = null; - - /** - * Gets the name. - * - * @return the name - */ - public String getName() { - return name; - } - - /** - * Gets the trigger. - * - * @return the trigger - */ - public BeanKeyRef getTrigger() { - return trigger; - } - - /** - * Gets the contexts. - * - * @return the contexts - */ - public BeanKeyRef[] getContexts() { - return contexts; - } - - /** - * Gets the task selection logic. - * - * @return the task selection logic - */ - public BeanLogic getTaskSelectionLogic() { - return taskSelectionLogic; - } - - /** - * Gets the tasks. - * - * @return the tasks - */ - public Map getTasks() { - return tasks; - } - - /** - * Gets the finalizers. - * - * @return the finalizers - */ - public Map getFinalizers() { - return finalizers; - } - - /** - * Gets the state outputs. - * - * @return the state outputs - */ - public Map getStateOutputs() { - return stateOutputs; - } - - /** - * Gets the default task. - * - * @return the default task - */ - public BeanKeyRef getDefaultTask() { - return defaultTask; - } - - /** - * Sets the default task. - * - * @param defaultTask the new default task - */ - public void setDefaultTask(final BeanKeyRef defaultTask) { - this.defaultTask = defaultTask; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "State [name=" + name + ", trigger=" + trigger + ", defaultTask=" + defaultTask + ", contexts=" - + Arrays.toString(contexts) + ", taskSelectionLogic=" + taskSelectionLogic + ", tasks=" + tasks - + ", finalizers=" + finalizers + ", stateOutputs=" + stateOutputs + "]"; - } - -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanStateOutput.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanStateOutput.java deleted file mode 100644 index f29f7d322..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanStateOutput.java +++ /dev/null @@ -1,62 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import javax.xml.bind.annotation.XmlType; - -/** - * The StateOutput Bean. - */ -@XmlType -public class BeanStateOutput extends BeanBase { - - private BeanKeyRef event = null; - private String nextState = null; - - /** - * Gets the event. - * - * @return the event - */ - public BeanKeyRef getEvent() { - return event; - } - - /** - * Gets the next state. - * - * @return the next state - */ - public String getNextState() { - return nextState; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "StateOutput [event=" + event + ", nextState=" + nextState + "]"; - } - -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanStateTaskRef.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanStateTaskRef.java deleted file mode 100644 index c3525b0c8..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanStateTaskRef.java +++ /dev/null @@ -1,71 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import javax.xml.bind.annotation.XmlType; - -/** - * The State Task Reference Bean. - */ -@XmlType -public class BeanStateTaskRef extends BeanBase { - private BeanKeyRef task = null; - private String outputType = null; - private String outputName = null; - - /** - * Gets the task. - * - * @return the task - */ - public BeanKeyRef getTask() { - return task; - } - - /** - * Gets the output type. - * - * @return the output type - */ - public String getOutputType() { - return outputType; - } - - /** - * Gets the output name. - * - * @return the output name - */ - public String getOutputName() { - return outputName; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "StateTaskRef [task=" + task + ", outputType=" + outputType + ", outputName=" + outputName + "]"; - } - -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanTask.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanTask.java deleted file mode 100644 index bf148386d..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanTask.java +++ /dev/null @@ -1,135 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import java.util.Arrays; -import java.util.Map; - -import javax.xml.bind.annotation.XmlType; - -/** - * The Task Bean. - */ -@XmlType -public class BeanTask extends BeanBase { - private String name = null; - private String version = null; - private String uuid = null; - private String description = null; - private BeanLogic taskLogic = null; - private Map inputFields = null; - private Map outputFields = null; - private Map parameters = null; - private BeanKeyRef[] contexts = null; - - /** - * Gets the name. - * - * @return the name - */ - public String getName() { - return name; - } - - /** - * Gets the version. - * - * @return the version - */ - public String getVersion() { - return version; - } - - /** - * Gets the uuid. - * - * @return the uuid - */ - public String getUuid() { - return uuid; - } - - /** - * Gets the description. - * - * @return the description - */ - public String getDescription() { - return description; - } - - /** - * Gets the task logic. - * - * @return the task logic - */ - public BeanLogic getTaskLogic() { - return taskLogic; - } - - /** - * Gets the input fields. - * - * @return the input fields - */ - public Map getInputFields() { - return inputFields; - } - - /** - * Gets the output fields. - * - * @return the output fields - */ - public Map getOutputFields() { - return outputFields; - } - - /** - * Gets the parameters. - * - * @return the parameters - */ - public Map getParameters() { - return parameters; - } - - /** - * Gets the contexts. - * - * @return the contexts - */ - public BeanKeyRef[] getContexts() { - return contexts; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "BeanTask [name=" + name + ", version=" + version + ", uuid=" + uuid + ", description=" + description - + ", taskLogic=" + taskLogic + ", inputFields=" + inputFields + ", outputFields=" + outputFields - + ", parameters=" + parameters + ", contexts=" + Arrays.toString(contexts) + "]"; - } -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanTaskParameter.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanTaskParameter.java deleted file mode 100644 index ef4f39af3..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/BeanTaskParameter.java +++ /dev/null @@ -1,62 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import javax.xml.bind.annotation.XmlType; - -/** - * The Task Parameter Bean. - */ -@XmlType -public class BeanTaskParameter extends BeanBase { - - private String parameterName = null; - private String defaultValue = null; - - /** - * Gets the parameter name. - * - * @return the parameter name - */ - public String getParameterName() { - return parameterName; - } - - /** - * Gets the default value. - * - * @return the default value - */ - public String getDefaultValue() { - return defaultValue; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "TaskParameter [parameterName=" + parameterName + ", defaultValue=" + defaultValue + "]"; - } - -} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/package-info.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/package-info.java deleted file mode 100644 index 0c286afba..000000000 --- a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/bean/package-info.java +++ /dev/null @@ -1,27 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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========================================================= - */ - -/** - * Contains JSON/XML serializable beans for use with the REST interface. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ - -package org.onap.policy.apex.client.editor.rest.bean; diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ApexEditorRestResource.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ApexEditorRestResource.java new file mode 100644 index 000000000..e5660c9fa --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ApexEditorRestResource.java @@ -0,0 +1,818 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling; + +import java.io.IOException; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; + +import org.onap.policy.apex.model.modelapi.ApexApiResult; +import org.onap.policy.apex.model.modelapi.ApexApiResult.Result; +import org.onap.policy.apex.model.utilities.TextFileUtils; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The class represents the root resource exposed at the base URL
The url to access this resource would be in the + * form {@code /rest//....}
For example: a PUT request to the following URL + * {@code http://localhost:8080/apex/rest/109/ContextSchema/Update}, with a JSON string payload containing the new + * {@code Schema} in the body, can be explained as:
  • The server or servlet is running at the base URL + * {@code http://localhost:8080/apex}
  • This resource {@code ApexRestEditorResource} is used because the path + * {@code rest/109} matches the {@code Path} filter specification for this Resource ({@code @Path("rest/{session}")}), + * where the {@code int} path parameter {@code session} is assigned the {@code int} value {@code 109}
  • The path + * {@code ContextSchema/Update} redirects this call to the method {@link #updateContextSchema(String)}, which should be + * a {@link javax.ws.rs.PUT}, with a single String in the body/payload which gets mapped to the single String parameter + * for the method.
  • So, in summary, the REST request updates a {@code ContextSchema} as specified in the payload for + * {@code session} number {@code 109}
+ * + * Note: An allocated {@code Session} identifier must be included in (almost) all requests. Models for different + * {@code Session} identifiers are completely isolated from one another. + * + * Note: To create a new {@code Session}, and have a new session ID allocated use {@link javax.ws.rs.GET} request + * to {@code /rest/-1/Session/Create} (for example: {@code http://localhost:8080/apex/rest/-1/Session/Create} ) + * + */ +@Path("editor/{session}") +@Produces( + { MediaType.APPLICATION_JSON }) +@Consumes( + { MediaType.APPLICATION_JSON }) + +public class ApexEditorRestResource implements RestCommandHandler { + // Get a reference to the logger + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorRestResource.class); + + // Location of the periodi event template + private static final String PERIODIC_EVENT_TEMPLATE = "src/main/resources/templates/PeriodicEventTemplate.json"; + + // Recurring string constants + private static final String NAME = "name"; + private static final String VERSION = "version"; + private static final String REST_COMMAND_NOT_RECOGNISED = "REST command not recognised"; + private static final String OK = ": OK"; + private static final String NOT_OK = ": Not OK"; + private static final String SESSION_CREATE = "Session/Create"; + private static final String SESSION_CREATE_NOT_OK = "Session/Create: Not OK"; + + // The session handler for sessions on the Apex editor + private static final RestSessionHandler SESSION_HANDLER = new RestSessionHandler(); + + // Handlers for the various parts of an Apex model + //@formatter:off + private static final ModelHandler MODEL_HANDLER = new ModelHandler(); + private static final KeyInfoHandler KEY_INFO_HANDLER = new KeyInfoHandler(); + private static final ContextSchemaHandler CONTEXT_SCHEMA_HANDLER = new ContextSchemaHandler(); + private static final ContextAlbumHandler CONTEXT_ALBUM_HANDLER = new ContextAlbumHandler(); + private static final EventHandler EVENT_HANDLER = new EventHandler(); + private static final TaskHandler TASK_HANDLER = new TaskHandler(); + private static final PolicyHandler POLICY_HANDLER = new PolicyHandler(); + //@formatter:on + + // The ID of this session. This gets injected from the URL. + @PathParam("session") + private int sessionId = -1; + + /** + * Creates a new session. Always call this method with sessionID -1, whereby a new sessionID will be allocated. If + * successful the new sessionID will be available in the first message in the result. + * + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}. This includes the session id + * for this session. + */ + @GET + @Path("Session/Create") + public ApexApiResult createSession() { + if (sessionId != -1) { + return new ApexApiResult(Result.FAILED, "Session ID must be set to -1 to create sessions: " + sessionId); + } + + ApexApiResult result = new ApexApiResult(); + SESSION_HANDLER.createSession(result); + return result; + } + + /** + * Load the model from a JSON string for this session. + * + * @param jsonString the JSON string to be parsed. The returned value(s) will be similar to {@link AxPolicyModel}, + * with merged {@linkplain AxKeyInfo} for the root object. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @PUT + @Path("/Model/Load") + public ApexApiResult loadFromString(final String jsonString) { + return processRestCommand(RestCommandType.MODEL, RestCommand.LOAD, jsonString); + } + + /** + * Analyse the model and return analysis results. If successful the analysis results will be available in the + * messages in the result. + * + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @GET + @Path("Model/Analyse") + public ApexApiResult analyse() { + return processRestCommand(RestCommandType.MODEL, RestCommand.ANALYSE); + } + + /** + * Validate the model and return validation results. If successful the validation results will be available in the + * messages in the result. + * + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @GET + @Path("Model/Validate") + public ApexApiResult validate() { + return processRestCommand(RestCommandType.MODEL, RestCommand.VALIDATE); + } + + /** + * Creates the new model model for this session. + * + * @param jsonString the JSON string to be parsed containing the new model. See {@linkplain BeanModel} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @POST + @Path("Model/Create") + public ApexApiResult createModel(final String jsonString) { + return processRestCommand(RestCommandType.MODEL, RestCommand.CREATE, jsonString); + } + + /** + * Update the model for this session. + * + * @param jsonString the JSON string to be parsed containing the updated model. See {@linkplain BeanModel} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @PUT + @Path("Model/Update") + public ApexApiResult updateModel(final String jsonString) { + return processRestCommand(RestCommandType.MODEL, RestCommand.UPDATE, jsonString); + } + + /** + * Gets the key for the model for this session. If successful the model key will be available in the first message + * in the result. See {@linkplain AxKey} + * + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @GET + @Path("Model/GetKey") + public ApexApiResult getModelKey() { + return processRestCommand(RestCommandType.MODEL, RestCommand.GET_KEY); + } + + /** + * Retrieve the model for this session. If successful the model will be available in the first message in the + * result. The returned value will be similar to a {@link AxPolicyModel}, with merged {@linkplain AxKeyInfo} for the + * root object. + * + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @GET + @Path("Model/Get") + public ApexApiResult listModel() { + return processRestCommand(RestCommandType.MODEL, RestCommand.LIST); + } + + /** + * Download the model for this session as a String. + * + * @return the model represented as a JSON string. See {@linkplain AxPolicyModel} + */ + @GET + @Path("Model/Download") + public String downloadModel() { + ApexApiResult result = processRestCommand(RestCommandType.MODEL, RestCommand.DOWNLOAD); + if (result != null && result.isOk()) { + return result.getMessage(); + } else { + return null; + } + } + + /** + * Delete the model for this session. + * + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @DELETE + @Path("Model/Delete") + public ApexApiResult deleteModel() { + return processRestCommand(RestCommandType.MODEL, RestCommand.DELETE); + } + + /** + * List key information with the given key names/versions. If successful the result(s) will be available in the + * result messages. See {@linkplain AxKeyInfo} + * + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @GET + @Path("KeyInformation/Get") + public ApexApiResult listKeyInformation(@QueryParam(NAME) final String name, + @QueryParam(VERSION) final String version) { + return processRestCommand(RestCommandType.KEY_INFO, RestCommand.LIST, name, version); + } + + /** + * Creates a context schema with the information in the JSON string passed. + * + * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextSchema} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @POST + @Path("ContextSchema/Create") + public ApexApiResult createContextSchema(final String jsonString) { + return processRestCommand(RestCommandType.CONTEXT_SCHEMA, RestCommand.CREATE, jsonString); + } + + /** + * Update a context schema with the information in the JSON string passed. + * + * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextSchema} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @PUT + @Path("ContextSchema/Update") + public ApexApiResult updateContextSchema(final String jsonString) { + return processRestCommand(RestCommandType.CONTEXT_SCHEMA, RestCommand.UPDATE, jsonString); + } + + /** + * List context schemas with the given key names/versions. If successful the result(s) will be available in the + * result messages. The returned value(s) will be similar to {@link AxContextSchema}, with merged + * {@linkplain AxKeyInfo} for the root object. + * + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @GET + @Path("ContextSchema/Get") + public ApexApiResult listContextSchemas(@QueryParam(NAME) final String name, + @QueryParam(VERSION) final String version) { + return processRestCommand(RestCommandType.CONTEXT_SCHEMA, RestCommand.LIST, name, version); + } + + /** + * Delete context schemas with the given key names/versions. + * + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @DELETE + @Path("ContextSchema/Delete") + public ApexApiResult deleteContextSchema(@QueryParam(NAME) final String name, + @QueryParam(VERSION) final String version) { + return processRestCommand(RestCommandType.CONTEXT_SCHEMA, RestCommand.DELETE, name, version); + } + + /** + * Validate context schemas with the given key names/versions. The result(s) will be available in the result + * messages. + * + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @GET + @Path("Validate/ContextSchema") + public ApexApiResult validateContextSchemas(@QueryParam(NAME) final String name, + @QueryParam(VERSION) final String version) { + return processRestCommand(RestCommandType.CONTEXT_SCHEMA, RestCommand.VALIDATE, name, version); + } + + /** + * Creates a context album with the information in the JSON string passed. + * + * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextAlbum} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @POST + @Path("ContextAlbum/Create") + public ApexApiResult createContextAlbum(final String jsonString) { + return processRestCommand(RestCommandType.CONTEXT_ALBUM, RestCommand.CREATE, jsonString); + } + + /** + * Update a context album with the information in the JSON string passed. + * + * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextAlbum} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @PUT + @Path("ContextAlbum/Update") + public ApexApiResult updateContextAlbum(final String jsonString) { + return processRestCommand(RestCommandType.CONTEXT_ALBUM, RestCommand.UPDATE, jsonString); + } + + /** + * List context albums with the given key names/versions. If successful the result(s) will be available in the + * result messages. The returned value(s) will be similar to {@link AxContextAlbum}, with merged + * {@linkplain AxKeyInfo} for the root object. + * + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @GET + @Path("ContextAlbum/Get") + public ApexApiResult listContextAlbums(@QueryParam(NAME) final String name, + @QueryParam(VERSION) final String version) { + return processRestCommand(RestCommandType.CONTEXT_ALBUM, RestCommand.LIST, name, version); + } + + /** + * Delete context albums with the given key names/versions. + * + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @DELETE + @Path("ContextAlbum/Delete") + public ApexApiResult deleteContextAlbum(@QueryParam(NAME) final String name, + @QueryParam(VERSION) final String version) { + return processRestCommand(RestCommandType.CONTEXT_ALBUM, RestCommand.DELETE, name, version); + } + + /** + * Validate context albums with the given key names/versions. The result(s) will be available in the result + * messages. + * + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @GET + @Path("Validate/ContextAlbum") + public ApexApiResult validateContextAlbums(@QueryParam(NAME) final String name, + @QueryParam(VERSION) final String version) { + return processRestCommand(RestCommandType.CONTEXT_ALBUM, RestCommand.VALIDATE, name, version); + } + + /** + * Creates an event with the information in the JSON string passed. + * + * @param jsonString the JSON string to be parsed. See {@linkplain BeanEvent} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @POST + @Path("Event/Create") + public ApexApiResult createEvent(final String jsonString) { + return processRestCommand(RestCommandType.EVENT, RestCommand.CREATE, jsonString); + } + + /** + * Update an event with the information in the JSON string passed. + * + * @param jsonString the JSON string to be parsed. See {@linkplain BeanEvent} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @PUT + @Path("Event/Update") + public ApexApiResult updateEvent(final String jsonString) { + return processRestCommand(RestCommandType.EVENT, RestCommand.UPDATE, jsonString); + } + + /** + * List events with the given key names/versions. If successful the result(s) will be available in the result + * messages. The returned value(s) will be similar to {@link AxEvent}, with merged {@linkplain AxKeyInfo} for the + * root object. + * + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @GET + @Path("Event/Get") + public ApexApiResult listEvent(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) { + return processRestCommand(RestCommandType.EVENT, RestCommand.LIST, name, version); + } + + /** + * Delete events with the given key names/versions. + * + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @DELETE + @Path("Event/Delete") + public ApexApiResult deleteEvent(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) { + return processRestCommand(RestCommandType.EVENT, RestCommand.DELETE, name, version); + } + + /** + * Validate events with the given key names/versions. The result(s) will be available in the result messages. + * + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @GET + @Path("Validate/Event") + public ApexApiResult validateEvent(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) { + return processRestCommand(RestCommandType.EVENT, RestCommand.VALIDATE, name, version); + } + + /** + * Creates a task with the information in the JSON string passed. + * + * @param jsonString the JSON string to be parsed. See {@linkplain BeanTask} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @POST + @Path("Task/Create") + public ApexApiResult createTask(final String jsonString) { + return processRestCommand(RestCommandType.TASK, RestCommand.CREATE, jsonString); + } + + /** + * Update a task with the information in the JSON string passed. + * + * @param jsonString the JSON string to be parsed. See {@linkplain BeanTask} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @PUT + @Path("Task/Update") + public ApexApiResult updateTask(final String jsonString) { + return processRestCommand(RestCommandType.TASK, RestCommand.UPDATE, jsonString); + } + + /** + * List tasks with the given key names/versions. If successful the result(s) will be available in the result + * messages. The returned value(s) will be similar to {@link AxTask}, with merged {@linkplain AxKeyInfo} for the + * root object. + * + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @GET + @Path("Task/Get") + public ApexApiResult listTask(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) { + return processRestCommand(RestCommandType.TASK, RestCommand.LIST, name, version); + } + + /** + * Delete tasks with the given key names/versions. + * + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @DELETE + @Path("Task/Delete") + public ApexApiResult deleteTask(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) { + return processRestCommand(RestCommandType.TASK, RestCommand.DELETE, name, version); + } + + /** + * Validate tasks with the given key names/versions. The result(s) will be available in the result messages. + * + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @GET + @Path("Validate/Task") + public ApexApiResult validateTask(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) { + return processRestCommand(RestCommandType.TASK, RestCommand.VALIDATE, name, version); + } + + // CHECKSTYLE:OFF: MethodLength + /** + * Creates a policy with the information in the JSON string passed. + * + * @param jsonString the JSON string to be parsed See {@linkplain BeanPolicy} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @POST + @Path("Policy/Create") + public ApexApiResult createPolicy(final String jsonString) { + return processRestCommand(RestCommandType.POLICY, RestCommand.CREATE, jsonString); + } + + /** + * Update a policy with the information in the JSON string passed. + * + * @param firstStatePeriodic indicates if periodic event should be created and added to model + * @param jsonString the JSON string to be parsed. See {@linkplain BeanPolicy} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @PUT + @Path("Policy/Update") + public ApexApiResult updatePolicy(@QueryParam("firstStatePeriodic") final boolean firstStatePeriodic, + final String jsonString) { + + ApexApiResult result = processRestCommand(RestCommandType.POLICY, RestCommand.UPDATE, jsonString); + if (result != null && result.isOk() && firstStatePeriodic) { + result = createPeriodicEvent(); + } + return result; + } + + /** + * List policies with the given key names/versions. If successful the result(s) will be available in the result + * messages. The returned value(s) will be similar to {@link AxPolicy}, with merged {@linkplain AxKey Info} for the + * root object. + * + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @GET + @Path("Policy/Get") + public ApexApiResult listPolicy(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) { + return processRestCommand(RestCommandType.POLICY, RestCommand.LIST, name, version); + } + + /** + * Delete policies with the given key names/versions. + * + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + @DELETE + @Path("Policy/Delete") + public ApexApiResult deletePolicy(@QueryParam(NAME) final String name, @QueryParam(VERSION) final String version) { + return processRestCommand(RestCommandType.POLICY, RestCommand.DELETE, name, version); + } + + /** + * This method routes REST commands that take no parameters to their caller. + * + * @param commandType the type of REST command to process + * @param command the REST command to process + * @return the result of the REST command + */ + private ApexApiResult processRestCommand(final RestCommandType commandType, final RestCommand command) { + LOGGER.entry(commandType); + try { + ApexApiResult result = new ApexApiResult(); + RestSession session = SESSION_HANDLER.getSession(sessionId, result); + if (session == null) { + return result; + } + result = executeRestCommand(session, commandType, command); + LOGGER.exit(SESSION_CREATE + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } catch (final Exception e) { + LOGGER.catching(e); + LOGGER.exit(SESSION_CREATE_NOT_OK); + throw e; + } + } + + /** + * This method routes REST commands that take a JSON string to their caller. + * + * @param commandType the type of REST command to process + * @param command the REST command to process + * @param jsonString the JSON string received in the REST request + * @return the result of the REST command + */ + private ApexApiResult processRestCommand(final RestCommandType commandType, final RestCommand command, + final String jsonString) { + LOGGER.entry(commandType, jsonString); + try { + ApexApiResult result = new ApexApiResult(); + RestSession session = SESSION_HANDLER.getSession(sessionId, result); + if (session == null) { + return result; + } + result = executeRestCommand(session, commandType, command, jsonString); + LOGGER.exit(SESSION_CREATE + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } catch (final Exception e) { + LOGGER.catching(e); + LOGGER.exit(SESSION_CREATE_NOT_OK); + throw e; + } + } + + /** + * This method routes REST commands that take a name and version to their caller. + * + * @param commandType the type of REST command to process + * @param command the REST command to process + * @param name the name received in the REST request + * @param version the name received in the REST request + * @return the result of the REST command + */ + private ApexApiResult processRestCommand(final RestCommandType commandType, final RestCommand command, + final String name, final String version) { + LOGGER.entry(commandType, name, version); + try { + ApexApiResult result = new ApexApiResult(); + RestSession session = SESSION_HANDLER.getSession(sessionId, result); + if (session == null) { + return result; + } + result = executeRestCommand(session, commandType, command, name, version); + LOGGER.exit(SESSION_CREATE + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } catch (final Exception e) { + LOGGER.catching(e); + LOGGER.exit(SESSION_CREATE_NOT_OK); + throw e; + } + } + + /** + * This method invokes callers to run REST commands that take no parameters. + * + * @param session the Apex editor session + * @param commandType the type of REST command to process + * @param command the REST command to process + * @return the result of the REST command + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command) { + switch (commandType) { + case MODEL: + return MODEL_HANDLER.executeRestCommand(session, commandType, command); + case KEY_INFO: + return KEY_INFO_HANDLER.executeRestCommand(session, commandType, command); + case CONTEXT_SCHEMA: + return CONTEXT_SCHEMA_HANDLER.executeRestCommand(session, commandType, command); + case CONTEXT_ALBUM: + return CONTEXT_ALBUM_HANDLER.executeRestCommand(session, commandType, command); + case EVENT: + return EVENT_HANDLER.executeRestCommand(session, commandType, command); + case TASK: + return TASK_HANDLER.executeRestCommand(session, commandType, command); + case POLICY: + return POLICY_HANDLER.executeRestCommand(session, commandType, command); + default: + return new ApexApiResult(Result.FAILED, REST_COMMAND_NOT_RECOGNISED); + } + } + + /** + * This method invokes callers to run REST commands that take a JSON string. + * + * @param session the Apex editor session + * @param commandType the type of REST command to process + * @param command the REST command to process + * @param jsonString the JSON string received in the REST request + * @return the result of the REST command + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String jsonString) { + switch (commandType) { + case MODEL: + return MODEL_HANDLER.executeRestCommand(session, commandType, command, jsonString); + case KEY_INFO: + return KEY_INFO_HANDLER.executeRestCommand(session, commandType, command, jsonString); + case CONTEXT_SCHEMA: + return CONTEXT_SCHEMA_HANDLER.executeRestCommand(session, commandType, command, jsonString); + case CONTEXT_ALBUM: + return CONTEXT_ALBUM_HANDLER.executeRestCommand(session, commandType, command, jsonString); + case EVENT: + return EVENT_HANDLER.executeRestCommand(session, commandType, command, jsonString); + case TASK: + return TASK_HANDLER.executeRestCommand(session, commandType, command, jsonString); + case POLICY: + return POLICY_HANDLER.executeRestCommand(session, commandType, command, jsonString); + default: + return new ApexApiResult(Result.FAILED, REST_COMMAND_NOT_RECOGNISED); + } + } + + /** + * This method invokes callers to run REST commands that take a name and version. + * + * @param session the Apex editor session + * @param commandType the type of REST command to process + * @param command the REST command to process + * @param name the name received in the REST request + * @param version the name received in the REST request + * @return the result of the REST command + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String name, final String version) { + switch (commandType) { + case MODEL: + return MODEL_HANDLER.executeRestCommand(session, commandType, command, name, version); + case KEY_INFO: + return KEY_INFO_HANDLER.executeRestCommand(session, commandType, command, name, version); + case CONTEXT_SCHEMA: + return CONTEXT_SCHEMA_HANDLER.executeRestCommand(session, commandType, command, name, version); + case CONTEXT_ALBUM: + return CONTEXT_ALBUM_HANDLER.executeRestCommand(session, commandType, command, name, version); + case EVENT: + return EVENT_HANDLER.executeRestCommand(session, commandType, command, name, version); + case TASK: + return TASK_HANDLER.executeRestCommand(session, commandType, command, name, version); + case POLICY: + return POLICY_HANDLER.executeRestCommand(session, commandType, command, name, version); + default: + return new ApexApiResult(Result.FAILED, REST_COMMAND_NOT_RECOGNISED); + } + } + + /** + * Create a periodic event from the periodic event template. + */ + private ApexApiResult createPeriodicEvent() { + String periodicEventJsonString; + try { + periodicEventJsonString = TextFileUtils.getTextFileAsString(PERIODIC_EVENT_TEMPLATE); + } catch (IOException ioException) { + String message = "read of periodic event tempalte from " + PERIODIC_EVENT_TEMPLATE + "failed: " + + ioException.getMessage(); + LOGGER.debug(message, ioException); + return new ApexApiResult(Result.FAILED, message); + } + + return processRestCommand(RestCommandType.EVENT, RestCommand.CREATE, periodicEventJsonString); + } + + /* + * This method is used only for testing and is used to cause an exception on calls from unit test to test exception + * handling. + */ + protected static int createCorruptSession() { + final ApexEditorRestResource apexEditorRestResource = new ApexEditorRestResource(); + final ApexApiResult result = apexEditorRestResource.createSession(); + final int corruptSessionId = Integer.parseInt(result.getMessages().get(0)); + + SESSION_HANDLER.setCorruptSession(corruptSessionId); + + return corruptSessionId; + } + +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ContextAlbumHandler.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ContextAlbumHandler.java new file mode 100644 index 000000000..30886db5d --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ContextAlbumHandler.java @@ -0,0 +1,204 @@ +/*- + * ============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.client.editor.rest.handling; + +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanContextAlbum; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; +import org.onap.policy.apex.model.modelapi.ApexApiResult; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class handles commands on context albums in Apex models. + */ +public class ContextAlbumHandler implements RestCommandHandler { + // Get a reference to the logger + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextAlbumHandler.class); + + // Recurring string constants + private static final String OK = ": OK"; + private static final String NOT_OK = ": Not OK"; + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String jsonString) { + if (!RestCommandType.CONTEXT_ALBUM.equals(commandType)) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + switch (command) { + case CREATE: + return createContextAlbum(session, jsonString); + case UPDATE: + return updateContextAlbum(session, jsonString); + default: + return getUnsupportedCommandResultMessage(session, commandType, command); + } + } + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String name, final String version) { + if (!RestCommandType.CONTEXT_ALBUM.equals(commandType)) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + switch (command) { + case LIST: + return listContextAlbums(session, name, version); + case DELETE: + return deleteContextAlbum(session, name, version); + case VALIDATE: + return validateContextAlbum(session, name, version); + default: + return getUnsupportedCommandResultMessage(session, commandType, command); + } + } + + /** + * Creates a context album with the information in the JSON string passed. + * + * @param session the Apex model editing session + * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextAlbum} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult createContextAlbum(final RestSession session, final String jsonString) { + LOGGER.entry(jsonString); + + session.editModel(); + + final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class); + + ApexApiResult result = session.getApexModelEdited().createContextAlbum(jsonbean.getName(), + jsonbean.getVersion(), jsonbean.getScope(), Boolean.toString(jsonbean.getWriteable()), + jsonbean.getItemSchema().getName(), jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(), + jsonbean.getDescription()); + + session.finishSession(result.isOk()); + + LOGGER.exit("ContextAlbum/Create" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Update a context album with the information in the JSON string passed. + * + * @param session the Apex model editing session + * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextAlbum} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult updateContextAlbum(final RestSession session, final String jsonString) { + LOGGER.entry(jsonString); + + session.editModel(); + + final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class); + + ApexApiResult result = session.getApexModelEdited().updateContextAlbum(jsonbean.getName(), + jsonbean.getVersion(), jsonbean.getScope(), Boolean.toString(jsonbean.getWriteable()), + jsonbean.getItemSchema().getName(), jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(), + jsonbean.getDescription()); + + session.finishSession(result.isOk()); + + LOGGER.exit("ContextAlbum/Update" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * List context albums with the given key names/versions. If successful the result(s) will be available in the + * result messages. The returned value(s) will be similar to {@link AxContextAlbum}, with merged + * {@linkplain AxKeyInfo} for the root object. + * + * @param session the Apex model editing session + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult listContextAlbums(final RestSession session, final String name, final String version) { + LOGGER.entry(name, version); + + ApexApiResult result = session.getApexModel().listContextAlbum(blank2Null(name), blank2Null(version)); + + LOGGER.exit("ContextAlbum/Get" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Delete context albums with the given key names/versions. + * + * @param session the Apex model editing session + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult deleteContextAlbum(final RestSession session, final String name, final String version) { + LOGGER.entry(name, version); + + session.editModel(); + + ApexApiResult result = session.getApexModelEdited().deleteContextAlbum(blank2Null(name), blank2Null(version)); + + session.finishSession(result.isOk()); + + LOGGER.exit("ContextAlbum/Delete" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Validate context albums with the given key names/versions. The result(s) will be available in the result + * messages. + * + * @param session the Apex model editing session + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult validateContextAlbum(final RestSession session, final String name, final String version) { + LOGGER.entry(name, version); + + ApexApiResult result = session.getApexModel().validateContextAlbum(blank2Null(name), blank2Null(version)); + + LOGGER.exit("Validate/ContextAlbum" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ContextSchemaHandler.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ContextSchemaHandler.java new file mode 100644 index 000000000..c65ca7288 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ContextSchemaHandler.java @@ -0,0 +1,191 @@ +/*- + * ============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.client.editor.rest.handling; + +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanContextSchema; +import org.onap.policy.apex.model.modelapi.ApexApiResult; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class handles commands on context schemas in Apex models. + */ +public class ContextSchemaHandler implements RestCommandHandler { + // Get a reference to the logger + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextSchemaHandler.class); + + // Recurring string constants + private static final String OK = ": OK"; + private static final String NOT_OK = ": Not OK"; + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String jsonString) { + if (!RestCommandType.CONTEXT_SCHEMA.equals(commandType)) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + switch (command) { + case CREATE: + return createContextSchema(session, jsonString); + case UPDATE: + return updateContextSchema(session, jsonString); + default: + return getUnsupportedCommandResultMessage(session, commandType, command); + } + } + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String name, final String version) { + if (!RestCommandType.CONTEXT_SCHEMA.equals(commandType)) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + switch (command) { + case LIST: + return listContextSchemas(session, name, version); + case DELETE: + return deleteContextSchema(session, name, version); + case VALIDATE: + return validateContextSchemas(session, name, version); + default: + return getUnsupportedCommandResultMessage(session, commandType, command); + } + } + + /** + * Creates a context schema. + * + * @param session the session holding the Apex model + * @param jsonString the JSON string with the context schema parameters + * @return the result of the operation + */ + private ApexApiResult createContextSchema(final RestSession session, final String jsonString) { + LOGGER.entry(jsonString); + + session.editModel(); + + final BeanContextSchema jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class); + ApexApiResult result = session.getApexModelEdited().createContextSchema(jsonbean.getName(), + jsonbean.getVersion(), jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(), + jsonbean.getUuid(), jsonbean.getDescription()); + + session.finishSession(result.isOk()); + + LOGGER.exit("ContextSchema/create" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Update a context schema. + * + * @param session the session holding the Apex model + * @param jsonString the JSON string with the context schema parameters + * @return the result of the operation + */ + private ApexApiResult updateContextSchema(final RestSession session, final String jsonString) { + LOGGER.entry(jsonString); + + session.editModel(); + + final BeanContextSchema jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class); + + ApexApiResult result = session.getApexModelEdited().updateContextSchema(jsonbean.getName(), + jsonbean.getVersion(), jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(), + jsonbean.getUuid(), jsonbean.getDescription()); + + session.finishSession(result.isOk()); + + LOGGER.exit("ContextSchema/Update" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * List context schemas. + * + * @param session the session holding the Apex model + * @param name the context schema name to operate on + * @param version the context schema version to operate on + * @return the result of the operation + */ + private ApexApiResult listContextSchemas(final RestSession session, final String name, final String version) { + LOGGER.entry(name, version); + + ApexApiResult result = session.getApexModel().listContextSchemas(blank2Null(name), blank2Null(version)); + + LOGGER.exit("ContextSchema/Get" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Delete a context schema. + * + * @param session the session holding the Apex model + * @param name the context schema name to operate on + * @param version the context schema version to operate on + * @return the result of the operation + */ + private ApexApiResult deleteContextSchema(final RestSession session, final String name, final String version) { + LOGGER.entry(name, version); + + session.editModel(); + + ApexApiResult result = session.getApexModelEdited().deleteContextSchema(blank2Null(name), blank2Null(version)); + + session.finishSession(result.isOk()); + + LOGGER.exit("ContextSchema/Delete" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Validate a context schema. + * + * @param session the session holding the Apex model + * @param name the context schema name to operate on + * @param version the context schema version to operate on + * @return the result of the operation + */ + private ApexApiResult validateContextSchemas(final RestSession session, final String name, final String version) { + LOGGER.entry(name, version); + + ApexApiResult result = session.getApexModel().validateContextSchemas(blank2Null(name), blank2Null(version)); + + LOGGER.exit("Validate/ContextSchema" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/EventHandler.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/EventHandler.java new file mode 100644 index 000000000..08b487321 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/EventHandler.java @@ -0,0 +1,264 @@ +/*- + * ============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.client.editor.rest.handling; + +import java.util.Map.Entry; + +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanEvent; +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanField; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; +import org.onap.policy.apex.model.modelapi.ApexApiResult; +import org.onap.policy.apex.model.modelapi.ApexApiResult.Result; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class handles commands on events in Apex models. + */ +public class EventHandler implements RestCommandHandler { + // Get a reference to the logger + private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventHandler.class); + + // Recurring string constants + private static final String OK = ": OK"; + private static final String NOT_OK = ": Not OK"; + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String jsonString) { + if (!RestCommandType.EVENT.equals(commandType)) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + switch (command) { + case CREATE: + return createEvent(session, jsonString); + case UPDATE: + return updateEvent(session, jsonString); + default: + return getUnsupportedCommandResultMessage(session, commandType, command); + } + } + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String name, final String version) { + if (!RestCommandType.EVENT.equals(commandType)) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + switch (command) { + case LIST: + return listEvents(session, name, version); + case DELETE: + return deleteEvent(session, name, version); + case VALIDATE: + return validateEvent(session, name, version); + default: + return getUnsupportedCommandResultMessage(session, commandType, command); + } + } + + /** + * Creates an event with the information in the JSON string passed. + * + * @param session the Apex model editing session + * @param jsonString the JSON string to be parsed. See {@linkplain BeanEvent} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult createEvent(final RestSession session, final String jsonString) { + LOGGER.entry(jsonString); + + final BeanEvent jsonbean = RestUtils.getJsonParameters(jsonString, BeanEvent.class); + + session.editModel(); + + ApexApiResult result = session.getApexModelEdited().createEvent(jsonbean.getName(), jsonbean.getVersion(), + jsonbean.getNameSpace(), jsonbean.getSource(), jsonbean.getTarget(), jsonbean.getUuid(), + jsonbean.getDescription()); + + if (result.isOk()) { + result = createEventParameters(session, jsonbean); + } + + session.finishSession(result.isOk()); + + LOGGER.exit("Event/Create" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Create the parameters on an event. + * + * @param session the Apex editor session + * @param jsonbean the JSON bean holding the parameters + * @param result the result of the parameter creation operation + * @return + */ + private ApexApiResult createEventParameters(final RestSession session, final BeanEvent jsonbean) { + ApexApiResult result = new ApexApiResult(); + + if (jsonbean.getParameters() == null || jsonbean.getParameters().isEmpty()) { + return result; + } + + for (final Entry parameterEntry : jsonbean.getParameters().entrySet()) { + if (parameterEntry.getValue() == null) { + result.setResult(Result.FAILED); + result.addMessage("Null event parameter information for parameter \"" + parameterEntry.getKey() + + "\" in event " + jsonbean.getName() + ":" + jsonbean.getVersion() + + ". The event was created, but there was an error adding the event parameters." + + " The event has only been partially defined."); + continue; + } + + final ApexApiResult createParResult = session.getApexModelEdited().createEventPar(jsonbean.getName(), + jsonbean.getVersion(), parameterEntry.getKey(), parameterEntry.getValue().getName(), + parameterEntry.getValue().getVersion(), parameterEntry.getValue().getOptional()); + if (createParResult.isNok()) { + result.setResult(createParResult.getResult()); + result.addMessage("Failed to add event parameter information for parameter \"" + parameterEntry.getKey() + + "\" in event " + jsonbean.getName() + ":" + jsonbean.getVersion() + + ". The event was created, but there was an error adding the event parameters." + + " The event has only been partially defined."); + } + } + + return result; + } + + /** + * Update an event with the information in the JSON string passed. + * + * @param session the Apex model editing session + * @param jsonString the JSON string to be parsed. See {@linkplain BeanEvent} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult updateEvent(final RestSession session, final String jsonString) { + LOGGER.entry(jsonString); + + final BeanEvent jsonbean = RestUtils.getJsonParameters(jsonString, BeanEvent.class); + + if (blank2Null(jsonbean.getName()) == null || blank2Null(jsonbean.getVersion()) == null) { + LOGGER.exit("Event/Update" + NOT_OK); + return new ApexApiResult(Result.FAILED, "Null/Empty event name/version (\"" + jsonbean.getName() + ":" + + jsonbean.getVersion() + "\" passed to UpdateEvent"); + } + + session.editModel(); + + ApexApiResult result = session.getApexModelEdited().deleteEvent(blank2Null(jsonbean.getName()), + blank2Null(jsonbean.getVersion())); + + if (result.isOk()) { + result = session.getApexModelEdited().createEvent(jsonbean.getName(), jsonbean.getVersion(), + jsonbean.getNameSpace(), jsonbean.getSource(), jsonbean.getTarget(), jsonbean.getUuid(), + jsonbean.getDescription()); + + if (result.isOk() && jsonbean.getParameters() != null) { + result = createEventParameters(session, jsonbean); + } + } + + session.finishSession(result.isOk()); + + LOGGER.exit("Event/Update" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * List events with the given key names/versions. If successful the result(s) will be available in the result + * messages. The returned value(s) will be similar to {@link AxEvent}, with merged {@linkplain AxKeyInfo} for the + * root object. + * + * @param session the Apex model editing session + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult listEvents(final RestSession session, final String name, final String version) { + LOGGER.entry(name, version); + + ApexApiResult result = session.getApexModel().listEvent(blank2Null(name), blank2Null(version)); + + LOGGER.exit("Event/Get" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Delete events with the given key names/versions. + * + * @param session the Apex model editing session + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult deleteEvent(final RestSession session, final String name, final String version) { + LOGGER.entry(name, version); + + session.editModel(); + + ApexApiResult result = session.getApexModelEdited().deleteEvent(blank2Null(name), blank2Null(version)); + + session.finishSession(result.isOk()); + + LOGGER.exit("Event/Delete" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Validate events with the given key names/versions. The result(s) will be available in the result messages. + * + * @param session the Apex model editing session + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult validateEvent(final RestSession session, final String name, final String version) { + LOGGER.entry(name, version); + + ApexApiResult result = session.getApexModel().validateEvent(blank2Null(name), blank2Null(version)); + + LOGGER.exit("Validate/Event" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/KeyInfoHandler.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/KeyInfoHandler.java new file mode 100644 index 000000000..ffb8900cf --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/KeyInfoHandler.java @@ -0,0 +1,72 @@ +/*- + * ============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.client.editor.rest.handling; + +import org.onap.policy.apex.model.modelapi.ApexApiResult; + +/** + * This class handles commands on key information in Apex models. + */ +public class KeyInfoHandler implements RestCommandHandler { + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String jsonString) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String name, final String version) { + + if (RestCommandType.KEY_INFO.equals(commandType) && RestCommand.LIST.equals(command)) { + return listKeyInformation(session, name, version); + } + else { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + } + + /** + * Get the key information for a concept with the given name and version. + * @param session the editor session containing the Apex model + * @param name the name for the search + * @param version the version for the search + * @return the key information + */ + private ApexApiResult listKeyInformation(final RestSession session, final String name, final String version) { + return session.getApexModel().listKeyInformation(blank2Null(name), blank2Null(version)); + } +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ModelHandler.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ModelHandler.java new file mode 100644 index 000000000..8b223efc7 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/ModelHandler.java @@ -0,0 +1,446 @@ +/*- + * ============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.client.editor.rest.handling; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; + +import java.util.ArrayList; +import java.util.List; + +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanModel; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; +import org.onap.policy.apex.model.modelapi.ApexApiResult; +import org.onap.policy.apex.model.modelapi.ApexApiResult.Result; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class handles commands on Apex models. + */ +public class ModelHandler implements RestCommandHandler { + + // Get a reference to the logger + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ModelHandler.class); + + // Recurring string constants + private static final String OK = ": OK"; + private static final String NOT_OK = ": Not OK"; + private static final String KEY = "key"; + private static final String NAME = "name"; + private static final String VERSION = "version"; + private static final String UUID = "uuid"; + private static final String DESCRIPTION = "description"; + private static final String POLICY_KEY = "policyKey"; + private static final String APEX_KEY_INFO = "apexKeyInfo"; + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command) { + if (!RestCommandType.MODEL.equals(commandType)) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + switch (command) { + case ANALYSE: + return analyse(session); + case VALIDATE: + return validate(session); + case GET_KEY: + return getModelKey(session); + case LIST: + return listModel(session); + case DOWNLOAD: + return downloadModel(session); + case DELETE: + return deleteModel(session); + default: + return getUnsupportedCommandResultMessage(session, commandType, command); + } + } + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String jsonString) { + if (!RestCommandType.MODEL.equals(commandType)) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + switch (command) { + case LOAD: + return loadFromString(session, jsonString); + case CREATE: + return createModel(session, jsonString); + case UPDATE: + return updateModel(session, jsonString); + default: + return getUnsupportedCommandResultMessage(session, commandType, command); + } + } + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String name, final String version) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + /** + * Load the model from a JSON string for this session. + * + * @param session the Apex model editing session + * @param jsonString the JSON string to be parsed. The returned value(s) will be similar to {@link AxPolicyModel}, + * with merged {@linkplain AxKeyInfo} for the root object. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult loadFromString(final RestSession session, final String jsonString) { + LOGGER.entry(jsonString); + + session.editModel(); + + ApexApiResult result = session.getApexModelEdited().loadFromString(jsonString); + + session.finishSession(result.isOk()); + + LOGGER.exit("Model/Load" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Analyse the model and return analysis results. If successful the analysis results will be available in the + * messages in the result. + * + * @param session the Apex model editing session + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult analyse(final RestSession session) { + LOGGER.entry(); + + ApexApiResult result = session.getApexModel().analyse(); + + LOGGER.exit("Model/Analyse" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Validate the model and return validation results. If successful the validation results will be available in the + * messages in the result. + * + * @param session the Apex model editing session + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult validate(final RestSession session) { + LOGGER.entry(); + + ApexApiResult result = session.getApexModel().validate(); + + LOGGER.exit("Model/Validate" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Creates the new model model for this session. + * + * @param session the Apex model editing session + * @param jsonString the JSON string to be parsed containing the new model. See {@linkplain BeanModel} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult createModel(final RestSession session, final String jsonString) { + LOGGER.entry(jsonString); + + final BeanModel jsonbean = RestUtils.getJsonParameters(jsonString, BeanModel.class); + + session.editModel(); + + ApexApiResult result = session.getApexModelEdited().createModel(jsonbean.getName(), jsonbean.getVersion(), + jsonbean.getUuid(), jsonbean.getDescription()); + + session.finishSession(result.isOk()); + + LOGGER.exit("Model/Create" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Update the model for this session. + * + * @param session the Apex model editing session + * @param jsonString the JSON string to be parsed containing the updated model. See {@linkplain BeanModel} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult updateModel(final RestSession session, final String jsonString) { + LOGGER.entry(jsonString); + + final BeanModel jsonbean = RestUtils.getJsonParameters(jsonString, BeanModel.class); + + session.editModel(); + + ApexApiResult result = session.getApexModelEdited().updateModel(jsonbean.getName(), jsonbean.getVersion(), + jsonbean.getUuid(), jsonbean.getDescription()); + + session.finishSession(result.isOk()); + + LOGGER.exit("Model/Update" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Gets the key for the model for this session. If successful the model key will be available in the first message + * in the result. See {@linkplain AxKey} + * + * @param session the Apex model editing session + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult getModelKey(final RestSession session) { + LOGGER.entry(); + + ApexApiResult result = session.getApexModel().getModelKey(); + + LOGGER.exit("Model/GetKey" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Retrieve the model for this session. If successful the model will be available in the first message in the + * result. The returned value will be similar to a {@link AxPolicyModel}, with merged {@linkplain AxKeyInfo} for the + * root object. + * + * @param session the Apex model editing session + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult listModel(final RestSession session) { + LOGGER.entry(); + + ApexApiResult result = session.getApexModel().listModel(); + + result = addKeyInfo2Messages(session, result); + + LOGGER.exit("Model/Get" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Download the model for this session as a String. + * + * @param session the Apex model editing session + * @return the model represented as a JSON string. See {@linkplain AxPolicyModel} + */ + private ApexApiResult downloadModel(final RestSession session) { + LOGGER.entry(); + + ApexApiResult result = session.getApexModel().listModel(); + + LOGGER.exit("Model/Download" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Delete the model for this session. + * + * @param session the Apex model editing session + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult deleteModel(final RestSession session) { + LOGGER.entry(); + + session.editModel(); + + ApexApiResult result = session.getApexModel().deleteModel(); + + session.finishSession(result.isOk()); + + LOGGER.exit("Model/Delete" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * The json strings representing the objects listed, stored in result.messages[], does not contain the + * AxKeyInformation for that object. This utility method retrieves the AxKeyInfo for each object and adds it to the + * json for the object. + * + * @param session the Apex model editing session + * @param incomingResult The list result, containing JSON representations of objects stored in its "messages" array + * @return The list result, containing JSON augmented representations of objects stored in its "messages" array + */ + private ApexApiResult addKeyInfo2Messages(final RestSession session, final ApexApiResult incomingResult) { + final ApexApiResult result = new ApexApiResult(incomingResult.getResult()); + result.setMessages(incomingResult.getMessages()); + + final List messages = incomingResult.getMessages(); + final List augmentedMessages = new ArrayList<>(messages.size()); + + for (final String message : messages) { + augmentedMessages.add(addKeyInfo2Message(session, message)); + } + result.setMessages(augmentedMessages); + + if (messages.size() != augmentedMessages.size()) { + result.setResult(Result.OTHER_ERROR); + result.addMessage("Failed to add KeyInfo to all results. Results are not complete"); + } + + return result; + } + + /** + * Augment a message with key information. + * + * @param session the Apex model editing session + * @param message The message to augment + * @return the augmented message + */ + private String addKeyInfo2Message(final RestSession session, final String message) { + final Gson gson = new GsonBuilder().serializeNulls().enableComplexMapKeySerialization().create(); + + JsonObject jsonObject = gson.fromJson(message, JsonObject.class); + if (jsonObject == null) { + return message; + } + + String name = readFieldFromJsonObject(jsonObject, NAME, null); + String version = readFieldFromJsonObject(jsonObject, VERSION, null); + + if (name == null && version == null) { + JsonObject newJsonObject = getSubJsonObject(jsonObject); + + if (newJsonObject != null) { + jsonObject = newJsonObject; + name = readFieldFromJsonObject(jsonObject, NAME, name); + version = readFieldFromJsonObject(jsonObject, VERSION, version); + } + } + + if (name == null || version == null || !setUuidAndDescription(session, jsonObject, name, version)) { + jsonObject.addProperty(UUID, (String) null); + jsonObject.addProperty(DESCRIPTION, (String) null); + } + + return gson.toJson(jsonObject); + } + + /** + * Get an embedded JSON object for the given JSON object. + * + * @param jsonObject the input JSON object + * @return the embedded JSON object + */ + private JsonObject getSubJsonObject(JsonObject jsonObject) { + if (jsonObject.entrySet() != null && !jsonObject.entrySet().isEmpty()) { + return (JsonObject) jsonObject.entrySet().iterator().next().getValue(); + } else { + return null; + } + } + + /** + * Condition a field so its key information can be looked up. + * + * @param jsonObject the object to query + * @param fieldTag the tag of the field to condition + * @param fieldValue the value of the field to condition + * @return + */ + private String readFieldFromJsonObject(final JsonObject jsonObject, final String fieldTag, final String value) { + String lookedupValue = value; + + if (jsonObject != null && jsonObject.get(KEY) != null && jsonObject.get(KEY).isJsonObject() + && jsonObject.getAsJsonObject(KEY).get(fieldTag) != null) { + lookedupValue = jsonObject.getAsJsonObject(KEY).get(fieldTag).getAsString(); + } else if (jsonObject != null && jsonObject.get(POLICY_KEY) != null && jsonObject.get(POLICY_KEY).isJsonObject() + && jsonObject.getAsJsonObject(POLICY_KEY).get(fieldTag) != null) { + lookedupValue = jsonObject.getAsJsonObject(POLICY_KEY).get(fieldTag).getAsString(); + } + return lookedupValue; + } + + /** + * Look up the UUID and description in the key information for a concept. + * + * @param session the Apex editor session + * @param jsonObject the JSON object to place the fields in + * @param name the concept name to look up + * @param version the concept version to look up + */ + private boolean setUuidAndDescription(final RestSession session, JsonObject jsonObject, String name, + String version) { + // Look up the key information for the name and version + JsonObject keyInfoJsonObject = lookupKeyInfo(session, name, version); + if (keyInfoJsonObject == null || keyInfoJsonObject.get(APEX_KEY_INFO) != null) { + return false; + } + + if (keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject().get("UUID") != null) { + jsonObject.addProperty(UUID, + keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject().get("UUID").getAsString()); + } else { + jsonObject.addProperty(UUID, (String) null); + } + + if (keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject().get(DESCRIPTION) != null) { + jsonObject.addProperty(DESCRIPTION, + keyInfoJsonObject.get(APEX_KEY_INFO).getAsJsonObject().get(DESCRIPTION).getAsString()); + } else { + jsonObject.addProperty(DESCRIPTION, (String) null); + } + + return true; + } + + /** + * Look up the key information for the given concept name and value. + * + * @param session the Apex editor session + * @param name the concept name to look up + * @param version the concept version to look up + * @return a JSON version of the concept key information + */ + private JsonObject lookupKeyInfo(final RestSession session, final String name, final String version) { + final ApexApiResult keyInfoResult = session.getApexModel().listKeyInformation(name, version); + final List keyInfoMessages = keyInfoResult.getMessages(); + + if (keyInfoResult.isNok() || keyInfoMessages == null || keyInfoMessages.isEmpty()) { + return null; + } + + final Gson gson = new GsonBuilder().serializeNulls().enableComplexMapKeySerialization().create(); + final String keyInfoJson = keyInfoMessages.get(0); + return gson.fromJson(keyInfoJson, JsonObject.class); + } +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/PolicyHandler.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/PolicyHandler.java new file mode 100644 index 000000000..c1887b834 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/PolicyHandler.java @@ -0,0 +1,578 @@ +/*- + * ============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.client.editor.rest.handling; + +import java.util.Map; + +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanKeyRef; +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanLogic; +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanPolicy; +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanState; +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanStateOutput; +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanStateTaskRef; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.modelapi.ApexApiResult; +import org.onap.policy.apex.model.modelapi.ApexApiResult.Result; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicy; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class handles commands on policies in Apex models. + */ +public class PolicyHandler implements RestCommandHandler { + // Get a reference to the logger + private static final XLogger LOGGER = XLoggerFactory.getXLogger(PolicyHandler.class); + + // Recurring string constants + private static final String OK = ": OK"; + private static final String NOT_OK = ": Not OK"; + private static final String POLICY_WAS_CREATED = "\". The policy was created, "; + private static final String POLICY_STATE_CREATED = "\". The policy and state were created, "; + private static final String POLICY_PARTIALLY_DEFINED = " The policy has only been partially defined."; + private static final String FOR_POLICY = "\" for policy \""; + private static final String IN_STATE = "\" in state \""; + private static final String POLICY_CREATED_STATE_ERROR = POLICY_WAS_CREATED + + "but there was an error adding the state."; + private static final String POLICY_STATE_CREATED_OTHER_ERROR = POLICY_STATE_CREATED + + "but there was an error adding the"; + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String jsonString) { + + if (!RestCommandType.POLICY.equals(commandType)) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + switch (command) { + case CREATE: + return createPolicy(session, jsonString); + case UPDATE: + return updatePolicy(session, jsonString); + default: + return getUnsupportedCommandResultMessage(session, commandType, command); + } + } + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String name, final String version) { + if (!RestCommandType.POLICY.equals(commandType)) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + switch (command) { + case LIST: + return listPolicies(session, name, version); + case DELETE: + return deletePolicy(session, name, version); + default: + return getUnsupportedCommandResultMessage(session, commandType, command); + } + } + + /** + * Creates a policy with the information in the JSON string passed. + * + * @param session the Apex model editing session + * @param jsonString the JSON string to be parsed See {@linkplain BeanPolicy} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + public ApexApiResult createPolicy(final RestSession session, final String jsonString) { + LOGGER.entry(jsonString); + + final BeanPolicy jsonbean = RestUtils.getJsonParameters(jsonString, BeanPolicy.class); + + session.editModel(); + + ApexApiResult result = session.getApexModelEdited().createPolicy(jsonbean.getName(), jsonbean.getVersion(), + jsonbean.getTemplate(), jsonbean.getFirstState(), jsonbean.getUuid(), + jsonbean.getDescription()); + + if (result.isOk()) { + result = createPolicyContent(session, jsonbean); + } + + session.finishSession(result.isOk()); + + LOGGER.exit("Policy/Create" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Create the content of the policy. + * + * @param session the Apex model editing session + * @param jsonString the JSON string to be parsed See {@linkplain BeanPolicy} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult createPolicyContent(RestSession session, BeanPolicy jsonbean) { + ApexApiResult result = new ApexApiResult(); + + if (jsonbean.getStates() == null || jsonbean.getStates().isEmpty()) { + result.setResult(Result.FAILED); + result.addMessage("Null or empty state map; no states defined for policy \"" + jsonbean.getName() + ":" + + jsonbean.getVersion() + + "\". The policy was created, but there was an error adding states." + + POLICY_PARTIALLY_DEFINED); + return result; + } + + // States reference each other so all states must be created before they are populated + for (final Map.Entry stateEntry : jsonbean.getStates().entrySet()) { + ApexApiResult stateCreateResult = createState(session, jsonbean.getName(), jsonbean.getVersion(), + stateEntry.getKey(), stateEntry.getValue()); + + if (stateCreateResult.isNok()) { + result.setResult(stateCreateResult.getResult()); + result.addMessage(stateCreateResult.getMessage()); + } + } + + // Bale out if the state creation did not work + if (result.isNok()) { + return result; + } + + // Now create the content of each state + for (final Map.Entry stateEntry : jsonbean.getStates().entrySet()) { + ApexApiResult stateContentCreateResult = createStateContent(session, jsonbean.getName(), + jsonbean.getVersion(), stateEntry.getKey(), stateEntry.getValue()); + + if (stateContentCreateResult.isNok()) { + result.setResult(stateContentCreateResult.getResult()); + result.addMessage(stateContentCreateResult.getMessage()); + } + } + + return result; + } + + /** + * Create a state on the policy. + * + * @param session the Apex model editing session + * @param policyName the policy name + * @param policVersion the policy version + * @param stateName the name of the state + * @param stateBean the information on the state to create + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult createState(final RestSession session, final String policyName, final String policyVersion, + final String stateName, final BeanState stateBean) { + + if (stateBean == null) { + return new ApexApiResult(Result.FAILED, + "Null or invalid state information for state \"" + stateName + FOR_POLICY + policyName + ":" + + policyVersion + POLICY_CREATED_STATE_ERROR + POLICY_PARTIALLY_DEFINED); + } + + if (stateBean.getTrigger() == null) { + return new ApexApiResult(Result.FAILED, + "Null or invalid state trigger for state \"" + stateName + FOR_POLICY + policyName + ":" + + policyVersion + POLICY_CREATED_STATE_ERROR + POLICY_PARTIALLY_DEFINED); + } + + if (stateBean.getDefaultTask() == null) { + return new ApexApiResult(Result.FAILED, "Null or invalid default task for state \"" + stateName + FOR_POLICY + + policyName + ":" + policyVersion + POLICY_CREATED_STATE_ERROR + POLICY_PARTIALLY_DEFINED); + } + + return session.getApexModelEdited().createPolicyState(policyName, policyVersion, stateName, + stateBean.getTrigger().getName(), stateBean.getTrigger().getVersion(), + stateBean.getDefaultTask().getName(), stateBean.getDefaultTask().getVersion()); + } + + /** + * Create the content of a state on the policy. + * + * @param session the Apex model editing session + * @param policyName the policy name + * @param policVersion the policy version + * @param stateName the name of the state + * @param stateBean the information on the state to create + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult createStateContent(final RestSession session, final String policyName, + final String policyVersion, final String stateName, final BeanState stateBean) { + + ApexApiResult ret = createStateTaskSelectionLogic(session, policyName, policyVersion, stateName, stateBean); + + if (ret.isOk()) { + ret = createStateContextReferences(session, policyName, policyVersion, stateName, stateBean); + } + + if (ret.isOk()) { + ret = createStateFinalizers(session, policyName, policyVersion, stateName, stateBean); + } + + if (ret.isOk()) { + ret = createStateOutputs(session, policyName, policyVersion, stateName, stateBean); + } + + if (ret.isOk()) { + ret = createStateTaskReferences(session, policyName, policyVersion, stateName, stateBean); + } + + return ret; + } + + /** + * Create the task selection logic for the state. + * + * @param session the Apex model editing session + * @param policyName the policy name + * @param policVersion the policy version + * @param stateName the name of the state + * @param stateBean the information on the state to create + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult createStateTaskSelectionLogic(final RestSession session, final String policyName, + final String policyVersion, final String stateName, final BeanState stateBean) { + + final BeanLogic tsl = stateBean.getTaskSelectionLogic(); + if (tsl == null) { + return new ApexApiResult(); + } + + ApexApiResult result = session.getApexModelEdited().createPolicyStateTaskSelectionLogic(policyName, + policyVersion, stateName, tsl.getLogicFlavour(), tsl.getLogic()); + + if (result.isNok()) { + result.addMessage("Failed to add task selection logic for state \"" + stateName + "\" for" + " policy \"" + + policyName + ":" + policyVersion + POLICY_WAS_CREATED + + "but there was an error adding the task selection logic " + + "for the state. The policy has only been partially defined."); + } + return result; + } + + /** + * Create the context references for the state. + * + * @param session the Apex model editing session + * @param policyName the policy name + * @param policVersion the policy version + * @param stateName the name of the state + * @param stateBean the information on the state to create + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult createStateContextReferences(final RestSession session, final String policyName, + final String policyVersion, final String stateName, final BeanState stateBean) { + + ApexApiResult result = new ApexApiResult(); + + final BeanKeyRef[] contextReferences = stateBean.getContexts(); + if (contextReferences == null || contextReferences.length == 0) { + return result; + } + + for (final BeanKeyRef contextReference : contextReferences) { + if (contextReference == null) { + result.setResult(Result.FAILED); + result.addMessage("Null or invalid context reference \"" + contextReference + "\" for" + " state \"" + + stateName + FOR_POLICY + policyName + ":" + policyVersion + + "\". The policy was created, but there was an error adding the context " + + "reference for the state. The policy has only been partially defined."); + continue; + } + + ApexApiResult contextRefResult = session.getApexModelEdited().createPolicyStateContextRef(policyName, + policyVersion, stateName, contextReference.getName(), contextReference.getVersion()); + + if (contextRefResult.isNok()) { + result.setResult(contextRefResult.getResult()); + result.addMessage("Failed to add context reference \"" + contextReference + "\" for state \"" + + stateName + FOR_POLICY + policyName + ":" + policyVersion + POLICY_WAS_CREATED + + "but there was an error adding the context reference " + + "for the state. The policy has only been partially defined."); + } + } + + return result; + } + + /** + * Create the state finalizers for the state. + * + * @param session the Apex model editing session + * @param policyName the policy name + * @param policVersion the policy version + * @param stateName the name of the state + * @param stateBean the information on the state to create + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult createStateFinalizers(final RestSession session, final String policyName, + final String policyVersion, final String stateName, final BeanState stateBean) { + + ApexApiResult result = new ApexApiResult(); + + final Map finalizers = stateBean.getFinalizers(); + if (finalizers == null || finalizers.isEmpty()) { + return result; + } + + for (final Map.Entry finalizerEntry : finalizers.entrySet()) { + if (finalizerEntry.getKey() == null || finalizerEntry.getValue() == null) { + result.setResult(Result.FAILED); + result.addMessage("Null or invalid finalizer information for finalizer " + "named \"" + + finalizerEntry.getKey() + IN_STATE + stateName + FOR_POLICY + policyName + ":" + + policyVersion + POLICY_STATE_CREATED_OTHER_ERROR + " finalizer. The policy has only " + + "been partially defined."); + continue; + } + + ApexApiResult finalizerResult = session.getApexModelEdited().createPolicyStateFinalizerLogic(policyName, + policyVersion, stateName, finalizerEntry.getKey(), + finalizerEntry.getValue().getLogicFlavour(), finalizerEntry.getValue().getLogic()); + + if (finalizerResult.isNok()) { + result.setResult(finalizerResult.getResult()); + result.addMessage("Failed to add finalizer information for finalizer named \"" + finalizerEntry.getKey() + + "\" in" + " state \"" + stateName + FOR_POLICY + policyName + ":" + policyVersion + + POLICY_STATE_CREATED_OTHER_ERROR + + " finalizer. The policy has only been partially defined."); + } + } + + return result; + } + + /** + * Create the state outputs for the state. + * + * @param session the Apex model editing session + * @param policyName the policy name + * @param policVersion the policy version + * @param stateName the name of the state + * @param stateBean the information on the state to create + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult createStateOutputs(final RestSession session, final String policyName, + final String policyVersion, final String stateName, final BeanState stateBean) { + + ApexApiResult result = new ApexApiResult(); + + final Map stateOutputs = stateBean.getStateOutputs(); + if (stateOutputs == null || stateOutputs.isEmpty()) { + result.setResult(Result.FAILED); + result.addMessage("No state outputs have been defined in state \"" + stateName + FOR_POLICY + policyName + + ":" + policyVersion + + "\". The policy and state were created, but there was an error adding state" + + " outputs. The policy has only been partially defined."); + return result; + } + + for (final Map.Entry stateOutput : stateOutputs.entrySet()) { + final String outputName = stateOutput.getKey(); + final BeanStateOutput output = stateOutput.getValue(); + + if (outputName == null || output == null || output.getEvent() == null) { + result.setResult(Result.FAILED); + result.addMessage("Null or invalid output information for output named \"" + outputName + IN_STATE + + stateName + FOR_POLICY + policyName + ":" + policyVersion + + POLICY_STATE_CREATED_OTHER_ERROR + + " output. The policy has only been partially defined."); + continue; + } + + ApexApiResult outputResult = session.getApexModelEdited().createPolicyStateOutput(policyName, policyVersion, + stateName, outputName, output.getEvent().getName(), output.getEvent().getVersion(), + output.getNextState()); + + if (outputResult.isNok()) { + result.setResult(outputResult.getResult()); + result.addMessage("Failed to add output information for output named \"" + outputName + IN_STATE + + stateName + FOR_POLICY + policyName + ":" + policyVersion + POLICY_STATE_CREATED + + "but there was an error adding the output." + POLICY_PARTIALLY_DEFINED); + } + } + + return result; + } + + /** + * Create the task references for the state. + * + * @param session the Apex model editing session + * @param policyName the policy name + * @param policVersion the policy version + * @param stateName the name of the state + * @param stateBean the information on the state to create + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult createStateTaskReferences(final RestSession session, final String policyName, + final String policyVersion, final String stateName, final BeanState stateBean) { + + ApexApiResult result = new ApexApiResult(); + + final Map taskMap = stateBean.getTasks(); + if (taskMap == null || taskMap.isEmpty()) { + result.setResult(Result.FAILED); + result.addMessage("No tasks have been defined in state \"" + stateName + FOR_POLICY + policyName + ":" + + policyVersion + + "\". The policy and state were created, but there was an error adding tasks." + + POLICY_PARTIALLY_DEFINED); + return result; + } + + for (final Map.Entry taskEntry : taskMap.entrySet()) { + final String taskLocalName = taskEntry.getKey(); + final BeanStateTaskRef taskReference = taskEntry.getValue(); + + if (taskLocalName == null || taskReference == null || taskReference.getTask() == null) { + result.setResult(Result.FAILED); + result.addMessage("Null or invalid task information for task named \"" + taskLocalName + IN_STATE + + stateName + "\" for for policy \"" + policyName + ":" + policyVersion + + "\". The policy and state were created, but there was an error adding the " + + "task. The policy has only been partially defined."); + continue; + } + + ApexApiResult taskRefResult = session.getApexModelEdited().createPolicyStateTaskRef(policyName, + policyVersion, stateName, taskLocalName, taskReference.getTask().getName(), + taskReference.getTask().getVersion(), taskReference.getOutputType(), + taskReference.getOutputName()); + + if (taskRefResult.isNok()) { + result.setResult(taskRefResult.getResult()); + result.addMessage("Failed to add task reference \"" + taskEntry + "\" for state \"" + stateName + + FOR_POLICY + policyName + ":" + policyVersion + POLICY_WAS_CREATED + + "but there was an error adding the task reference for" + + " the state. The policy has only been partially defined."); + } + } + + return result; + } + + /** + * Update a policy with the information in the JSON string passed. + * + * @param session the Apex model editing session + * @param jsonString the JSON string to be parsed. See {@linkplain BeanPolicy} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult updatePolicy(final RestSession session, final String jsonString) { + + LOGGER.entry(jsonString); + + final BeanPolicy jsonbean = RestUtils.getJsonParameters(jsonString, BeanPolicy.class); + + if (blank2Null(jsonbean.getName()) == null || blank2Null(jsonbean.getVersion()) == null) { + LOGGER.exit("Task/Update" + NOT_OK); + return new ApexApiResult(Result.FAILED, "Null/Empty Policy name/version (\"" + jsonbean.getName() + ":" + + jsonbean.getVersion() + "\" passed to UpdatePolicy"); + } + + session.editModel(); + + ApexApiResult result = session.getApexModelEdited().deletePolicy(jsonbean.getName(), jsonbean.getVersion()); + + if (result.isOk()) { + result = session.getApexModelEdited().createPolicy(jsonbean.getName(), jsonbean.getVersion(), + jsonbean.getTemplate(), jsonbean.getFirstState(), jsonbean.getUuid(), + jsonbean.getDescription()); + + if (result.isOk()) { + result = createPolicyContent(session, jsonbean); + } + } + + session.finishSession(result.isOk()); + + LOGGER.exit("Policy/Update" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + + } + + /** + * List policies with the given key names/versions. If successful the result(s) will be available in the result + * messages. The returned value(s) will be similar to {@link AxPolicy}, with merged {@linkplain AxKey Info} for the + * root object. + * + * @param session the Apex model editing session + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult listPolicies(final RestSession session, final String name, final String version) { + LOGGER.entry(name, version); + + ApexApiResult result = session.getApexModel().listPolicy(blank2Null(name), blank2Null(version)); + + LOGGER.exit("Policy/Get" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Delete policies with the given key names/versions. + * + * @param session the Apex model editing session + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult deletePolicy(final RestSession session, final String name, final String version) { + LOGGER.entry(name, version); + + session.editModel(); + + // all input/output fields, parameters, logic, context references is "owned"/contained + // in the task, so + // deleting the task removes all of these + ApexApiResult result = session.getApexModelEdited().deletePolicy(blank2Null(name), blank2Null(version)); + + session.finishSession(result.isOk()); + + LOGGER.exit("Policy/Delete" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestCommand.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestCommand.java new file mode 100644 index 000000000..7b1bc020d --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestCommand.java @@ -0,0 +1,64 @@ +/*- + * ============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.client.editor.rest.handling; + +/** + * This enum maps REST calls to the handlers that process them. + * + */ +public enum RestCommand { + /** + * Create the target. + */ + CREATE, + /** + * Update the target. + */ + UPDATE, + /** + * List the target. + */ + LIST, + /** + * Delete the target. + */ + DELETE, + /** + * Validate the target. + */ + VALIDATE, + /** + * Load the target. + */ + LOAD, + /** + * Analyse the target. + */ + ANALYSE, + /** + * Get the key of the currently loaded apex model. + */ + GET_KEY, + /** + * Download the currently loaded apex model. + */ + DOWNLOAD +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestCommandHandler.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestCommandHandler.java new file mode 100644 index 000000000..c4fd6d6f4 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestCommandHandler.java @@ -0,0 +1,102 @@ +/*- + * ============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.client.editor.rest.handling; + +import org.onap.policy.apex.model.modelapi.ApexApiResult; +import org.onap.policy.apex.model.modelapi.ApexApiResult.Result; + +// TODO: Auto-generated Javadoc +/** + * This interface defines the methods that a REST handler must implement to handle REST editor commands. + * + */ +public interface RestCommandHandler { + + /** + * Process a REST command. + * + * @param session the Apex editor session + * @param commandType the type of REST command to execute + * @param command the REST command to execute + * @return the apex api result the result of the execution + */ + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command); + + /** + * Process a REST command. + * + * @param session the Apex editor session + * @param commandType the type of REST command to execute + * @param command the REST command to execute + * @param jsonString the json string to use to execute the command + * @return the apex api result the result of the execution + */ + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String jsonString); + + /** + * Process a REST command. + * + * @param session the Apex editor session + * @param commandType the type of REST command to execute + * @param command the REST command to execute + * @param name the concept name on which to execute + * @param version the concept version the version on which to execute + * @return the apex api result the result of the execution + */ + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String name, final String version); + + /** + * Get an unsupported command result message. + * + * @param session the Apex editor session + * @param commandType the type of REST command to execute + * @param command the REST command to execute + */ + public default ApexApiResult getUnsupportedCommandResultMessage(final RestSession session, + final RestCommandType commandType, final RestCommand command) { + return new ApexApiResult(Result.FAILED, "session " + session.getSessionId() + ", command type " + commandType + + ", command" + command + " invalid"); + } + + /** + * Convert blank incoming fields to nulls. + * + * @param incomingField the field to check + * @return null if the field is blank, otherwise, the field trimmed + */ + public default String blank2Null(final String incomingField) { + if (incomingField == null) { + return null; + } + + String trimmedField = incomingField.trim(); + + if (trimmedField.isEmpty()) { + return null; + } + else { + return trimmedField; + } + } +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestCommandType.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestCommandType.java new file mode 100644 index 000000000..c546cb02a --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestCommandType.java @@ -0,0 +1,56 @@ +/*- + * ============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.client.editor.rest.handling; + +/** + * This enum maps REST calls to the handlers that process them. + * + */ +public enum RestCommandType { + /** + * Model commands. + */ + MODEL, + /** + * Key Information commands. + */ + KEY_INFO, + /** + * Context schema commands. + */ + CONTEXT_SCHEMA, + /** + * Context album commands. + */ + CONTEXT_ALBUM, + /** + * Event Commands. + */ + EVENT, + /** + * Task commands. + */ + TASK, + /** + * Policy commands. + */ + POLICY +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestSession.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestSession.java new file mode 100644 index 000000000..3e616ef3a --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestSession.java @@ -0,0 +1,125 @@ +/*- + * ============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.client.editor.rest.handling; + +import org.onap.policy.apex.model.modelapi.ApexApiResult; +import org.onap.policy.apex.model.modelapi.ApexApiResult.Result; +import org.onap.policy.apex.model.modelapi.ApexModel; +import org.onap.policy.apex.model.modelapi.ApexModelFactory; + +/** + * This class represents an ongoing editor session in the Apex editor and holds the information for the session. + * + */ +public class RestSession { + // The ID of the session + private int sessionId; + + // The Apex policy model of the session + private ApexModel apexModel; + + // The Apex policy model being edited + private ApexModel apexModelEdited; + + public RestSession(final int sessionId) { + this.sessionId = sessionId; + this.apexModel = new ApexModelFactory().createApexModel(null, true); + } + + /** + * Commence making changes to the Apex model. + * @return the result of the edit commencement operation + */ + public synchronized ApexApiResult editModel() { + if (apexModelEdited != null) { + return new ApexApiResult(Result.FAILED, "model is already being edited"); + } + + apexModelEdited = apexModel.clone(); + return new ApexApiResult(); + } + + /** + * Commit the changes to the Apex model. + * @return the result of the commit operation + */ + public synchronized ApexApiResult commitChanges() { + if (apexModelEdited == null) { + return new ApexApiResult(Result.FAILED, "model is not being edited"); + } + + apexModel = apexModelEdited; + apexModelEdited = null; + return new ApexApiResult(); + } + + /** + * Discard the changes to the Apex model. + * @return the result of the discard operation + */ + public synchronized ApexApiResult discardChanges() { + if (apexModelEdited == null) { + return new ApexApiResult(Result.FAILED, "model is not being edited"); + } + + apexModelEdited = null; + return new ApexApiResult(); + } + + + /** + * Finish a session by committing or discarding the changes. + * + * @param commitFlag if ture, commit changes otherwise discard them + */ + public void finishSession(boolean commitFlag) { + if (commitFlag) { + commitChanges(); + } + else { + discardChanges(); + } + } + + /** + * Get the session ID of the session. + * @return the sessionId + */ + public int getSessionId() { + return sessionId; + } + + /** + * Get the Apex model of the session. + * @return the apexModel + */ + public ApexModel getApexModel() { + return apexModel; + } + + /** + * Get the edited Apex model of the session. + * @return the apexModel + */ + public ApexModel getApexModelEdited() { + return apexModelEdited; + } +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestSessionHandler.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestSessionHandler.java new file mode 100644 index 000000000..b717944ed --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestSessionHandler.java @@ -0,0 +1,110 @@ +/*- + * ============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.client.editor.rest.handling; + +import java.util.Map; +import java.util.TreeMap; +import java.util.concurrent.atomic.AtomicInteger; + +import org.onap.policy.apex.model.modelapi.ApexApiResult; +import org.onap.policy.apex.model.modelapi.ApexApiResult.Result; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class carries out session handling for Apex REST editor sessions. + */ +public class RestSessionHandler { + // Get a reference to the logger + private static final XLogger LOGGER = XLoggerFactory.getXLogger(RestSessionHandler.class); + + // The next session will have this number, stating at 0 + private AtomicInteger nextSessionId = new AtomicInteger(); + + // All REST editor sessions being handled by this handler + private final Map sessionMap = new TreeMap<>(); + + /** + * Create a new session. + * @param result the result of session creation + * @return the new session object + */ + public RestSession createSession(ApexApiResult result) { + LOGGER.entry("creating session"); + + // Create the session with the next session ID + final int newSessionId = nextSessionId.getAndIncrement(); + sessionMap.put(newSessionId, new RestSession(newSessionId)); + + result.addMessage(Integer.toString(newSessionId)); + + LOGGER.exit("created session with ID: " + newSessionId); + return sessionMap.get(newSessionId); + } + + /** + * Get a session for the given session ID. + * @param sessionId the session ID of the session we require + * @param result the result of the session get + * @return the session + */ + public RestSession getSession(final int sessionId, ApexApiResult result) { + LOGGER.entry("finding session: " + sessionId); + + // Check for valid session IDs + if (sessionId < 0) { + result.setResult(Result.FAILED); + result.addMessage("Session ID \"" + sessionId + "\" is negative"); + LOGGER.exit(result.getMessage()); + return null; + } + + // Check if session exits + if (!sessionMap.containsKey(sessionId)) { + result.setResult(Result.FAILED); + result.addMessage("A session with session ID \"" + sessionId + "\" does not exist"); + LOGGER.exit(result.getMessage()); + return null; + } + + RestSession session = sessionMap.get(sessionId); + + // Check if session is valid + if (session == null) { + result.setResult(Result.FAILED); + result.addMessage("The session with session ID \"" + sessionId + "\" is corrupt"); + LOGGER.exit(result.getMessage()); + return null; + } + + // Return the session + LOGGER.exit("session found: " + sessionId); + return session; + } + + /* + * This is a test method to set a corrupt session ID in the session map + * @param corruptSessionId the ID of the corrupt session + */ + protected void setCorruptSession(int corruptSessionId) { + sessionMap.put(corruptSessionId, null); + } +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestUtils.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestUtils.java new file mode 100644 index 000000000..5cb7af840 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/RestUtils.java @@ -0,0 +1,184 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling; + +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; + +import java.io.StringReader; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +import javax.ws.rs.core.MediaType; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import javax.xml.transform.stream.StreamSource; + +import org.eclipse.persistence.jaxb.MarshallerProperties; +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanBase; +import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; + +/** + * Utilities for handling RESTful communication for Apex. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public abstract class RestUtils { + // Regular expressions for checking input types + private static final String XML_INPUT_TYPE_REGEXP = "^\\s*<\\?xml.*>\\s*"; //starts with + /** + * starts with some kind of bracket [ or ( or {, then has something, then has bracket. + */ + private static final String JSON_INPUT_TYPE_REGEXP = "^\\s*[\\(\\{\\[][\\s+\\S]*[\\)\\}\\]]"; + + /** + * Constructor, block inheritance. + */ + private RestUtils() {} + + /** + * HTTP POST requests can't send nulls so we interpret blanks as nulls. + * + * @param parameter the parameter to convert from blank to null + * @return null if the parameter us blank, otherwise the original parameter + */ + private static String blank2null(final String parameter) { + return (parameter.length() == 0 ? null : parameter); + } + + /** + * HTTP POST requests can't send nulls so we interpret blanks as nulls. + * + * @param val the val + * @return null if the parameter us blank, otherwise the original parameter + */ + private static JsonElement blank2null(final JsonElement val) { + if (val == null) { + return JsonNull.INSTANCE; + } + if (val.isJsonPrimitive() && ((JsonPrimitive) val).isString()) { + final String v = ((JsonPrimitive) val).getAsString(); + if (v == null || "".equals(v)) { + return JsonNull.INSTANCE; + } + } + if (val.isJsonArray()) { + final JsonArray arr = val.getAsJsonArray(); + for (int i = 0; i < arr.size(); i++) { + arr.set(i, blank2null(arr.get(i))); + } + } + if (val.isJsonObject()) { + final JsonObject o = val.getAsJsonObject(); + for (final Entry e : o.entrySet()) { + e.setValue(blank2null(e.getValue())); + } + } + return val; + } + + /** + * Apex HTTP PUT requests send simple single level JSON strings, this method reads those strings + * into a map. + * + * @param jsonString the incoming JSON string + * @return a map of the JSON strings + */ + public static Map getJsonParameters(final String jsonString) { + final GsonBuilder gb = new GsonBuilder(); + gb.serializeNulls().enableComplexMapKeySerialization(); + final JsonObject jsonObject = gb.create().fromJson(jsonString, JsonObject.class); + + final Map jsonMap = new TreeMap<>(); + for (final Entry jsonEntry : jsonObject.entrySet()) { + jsonMap.put(jsonEntry.getKey(), (jsonEntry.getValue() == JsonNull.INSTANCE ? null + : blank2null(jsonEntry.getValue().getAsString()))); + } + return jsonMap; + } + + /** + * Apex HTTP PUT requests send simple single level JSON strings, this method reads those strings + * into a map. + * + * @param the generic type + * @param jsonString the incoming JSON string + * @param clz the clz + * @return a map of the JSON strings + */ + public static C getJsonParameters(final String jsonString, final Class clz) { + final GsonBuilder gb = new GsonBuilder(); + gb.serializeNulls().enableComplexMapKeySerialization(); + final JsonObject jsonObject = gb.create().fromJson(jsonString, JsonObject.class); + + for (final Entry jsonEntry : jsonObject.entrySet()) { + final JsonElement val = jsonEntry.getValue(); + jsonEntry.setValue(blank2null(val)); + } + return gb.create().fromJson(jsonObject, clz); + } + + /** + * Gets the concept from JSON. + * + * @param the generic type + * @param jsonString the json string + * @param clz the clz + * @return the concept from JSON + * @throws JAXBException the JAXB exception + */ + public static C getConceptFromJson(final String jsonString, final Class clz) + throws JAXBException { + Unmarshaller unmarshaller = null; + final JAXBContext jaxbContext = JAXBContext.newInstance(clz); + unmarshaller = jaxbContext.createUnmarshaller(); + if (jsonString.matches(JSON_INPUT_TYPE_REGEXP)) { + unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON); + unmarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); + } else if (jsonString.matches(XML_INPUT_TYPE_REGEXP)) { + unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML); + } else { + return null; + } + final StreamSource source = new StreamSource(new StringReader(jsonString)); + final JAXBElement rootElement = unmarshaller.unmarshal(source, clz); + return rootElement.getValue(); + } + + /** + * Gets the JSO nfrom concept. + * + * @param object the object + * @return the JSO nfrom concept + */ + public static String getJsonfromConcept(final Object object) { + final GsonBuilder gb = new GsonBuilder(); + gb.serializeNulls().enableComplexMapKeySerialization(); + return gb.create().toJson(object); + } +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/TaskHandler.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/TaskHandler.java new file mode 100644 index 000000000..efef44006 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/TaskHandler.java @@ -0,0 +1,460 @@ +/*- + * ============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.client.editor.rest.handling; + +import java.util.Map.Entry; + +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanField; +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanKeyRef; +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanLogic; +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanTask; +import org.onap.policy.apex.client.editor.rest.handling.bean.BeanTaskParameter; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; +import org.onap.policy.apex.model.modelapi.ApexApiResult; +import org.onap.policy.apex.model.modelapi.ApexApiResult.Result; +import org.onap.policy.apex.model.policymodel.concepts.AxTask; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class handles commands on tasks in Apex models. + */ +public class TaskHandler implements RestCommandHandler { + // Get a reference to the logger + private static final XLogger LOGGER = XLoggerFactory.getXLogger(TaskHandler.class); + + // Recurring string constants + private static final String OK = ": OK"; + private static final String NOT_OK = ": Not OK"; + private static final String IN_TASK = "\" in task "; + private static final String TASK_PARTIALLY_DEFINED = " The task has only been partially defined."; + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String jsonString) { + if (!RestCommandType.TASK.equals(commandType)) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + switch (command) { + case CREATE: + return createTask(session, jsonString); + case UPDATE: + return updateTask(session, jsonString); + default: + return getUnsupportedCommandResultMessage(session, commandType, command); + } + } + + /** + * {@inheritDoc} + */ + @Override + public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType, + final RestCommand command, final String name, final String version) { + if (!RestCommandType.TASK.equals(commandType)) { + return getUnsupportedCommandResultMessage(session, commandType, command); + } + + switch (command) { + case LIST: + return listTasks(session, name, version); + case DELETE: + return deleteTask(session, name, version); + case VALIDATE: + return validateTask(session, name, version); + default: + return getUnsupportedCommandResultMessage(session, commandType, command); + } + } + + /** + * Creates a task with the information in the JSON string passed. + * + * @param session the Apex model editing session + * @param jsonString the JSON string to be parsed. See {@linkplain BeanTask} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult createTask(final RestSession session, final String jsonString) { + LOGGER.entry(jsonString); + + final BeanTask jsonbean = RestUtils.getJsonParameters(jsonString, BeanTask.class); + + session.editModel(); + + ApexApiResult result = session.getApexModelEdited().createTask(jsonbean.getName(), jsonbean.getVersion(), + jsonbean.getUuid(), jsonbean.getDescription()); + + if (result.isOk()) { + result = createTaskContent(session, jsonbean); + } + + session.finishSession(result.isOk()); + + LOGGER.exit("Task/Create" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Create the content of the task. + * + * @param session the Apex model editing session + * @param jsonString the JSON string to be parsed. See {@linkplain BeanTask} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult createTaskContent(final RestSession session, final BeanTask jsonbean) { + ApexApiResult result = createInputFields(session, jsonbean); + + if (result.isOk()) { + result = createOutputFields(session, jsonbean); + } + + if (result.isOk()) { + result = createTaskLogic(session, jsonbean); + } + + if (result.isOk()) { + result = createTaskParameters(session, jsonbean); + } + + if (result.isOk()) { + result = createContextReferences(session, jsonbean); + } + return result; + } + + /** + * Create the input fields for the task. + * + * @param session the Apex model editing session + * @param jsonbean the ban containing the fields + * @return the result of the operation + */ + private ApexApiResult createInputFields(final RestSession session, final BeanTask jsonbean) { + ApexApiResult result = new ApexApiResult(); + + if (jsonbean.getInputFields() == null || jsonbean.getInputFields().isEmpty()) { + return result; + } + + for (final Entry fieldEntry : jsonbean.getInputFields().entrySet()) { + if (fieldEntry.getValue() == null) { + result.setResult(Result.FAILED); + result.addMessage("Null task input field information for field \"" + fieldEntry.getKey() + IN_TASK + + jsonbean.getName() + ":" + jsonbean.getVersion() + + ". The task was created, but there was an error adding the input fields." + + TASK_PARTIALLY_DEFINED); + continue; + } + + if (fieldEntry.getKey() == null || !fieldEntry.getKey().equals(fieldEntry.getValue().getLocalName())) { + result.setResult(Result.FAILED); + result.addMessage("Invalid task input field information for field \"" + fieldEntry.getKey() + IN_TASK + + jsonbean.getName() + ":" + jsonbean.getVersion() + ". The localName of the field (\"" + + fieldEntry.getValue().getLocalName() + "\") is not the same as the field name. " + + "The task was created, but there was an error adding the input fields." + + TASK_PARTIALLY_DEFINED); + } else { + ApexApiResult fieldCreationResult = session.getApexModelEdited().createTaskInputField( + jsonbean.getName(), jsonbean.getVersion(), fieldEntry.getKey(), + fieldEntry.getValue().getName(), fieldEntry.getValue().getVersion(), + fieldEntry.getValue().getOptional()); + + if (fieldCreationResult.isNok()) { + result.setResult(fieldCreationResult.getResult()); + result.addMessage("Failed to add task input field information for field \"" + fieldEntry.getKey() + + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion() + + ". The task was created, but there was an error adding the input fields." + + TASK_PARTIALLY_DEFINED); + } + } + } + + return result; + } + + /** + * Create the output fields for the task. + * + * @param session the Apex model editing session + * @param jsonbean the ban containing the fields + * @return the result of the operation + */ + private ApexApiResult createOutputFields(final RestSession session, final BeanTask jsonbean) { + ApexApiResult result = new ApexApiResult(); + + if (jsonbean.getOutputFields() == null || jsonbean.getOutputFields().isEmpty()) { + return result; + } + + for (final Entry fieldEntry : jsonbean.getOutputFields().entrySet()) { + if (fieldEntry.getValue() == null) { + result.setResult(Result.FAILED); + result.addMessage("Null task output field information for field \"" + fieldEntry.getKey() + IN_TASK + + jsonbean.getName() + ":" + jsonbean.getVersion() + + ". The task was created, but there was an error adding the output fields." + + TASK_PARTIALLY_DEFINED); + continue; + } + + if (fieldEntry.getKey() == null || !fieldEntry.getKey().equals(fieldEntry.getValue().getLocalName())) { + result.setResult(Result.FAILED); + result.addMessage("Invalid task output field information for field \"" + fieldEntry.getKey() + IN_TASK + + jsonbean.getName() + ":" + jsonbean.getVersion() + ". The localName of the field (\"" + + fieldEntry.getValue().getLocalName() + "\") is not the same as the field name. " + + "The task was created, but there was an error adding the output fields." + + TASK_PARTIALLY_DEFINED); + } else { + ApexApiResult fieldCreationResult = session.getApexModelEdited().createTaskOutputField( + jsonbean.getName(), jsonbean.getVersion(), fieldEntry.getKey(), + fieldEntry.getValue().getName(), fieldEntry.getValue().getVersion(), + fieldEntry.getValue().getOptional()); + if (fieldCreationResult.isNok()) { + result.setResult(fieldCreationResult.getResult()); + result.addMessage("Failed to add task output field information for field \"" + fieldEntry.getKey() + + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion() + + ". The task was created, but there was an error adding the output fields." + + TASK_PARTIALLY_DEFINED); + } + } + } + + return result; + } + + /** + * Create the task logic for the task. + * + * @param session the Apex model editing session + * @param jsonbean the bean containing the logic + * @return the result of the operation + */ + private ApexApiResult createTaskLogic(final RestSession session, final BeanTask jsonbean) { + ApexApiResult result = new ApexApiResult(); + + if (jsonbean.getTaskLogic() == null) { + return result; + } + + final BeanLogic logic = jsonbean.getTaskLogic(); + result = session.getApexModelEdited().createTaskLogic(jsonbean.getName(), jsonbean.getVersion(), + logic.getLogicFlavour(), logic.getLogic()); + + if (result.isNok()) { + result.addMessage("Failed to add task logic in task " + jsonbean.getName() + ":" + jsonbean.getVersion() + + ". The task was created, but there was an error adding the logic." + + TASK_PARTIALLY_DEFINED); + } + + return result; + } + + /** + * Create the task parameters for the task. + * + * @param session the Apex model editing session + * @param jsonbean the bean containing the parameters + * @return the result of the operation + */ + private ApexApiResult createTaskParameters(final RestSession session, final BeanTask jsonbean) { + ApexApiResult result = new ApexApiResult(); + + if (jsonbean.getParameters() == null || jsonbean.getParameters().isEmpty()) { + return result; + } + + for (final Entry parameterEntry : jsonbean.getParameters().entrySet()) { + if (parameterEntry.getKey() == null || parameterEntry.getValue() == null + || !parameterEntry.getKey().equals(parameterEntry.getValue().getParameterName())) { + result.setResult(Result.FAILED); + result.addMessage("Null or invalid task parameter information for parameter \"" + + parameterEntry.getKey() + IN_TASK + jsonbean.getName() + ":" + jsonbean.getVersion() + + ". The task was created, " + "but there was an error adding the parameters." + + TASK_PARTIALLY_DEFINED); + continue; + } + ApexApiResult createParResult = session.getApexModelEdited().createTaskParameter(jsonbean.getName(), + jsonbean.getVersion(), parameterEntry.getValue().getParameterName(), + parameterEntry.getValue().getDefaultValue()); + if (createParResult.isNok()) { + result.setResult(createParResult.getResult()); + result.addMessage("Failed to add task parameter \"" + parameterEntry.getKey() + IN_TASK + + jsonbean.getName() + ":" + jsonbean.getVersion() + + ". The task was created, but there was an error adding the parameters." + + TASK_PARTIALLY_DEFINED); + } + } + + return result; + } + + /** + * Create the context references for the task. + * + * @param session the Apex model editing session + * @param jsonbean the bean containing the context references + * @return the result of the operation + */ + private ApexApiResult createContextReferences(final RestSession session, final BeanTask jsonbean) { + ApexApiResult result = new ApexApiResult(); + + if (jsonbean.getContexts() == null || jsonbean.getContexts().length == 0) { + return result; + } + + for (final BeanKeyRef contextalbum : jsonbean.getContexts()) { + if (contextalbum.getName() == null || contextalbum.getVersion() == null) { + result.setResult(Result.FAILED); + result.addMessage("Null or invalid context album reference information in task " + jsonbean.getName() + + ":" + jsonbean.getVersion() + + ". The task was created, but there was an error adding the" + + " context album reference. " + "The task has only been partially defined."); + continue; + } + ApexApiResult createRefResult = session.getApexModelEdited().createTaskContextRef(jsonbean.getName(), + jsonbean.getVersion(), contextalbum.getName(), contextalbum.getVersion()); + if (createRefResult.isNok()) { + result.setResult(createRefResult.getResult()); + result.addMessage("Failed to add context album reference information in task " + jsonbean.getName() + + ":" + jsonbean.getVersion() + + ". The task was created, but there was an error adding the" + + " context album reference. " + "The task has only been partially defined."); + } + } + + return result; + } + + /** + * Update a task with the information in the JSON string passed. + * + * @param session the Apex model editing session + * @param jsonString the JSON string to be parsed. See {@linkplain BeanTask} + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult updateTask(final RestSession session, final String jsonString) { + LOGGER.entry(jsonString); + + final BeanTask jsonbean = RestUtils.getJsonParameters(jsonString, BeanTask.class); + + if (blank2Null(jsonbean.getName()) == null || blank2Null(jsonbean.getVersion()) == null) { + LOGGER.exit("Task/Update" + NOT_OK); + return new ApexApiResult(Result.FAILED, "Null/Empty task name/version (\"" + jsonbean.getName() + ":" + + jsonbean.getVersion() + "\" passed to UpdateTask"); + } + + session.editModel(); + + ApexApiResult result = session.getApexModelEdited().deleteTask(jsonbean.getName(), jsonbean.getVersion()); + + if (result.isOk()) { + result = session.getApexModelEdited().createTask(jsonbean.getName(), jsonbean.getVersion(), + jsonbean.getUuid(), jsonbean.getDescription()); + + if (result.isOk()) { + result = createTaskContent(session, jsonbean); + } + } + + session.finishSession(result.isOk()); + + LOGGER.exit("Task/Update" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * List tasks with the given key names/versions. If successful the result(s) will be available in the result + * messages. The returned value(s) will be similar to {@link AxTask}, with merged {@linkplain AxKeyInfo} for the + * root object. + * + * @param session the Apex model editing session + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult listTasks(final RestSession session, final String name, final String version) { + LOGGER.entry(name, version); + + ApexApiResult result = session.getApexModel().listTask(blank2Null(name), blank2Null(version)); + + LOGGER.exit("Task/Get" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Delete tasks with the given key names/versions. + * + * @param session the Apex model editing session + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult deleteTask(final RestSession session, final String name, final String version) { + LOGGER.entry(name, version); + + session.editModel(); + + // all input/output fields, parameters, logic, context references is "owned"/contained + // in the task, so + // deleting the task removes all of these + ApexApiResult result = session.getApexModelEdited().deleteTask(blank2Null(name), blank2Null(version)); + + session.finishSession(result.isOk()); + + LOGGER.exit("Task/Delete" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } + + /** + * Validate tasks with the given key names/versions. The result(s) will be available in the result messages. + * + * @param session the Apex model editing session + * @param name the name to search for. If null or empty, then all names will be queried + * @param version the version to search for. If null then all versions will be searched for. + * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any + * messages/errors can be retrieved using {@link ApexApiResult#getMessages()} + */ + private ApexApiResult validateTask(final RestSession session, final String name, final String version) { + LOGGER.entry(name, version); + + ApexApiResult result = session.getApexModel().validateTask(blank2Null(name), blank2Null(version)); + + LOGGER.exit("Validate/Task" + (result != null && result.isOk() ? OK : NOT_OK)); + return result; + } +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanBase.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanBase.java new file mode 100644 index 000000000..20f8d6f07 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanBase.java @@ -0,0 +1,70 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +/** + * The base class for Beans. + */ +public abstract class BeanBase { + // Recurring string constants + private static final String PROBLEM_RETRIEVING_FIELD_PREFIX = "Problem retrieving field called ('"; + private static final String JSON_BEAN_SUFFIX = "') from JSON bean "; + + /** + * Gets a named field from the bean. + * + * @param field the field name + * @return the value for the field + */ + public String get(final String field) { + // CHECKSTYLE:OFF: MagicNumber + // use getter preferably + for (final Method method : this.getClass().getMethods()) { + if ((method.getName().startsWith("get")) && (method.getName().length() == (field.length() + 3))) { + if (method.getName().toLowerCase().endsWith(field.toLowerCase())) { + try { + return (String) method.invoke(this); + } catch (final Exception e) { + throw new IllegalArgumentException( + PROBLEM_RETRIEVING_FIELD_PREFIX + field + JSON_BEAN_SUFFIX + this, e); + } + } + } + } + // Use field approach + if (field != null) { + try { + final Field f = this.getClass().getDeclaredField(field); + if (f != null) { + f.setAccessible(true); + return (String) (f.get(this)); + } + } catch (final Exception e) { + throw new IllegalArgumentException( + PROBLEM_RETRIEVING_FIELD_PREFIX + field + JSON_BEAN_SUFFIX + this, e); + } + } + throw new IllegalArgumentException(PROBLEM_RETRIEVING_FIELD_PREFIX + field + JSON_BEAN_SUFFIX + this); + } +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanContextAlbum.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanContextAlbum.java new file mode 100644 index 000000000..53cb911e1 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanContextAlbum.java @@ -0,0 +1,111 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import javax.xml.bind.annotation.XmlType; + +/** + * The ContextAlbum Bean. + */ +@XmlType +public class BeanContextAlbum extends BeanBase { + private String name = null; + private String version = null; + private String scope = null; + private String uuid = null; + private String description = null; + private BeanKeyRef itemSchema = null; + private boolean writeable; + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Gets the version. + * + * @return the version + */ + public String getVersion() { + return version; + } + + /** + * Gets the scope. + * + * @return the scope + */ + public String getScope() { + return scope; + } + + /** + * Gets the uuid. + * + * @return the uuid + */ + public String getUuid() { + return uuid; + } + + /** + * Gets the description. + * + * @return the description + */ + public String getDescription() { + return description; + } + + /** + * Gets the item schema. + * + * @return the item schema + */ + public BeanKeyRef getItemSchema() { + return itemSchema; + } + + /** + * Gets the writeable. + * + * @return the writeable + */ + public boolean getWriteable() { + return writeable; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "ContextAlbum [name=" + name + ", version=" + version + ", scope=" + scope + ", uuid=" + uuid + + ", description=" + description + ", itemSchema=" + itemSchema + ", writeable=" + writeable + "]"; + } +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanContextSchema.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanContextSchema.java new file mode 100644 index 000000000..cab624c15 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanContextSchema.java @@ -0,0 +1,101 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import javax.xml.bind.annotation.XmlType; + +/** + * The ContextSchema Bean. + */ +@XmlType +public class BeanContextSchema extends BeanBase { + private String name = null; + private String version = null; + private String schemaFlavour = null; + private String schemaDefinition = null; + private String uuid = null; + private String description = null; + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Gets the version. + * + * @return the version + */ + public String getVersion() { + return version; + } + + /** + * Gets the uuid. + * + * @return the uuid + */ + public String getUuid() { + return uuid; + } + + /** + * Gets the description. + * + * @return the description + */ + public String getDescription() { + return description; + } + + /** + * Gets the schema flavour. + * + * @return the schema flavour + */ + public String getSchemaFlavour() { + return schemaFlavour; + } + + /** + * Gets the schema definition. + * + * @return the schema definition + */ + public String getSchemaDefinition() { + return schemaDefinition; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "ContextSchema [name=" + name + ", version=" + version + ", schemaFlavour=" + schemaFlavour + + ", schemaDefinition=" + schemaDefinition + ", uuid=" + uuid + ", description=" + description + "]"; + } +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanEvent.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanEvent.java new file mode 100644 index 000000000..16cb16cf8 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanEvent.java @@ -0,0 +1,138 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import java.util.Map; + +import javax.xml.bind.annotation.XmlType; + +/** + * The Event Bean. + */ +@XmlType +public class BeanEvent extends BeanBase { + private String name = null; + private String version = null; + private String nameSpace = null; + private String source = null; + private String target = null; + private String uuid = null; + private String description = null; + private Map parameters = null; + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Gets the version. + * + * @return the version + */ + public String getVersion() { + return version; + } + + /** + * Gets the name space. + * + * @return the name space + */ + public String getNameSpace() { + return nameSpace; + } + + /** + * Gets the source. + * + * @return the source + */ + public String getSource() { + return source; + } + + /** + * Gets the target. + * + * @return the target + */ + public String getTarget() { + return target; + } + + /** + * Gets the uuid. + * + * @return the uuid + */ + public String getUuid() { + return uuid; + } + + /** + * Gets the description. + * + * @return the description + */ + public String getDescription() { + return description; + } + + /** + * Gets the parameters. + * + * @return the parameters + */ + public Map getParameters() { + return parameters; + } + + /** + * Gets the parameter. + * + * @param ps the parameter string + * @return the parameter + */ + public BeanField getParameter(final String ps) { + if (parameters != null) { + return parameters.get(ps); + } + return null; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "Event [name=" + name + ", version=" + version + ", nameSpace=" + nameSpace + ", source=" + source + + ", target=" + target + ", uuid=" + uuid + ", description=" + description + ", parameters=" + + getParameters() + "]"; + } + +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanField.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanField.java new file mode 100644 index 000000000..0fcc58140 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanField.java @@ -0,0 +1,63 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import javax.xml.bind.annotation.XmlType; + +/** + * The Field Bean. + */ +@XmlType +public class BeanField extends BeanKeyRef { + + private boolean optional = true; + private String localName = null; + + /** + * Gets the local name for this field. + * + * @return the local name for this field. + */ + public String getLocalName() { + return localName; + } + + /** + * Gets the optional flag. + * + * @return the optional flag + */ + public boolean getOptional() { + return optional; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.client.editor.rest.bean.Bean_KeyRef#toString() + */ + @Override + public String toString() { + return "Field [localName=" + getLocalName() + ", name=" + getName() + ", version=" + getVersion() + + ", optional=" + getOptional() + "]"; + } + +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanKeyRef.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanKeyRef.java new file mode 100644 index 000000000..7aabacd57 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanKeyRef.java @@ -0,0 +1,61 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import javax.xml.bind.annotation.XmlType; + +/** + * The Key Reference Bean. + */ +@XmlType +public class BeanKeyRef extends BeanBase { + private String name = null; + private String version = null; + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Gets the version. + * + * @return the version + */ + public String getVersion() { + return version; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "KeyRef [name=" + name + ", version=" + version + "]"; + } + +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanLogic.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanLogic.java new file mode 100644 index 000000000..3c38dbde8 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanLogic.java @@ -0,0 +1,60 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import javax.xml.bind.annotation.XmlType; + +/** + * The Logic Bean. + */ +@XmlType +public class BeanLogic extends BeanBase { + private String logic = null; + private String logicFlavour = null; + + /** + * Gets the logic flavour. + * + * @return the logic flavour + */ + public String getLogicFlavour() { + return logicFlavour; + } + + /** + * Gets the logic. + * + * @return the logic + */ + public String getLogic() { + return logic; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "Logic [logicFlavour=" + logicFlavour + ", logic=" + logic + "]"; + } +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanModel.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanModel.java new file mode 100644 index 000000000..7f03a2413 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanModel.java @@ -0,0 +1,82 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import javax.xml.bind.annotation.XmlType; + +/** + * The Model Bean. + */ +@XmlType +public class BeanModel extends BeanBase { + + private String name = null; + private String version = null; + private String uuid = null; + private String description = null; + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Gets the version. + * + * @return the version + */ + public String getVersion() { + return version; + } + + /** + * Gets the uuid. + * + * @return the uuid + */ + public String getUuid() { + return uuid; + } + + /** + * Gets the description. + * + * @return the description + */ + public String getDescription() { + return description; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "Model [name=" + name + ", version=" + version + ", uuid=" + uuid + ", description=" + description + "]"; + } + +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanPolicy.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanPolicy.java new file mode 100644 index 000000000..cb960b88a --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanPolicy.java @@ -0,0 +1,114 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import java.util.Map; + +import javax.xml.bind.annotation.XmlType; + +/** + * The Policy Bean. + */ +@XmlType +public class BeanPolicy extends BeanBase { + private String name = null; + private String version = null; + private String uuid = null; + private String description = null; + private String firstState = null; + private String template = null; + private Map states = null; + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Gets the version. + * + * @return the version + */ + public String getVersion() { + return version; + } + + /** + * Gets the uuid. + * + * @return the uuid + */ + public String getUuid() { + return uuid; + } + + /** + * Gets the description. + * + * @return the description + */ + public String getDescription() { + return description; + } + + /** + * Gets the first state. + * + * @return the first state + */ + public String getFirstState() { + return firstState; + } + + /** + * Gets the template. + * + * @return the template + */ + public String getTemplate() { + return template; + } + + /** + * Gets the states. + * + * @return the states + */ + public Map getStates() { + return states; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "Policy [name=" + name + ", version=" + version + ", uuid=" + uuid + ", description=" + description + + ", firstState=" + firstState + ", template=" + template + ", states=" + states + "]"; + } + +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanState.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanState.java new file mode 100644 index 000000000..9472c3b89 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanState.java @@ -0,0 +1,136 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import java.util.Arrays; +import java.util.Map; + +import javax.xml.bind.annotation.XmlType; + +/** + * The State Bean. + */ +@XmlType +public class BeanState extends BeanBase { + + private String name = null; + private BeanKeyRef trigger = null; + private BeanKeyRef defaultTask = null; + private BeanKeyRef[] contexts = null; + private BeanLogic taskSelectionLogic = null; + private Map tasks = null; + private Map finalizers = null; + private Map stateOutputs = null; + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Gets the trigger. + * + * @return the trigger + */ + public BeanKeyRef getTrigger() { + return trigger; + } + + /** + * Gets the contexts. + * + * @return the contexts + */ + public BeanKeyRef[] getContexts() { + return contexts; + } + + /** + * Gets the task selection logic. + * + * @return the task selection logic + */ + public BeanLogic getTaskSelectionLogic() { + return taskSelectionLogic; + } + + /** + * Gets the tasks. + * + * @return the tasks + */ + public Map getTasks() { + return tasks; + } + + /** + * Gets the finalizers. + * + * @return the finalizers + */ + public Map getFinalizers() { + return finalizers; + } + + /** + * Gets the state outputs. + * + * @return the state outputs + */ + public Map getStateOutputs() { + return stateOutputs; + } + + /** + * Gets the default task. + * + * @return the default task + */ + public BeanKeyRef getDefaultTask() { + return defaultTask; + } + + /** + * Sets the default task. + * + * @param defaultTask the new default task + */ + public void setDefaultTask(final BeanKeyRef defaultTask) { + this.defaultTask = defaultTask; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "State [name=" + name + ", trigger=" + trigger + ", defaultTask=" + defaultTask + ", contexts=" + + Arrays.toString(contexts) + ", taskSelectionLogic=" + taskSelectionLogic + ", tasks=" + tasks + + ", finalizers=" + finalizers + ", stateOutputs=" + stateOutputs + "]"; + } + +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanStateOutput.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanStateOutput.java new file mode 100644 index 000000000..9d60897d4 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanStateOutput.java @@ -0,0 +1,62 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import javax.xml.bind.annotation.XmlType; + +/** + * The StateOutput Bean. + */ +@XmlType +public class BeanStateOutput extends BeanBase { + + private BeanKeyRef event = null; + private String nextState = null; + + /** + * Gets the event. + * + * @return the event + */ + public BeanKeyRef getEvent() { + return event; + } + + /** + * Gets the next state. + * + * @return the next state + */ + public String getNextState() { + return nextState; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "StateOutput [event=" + event + ", nextState=" + nextState + "]"; + } + +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanStateTaskRef.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanStateTaskRef.java new file mode 100644 index 000000000..c82bd7828 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanStateTaskRef.java @@ -0,0 +1,71 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import javax.xml.bind.annotation.XmlType; + +/** + * The State Task Reference Bean. + */ +@XmlType +public class BeanStateTaskRef extends BeanBase { + private BeanKeyRef task = null; + private String outputType = null; + private String outputName = null; + + /** + * Gets the task. + * + * @return the task + */ + public BeanKeyRef getTask() { + return task; + } + + /** + * Gets the output type. + * + * @return the output type + */ + public String getOutputType() { + return outputType; + } + + /** + * Gets the output name. + * + * @return the output name + */ + public String getOutputName() { + return outputName; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "StateTaskRef [task=" + task + ", outputType=" + outputType + ", outputName=" + outputName + "]"; + } + +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanTask.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanTask.java new file mode 100644 index 000000000..f9c1706cf --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanTask.java @@ -0,0 +1,135 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import java.util.Arrays; +import java.util.Map; + +import javax.xml.bind.annotation.XmlType; + +/** + * The Task Bean. + */ +@XmlType +public class BeanTask extends BeanBase { + private String name = null; + private String version = null; + private String uuid = null; + private String description = null; + private BeanLogic taskLogic = null; + private Map inputFields = null; + private Map outputFields = null; + private Map parameters = null; + private BeanKeyRef[] contexts = null; + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Gets the version. + * + * @return the version + */ + public String getVersion() { + return version; + } + + /** + * Gets the uuid. + * + * @return the uuid + */ + public String getUuid() { + return uuid; + } + + /** + * Gets the description. + * + * @return the description + */ + public String getDescription() { + return description; + } + + /** + * Gets the task logic. + * + * @return the task logic + */ + public BeanLogic getTaskLogic() { + return taskLogic; + } + + /** + * Gets the input fields. + * + * @return the input fields + */ + public Map getInputFields() { + return inputFields; + } + + /** + * Gets the output fields. + * + * @return the output fields + */ + public Map getOutputFields() { + return outputFields; + } + + /** + * Gets the parameters. + * + * @return the parameters + */ + public Map getParameters() { + return parameters; + } + + /** + * Gets the contexts. + * + * @return the contexts + */ + public BeanKeyRef[] getContexts() { + return contexts; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "BeanTask [name=" + name + ", version=" + version + ", uuid=" + uuid + ", description=" + description + + ", taskLogic=" + taskLogic + ", inputFields=" + inputFields + ", outputFields=" + outputFields + + ", parameters=" + parameters + ", contexts=" + Arrays.toString(contexts) + "]"; + } +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanTaskParameter.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanTaskParameter.java new file mode 100644 index 000000000..33b70056e --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanTaskParameter.java @@ -0,0 +1,62 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import javax.xml.bind.annotation.XmlType; + +/** + * The Task Parameter Bean. + */ +@XmlType +public class BeanTaskParameter extends BeanBase { + + private String parameterName = null; + private String defaultValue = null; + + /** + * Gets the parameter name. + * + * @return the parameter name + */ + public String getParameterName() { + return parameterName; + } + + /** + * Gets the default value. + * + * @return the default value + */ + public String getDefaultValue() { + return defaultValue; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TaskParameter [parameterName=" + parameterName + ", defaultValue=" + defaultValue + "]"; + } + +} diff --git a/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/package-info.java b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/package-info.java new file mode 100644 index 000000000..9107f6af9 --- /dev/null +++ b/client/client-editor/src/main/java/org/onap/policy/apex/client/editor/rest/handling/bean/package-info.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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========================================================= + */ + +/** + * Contains JSON/XML serializable beans for use with the REST interface. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.client.editor.rest.handling.bean; diff --git a/client/client-editor/src/main/resources/templates/PeriodicEventTemplate.json b/client/client-editor/src/main/resources/templates/PeriodicEventTemplate.json new file mode 100644 index 000000000..a2656515c --- /dev/null +++ b/client/client-editor/src/main/resources/templates/PeriodicEventTemplate.json @@ -0,0 +1,11 @@ +{ + "name": "PeriodicEvent", + "version": "0.0.1", + "uuid": "40f28ab0-bc27-11e8-a355-529269fb1459", + "description": "Default Periodic Event", + "source": "System", + "target": "Apex", + "nameSpace": "org.onap.policy.apex.domains.aadm.events", + "parameters": { + } +} diff --git a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/TestApexEditorRestResource.java b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/TestApexEditorRestResource.java deleted file mode 100644 index 4d45289b1..000000000 --- a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/TestApexEditorRestResource.java +++ /dev/null @@ -1,1515 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -import java.io.IOException; - -import javax.ws.rs.client.Entity; -import javax.ws.rs.core.Application; -import javax.ws.rs.core.MediaType; - -import org.glassfish.jersey.server.ResourceConfig; -import org.glassfish.jersey.test.JerseyTest; -import org.junit.Test; -import org.onap.policy.apex.model.modelapi.ApexApiResult; -import org.onap.policy.apex.model.modelapi.ApexApiResult.Result; -import org.onap.policy.apex.model.utilities.TextFileUtils; - -/** - * Test Apex Editor Rest Resource. - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestApexEditorRestResource extends JerseyTest { - @Override - protected Application configure() { - return new ResourceConfig(ApexEditorRestResource.class); - } - - @Test - public void testSessionCreate() { - ApexApiResult result = target("editor/-2/Session/Create").request().get(ApexApiResult.class); - assertEquals(Result.FAILED, result.getResult()); - - result = target("editor/-1/Session/Create").request().get(ApexApiResult.class); - assertEquals(Result.SUCCESS, result.getResult()); - final int sessionId = new Integer(result.getMessages().get(0)); - - result = target("editor/-1/Session/Create").request().get(ApexApiResult.class); - assertEquals(Result.SUCCESS, result.getResult()); - - final int corruptSessionId = ApexEditorRestResource.createCorruptSession(); - - try { - target("editor/" + corruptSessionId + "/Model/Analyse").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/Model/Analyse").request().get(ApexApiResult.class); - assertEquals(Result.SUCCESS, result.getResult()); - result = target("editor/-12345/Model/Analyse").request().get(ApexApiResult.class); - assertEquals(Result.FAILED, result.getResult()); - result = target("editor/12345/Model/Analyse").request().get(ApexApiResult.class); - assertEquals(Result.FAILED, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Model/Validate").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/Model/Validate").request().get(ApexApiResult.class); - assertEquals(Result.FAILED, result.getResult()); - result = target("editor/-12345/Model/Validate").request().get(ApexApiResult.class); - assertEquals(Result.FAILED, result.getResult()); - result = target("editor/12345/Model/Validate").request().get(ApexApiResult.class); - assertEquals(Result.FAILED, result.getResult()); - - final String modelString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002699\"," - + "\"description\" : \"A description of the model\"" + "}"; - final Entity csEntity = Entity.entity(modelString, MediaType.APPLICATION_JSON); - result = target("editor/-12345/Model/Create").request().post(csEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/-12345/Model/Create").request().post(csEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/1234545/Model/Create").request().post(csEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Model/Create").request().post(csEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Model/Create").request().post(csEntity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/-12345/Model/Update").request().put(csEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/-12345/Model/Update").request().put(csEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/1234545/Model/Update").request().put(csEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Model/Update").request().put(csEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Model/Update").request().put(csEntity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - try { - result = target("editor/" + corruptSessionId + "/Model/GetKey").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/Model/GetKey").request().get(ApexApiResult.class); - assertEquals(Result.SUCCESS, result.getResult()); - result = target("editor/-12345/Model/GetKey").request().get(ApexApiResult.class); - assertEquals(Result.FAILED, result.getResult()); - result = target("editor/12345/Model/GetKey").request().get(ApexApiResult.class); - assertEquals(Result.FAILED, result.getResult()); - - try { - result = target("editor/" + corruptSessionId + "/Model/Get").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/Model/Get").request().get(ApexApiResult.class); - assertEquals(Result.SUCCESS, result.getResult()); - result = target("editor/-12345/Model/Get").request().get(ApexApiResult.class); - assertEquals(Result.FAILED, result.getResult()); - result = target("editor/12345/Model/Get").request().get(ApexApiResult.class); - assertEquals(Result.FAILED, result.getResult()); - - try { - result = target("editor/" + corruptSessionId + "/Model/Download").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/Model/Download").request().get(ApexApiResult.class); - assertEquals(Result.SUCCESS, result.getResult()); - try { - target("editor/-12345/Model/Download").request().get(ApexApiResult.class); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - try { - target("editor/12345/Model/Download").request().get(ApexApiResult.class); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - try { - result = target("editor/" + corruptSessionId + "/KeyInformation/Get").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/KeyInformation/Get").request().get(ApexApiResult.class); - assertEquals(Result.SUCCESS, result.getResult()); - result = target("editor/-12345/KeyInformation/Get").request().get(ApexApiResult.class); - assertEquals(Result.FAILED, result.getResult()); - result = target("editor/12345/KeyInformation/Get").request().get(ApexApiResult.class); - assertEquals(Result.FAILED, result.getResult()); - - try { - result = target("editor/" + corruptSessionId + "/Model/Delete").request().delete(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/Model/Delete").request().delete(ApexApiResult.class); - assertEquals(Result.SUCCESS, result.getResult()); - result = target("editor/-12345/Model/Delete").request().delete(ApexApiResult.class); - assertEquals(Result.FAILED, result.getResult()); - result = target("editor/12345/Model/Delete").request().delete(ApexApiResult.class); - assertEquals(Result.FAILED, result.getResult()); - } - - @Test - public void testContextSchema() throws IOException { - ApexApiResult result = target("editor/-1/Session/Create").request().get(ApexApiResult.class); - assertEquals(Result.SUCCESS, result.getResult()); - final int sessionId = new Integer(result.getMessages().get(0)); - - final int corruptSessionId = ApexEditorRestResource.createCorruptSession(); - - result = target("editor/-12345/Validate/ContextSchema").request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Validate/ContextSchema").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/Validate/ContextSchema").request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - - result = target("editor/" + sessionId + "/Validate/ContextSchema").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - - result = target("editor/" + sessionId + "/Validate/ContextSchema").queryParam("name", "%%%$£") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - final String modelString = TextFileUtils.getTextFileAsString("src/test/resources/models/PolicyModel.json"); - - Entity modelEntity = Entity.entity("Somewhere over the rainbow", MediaType.APPLICATION_JSON); - result = target("editor/" + -12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + 12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - modelEntity = Entity.entity("", MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - modelEntity = Entity.entity(modelString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/ContextSchema/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/ContextSchema/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - String csString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," - + "\"schemaFlavour\" : \"Java\"," + "\"schemaDefinition\" : \"java.lang.String\"," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - Entity csEntity = Entity.entity(csString, MediaType.APPLICATION_JSON); - result = target("editor/-12345/ContextSchema/Create").request().post(csEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/1234545/ContextSchema/Create").request().post(csEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/ContextSchema/Create").request().post(csEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/ContextSchema/Create").request().post(csEntity, - ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - csString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," - + "\"schemaFlavour\" : \"Java\"," + "\"schemaDefinition\" : \"my.perfect.String\"," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - csEntity = Entity.entity(csString, MediaType.APPLICATION_JSON); - result = target("editor/-12345/ContextSchema/Update").request().put(csEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/1234545/ContextSchema/Update").request().put(csEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/ContextSchema/Update").request().put(csEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/ContextSchema/Update").request().put(csEntity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/ContextSchema/Get").queryParam("name", "Hello") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/ContextSchema/Get").queryParam("name", "NonExistant") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - result = target("editor/-123345/ContextSchema/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/123345/ContextSchema/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/ContextSchema/Get").queryParam("name", "Hello") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/Validate/ContextSchema").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/ContextSchema/Delete").queryParam("name", "Hello") - .queryParam("version", "0.0.2").request().delete(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/-123345/ContextSchema/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/123345/ContextSchema/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/ContextSchema/Delete").queryParam("name", "Hello") - .queryParam("version", "0.0.2").request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/ContextSchema/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - } - - @Test - public void testContextAlbum() throws IOException { - ApexApiResult result = target("editor/-1/Session/Create").request().get(ApexApiResult.class); - assertEquals(Result.SUCCESS, result.getResult()); - final int sessionId = new Integer(result.getMessages().get(0)); - final int corruptSessionId = ApexEditorRestResource.createCorruptSession(); - - result = target("editor/-12345/Validate/ContextAlbum").request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Validate/ContextAlbum").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/Validate/ContextAlbum").request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - result = target("editor/" + sessionId + "/Validate/ContextAlbum").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - result = target("editor/" + sessionId + "/Validate/ContextAlbum").queryParam("name", "%%%$£") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - final String modelString = TextFileUtils.getTextFileAsString("src/test/resources/models/PolicyModel.json"); - - Entity modelEntity = Entity.entity("Somewhere over the rainbow", MediaType.APPLICATION_JSON); - result = target("editor/" + -12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + 12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - modelEntity = Entity.entity("", MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - modelEntity = Entity.entity(modelString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/ContextAlbum/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - String caString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," - + "\"scope\" : \"Domain\"," + "\"writeable\" : false," - + "\"itemSchema\" : {\"name\" : \"StringType\", \"version\" : \"0.0.1\"}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - Entity caEntity = Entity.entity(caString, MediaType.APPLICATION_JSON); - result = target("editor/-12345/ContextAlbum/Create").request().post(caEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/1234545/ContextAlbum/Create").request().post(caEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/ContextAlbum/Create").request().post(caEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/ContextAlbum/Create").request().post(caEntity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - caString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," - + "\"scope\" : \"Global\"," + "\"writeable\" : false," - + "\"itemSchema\" : {\"name\" : \"StringType\", \"version\" : \"0.0.1\"}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - caEntity = Entity.entity(caString, MediaType.APPLICATION_JSON); - result = target("editor/-12345/ContextAlbum/Update").request().put(caEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/1234545/ContextAlbum/Update").request().put(caEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/ContextAlbum/Update").request().put(caEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/ContextAlbum/Update").request().put(caEntity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - try { - target("editor/" + corruptSessionId + "/ContextAlbum/Get").queryParam("name", "Hello") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/ContextAlbum/Get").queryParam("name", "Hello") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/ContextAlbum/Get").queryParam("name", "IDontExist") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - result = target("editor/-123345/ContextAlbum/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/123345/ContextAlbum/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - result = target("editor/" + sessionId + "/Validate/ContextAlbum").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/ContextAlbum/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/-123345/ContextAlbum/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/123345/ContextAlbum/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/ContextAlbum/Delete").queryParam("name", "Hello") - .queryParam("version", "0.0.2").request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/ContextAlbum/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - } - - @Test - public void testEvent() throws IOException { - final int corruptSessionId = ApexEditorRestResource.createCorruptSession(); - - ApexApiResult result = target("editor/-1/Session/Create").request().get(ApexApiResult.class); - assertEquals(Result.SUCCESS, result.getResult()); - final int sessionId = new Integer(result.getMessages().get(0)); - - result = target("editor/-12345/Validate/Event").request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - result = target("editor/" + sessionId + "/Validate/Event").request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Validate/Event").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/Validate/Event").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - result = target("editor/" + sessionId + "/Validate/Event").queryParam("name", "%%%$£") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - final String modelString = TextFileUtils.getTextFileAsString("src/test/resources/models/PolicyModel.json"); - - Entity modelEntity = Entity.entity("Somewhere over the rainbow", MediaType.APPLICATION_JSON); - result = target("editor/" + -12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + 12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - modelEntity = Entity.entity("", MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - modelEntity = Entity.entity(modelString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/Event/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - String entityString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," - + "\"namespace\" : \"somewhere.over.the.rainbow\"," + "\"source\" : \"beginning\"," - + "\"target\" : \"end\"," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - Entity entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/-12345/Event/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/1234545/Event/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Event/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/Event/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_EXISTS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Event/Create").request().post(entity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - entityString = "{" + "\"name\" : \"Hiya\"," + "\"version\" : \"0.0.2\"," - + "\"namespace\" : \"somewhere.over.the.rainbow\"," + "\"source\" : \"beginning\"," - + "\"target\" : \"end\"," + "\"parameters\" : {}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Event/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - entityString = "{" + "\"name\" : \"HowsItGoing\"," + "\"version\" : \"0.0.2\"," - + "\"namespace\" : \"somewhere.over.the.rainbow\"," + "\"source\" : \"beginning\"," - + "\"target\" : \"end\"," - + "\"parameters\" : {\"Par0\" : {\"name\" : \"StringType\", \"version\" : \"0.0.1\", " - + "\"localName\" : \"Par0\", \"optional\" : false}}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Event/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - entityString = "{" + "\"name\" : \"Hi\"," + "\"version\" : \"0.0.2\"," - + "\"namespace\" : \"somewhere.over.the.rainbow\"," + "\"source\" : \"beginning\"," - + "\"target\" : \"end\"," + "\"parameters\" : {\"Par0\" : null}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Event/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"GoodDay\"," + "\"version\" : \"0.0.2\"," - + "\"namespace\" : \"somewhere.over.the.rainbow\"," + "\"source\" : \"beginning\"," - + "\"target\" : \"end\"," - + "\"parameters\" : {\"Par0\" : {\"name\" : \"NonExistantType\", \"version\" : \"0.0.1\", " - + "\"localName\" : \"Par0\", \"optional\" : false}}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Event/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," - + "\"namespace\" : \"somewhere.over.someone.elses.rainbow\"," - + "\"source\" : \"start\"," + "\"target\" : \"finish\"," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/-12345/Event/Update").request().put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/1234545/Event/Update").request().put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Event/Update").request().put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Event/Update").request().put(entity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - entityString = "{" + "\"name\" : null," + "\"version\" : \"0.0.2\"," - + "\"namespace\" : \"somewhere.over.someone.elses.rainbow\"," - + "\"source\" : \"start\"," + "\"target\" : \"finish\"," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Event/Update").request().put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"NonExistantEvent\"," + "\"version\" : \"0.0.2\"," - + "\"namespace\" : \"somewhere.over.someone.elses.rainbow\"," - + "\"source\" : \"start\"," + "\"target\" : \"finish\"," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Event/Update").request().put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - - result = target("editor/" + sessionId + "/Event/Get").queryParam("name", "Hello") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/Event/Get").queryParam("name", "IDontExist") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - result = target("editor/-123345/Event/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/123345/Event/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Event/Get").queryParam("name", "Hello") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/Validate/Event").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/-12345/Validate/Event").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/12345/Validate/Event").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Event/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/-123345/Event/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/123345/Event/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Event/Delete").queryParam("name", "Hello") - .queryParam("version", "0.0.2").request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/Event/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - } - - @Test - public void testTask() throws IOException { - final int corruptSessionId = ApexEditorRestResource.createCorruptSession(); - - ApexApiResult result = target("editor/-1/Session/Create").request().get(ApexApiResult.class); - assertEquals(Result.SUCCESS, result.getResult()); - final int sessionId = new Integer(result.getMessages().get(0)); - - result = target("editor/-12345/Validate/Task").request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - result = target("editor/" + sessionId + "/Validate/Task").request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Validate/Task").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/Validate/Task").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - result = target("editor/" + sessionId + "/Validate/Task").queryParam("name", "%%%$£") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - final String modelString = TextFileUtils.getTextFileAsString("src/test/resources/models/PolicyModel.json"); - - Entity modelEntity = Entity.entity("Somewhere over the rainbow", MediaType.APPLICATION_JSON); - result = target("editor/" + -12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + 12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - modelEntity = Entity.entity("", MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - modelEntity = Entity.entity(modelString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/Event/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - String entityString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - Entity entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/-12345/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/1234545/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_EXISTS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - entityString = "{" + "\"name\" : \"Hiya\"," + "\"version\" : \"0.0.2\"," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - entityString = "{" + "\"name\" : \"HowsItGoing\"," + "\"version\" : \"0.0.2\"," - + "\"inputFields\" : {\"IField0\" : {\"name\" : \"StringType\", \"version\" : \"0.0.1\", " - + "\"localName\" : \"IField0\", \"optional\" : false}}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - entityString = "{" + "\"name\" : \"Hi\"," + "\"version\" : \"0.0.2\"," - + "\"inputFields\" : {\"IField0\" : null}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"GoodDay\"," + "\"version\" : \"0.0.2\"," - + "\"inputFields\" : {\"IField0\" : {\"name\" : \"NonExistantType\", \"version\" : \"0.0.1\", " - + "\"localName\" : \"IField0\", \"optional\" : false}}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - - entityString = "{" + "\"name\" : \"Howdy\"," + "\"version\" : \"0.0.2\"," - + "\"inputFields\" : {\"IField0\" : {\"name\" : \"NonExistantType\", \"version\" : \"0.0.1\", " - + "\"localName\" : \"NotIField0\", \"optional\" : false}}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"HowsItGoing2\"," + "\"version\" : \"0.0.2\"," - + "\"outputFields\" : {\"OField0\" : {\"name\" : \"StringType\", \"version\" : \"0.0.1\", " - + "\"localName\" : \"OField0\", \"optional\" : false}}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - entityString = "{" + "\"name\" : \"Hi2\"," + "\"version\" : \"0.0.2\"," - + "\"outputFields\" : {\"OField0\" : null}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"GoodDay2\"," + "\"version\" : \"0.0.2\"," - + "\"outputFields\" : {\"OField0\" : {\"name\" : \"NonExistantType\", \"version\" : \"0.0.1\"," - + " \"localName\" : \"OField0\", \"optional\" : false}}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - - entityString = "{" + "\"name\" : \"Howdy2\"," + "\"version\" : \"0.0.2\"," - + "\"outputFields\" : {\"OField0\" : {\"name\" : \"NonExistantType\", \"version\" : \"0.0.1\", " - + "\"localName\" : \"NotOField0\", \"optional\" : false}}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"HowsItGoing3\"," + "\"version\" : \"0.0.2\"," - + "\"taskLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons," - + " lots of lime\"}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - entityString = "{" + "\"name\" : \"Hi3\"," + "\"version\" : \"0.0.2\"," - + "\"taskLogic\" : null," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - entityString = "{" + "\"name\" : \"GoodDay3\"," + "\"version\" : \"0.0.2\"," - + "\"namespace\" : \"somewhere.over.the.rainbow\"," + "\"source\" : \"beginning\"," - + "\"target\" : \"end\"," - + "\"taskLogic\" : {\"logicFlavour\" : \"UNDEFINED\", \"logic\" : \"lots of lemons," - + " lots of lime\"}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - entityString = "{" + "\"name\" : \"Howdy3\"," + "\"version\" : \"0.0.2\"," - + "\"taskLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : null}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"HowsItGoing4\"," + "\"version\" : \"0.0.2\"," - + "\"parameters\" : {\"Par0\" : {\"parameterName\" : \"Par0\", " - + "\"defaultValue\" : \"Parameter Defaultvalue\"}}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - entityString = "{" + "\"name\" : \"Hi4\"," + "\"version\" : \"0.0.2\"," - + "\"parameters\" : {\"Par0\" : null}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"GoodDay4\"," + "\"version\" : \"0.0.2\"," - + "\"parameters\" : {\"Par0\" : {\"parameterName\" : \"NotPar0\", \"defaultValue\" : " - + "\"Parameter Defaultvalue\"}}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"Howdy4\"," + "\"version\" : \"0.0.2\"," - + "\"parameters\" : {\"Par0\" : {\"parameterName\" : \"MyParameter\", \"defaultValue\" : null}}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"HowsItGoing5\"," + "\"version\" : \"0.0.2\"," - + "\"contexts\" : [{\"name\" : \"contextAlbum0\", \"version\" : \"0.0.1\"}]," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - entityString = "{" + "\"name\" : \"Hi5\"," + "\"version\" : \"0.0.2\"," - + "\"contexts\" : []," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - entityString = "{" + "\"name\" : \"GoodDay5\"," + "\"version\" : \"0.0.2\"," - + "\"contexts\" : [{\"name\" : \"NonExistantType\", \"version\" : \"0.0.1\"}]," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"Howdy5\"," + "\"version\" : \"0.0.2\"," - + "\"contexts\" : [{\"name\" : null, \"version\" : \"0.0.1\"}]," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/-12345/Task/Update").request().put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/1234545/Task/Update").request().put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Task/Update").request().put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Task/Update").request().put(entity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - entityString = "{" + "\"name\" : null," + "\"version\" : \"0.0.2\"," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Update").request().put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"NonExistantEvent\"," + "\"version\" : \"0.0.2\"," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Task/Update").request().put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - - result = target("editor/" + sessionId + "/Task/Get").queryParam("name", "Hello") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/Task/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/Task/Get").queryParam("name", "IDontExist") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - result = target("editor/-123345/Task/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/123345/Task/Get").queryParam("name", (String) null).queryParam("version", (String) null) - .request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Task/Get").queryParam("name", "Hello") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/Validate/Event").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/-12345/Validate/Event").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/12345/Validate/Event").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Task/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/-123345/Task/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/123345/Task/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Task/Delete").queryParam("name", "Hello") - .queryParam("version", "0.0.2").request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/Task/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - } - - @Test - public void testPolicy() throws IOException { - final int corruptSessionId = ApexEditorRestResource.createCorruptSession(); - - ApexApiResult result = target("editor/-1/Session/Create").request().get(ApexApiResult.class); - assertEquals(Result.SUCCESS, result.getResult()); - final int sessionId = new Integer(result.getMessages().get(0)); - - result = target("editor/-12345/Model/Validate").request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - result = target("editor/" + sessionId + "/Model/Validate").request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Model/Validate").request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/Model/Validate").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - result = target("editor/" + sessionId + "/Model/Validate").queryParam("name", "%%%$£") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - final String modelString = TextFileUtils.getTextFileAsString("src/test/resources/models/PolicyModel.json"); - - Entity modelEntity = Entity.entity("Somewhere over the rainbow", MediaType.APPLICATION_JSON); - result = target("editor/" + -12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + 12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - modelEntity = Entity.entity("", MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - modelEntity = Entity.entity(modelString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/Event/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - String entityString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" - + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - Entity entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/-12345/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/1234545/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_EXISTS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - entityString = "{" + "\"name\" : \"GoodTaSeeYa\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : null," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"HelloAnother\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" - + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello2\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," - + " \"trigger\" : null," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" - + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello3\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : null," + " \"stateOutputs\" : {" + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello4\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"stateOutputs\" : null," + " \"tasks\" : {" + " \"tr0\" : {" - + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello5\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" - + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : null" + " }" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello6\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"IDontExist\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" - + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello7\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : null" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello8\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" - + " \"so0\" : {" - + " \"event\" : {\"name\" : \"IDontExist\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello9\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" - + " \"so0\" : {" + " \"event\" : null," + " \"nextState\" : null" + " }" - + " }," + " \"tasks\" : {" + " \"tr0\" : {" - + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - System.err.println(result); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello10\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" - + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" - + " \"task\" : {\"name\" : \"IDontExist\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello11\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" - + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" + " \"task\" : null," + " \"outputType\" : \"DIRECT\"," - + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello12\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" - + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " - + "lots of lime\"}," + " \"stateOutputs\" : {" + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" - + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" - + " }" + "}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello13\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" - + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : null}," - + " \"stateOutputs\" : {" + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" - + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" - + " }" + "}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello14\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" - + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " - + "lots of lime\"}," - + " \"contexts\" : [{\"name\" : \"contextAlbum0\", \"version\" : \"0.0.1\"}]," - + " \"stateOutputs\" : {" + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" - + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" - + " }" + "}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello15\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" - + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " - + "lots of lime\"}," - + " \"contexts\" : [{\"name\" : \"IDontExist\", \"version\" : \"0.0.1\"}]," - + " \"stateOutputs\" : {" + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" - + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" - + " }" + "}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello16\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" - + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " - + "lots of lime\"}," - + " \"contexts\" : [null]," + " \"stateOutputs\" : {" + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" - + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" - + " }" + "}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello17\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" - + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " - + "lots of lime\"}," - + " \"contexts\" : [{\"name\" : \"contextAlbum0\", \"version\" : \"0.0.1\"}]," - + " \"stateOutputs\" : {" + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" - + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }," - + " \"finalizers\" : {" - + " \"sf0\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " - + "lots of lime\"}" - + " }" + " }" + "}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello18\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" - + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " - + "lots of lime\"}," - + " \"contexts\" : [{\"name\" : \"contextAlbum0\", \"version\" : \"0.0.1\"}]," - + " \"stateOutputs\" : {" + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" - + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }," - + " \"finalizers\" : {" + " \"sf0\" : null" + " }" + " }" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"Hello19\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" - + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " - + "lots of lime\"}," - + " \"contexts\" : [{\"name\" : \"contextAlbum0\", \"version\" : \"0.0.1\"}]," - + " \"stateOutputs\" : {" + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" - + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }," - + " \"finalizers\" : {" - + " \"sf0\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : null}" + " }" + " }" - + "}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"HelloAnother\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" - + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A better description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/-12345/Policy/Update").request().put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/1234545/Policy/Update").request().put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Policy/Update").request().put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - result = target("editor/" + sessionId + "/Policy/Update").queryParam("firstStatePeriodic", "true").request() - .put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Policy/Update").request().put(entity, ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - entityString = "{" + "\"name\" : null," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" - + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A better description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Update").request().put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - entityString = "{" + "\"name\" : \"IDontExist\"," + "\"version\" : \"0.0.2\"," - + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," - + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," - + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" - + " \"so0\" : {" - + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," - + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" - + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," - + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," - + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," - + "\"description\" : \"A better description of hello\"" + "}"; - entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); - result = target("editor/" + sessionId + "/Policy/Update").request().put(entity, ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - - result = target("editor/" + sessionId + "/Policy/Get").queryParam("name", "Hello") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/Policy/Get").queryParam("name", "IDontExist") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); - result = target("editor/" + sessionId + "/Policy/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/-123345/Policy/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/123345/Policy/Get").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Policy/Get").queryParam("name", "Hello") - .queryParam("version", (String) null).request().get(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/" + sessionId + "/Validate/Event").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/-12345/Validate/Event").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/12345/Validate/Event").queryParam("name", (String) null) - .queryParam("version", (String) null).request().get(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - - try { - target("editor/" + corruptSessionId + "/Policy/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - } catch (final Exception e) { - assertEquals("HTTP 500 Request failed.", e.getMessage()); - } - - result = target("editor/-123345/Policy/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/123345/Policy/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.FAILED, result.getResult()); - result = target("editor/" + sessionId + "/Policy/Delete").queryParam("name", "Hello") - .queryParam("version", "0.0.2").request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - result = target("editor/" + sessionId + "/Policy/Delete").queryParam("name", (String) null) - .queryParam("version", (String) null).request().delete(ApexApiResult.class); - assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); - } -} diff --git a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/bean/BeanFake.java b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/bean/BeanFake.java deleted file mode 100644 index c166dc267..000000000 --- a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/bean/BeanFake.java +++ /dev/null @@ -1,49 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import javax.xml.bind.annotation.XmlType; - -/** - * The Event Bean. - */ -@XmlType -public class BeanFake extends BeanBase { - private String name = null; - private String version = null; - private String field1 = null; - private int field2 = 0; - private int field3 = 0; - - public String getName() { - field1 = name; - return field1; - } - - public String getVersion() { - return version; - } - - public int getField2() { - field3 = field2; - return field3; - } -} diff --git a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/bean/TestBeans.java b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/bean/TestBeans.java deleted file mode 100644 index df08dbaf2..000000000 --- a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/bean/TestBeans.java +++ /dev/null @@ -1,88 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.client.editor.rest.bean; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; - -import org.junit.Test; - -/** - * Test the beans. - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestBeans { - - @Test - public void testBeans() { - assertNotNull(new BeanEvent().toString()); - assertNotNull(new BeanState().toString()); - assertNotNull(new BeanContextAlbum().toString()); - assertNotNull(new BeanPolicy().toString()); - assertNotNull(new BeanContextSchema().toString()); - assertNotNull(new BeanField().toString()); - assertNotNull(new BeanModel().toString()); - assertNotNull(new BeanLogic().toString()); - assertNotNull(new BeanStateOutput().toString()); - assertNotNull(new BeanTaskParameter().toString()); - assertNotNull(new BeanKeyRef().toString()); - assertNotNull(new BeanStateTaskRef().toString()); - assertNotNull(new BeanTask().toString()); - - final BeanState beanState = new BeanState(); - assertNull(beanState.getName()); - beanState.setDefaultTask(new BeanKeyRef()); - assertNotNull(beanState.getDefaultTask()); - - final BeanEvent beanEvent = new BeanEvent(); - assertNull(beanEvent.get("name")); - - final BeanFake beanFake = new BeanFake(); - assertNull(beanFake.get("name")); - assertNull(beanFake.get("field1")); - - try { - beanFake.get("iDontExist"); - fail("test should throw an exception here"); - } catch (final IllegalArgumentException e) { - assertNotNull(e); - } - try { - beanFake.get("nome"); - fail("test should throw an exception here"); - } catch (final IllegalArgumentException e) { - assertNotNull(e); - } - try { - beanFake.get("field2"); - fail("test should throw an exception here"); - } catch (final IllegalArgumentException e) { - assertNotNull(e); - } - try { - beanFake.get("field3"); - fail("test should throw an exception here"); - } catch (final IllegalArgumentException e) { - assertNotNull(e); - } - } -} diff --git a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/TestApexEditorRestResource.java b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/TestApexEditorRestResource.java new file mode 100644 index 000000000..3585959f5 --- /dev/null +++ b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/TestApexEditorRestResource.java @@ -0,0 +1,1506 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.MediaType; + +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.junit.Test; +import org.onap.policy.apex.model.modelapi.ApexApiResult; +import org.onap.policy.apex.model.modelapi.ApexApiResult.Result; +import org.onap.policy.apex.model.utilities.TextFileUtils; + +/** + * Test Apex Editor Rest Resource. + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TestApexEditorRestResource extends JerseyTest { + @Override + protected Application configure() { + return new ResourceConfig(ApexEditorRestResource.class); + } + + @Test + public void testSessionCreate() { + ApexApiResult result = target("editor/-2/Session/Create").request().get(ApexApiResult.class); + assertEquals(Result.FAILED, result.getResult()); + + result = target("editor/-1/Session/Create").request().get(ApexApiResult.class); + assertEquals(Result.SUCCESS, result.getResult()); + final int sessionId = new Integer(result.getMessages().get(0)); + + result = target("editor/-1/Session/Create").request().get(ApexApiResult.class); + assertEquals(Result.SUCCESS, result.getResult()); + + final int corruptSessionId = ApexEditorRestResource.createCorruptSession(); + + try { + target("editor/" + corruptSessionId + "/Model/Analyse").request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/Model/Analyse").request().get(ApexApiResult.class); + assertEquals(Result.SUCCESS, result.getResult()); + result = target("editor/-12345/Model/Analyse").request().get(ApexApiResult.class); + assertEquals(Result.FAILED, result.getResult()); + result = target("editor/12345/Model/Analyse").request().get(ApexApiResult.class); + assertEquals(Result.FAILED, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Model/Validate").request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/Model/Validate").request().get(ApexApiResult.class); + assertEquals(Result.FAILED, result.getResult()); + result = target("editor/-12345/Model/Validate").request().get(ApexApiResult.class); + assertEquals(Result.FAILED, result.getResult()); + result = target("editor/12345/Model/Validate").request().get(ApexApiResult.class); + assertEquals(Result.FAILED, result.getResult()); + + final String modelString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002699\"," + + "\"description\" : \"A description of the model\"" + "}"; + final Entity csEntity = Entity.entity(modelString, MediaType.APPLICATION_JSON); + result = target("editor/-12345/Model/Create").request().post(csEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/-12345/Model/Create").request().post(csEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/1234545/Model/Create").request().post(csEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Model/Create").request().post(csEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Model/Create").request().post(csEntity, ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/-12345/Model/Update").request().put(csEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/-12345/Model/Update").request().put(csEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/1234545/Model/Update").request().put(csEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Model/Update").request().put(csEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Model/Update").request().put(csEntity, ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + try { + result = target("editor/" + corruptSessionId + "/Model/GetKey").request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/Model/GetKey").request().get(ApexApiResult.class); + assertEquals(Result.SUCCESS, result.getResult()); + result = target("editor/-12345/Model/GetKey").request().get(ApexApiResult.class); + assertEquals(Result.FAILED, result.getResult()); + result = target("editor/12345/Model/GetKey").request().get(ApexApiResult.class); + assertEquals(Result.FAILED, result.getResult()); + + try { + result = target("editor/" + corruptSessionId + "/Model/Get").request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/Model/Get").request().get(ApexApiResult.class); + assertEquals(Result.SUCCESS, result.getResult()); + result = target("editor/-12345/Model/Get").request().get(ApexApiResult.class); + assertEquals(Result.FAILED, result.getResult()); + result = target("editor/12345/Model/Get").request().get(ApexApiResult.class); + assertEquals(Result.FAILED, result.getResult()); + + String resultString = target("editor/" + corruptSessionId + "/Model/Download").request().get(String.class); + assertEquals("", resultString); + + resultString = target("editor/" + sessionId + "/Model/Download").request().get(String.class); + assertNotNull(resultString); + + resultString = target("editor/-12345/Model/Download").request().get(String.class); + assertEquals("", resultString); + + resultString = target("editor/12345/Model/Download").request().get(String.class); + assertEquals("", resultString); + + try { + result = target("editor/" + corruptSessionId + "/KeyInformation/Get").request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/KeyInformation/Get").request().get(ApexApiResult.class); + assertEquals(Result.SUCCESS, result.getResult()); + result = target("editor/-12345/KeyInformation/Get").request().get(ApexApiResult.class); + assertEquals(Result.FAILED, result.getResult()); + result = target("editor/12345/KeyInformation/Get").request().get(ApexApiResult.class); + assertEquals(Result.FAILED, result.getResult()); + + try { + result = target("editor/" + corruptSessionId + "/Model/Delete").request().delete(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/Model/Delete").request().delete(ApexApiResult.class); + assertEquals(Result.SUCCESS, result.getResult()); + result = target("editor/-12345/Model/Delete").request().delete(ApexApiResult.class); + assertEquals(Result.FAILED, result.getResult()); + result = target("editor/12345/Model/Delete").request().delete(ApexApiResult.class); + assertEquals(Result.FAILED, result.getResult()); + } + + @Test + public void testContextSchema() throws IOException { + ApexApiResult result = target("editor/-1/Session/Create").request().get(ApexApiResult.class); + assertEquals(Result.SUCCESS, result.getResult()); + final int sessionId = new Integer(result.getMessages().get(0)); + + final int corruptSessionId = ApexEditorRestResource.createCorruptSession(); + + result = target("editor/-12345/Validate/ContextSchema").request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Validate/ContextSchema").request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/Validate/ContextSchema").request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + result = target("editor/" + sessionId + "/Validate/ContextSchema").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + result = target("editor/" + sessionId + "/Validate/ContextSchema").queryParam("name", "%%%$£") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + final String modelString = TextFileUtils.getTextFileAsString("src/test/resources/models/PolicyModel.json"); + + Entity modelEntity = Entity.entity("Somewhere over the rainbow", MediaType.APPLICATION_JSON); + result = target("editor/" + -12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + 12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + modelEntity = Entity.entity("", MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + modelEntity = Entity.entity(modelString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/ContextSchema/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/ContextSchema/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + String csString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," + + "\"schemaFlavour\" : \"Java\"," + "\"schemaDefinition\" : \"java.lang.String\"," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + Entity csEntity = Entity.entity(csString, MediaType.APPLICATION_JSON); + result = target("editor/-12345/ContextSchema/Create").request().post(csEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/1234545/ContextSchema/Create").request().post(csEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/ContextSchema/Create").request().post(csEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/ContextSchema/Create").request().post(csEntity, + ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + csString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," + + "\"schemaFlavour\" : \"Java\"," + "\"schemaDefinition\" : \"my.perfect.String\"," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + csEntity = Entity.entity(csString, MediaType.APPLICATION_JSON); + result = target("editor/-12345/ContextSchema/Update").request().put(csEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/1234545/ContextSchema/Update").request().put(csEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/ContextSchema/Update").request().put(csEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/ContextSchema/Update").request().put(csEntity, ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/ContextSchema/Get").queryParam("name", "Hello") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/ContextSchema/Get").queryParam("name", "NonExistant") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + result = target("editor/-123345/ContextSchema/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/123345/ContextSchema/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/ContextSchema/Get").queryParam("name", "Hello") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/Validate/ContextSchema").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/ContextSchema/Delete").queryParam("name", "Hello") + .queryParam("version", "0.0.2").request().delete(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/-123345/ContextSchema/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/123345/ContextSchema/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/ContextSchema/Delete").queryParam("name", "Hello") + .queryParam("version", "0.0.2").request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/ContextSchema/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + } + + @Test + public void testContextAlbum() throws IOException { + ApexApiResult result = target("editor/-1/Session/Create").request().get(ApexApiResult.class); + assertEquals(Result.SUCCESS, result.getResult()); + final int sessionId = new Integer(result.getMessages().get(0)); + final int corruptSessionId = ApexEditorRestResource.createCorruptSession(); + + result = target("editor/-12345/Validate/ContextAlbum").request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Validate/ContextAlbum").request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/Validate/ContextAlbum").request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + result = target("editor/" + sessionId + "/Validate/ContextAlbum").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + result = target("editor/" + sessionId + "/Validate/ContextAlbum").queryParam("name", "%%%$£") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + final String modelString = TextFileUtils.getTextFileAsString("src/test/resources/models/PolicyModel.json"); + + Entity modelEntity = Entity.entity("Somewhere over the rainbow", MediaType.APPLICATION_JSON); + result = target("editor/" + -12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + 12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + modelEntity = Entity.entity("", MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + modelEntity = Entity.entity(modelString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/ContextAlbum/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + String caString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," + + "\"scope\" : \"Domain\"," + "\"writeable\" : false," + + "\"itemSchema\" : {\"name\" : \"StringType\", \"version\" : \"0.0.1\"}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + Entity caEntity = Entity.entity(caString, MediaType.APPLICATION_JSON); + result = target("editor/-12345/ContextAlbum/Create").request().post(caEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/1234545/ContextAlbum/Create").request().post(caEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/ContextAlbum/Create").request().post(caEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/ContextAlbum/Create").request().post(caEntity, ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + caString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," + + "\"scope\" : \"Global\"," + "\"writeable\" : false," + + "\"itemSchema\" : {\"name\" : \"StringType\", \"version\" : \"0.0.1\"}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + caEntity = Entity.entity(caString, MediaType.APPLICATION_JSON); + result = target("editor/-12345/ContextAlbum/Update").request().put(caEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/1234545/ContextAlbum/Update").request().put(caEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/ContextAlbum/Update").request().put(caEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/ContextAlbum/Update").request().put(caEntity, ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + try { + target("editor/" + corruptSessionId + "/ContextAlbum/Get").queryParam("name", "Hello") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/ContextAlbum/Get").queryParam("name", "Hello") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/ContextAlbum/Get").queryParam("name", "IDontExist") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + result = target("editor/-123345/ContextAlbum/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/123345/ContextAlbum/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + result = target("editor/" + sessionId + "/Validate/ContextAlbum").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/ContextAlbum/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/-123345/ContextAlbum/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/123345/ContextAlbum/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/ContextAlbum/Delete").queryParam("name", "Hello") + .queryParam("version", "0.0.2").request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/ContextAlbum/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + } + + @Test + public void testEvent() throws IOException { + final int corruptSessionId = ApexEditorRestResource.createCorruptSession(); + + ApexApiResult result = target("editor/-1/Session/Create").request().get(ApexApiResult.class); + assertEquals(Result.SUCCESS, result.getResult()); + final int sessionId = new Integer(result.getMessages().get(0)); + + result = target("editor/-12345/Validate/Event").request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + result = target("editor/" + sessionId + "/Validate/Event").request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Validate/Event").request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/Validate/Event").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + result = target("editor/" + sessionId + "/Validate/Event").queryParam("name", "%%%$£") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + final String modelString = TextFileUtils.getTextFileAsString("src/test/resources/models/PolicyModel.json"); + + Entity modelEntity = Entity.entity("Somewhere over the rainbow", MediaType.APPLICATION_JSON); + result = target("editor/" + -12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + 12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + modelEntity = Entity.entity("", MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + modelEntity = Entity.entity(modelString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/Event/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + String entityString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," + + "\"namespace\" : \"somewhere.over.the.rainbow\"," + "\"source\" : \"beginning\"," + + "\"target\" : \"end\"," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + Entity entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/-12345/Event/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/1234545/Event/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Event/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/Event/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_EXISTS, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Event/Create").request().post(entity, ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + entityString = "{" + "\"name\" : \"Hiya\"," + "\"version\" : \"0.0.2\"," + + "\"namespace\" : \"somewhere.over.the.rainbow\"," + "\"source\" : \"beginning\"," + + "\"target\" : \"end\"," + "\"parameters\" : {}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Event/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + entityString = "{" + "\"name\" : \"HowsItGoing\"," + "\"version\" : \"0.0.2\"," + + "\"namespace\" : \"somewhere.over.the.rainbow\"," + "\"source\" : \"beginning\"," + + "\"target\" : \"end\"," + + "\"parameters\" : {\"Par0\" : {\"name\" : \"StringType\", \"version\" : \"0.0.1\", " + + "\"localName\" : \"Par0\", \"optional\" : false}}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Event/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + entityString = "{" + "\"name\" : \"Hi\"," + "\"version\" : \"0.0.2\"," + + "\"namespace\" : \"somewhere.over.the.rainbow\"," + "\"source\" : \"beginning\"," + + "\"target\" : \"end\"," + "\"parameters\" : {\"Par0\" : null}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Event/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"GoodDay\"," + "\"version\" : \"0.0.2\"," + + "\"namespace\" : \"somewhere.over.the.rainbow\"," + "\"source\" : \"beginning\"," + + "\"target\" : \"end\"," + + "\"parameters\" : {\"Par0\" : {\"name\" : \"NonExistantType\", \"version\" : \"0.0.1\", " + + "\"localName\" : \"Par0\", \"optional\" : false}}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Event/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," + + "\"namespace\" : \"somewhere.over.someone.elses.rainbow\"," + + "\"source\" : \"start\"," + "\"target\" : \"finish\"," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/-12345/Event/Update").request().put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/1234545/Event/Update").request().put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Event/Update").request().put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Event/Update").request().put(entity, ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + entityString = "{" + "\"name\" : null," + "\"version\" : \"0.0.2\"," + + "\"namespace\" : \"somewhere.over.someone.elses.rainbow\"," + + "\"source\" : \"start\"," + "\"target\" : \"finish\"," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Event/Update").request().put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"NonExistantEvent\"," + "\"version\" : \"0.0.2\"," + + "\"namespace\" : \"somewhere.over.someone.elses.rainbow\"," + + "\"source\" : \"start\"," + "\"target\" : \"finish\"," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Event/Update").request().put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + result = target("editor/" + sessionId + "/Event/Get").queryParam("name", "Hello") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/Event/Get").queryParam("name", "IDontExist") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + result = target("editor/-123345/Event/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/123345/Event/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Event/Get").queryParam("name", "Hello") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/Validate/Event").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/-12345/Validate/Event").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/12345/Validate/Event").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Event/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/-123345/Event/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/123345/Event/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Event/Delete").queryParam("name", "Hello") + .queryParam("version", "0.0.2").request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/Event/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + } + + @Test + public void testTask() throws IOException { + final int corruptSessionId = ApexEditorRestResource.createCorruptSession(); + + ApexApiResult result = target("editor/-1/Session/Create").request().get(ApexApiResult.class); + assertEquals(Result.SUCCESS, result.getResult()); + final int sessionId = new Integer(result.getMessages().get(0)); + + result = target("editor/-12345/Validate/Task").request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + result = target("editor/" + sessionId + "/Validate/Task").request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Validate/Task").request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/Validate/Task").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + result = target("editor/" + sessionId + "/Validate/Task").queryParam("name", "%%%$£") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + final String modelString = TextFileUtils.getTextFileAsString("src/test/resources/models/PolicyModel.json"); + + Entity modelEntity = Entity.entity("Somewhere over the rainbow", MediaType.APPLICATION_JSON); + result = target("editor/" + -12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + 12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + modelEntity = Entity.entity("", MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + modelEntity = Entity.entity(modelString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/Event/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + String entityString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + Entity entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/-12345/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/1234545/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_EXISTS, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + entityString = "{" + "\"name\" : \"Hiya\"," + "\"version\" : \"0.0.2\"," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + entityString = "{" + "\"name\" : \"HowsItGoing\"," + "\"version\" : \"0.0.2\"," + + "\"inputFields\" : {\"IField0\" : {\"name\" : \"StringType\", \"version\" : \"0.0.1\", " + + "\"localName\" : \"IField0\", \"optional\" : false}}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + entityString = "{" + "\"name\" : \"Hi\"," + "\"version\" : \"0.0.2\"," + + "\"inputFields\" : {\"IField0\" : null}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"GoodDay\"," + "\"version\" : \"0.0.2\"," + + "\"inputFields\" : {\"IField0\" : {\"name\" : \"NonExistantType\", \"version\" : \"0.0.1\", " + + "\"localName\" : \"IField0\", \"optional\" : false}}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + entityString = "{" + "\"name\" : \"Howdy\"," + "\"version\" : \"0.0.2\"," + + "\"inputFields\" : {\"IField0\" : {\"name\" : \"NonExistantType\", \"version\" : \"0.0.1\", " + + "\"localName\" : \"NotIField0\", \"optional\" : false}}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"HowsItGoing2\"," + "\"version\" : \"0.0.2\"," + + "\"outputFields\" : {\"OField0\" : {\"name\" : \"StringType\", \"version\" : \"0.0.1\", " + + "\"localName\" : \"OField0\", \"optional\" : false}}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + entityString = "{" + "\"name\" : \"Hi2\"," + "\"version\" : \"0.0.2\"," + + "\"outputFields\" : {\"OField0\" : null}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"GoodDay2\"," + "\"version\" : \"0.0.2\"," + + "\"outputFields\" : {\"OField0\" : {\"name\" : \"NonExistantType\", \"version\" : \"0.0.1\"," + + " \"localName\" : \"OField0\", \"optional\" : false}}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + entityString = "{" + "\"name\" : \"Howdy2\"," + "\"version\" : \"0.0.2\"," + + "\"outputFields\" : {\"OField0\" : {\"name\" : \"NonExistantType\", \"version\" : \"0.0.1\", " + + "\"localName\" : \"NotOField0\", \"optional\" : false}}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"HowsItGoing3\"," + "\"version\" : \"0.0.2\"," + + "\"taskLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons," + + " lots of lime\"}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + entityString = "{" + "\"name\" : \"Hi3\"," + "\"version\" : \"0.0.2\"," + + "\"taskLogic\" : null," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + entityString = "{" + "\"name\" : \"GoodDay3\"," + "\"version\" : \"0.0.2\"," + + "\"namespace\" : \"somewhere.over.the.rainbow\"," + "\"source\" : \"beginning\"," + + "\"target\" : \"end\"," + + "\"taskLogic\" : {\"logicFlavour\" : \"UNDEFINED\", \"logic\" : \"lots of lemons," + + " lots of lime\"}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + entityString = "{" + "\"name\" : \"Howdy3\"," + "\"version\" : \"0.0.2\"," + + "\"taskLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : null}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"HowsItGoing4\"," + "\"version\" : \"0.0.2\"," + + "\"parameters\" : {\"Par0\" : {\"parameterName\" : \"Par0\", " + + "\"defaultValue\" : \"Parameter Defaultvalue\"}}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + entityString = "{" + "\"name\" : \"Hi4\"," + "\"version\" : \"0.0.2\"," + + "\"parameters\" : {\"Par0\" : null}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"GoodDay4\"," + "\"version\" : \"0.0.2\"," + + "\"parameters\" : {\"Par0\" : {\"parameterName\" : \"NotPar0\", \"defaultValue\" : " + + "\"Parameter Defaultvalue\"}}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"Howdy4\"," + "\"version\" : \"0.0.2\"," + + "\"parameters\" : {\"Par0\" : {\"parameterName\" : \"MyParameter\", \"defaultValue\" : null}}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"HowsItGoing5\"," + "\"version\" : \"0.0.2\"," + + "\"contexts\" : [{\"name\" : \"contextAlbum0\", \"version\" : \"0.0.1\"}]," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + entityString = "{" + "\"name\" : \"Hi5\"," + "\"version\" : \"0.0.2\"," + + "\"contexts\" : []," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + entityString = "{" + "\"name\" : \"GoodDay5\"," + "\"version\" : \"0.0.2\"," + + "\"contexts\" : [{\"name\" : \"NonExistantType\", \"version\" : \"0.0.1\"}]," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + entityString = "{" + "\"name\" : \"Howdy5\"," + "\"version\" : \"0.0.2\"," + + "\"contexts\" : [{\"name\" : null, \"version\" : \"0.0.1\"}]," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002799\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/-12345/Task/Update").request().put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/1234545/Task/Update").request().put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Task/Update").request().put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Task/Update").request().put(entity, ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + entityString = "{" + "\"name\" : null," + "\"version\" : \"0.0.2\"," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Update").request().put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"NonExistantEvent\"," + "\"version\" : \"0.0.2\"," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Task/Update").request().put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + result = target("editor/" + sessionId + "/Task/Get").queryParam("name", "Hello") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/Task/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/Task/Get").queryParam("name", "IDontExist") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + result = target("editor/-123345/Task/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/123345/Task/Get").queryParam("name", (String) null).queryParam("version", (String) null) + .request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Task/Get").queryParam("name", "Hello") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/Validate/Event").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/-12345/Validate/Event").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/12345/Validate/Event").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Task/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/-123345/Task/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/123345/Task/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Task/Delete").queryParam("name", "Hello") + .queryParam("version", "0.0.2").request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/Task/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + } + + @Test + public void testPolicy() throws IOException { + final int corruptSessionId = ApexEditorRestResource.createCorruptSession(); + + ApexApiResult result = target("editor/-1/Session/Create").request().get(ApexApiResult.class); + assertEquals(Result.SUCCESS, result.getResult()); + final int sessionId = new Integer(result.getMessages().get(0)); + + result = target("editor/-12345/Model/Validate").request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + result = target("editor/" + sessionId + "/Model/Validate").request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Model/Validate").request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/Model/Validate").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + result = target("editor/" + sessionId + "/Model/Validate").queryParam("name", "%%%$£") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + final String modelString = TextFileUtils.getTextFileAsString("src/test/resources/models/PolicyModel.json"); + + Entity modelEntity = Entity.entity("Somewhere over the rainbow", MediaType.APPLICATION_JSON); + result = target("editor/" + -12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + 12345 + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + modelEntity = Entity.entity("", MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + modelEntity = Entity.entity(modelString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Model/Load").request().put(modelEntity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/Event/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + String entityString = "{" + "\"name\" : \"Hello\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" + + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + Entity entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/-12345/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/1234545/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_EXISTS, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + entityString = "{" + "\"name\" : \"GoodTaSeeYa\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : null," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"HelloAnother\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" + + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello2\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," + + " \"trigger\" : null," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" + + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello3\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : null," + " \"stateOutputs\" : {" + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello4\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"stateOutputs\" : null," + " \"tasks\" : {" + " \"tr0\" : {" + + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello5\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" + + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : null" + " }" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello6\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"IDontExist\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" + + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello7\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : null" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello8\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" + + " \"so0\" : {" + + " \"event\" : {\"name\" : \"IDontExist\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello9\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" + + " \"so0\" : {" + " \"event\" : null," + " \"nextState\" : null" + " }" + + " }," + " \"tasks\" : {" + " \"tr0\" : {" + + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + System.err.println(result); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello10\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" + + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + + " \"task\" : {\"name\" : \"IDontExist\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello11\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" + + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + " \"task\" : null," + " \"outputType\" : \"DIRECT\"," + + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello12\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " + + "lots of lime\"}," + " \"stateOutputs\" : {" + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + + " }" + "}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello13\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : null}," + + " \"stateOutputs\" : {" + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + + " }" + "}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello14\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " + + "lots of lime\"}," + + " \"contexts\" : [{\"name\" : \"contextAlbum0\", \"version\" : \"0.0.1\"}]," + + " \"stateOutputs\" : {" + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + + " }" + "}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello15\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " + + "lots of lime\"}," + + " \"contexts\" : [{\"name\" : \"IDontExist\", \"version\" : \"0.0.1\"}]," + + " \"stateOutputs\" : {" + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + + " }" + "}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello16\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " + + "lots of lime\"}," + + " \"contexts\" : [null]," + " \"stateOutputs\" : {" + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + + " }" + "}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello17\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " + + "lots of lime\"}," + + " \"contexts\" : [{\"name\" : \"contextAlbum0\", \"version\" : \"0.0.1\"}]," + + " \"stateOutputs\" : {" + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }," + + " \"finalizers\" : {" + + " \"sf0\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " + + "lots of lime\"}" + + " }" + " }" + "}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello18\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " + + "lots of lime\"}," + + " \"contexts\" : [{\"name\" : \"contextAlbum0\", \"version\" : \"0.0.1\"}]," + + " \"stateOutputs\" : {" + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }," + + " \"finalizers\" : {" + " \"sf0\" : null" + " }" + " }" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"Hello19\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"taskSelectionLogic\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : \"lots of lemons, " + + "lots of lime\"}," + + " \"contexts\" : [{\"name\" : \"contextAlbum0\", \"version\" : \"0.0.1\"}]," + + " \"stateOutputs\" : {" + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }," + + " \"finalizers\" : {" + + " \"sf0\" : {\"logicFlavour\" : \"LemonAndLime\", \"logic\" : null}" + " }" + " }" + + "}," + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Create").request().post(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"HelloAnother\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" + + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A better description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/-12345/Policy/Update").request().put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/1234545/Policy/Update").request().put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Policy/Update").request().put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + result = target("editor/" + sessionId + "/Policy/Update").queryParam("firstStatePeriodic", "true").request() + .put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Policy/Update").request().put(entity, ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + entityString = "{" + "\"name\" : null," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" + + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A better description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Update").request().put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + entityString = "{" + "\"name\" : \"IDontExist\"," + "\"version\" : \"0.0.2\"," + + "\"template\" : \"somewhere.over.the.rainbow\"," + "\"firstState\" : \"state\"," + + "\"states\" : {" + " \"state\" : {" + " \"name\" : \"state\"," + + " \"trigger\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"defaultTask\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + " \"stateOutputs\" : {" + + " \"so0\" : {" + + " \"event\" : {\"name\" : \"inEvent\", \"version\" : \"0.0.1\"}," + + " \"nextState\" : null" + " }" + " }," + " \"tasks\" : {" + + " \"tr0\" : {" + " \"task\" : {\"name\" : \"task\", \"version\" : \"0.0.1\"}," + + " \"outputType\" : \"DIRECT\"," + " \"outputName\" : \"so0\"" + " }" + " }" + " }" + "}," + + "\"uuid\" : \"1fa2e430-f2b2-11e6-bc64-92361f002671\"," + + "\"description\" : \"A better description of hello\"" + "}"; + entity = Entity.entity(entityString, MediaType.APPLICATION_JSON); + result = target("editor/" + sessionId + "/Policy/Update").request().put(entity, ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + + result = target("editor/" + sessionId + "/Policy/Get").queryParam("name", "Hello") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/Policy/Get").queryParam("name", "IDontExist") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult()); + result = target("editor/" + sessionId + "/Policy/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/-123345/Policy/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/123345/Policy/Get").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Policy/Get").queryParam("name", "Hello") + .queryParam("version", (String) null).request().get(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/" + sessionId + "/Validate/Event").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/-12345/Validate/Event").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/12345/Validate/Event").queryParam("name", (String) null) + .queryParam("version", (String) null).request().get(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + + try { + target("editor/" + corruptSessionId + "/Policy/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + } catch (final Exception e) { + assertEquals("HTTP 500 Request failed.", e.getMessage()); + } + + result = target("editor/-123345/Policy/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/123345/Policy/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.FAILED, result.getResult()); + result = target("editor/" + sessionId + "/Policy/Delete").queryParam("name", "Hello") + .queryParam("version", "0.0.2").request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + result = target("editor/" + sessionId + "/Policy/Delete").queryParam("name", (String) null) + .queryParam("version", (String) null).request().delete(ApexApiResult.class); + assertEquals(ApexApiResult.Result.SUCCESS, result.getResult()); + } +} diff --git a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanFake.java b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanFake.java new file mode 100644 index 000000000..958f1a6cd --- /dev/null +++ b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/bean/BeanFake.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import javax.xml.bind.annotation.XmlType; + +/** + * The Event Bean. + */ +@XmlType +public class BeanFake extends BeanBase { + private String name = null; + private String version = null; + private String field1 = null; + private int field2 = 0; + private int field3 = 0; + + public String getName() { + field1 = name; + return field1; + } + + public String getVersion() { + return version; + } + + public int getField2() { + field3 = field2; + return field3; + } +} diff --git a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/bean/TestBeans.java b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/bean/TestBeans.java new file mode 100644 index 000000000..72cbae22c --- /dev/null +++ b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/handling/bean/TestBeans.java @@ -0,0 +1,88 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.client.editor.rest.handling.bean; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +import org.junit.Test; + +/** + * Test the beans. + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TestBeans { + + @Test + public void testBeans() { + assertNotNull(new BeanEvent().toString()); + assertNotNull(new BeanState().toString()); + assertNotNull(new BeanContextAlbum().toString()); + assertNotNull(new BeanPolicy().toString()); + assertNotNull(new BeanContextSchema().toString()); + assertNotNull(new BeanField().toString()); + assertNotNull(new BeanModel().toString()); + assertNotNull(new BeanLogic().toString()); + assertNotNull(new BeanStateOutput().toString()); + assertNotNull(new BeanTaskParameter().toString()); + assertNotNull(new BeanKeyRef().toString()); + assertNotNull(new BeanStateTaskRef().toString()); + assertNotNull(new BeanTask().toString()); + + final BeanState beanState = new BeanState(); + assertNull(beanState.getName()); + beanState.setDefaultTask(new BeanKeyRef()); + assertNotNull(beanState.getDefaultTask()); + + final BeanEvent beanEvent = new BeanEvent(); + assertNull(beanEvent.get("name")); + + final BeanFake beanFake = new BeanFake(); + assertNull(beanFake.get("name")); + assertNull(beanFake.get("field1")); + + try { + beanFake.get("iDontExist"); + fail("test should throw an exception here"); + } catch (final IllegalArgumentException e) { + assertNotNull(e); + } + try { + beanFake.get("nome"); + fail("test should throw an exception here"); + } catch (final IllegalArgumentException e) { + assertNotNull(e); + } + try { + beanFake.get("field2"); + fail("test should throw an exception here"); + } catch (final IllegalArgumentException e) { + assertNotNull(e); + } + try { + beanFake.get("field3"); + fail("test should throw an exception here"); + } catch (final IllegalArgumentException e) { + assertNotNull(e); + } + } +} -- cgit 1.2.3-korg