summaryrefslogtreecommitdiffstats
path: root/profiles/http/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'profiles/http/src/main/java')
-rw-r--r--profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java74
-rw-r--r--profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java15
-rw-r--r--profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java105
3 files changed, 187 insertions, 7 deletions
diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java b/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java
index 94baa7a7..9477cfa9 100644
--- a/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java
+++ b/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java
@@ -16,13 +16,6 @@
package org.onap.cli.fw.http.cmd;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-
import org.onap.cli.fw.cmd.OnapCommand;
import org.onap.cli.fw.cmd.OnapCommandType;
import org.onap.cli.fw.conf.OnapCommandConfig;
@@ -35,8 +28,10 @@ import org.onap.cli.fw.http.conf.OnapCommandHttpConstants;
import org.onap.cli.fw.http.connect.HttpInput;
import org.onap.cli.fw.http.connect.HttpResult;
import org.onap.cli.fw.http.error.OnapCommandFailedMocoGenerate;
+import org.onap.cli.fw.http.mock.MocoServer;
import org.onap.cli.fw.http.schema.OnapCommandSchemaHttpLoader;
import org.onap.cli.fw.http.utils.OnapCommandHttpUtils;
+import org.onap.cli.fw.input.OnapCommandParameter;
import org.onap.cli.fw.output.OnapCommandResultAttribute;
import org.onap.cli.fw.schema.OnapCommandSchema;
import org.onap.cli.fw.utils.OnapCommandUtils;
@@ -44,6 +39,14 @@ import org.onap.cli.http.mock.MockJsonGenerator;
import org.onap.cli.http.mock.MockRequest;
import org.onap.cli.http.mock.MockResponse;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
+
/**
* Oclip http Command.
*
@@ -61,6 +64,10 @@ public class OnapHttpCommand extends OnapCommand {
private OnapCommandHttpService oclipService = new OnapCommandHttpService();
+ private MocoServer mocoServer;
+
+ boolean shouldVerify = false;
+
public OnapHttpCommand() {
super.addDefaultSchemas(OnapCommandHttpConstants.DEFAULT_PARAMETER_HTTP_FILE_NAME);
}
@@ -131,6 +138,59 @@ public class OnapHttpCommand extends OnapCommand {
this.getInfo().getCommandType().equals(OnapCommandType.CATALOG));
}
+ private Optional<OnapCommandParameter> findParameterByName(String parameterName) {
+ return this.getParameters().stream()
+ .filter(e -> e.getName().equals(parameterName))
+ .findFirst();
+ }
+
+ @Override
+ protected void preRun() throws OnapCommandException {
+ Optional<OnapCommandParameter> verifyOpt = this.getParameters().stream()
+ .filter(e -> e.getName().equals("verify"))
+ .findFirst();
+ if(verifyOpt.isPresent()) {
+ shouldVerify = (boolean) verifyOpt.get().getValue();
+ }
+
+ if (shouldVerify) {
+ Optional<OnapCommandParameter> hostUrlParamOpt = findParameterByName(OnapCommandHttpConstants.VERIFY_HOST_PARAMETER_OPT);
+ Optional<OnapCommandParameter> noAuthParamOpt = findParameterByName(OnapCommandHttpConstants.VERIFY_NO_AUTH_PARAMETER_OPT);
+
+ if (hostUrlParamOpt.isPresent()) {
+ OnapCommandParameter onapCommandParameter = hostUrlParamOpt.get();
+ onapCommandParameter.setValue(
+ OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.VERIFY_MOCO_HOST)
+ + ":" + OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.VERIFY_MOCO_PORT));
+ }
+
+ if (noAuthParamOpt.isPresent()) {
+ OnapCommandParameter onapCommandParameter = noAuthParamOpt.get();
+ onapCommandParameter.setValue(true);
+ }
+
+
+ Optional<OnapCommandParameter> contextOpt = this.getParameters().stream()
+ .filter(e -> e.getName().equals(OnapCommandConstants.VERIFY_CONTEXT_PARAM))
+ .findFirst();
+
+ if (contextOpt.isPresent()) {
+ OnapCommandParameter context = contextOpt.get();
+ String mockedFile = ((Map<String, String>)context.getValue()).get(OnapCommandConstants.VERIFY_MOCO);
+
+ mocoServer = new MocoServer(mockedFile);
+ mocoServer.start();
+ }
+ }
+ }
+
+ @Override
+ protected void postRun() throws OnapCommandException {
+ if (shouldVerify) {
+ mocoServer.stop();
+ }
+ }
+
@Override
protected void run() throws OnapCommandException {
try {
diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java b/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java
index 9663f87b..185582cb 100644
--- a/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java
+++ b/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java
@@ -93,6 +93,21 @@ public class OnapCommandHttpConstants {
//context param
public static final String CONTEXT = "context";
public static final String CONTEXT_REMOVE_EMPTY_JSON_NODES = "remove_empty_node";
+
+ // moco server const
+ public static final String VERIFY_MOCO_HOST = "cli.verify.host";
+ public static final String VERIFY_MOCO_PORT = "cli.verify.port";
+
+ public static final String VERIFY_HOST_PARAMETER_OPT = DEAFULT_PARAMETER_HOST_URL;
+ public static final String VERIFY_NO_AUTH_PARAMETER_OPT = DEFAULT_PARAMETER_NO_AUTH;
+
+ public static final String VERIFY_REQUEST_URI = URI;
+ public static final String VERIFY_RESPONSE_STATUS = "status";
+ public static final String VERIFY_RESPONSE_JSON = "json";
+ public static final String VERIFY_REQUEST = REQUEST;
+ public static final String VERIFY_RESPONSE = "response";
+ public static final String VERIFY_CONTENT_TYPE = "Content-Type";
+ public static final String VERIFY_CONTENT_TYPE_VALUE = APPLICATION_JSON;
}
diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java b/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java
new file mode 100644
index 00000000..f6e58757
--- /dev/null
+++ b/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2017 Huawei Technologies Co., Ltd.
+ *
+ * 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.
+ */
+package org.onap.cli.fw.http.mock;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.github.dreamhead.moco.HttpServer;
+import com.github.dreamhead.moco.Moco;
+import com.github.dreamhead.moco.ResponseHandler;
+import com.github.dreamhead.moco.Runner;
+import org.onap.cli.fw.conf.OnapCommandConfig;
+import org.onap.cli.fw.conf.OnapCommandConstants;
+import org.onap.cli.fw.error.OnapCommandDiscoveryFailed;
+import org.onap.cli.fw.error.OnapCommandException;
+import org.onap.cli.fw.error.OnapCommandInvalidSchema;
+import org.onap.cli.fw.http.conf.OnapCommandHttpConstants;
+import org.onap.cli.fw.schema.OnapCommandSchemaLoader;
+import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils;
+import org.springframework.core.io.Resource;
+import org.yaml.snakeyaml.Yaml;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static com.github.dreamhead.moco.Runner.runner;
+
+public class MocoServer {
+
+ private Runner runner;
+ private Map<String, Object> mocoServerConfigs = new HashMap();
+
+ public MocoServer(String mockFile) throws OnapCommandException {
+ Resource resource = null;
+
+ try {
+ resource = OnapCommandDiscoveryUtils.findResource(mockFile,
+ OnapCommandConstants.VERIFY_SAMPLES_MOCK_PATTERN);
+ } catch (IOException e) {
+ throw new OnapCommandDiscoveryFailed(mockFile, e);
+ }
+
+ List<Map<String, ?>> stringMap = null;
+ try {
+ stringMap = (List<Map<String, ?>>) new Yaml().load(resource.getInputStream());
+ } catch (IOException e) {
+ throw new OnapCommandException("Invalid mocking file" + mockFile, e);
+ }
+ if(!stringMap.isEmpty()) {
+ Map<String, ?> jsonConfigs = stringMap.get(0);
+ Map<String, String> request = (Map<String, String>) jsonConfigs.get(OnapCommandHttpConstants.VERIFY_REQUEST);
+ mocoServerConfigs.put(OnapCommandHttpConstants.VERIFY_REQUEST_URI, request.get(OnapCommandHttpConstants.VERIFY_REQUEST_URI));
+
+ Map<String, String> response = (Map<String, String>) jsonConfigs.get(OnapCommandHttpConstants.VERIFY_RESPONSE);
+ mocoServerConfigs.put(OnapCommandHttpConstants.VERIFY_RESPONSE_STATUS, response.get(OnapCommandHttpConstants.VERIFY_RESPONSE_STATUS));
+
+ if(response.get(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON) != null) {
+ try {
+ mocoServerConfigs.put(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON,
+ new ObjectMapper().writeValueAsString(response.get(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON)));
+ } catch (JsonProcessingException e) {
+ throw new OnapCommandException("Invalid mocking file" + mockFile, e);
+ }
+ }
+ }
+ }
+
+ public void start() {
+ HttpServer server = Moco.httpServer(Integer.parseInt(
+ OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.VERIFY_MOCO_PORT)));
+
+ List<ResponseHandler> responseHandlers = new ArrayList<>();
+
+ if (mocoServerConfigs.containsKey(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON)) {
+ responseHandlers.add(Moco.with(mocoServerConfigs.get(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON).toString()));
+ }
+ responseHandlers.add(Moco.status((Integer) mocoServerConfigs.get(OnapCommandHttpConstants.VERIFY_RESPONSE_STATUS)));
+
+ server.request(Moco.by(Moco.uri((String) mocoServerConfigs.get(OnapCommandHttpConstants.VERIFY_REQUEST_URI))))
+ .response(Moco.header(OnapCommandHttpConstants.VERIFY_CONTENT_TYPE, OnapCommandHttpConstants.VERIFY_CONTENT_TYPE_VALUE),
+ responseHandlers.toArray(new ResponseHandler[responseHandlers.size()]));
+
+ runner = runner(server);
+ runner.start();
+ }
+
+ public void stop() {
+ runner.stop();
+ }
+}