From 781b1a6df324419c846c84ea983c18fc8362bfd3 Mon Sep 17 00:00:00 2001 From: Patrick Brady Date: Wed, 13 Dec 2017 11:19:06 -0800 Subject: Third part of onap rename This part of the commit changes the folder structure on all other folders of appc. Change-Id: I8acfa11cdfcdcd36be0e137245d1dd7324f1abd3 Signed-off-by: Patrick Brady Issue-ID: APPC-13 --- .../onap/appc/simulator/client/RequestHandler.java | 33 +++ .../appc/simulator/client/ResponseHandler.java | 31 +++ .../simulator/client/impl/JsonRequestHandler.java | 275 +++++++++++++++++++++ .../simulator/client/impl/JsonResponseHandler.java | 147 +++++++++++ .../appc/simulator/client/main/ClientRunner.java | 100 ++++++++ .../appc/simulator/client/RequestHandler.java | 33 --- .../appc/simulator/client/ResponseHandler.java | 31 --- .../simulator/client/impl/JsonRequestHandler.java | 275 --------------------- .../simulator/client/impl/JsonResponseHandler.java | 147 ----------- .../appc/simulator/client/main/ClientRunner.java | 100 -------- .../client/impl/TestJsonRequestHandler.java | 53 ++++ .../client/impl/TestJsonResponseHandler.java | 46 ++++ .../simulator/client/main/TestClientRunner.java | 102 ++++++++ .../client/impl/TestJsonRequestHandler.java | 53 ---- .../client/impl/TestJsonResponseHandler.java | 46 ---- .../simulator/client/main/TestClientRunner.java | 102 -------- 16 files changed, 787 insertions(+), 787 deletions(-) create mode 100644 appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/RequestHandler.java create mode 100644 appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/ResponseHandler.java create mode 100644 appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/impl/JsonRequestHandler.java create mode 100644 appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/impl/JsonResponseHandler.java create mode 100644 appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/main/ClientRunner.java delete mode 100644 appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/RequestHandler.java delete mode 100644 appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/ResponseHandler.java delete mode 100644 appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonRequestHandler.java delete mode 100644 appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonResponseHandler.java delete mode 100644 appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/main/ClientRunner.java create mode 100644 appc-client/client-simulator/src/test/java/org/onap/appc/simulator/client/impl/TestJsonRequestHandler.java create mode 100644 appc-client/client-simulator/src/test/java/org/onap/appc/simulator/client/impl/TestJsonResponseHandler.java create mode 100644 appc-client/client-simulator/src/test/java/org/onap/appc/simulator/client/main/TestClientRunner.java delete mode 100644 appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonRequestHandler.java delete mode 100644 appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonResponseHandler.java delete mode 100644 appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/main/TestClientRunner.java (limited to 'appc-client/client-simulator/src') diff --git a/appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/RequestHandler.java b/appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/RequestHandler.java new file mode 100644 index 000000000..293b72f89 --- /dev/null +++ b/appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/RequestHandler.java @@ -0,0 +1,33 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.simulator.client; + +import java.io.File; +import java.io.IOException; + +public interface RequestHandler { + void proceedFile(File source, File log) throws IOException, ClassNotFoundException, NoSuchMethodException; + void shutdown(boolean isForceShutdown); +} diff --git a/appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/ResponseHandler.java b/appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/ResponseHandler.java new file mode 100644 index 000000000..5a24028e8 --- /dev/null +++ b/appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/ResponseHandler.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.simulator.client; + +import java.io.File; + +public interface ResponseHandler { + void validateResponse(File output); +} diff --git a/appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/impl/JsonRequestHandler.java b/appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/impl/JsonRequestHandler.java new file mode 100644 index 000000000..243136d0e --- /dev/null +++ b/appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/impl/JsonRequestHandler.java @@ -0,0 +1,275 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.simulator.client.impl; + +import org.onap.appc.client.lcm.api.AppcClientServiceFactoryProvider; +import org.onap.appc.client.lcm.api.AppcLifeCycleManagerServiceFactory; +import org.onap.appc.client.lcm.api.ApplicationContext; +import org.onap.appc.client.lcm.api.LifeCycleManagerStateful; +import org.onap.appc.client.lcm.api.ResponseHandler; + +import org.onap.appc.client.lcm.exceptions.AppcClientException; +import org.onap.appc.simulator.client.RequestHandler; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Properties; + +public class JsonRequestHandler implements RequestHandler { + + private enum modeT { + SYNCH, + ASYNCH + } + + public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + private String rpcName = null; + private String inputClassName = null; + private String actionName = null; + private String methodName = null; + String packageName = null; + private LifeCycleManagerStateful service = null; + private Properties properties; + HashMap exceptRpcMap = null; + private final EELFLogger LOG = EELFManager.getInstance().getLogger(JsonRequestHandler.class); + private AppcLifeCycleManagerServiceFactory appcLifeCycleManagerServiceFactory = null; + + + public JsonRequestHandler(Properties prop) throws AppcClientException { + properties = prop; + packageName = properties.getProperty("ctx.model.package") + "."; + try { + service = createService(); + } catch (AppcClientException e) { + e.printStackTrace(); + } + exceptRpcMap = prepareExceptionsMap(); + } + + public JsonRequestHandler(){ + + } + + private HashMap prepareExceptionsMap() { + exceptRpcMap = new HashMap<>(); + + try (BufferedReader reader = new BufferedReader( + new FileReader(properties.getProperty( + "client.rpc.exceptions.map.file")))) { + String line; + while ((line = reader.readLine()) != null) { + String[] parts = line.split(":", 2); + if (parts.length >= 2) { + String key = parts[0]; + String value = parts[1]; + exceptRpcMap.put(key, value); + } else { + System.out.println("ignoring line: " + line); + } + } + } catch (FileNotFoundException e) { + return exceptRpcMap; + } catch (IOException e) { + e.printStackTrace(); + } + + return exceptRpcMap; + } + + @Override + public void proceedFile(File source, File log) throws IOException { + final JsonNode inputNode = OBJECT_MAPPER.readTree(source); + + try { + // proceed with inputNode and get some xxxInput object, depends on action + prepareNames(inputNode); + + Object input = prepareObject(inputNode); + + JsonResponseHandler response = new JsonResponseHandler(); + response.setFile(source.getPath().toString()); + switch (isSyncMode(inputNode)) { + case SYNCH: { + LOG.debug("Received input request will be processed in synchronously mode"); + Method rpc = LifeCycleManagerStateful.class.getDeclaredMethod(methodName, input.getClass()); + response.onResponse(rpc.invoke(service, input)); + break; + } + case ASYNCH: { + LOG.debug("Received input request will be processed in asynchronously mode"); + Method rpc = LifeCycleManagerStateful.class.getDeclaredMethod(methodName, input.getClass(), ResponseHandler.class); + rpc.invoke(service, input, response); + break; + } + default: { + throw new RuntimeException("Unrecognized request mode"); + } + } + } + catch(Exception ex){ + //ex.printStackTrace(); + } + + LOG.debug("Action <" + actionName + "> from input file <" + source.getPath().toString() + "> processed"); + } + + private modeT isSyncMode(JsonNode inputNode) { + // The following solution is for testing purposes only + // the sync/async decision logic may change upon request + try { + int mode = Integer.parseInt(inputNode.findValue("input").findValue("common-header").findValue("sub-request-id").asText()); + if ((mode % 2) == 0) { + return modeT.SYNCH; + } + }catch (Exception ex) { + //use ASYNC as default, if value is not integer. + } + return modeT.ASYNCH; + } + + private LifeCycleManagerStateful createService() throws AppcClientException { + appcLifeCycleManagerServiceFactory = AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class); + return appcLifeCycleManagerServiceFactory.createLifeCycleManagerStateful(new ApplicationContext(), properties); + } + + public void shutdown(boolean isForceShutdown){ + appcLifeCycleManagerServiceFactory.shutdownLifeCycleManager(isForceShutdown); + } + + public Object prepareObject(JsonNode input) { + try { + Class cls = Class.forName(inputClassName); + try { + // since payload is not mandatory field and not all actions contains payload + // so we have to check that during input parsing + alignPayload(input); + } catch (NoSuchFieldException e) { + LOG.debug("In " + actionName + " no payload defined"); + } + + return OBJECT_MAPPER.treeToValue(input.get("input"), cls); + } + catch(Exception ex){ + //ex.printStackTrace(); + } + return null; + } + + private void prepareNames(JsonNode input) throws NoSuchFieldException { + JsonNode inputNode = input.findValue("input"); + actionName = inputNode.findValue("action").asText(); + if (actionName.isEmpty()) { + throw new NoSuchFieldException("Input doesn't contains field "); + } + + rpcName = prepareRpcFromAction(actionName); + inputClassName = packageName + actionName + "Input"; + methodName = prepareMethodName(rpcName); + } + + private void alignPayload(JsonNode input) throws NoSuchFieldException { + JsonNode inputNode = input.findValue("input"); + JsonNode payload = inputNode.findValue("payload"); + if (payload == null || payload.asText().isEmpty() || payload.toString().isEmpty()) + throw new NoSuchFieldException("Input doesn't contains field "); + + String payloadData = payload.asText(); + if (payloadData.isEmpty()) + payloadData = payload.toString(); + ((ObjectNode)inputNode).put("payload", payloadData); + } + + private String prepareRpcFromAction(String action) { + String rpc = checkExceptionalRpcList(action); + if (rpc!= null && !rpc.isEmpty()) { + return rpc; // we found exceptional rpc, so no need to format it + } + + rpc = ""; + boolean makeItLowerCase = true; + for(int i = 0; i < action.length(); i++) + { + if(makeItLowerCase) // first character will make lower case + { + rpc+=Character.toLowerCase(action.charAt(i)); + makeItLowerCase = false; + } + else if((i+1 < action.length()) && Character.isUpperCase(action.charAt(i+1))) + { + rpc+=action.charAt(i) + "-"; + makeItLowerCase = true; + } + else + { + rpc+=action.charAt(i); + makeItLowerCase = false; + } + } + return rpc; + } + + private String checkExceptionalRpcList(String action) { + if (exceptRpcMap.isEmpty()) { + return null; + } + return exceptRpcMap.get(action); + } + + private String prepareMethodName(String inputRpcName) { + boolean makeItUpperCase = false; + String method = ""; + + for(int i = 0; i < inputRpcName.length(); i++) //to check the characters of string.. + { + if(Character.isLowerCase(inputRpcName.charAt(i)) && makeItUpperCase) // skip first character if it lower case + { + method+=Character.toUpperCase(inputRpcName.charAt(i)); + makeItUpperCase = false; + } + else if(inputRpcName.charAt(i) == '-') + { + makeItUpperCase = true; + } + else + { + method+=inputRpcName.charAt(i); + makeItUpperCase = false; + } + } + return method; + } + +} diff --git a/appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/impl/JsonResponseHandler.java b/appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/impl/JsonResponseHandler.java new file mode 100644 index 000000000..c19abb2fb --- /dev/null +++ b/appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/impl/JsonResponseHandler.java @@ -0,0 +1,147 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.simulator.client.impl; + +import org.onap.appc.client.lcm.api.ResponseHandler; +import org.onap.appc.client.lcm.exceptions.AppcClientException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.FileWriter; +import java.util.concurrent.atomic.AtomicInteger; + +public class JsonResponseHandler implements ResponseHandler { + + public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + private final EELFLogger LOG = EELFManager.getInstance().getLogger(JsonResponseHandler.class); + + private String fileName = "default"; + private static final int ACCEPT_FAMILY = 1; + private static final int SUCCESS_FAMILY = 4; + private static final int INTERMEDIATE_MESSAGES =5; + + private AtomicInteger messageCount =new AtomicInteger(1); + + public void setFile(String name) { + fileName = name; + } + + @Override + public void onResponse(Object response) { + + String output = null; + try { + output = OBJECT_MAPPER.writeValueAsString(response); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + LOG.debug("Received response : " + output); + + int errorCode = 0; + boolean isFinal = true; + try { + JsonNode code= OBJECT_MAPPER.readTree(output).findValue("status").findValue("code"); + if (code == null) + { + LOG.error("Status code doesn't exist. Malformed response : " + output); + flushToErrorFile(output, isFinal); + return; + } + errorCode = code.asInt(); + errorCode = errorCode / 100; + switch (errorCode) { + case ACCEPT_FAMILY: + isFinal = false; // for ACCEPT that it is not a final response + break; + case INTERMEDIATE_MESSAGES: + isFinal = false; // for 5xx series messages are not a final response + break; + default: + break; + } + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + + switch (errorCode) { + case ACCEPT_FAMILY: { + try { + System.out.println("== THR#" + Thread.currentThread().getId() + " Got ACCEPT on ReqID <" + + OBJECT_MAPPER.readTree(output).findValue("common-header").findValue("request-id").asText() + ">"); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + // no need to report ACCEPT into output file + break; + } + case SUCCESS_FAMILY: + flushToOutputFile(output, isFinal); + break; + case INTERMEDIATE_MESSAGES: + flushToMessageFile(output, isFinal); + break; + default: + flushToErrorFile(output, isFinal); + } + } + + @Override + public void onException(AppcClientException exception) { + flushToErrorFile("exception: " + exception, true); + } + + private void flushToOutputFile(String output, boolean isFinal) { + this.flushToFile(output, ".output", isFinal); + } + private void flushToMessageFile(String output, boolean isFinal) { + this.flushToFile(output, ".message" + messageCount.getAndIncrement(), isFinal); + + } + + private void flushToErrorFile(String output, boolean isFinal) { + this.flushToFile(output, ".error", isFinal); + } + + private void flushToFile(String output, String suffix, boolean isFinal) { + try (FileWriter fileWriter = new FileWriter(fileName + suffix);){ + LOG.info("Output file : " + fileName + suffix); + + fileWriter.write(output); + fileWriter.flush(); + if (isFinal){ + fileWriter.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + System.out.println("== THR#" +Thread.currentThread().getId()+ " Output file : " + fileName + suffix); + } +} diff --git a/appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/main/ClientRunner.java b/appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/main/ClientRunner.java new file mode 100644 index 000000000..c8da17e79 --- /dev/null +++ b/appc-client/client-simulator/src/main/java/org/onap/appc/simulator/client/main/ClientRunner.java @@ -0,0 +1,100 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.simulator.client.main; + +import org.apache.commons.io.filefilter.WildcardFileFilter; +import org.onap.appc.simulator.client.RequestHandler; +import org.onap.appc.simulator.client.impl.JsonRequestHandler; + +import java.io.*; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; + +public class ClientRunner { + + public static void main(String ... args) throws Exception { + String folder = args[0]; + if (folder == null) { + folder = System.getProperty("user.dir"); + } + System.out.println("== THR#" +Thread.currentThread().getId()+ " Reading files under the folder : " + folder); + + String inputType = args[1]; + if (inputType != null && !inputType.matches("JSON")) { + throw new RuntimeException("### ERROR ### - Unsupported file type <" + inputType + "> was provided"); + } + + Properties properties = getProperties(folder); + RequestHandler reqHandler = new JsonRequestHandler(properties); + List sources = getJsonFiles(folder); + File log = new File(folder + "/output.txt"); + int filesNum = 0; + for (File source: sources) { + reqHandler.proceedFile(source, log); + System.out.println("== THR#" +Thread.currentThread().getId()+ " File <" + source.getName() + "> processed."); + ++filesNum; + } + System.out.println("DONE with " + filesNum + " files under the folder : " + folder); + Thread.sleep(30); + System.out.println("Shutdown ..."); + reqHandler.shutdown(Boolean.parseBoolean(properties.getProperty("client.force.shutdown"))); +// System.exit(0); + } + + private static Properties getProperties(String folder) { + Properties prop = new Properties(); + + InputStream conf = null; + try { + conf = new FileInputStream(folder + "client-simulator.properties"); + } catch (FileNotFoundException e) { + + } + if (conf != null) { + try { + prop.load(conf); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + try { + prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("client-simulator.properties")); + } catch (Exception e) { + throw new RuntimeException("### ERROR ### - Could not load properties to test"); + } + } + return prop; + } + + private static List getJsonFiles(String folder) throws FileNotFoundException { + Path dir = Paths.get(folder); + FileFilter fileFilter = new WildcardFileFilter("*.json"); + return new ArrayList(Arrays.asList(dir.toFile().listFiles(fileFilter))); + } +} diff --git a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/RequestHandler.java b/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/RequestHandler.java deleted file mode 100644 index 293b72f89..000000000 --- a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/RequestHandler.java +++ /dev/null @@ -1,33 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.simulator.client; - -import java.io.File; -import java.io.IOException; - -public interface RequestHandler { - void proceedFile(File source, File log) throws IOException, ClassNotFoundException, NoSuchMethodException; - void shutdown(boolean isForceShutdown); -} diff --git a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/ResponseHandler.java b/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/ResponseHandler.java deleted file mode 100644 index 5a24028e8..000000000 --- a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/ResponseHandler.java +++ /dev/null @@ -1,31 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.simulator.client; - -import java.io.File; - -public interface ResponseHandler { - void validateResponse(File output); -} diff --git a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonRequestHandler.java b/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonRequestHandler.java deleted file mode 100644 index 243136d0e..000000000 --- a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonRequestHandler.java +++ /dev/null @@ -1,275 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.simulator.client.impl; - -import org.onap.appc.client.lcm.api.AppcClientServiceFactoryProvider; -import org.onap.appc.client.lcm.api.AppcLifeCycleManagerServiceFactory; -import org.onap.appc.client.lcm.api.ApplicationContext; -import org.onap.appc.client.lcm.api.LifeCycleManagerStateful; -import org.onap.appc.client.lcm.api.ResponseHandler; - -import org.onap.appc.client.lcm.exceptions.AppcClientException; -import org.onap.appc.simulator.client.RequestHandler; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Properties; - -public class JsonRequestHandler implements RequestHandler { - - private enum modeT { - SYNCH, - ASYNCH - } - - public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); - private String rpcName = null; - private String inputClassName = null; - private String actionName = null; - private String methodName = null; - String packageName = null; - private LifeCycleManagerStateful service = null; - private Properties properties; - HashMap exceptRpcMap = null; - private final EELFLogger LOG = EELFManager.getInstance().getLogger(JsonRequestHandler.class); - private AppcLifeCycleManagerServiceFactory appcLifeCycleManagerServiceFactory = null; - - - public JsonRequestHandler(Properties prop) throws AppcClientException { - properties = prop; - packageName = properties.getProperty("ctx.model.package") + "."; - try { - service = createService(); - } catch (AppcClientException e) { - e.printStackTrace(); - } - exceptRpcMap = prepareExceptionsMap(); - } - - public JsonRequestHandler(){ - - } - - private HashMap prepareExceptionsMap() { - exceptRpcMap = new HashMap<>(); - - try (BufferedReader reader = new BufferedReader( - new FileReader(properties.getProperty( - "client.rpc.exceptions.map.file")))) { - String line; - while ((line = reader.readLine()) != null) { - String[] parts = line.split(":", 2); - if (parts.length >= 2) { - String key = parts[0]; - String value = parts[1]; - exceptRpcMap.put(key, value); - } else { - System.out.println("ignoring line: " + line); - } - } - } catch (FileNotFoundException e) { - return exceptRpcMap; - } catch (IOException e) { - e.printStackTrace(); - } - - return exceptRpcMap; - } - - @Override - public void proceedFile(File source, File log) throws IOException { - final JsonNode inputNode = OBJECT_MAPPER.readTree(source); - - try { - // proceed with inputNode and get some xxxInput object, depends on action - prepareNames(inputNode); - - Object input = prepareObject(inputNode); - - JsonResponseHandler response = new JsonResponseHandler(); - response.setFile(source.getPath().toString()); - switch (isSyncMode(inputNode)) { - case SYNCH: { - LOG.debug("Received input request will be processed in synchronously mode"); - Method rpc = LifeCycleManagerStateful.class.getDeclaredMethod(methodName, input.getClass()); - response.onResponse(rpc.invoke(service, input)); - break; - } - case ASYNCH: { - LOG.debug("Received input request will be processed in asynchronously mode"); - Method rpc = LifeCycleManagerStateful.class.getDeclaredMethod(methodName, input.getClass(), ResponseHandler.class); - rpc.invoke(service, input, response); - break; - } - default: { - throw new RuntimeException("Unrecognized request mode"); - } - } - } - catch(Exception ex){ - //ex.printStackTrace(); - } - - LOG.debug("Action <" + actionName + "> from input file <" + source.getPath().toString() + "> processed"); - } - - private modeT isSyncMode(JsonNode inputNode) { - // The following solution is for testing purposes only - // the sync/async decision logic may change upon request - try { - int mode = Integer.parseInt(inputNode.findValue("input").findValue("common-header").findValue("sub-request-id").asText()); - if ((mode % 2) == 0) { - return modeT.SYNCH; - } - }catch (Exception ex) { - //use ASYNC as default, if value is not integer. - } - return modeT.ASYNCH; - } - - private LifeCycleManagerStateful createService() throws AppcClientException { - appcLifeCycleManagerServiceFactory = AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class); - return appcLifeCycleManagerServiceFactory.createLifeCycleManagerStateful(new ApplicationContext(), properties); - } - - public void shutdown(boolean isForceShutdown){ - appcLifeCycleManagerServiceFactory.shutdownLifeCycleManager(isForceShutdown); - } - - public Object prepareObject(JsonNode input) { - try { - Class cls = Class.forName(inputClassName); - try { - // since payload is not mandatory field and not all actions contains payload - // so we have to check that during input parsing - alignPayload(input); - } catch (NoSuchFieldException e) { - LOG.debug("In " + actionName + " no payload defined"); - } - - return OBJECT_MAPPER.treeToValue(input.get("input"), cls); - } - catch(Exception ex){ - //ex.printStackTrace(); - } - return null; - } - - private void prepareNames(JsonNode input) throws NoSuchFieldException { - JsonNode inputNode = input.findValue("input"); - actionName = inputNode.findValue("action").asText(); - if (actionName.isEmpty()) { - throw new NoSuchFieldException("Input doesn't contains field "); - } - - rpcName = prepareRpcFromAction(actionName); - inputClassName = packageName + actionName + "Input"; - methodName = prepareMethodName(rpcName); - } - - private void alignPayload(JsonNode input) throws NoSuchFieldException { - JsonNode inputNode = input.findValue("input"); - JsonNode payload = inputNode.findValue("payload"); - if (payload == null || payload.asText().isEmpty() || payload.toString().isEmpty()) - throw new NoSuchFieldException("Input doesn't contains field "); - - String payloadData = payload.asText(); - if (payloadData.isEmpty()) - payloadData = payload.toString(); - ((ObjectNode)inputNode).put("payload", payloadData); - } - - private String prepareRpcFromAction(String action) { - String rpc = checkExceptionalRpcList(action); - if (rpc!= null && !rpc.isEmpty()) { - return rpc; // we found exceptional rpc, so no need to format it - } - - rpc = ""; - boolean makeItLowerCase = true; - for(int i = 0; i < action.length(); i++) - { - if(makeItLowerCase) // first character will make lower case - { - rpc+=Character.toLowerCase(action.charAt(i)); - makeItLowerCase = false; - } - else if((i+1 < action.length()) && Character.isUpperCase(action.charAt(i+1))) - { - rpc+=action.charAt(i) + "-"; - makeItLowerCase = true; - } - else - { - rpc+=action.charAt(i); - makeItLowerCase = false; - } - } - return rpc; - } - - private String checkExceptionalRpcList(String action) { - if (exceptRpcMap.isEmpty()) { - return null; - } - return exceptRpcMap.get(action); - } - - private String prepareMethodName(String inputRpcName) { - boolean makeItUpperCase = false; - String method = ""; - - for(int i = 0; i < inputRpcName.length(); i++) //to check the characters of string.. - { - if(Character.isLowerCase(inputRpcName.charAt(i)) && makeItUpperCase) // skip first character if it lower case - { - method+=Character.toUpperCase(inputRpcName.charAt(i)); - makeItUpperCase = false; - } - else if(inputRpcName.charAt(i) == '-') - { - makeItUpperCase = true; - } - else - { - method+=inputRpcName.charAt(i); - makeItUpperCase = false; - } - } - return method; - } - -} diff --git a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonResponseHandler.java b/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonResponseHandler.java deleted file mode 100644 index c19abb2fb..000000000 --- a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/impl/JsonResponseHandler.java +++ /dev/null @@ -1,147 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.simulator.client.impl; - -import org.onap.appc.client.lcm.api.ResponseHandler; -import org.onap.appc.client.lcm.exceptions.AppcClientException; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -import java.io.FileWriter; -import java.util.concurrent.atomic.AtomicInteger; - -public class JsonResponseHandler implements ResponseHandler { - - public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); - private final EELFLogger LOG = EELFManager.getInstance().getLogger(JsonResponseHandler.class); - - private String fileName = "default"; - private static final int ACCEPT_FAMILY = 1; - private static final int SUCCESS_FAMILY = 4; - private static final int INTERMEDIATE_MESSAGES =5; - - private AtomicInteger messageCount =new AtomicInteger(1); - - public void setFile(String name) { - fileName = name; - } - - @Override - public void onResponse(Object response) { - - String output = null; - try { - output = OBJECT_MAPPER.writeValueAsString(response); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - LOG.debug("Received response : " + output); - - int errorCode = 0; - boolean isFinal = true; - try { - JsonNode code= OBJECT_MAPPER.readTree(output).findValue("status").findValue("code"); - if (code == null) - { - LOG.error("Status code doesn't exist. Malformed response : " + output); - flushToErrorFile(output, isFinal); - return; - } - errorCode = code.asInt(); - errorCode = errorCode / 100; - switch (errorCode) { - case ACCEPT_FAMILY: - isFinal = false; // for ACCEPT that it is not a final response - break; - case INTERMEDIATE_MESSAGES: - isFinal = false; // for 5xx series messages are not a final response - break; - default: - break; - } - - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException(e); - } - - switch (errorCode) { - case ACCEPT_FAMILY: { - try { - System.out.println("== THR#" + Thread.currentThread().getId() + " Got ACCEPT on ReqID <" + - OBJECT_MAPPER.readTree(output).findValue("common-header").findValue("request-id").asText() + ">"); - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException(e); - } - // no need to report ACCEPT into output file - break; - } - case SUCCESS_FAMILY: - flushToOutputFile(output, isFinal); - break; - case INTERMEDIATE_MESSAGES: - flushToMessageFile(output, isFinal); - break; - default: - flushToErrorFile(output, isFinal); - } - } - - @Override - public void onException(AppcClientException exception) { - flushToErrorFile("exception: " + exception, true); - } - - private void flushToOutputFile(String output, boolean isFinal) { - this.flushToFile(output, ".output", isFinal); - } - private void flushToMessageFile(String output, boolean isFinal) { - this.flushToFile(output, ".message" + messageCount.getAndIncrement(), isFinal); - - } - - private void flushToErrorFile(String output, boolean isFinal) { - this.flushToFile(output, ".error", isFinal); - } - - private void flushToFile(String output, String suffix, boolean isFinal) { - try (FileWriter fileWriter = new FileWriter(fileName + suffix);){ - LOG.info("Output file : " + fileName + suffix); - - fileWriter.write(output); - fileWriter.flush(); - if (isFinal){ - fileWriter.close(); - } - } catch (Exception e) { - e.printStackTrace(); - } - System.out.println("== THR#" +Thread.currentThread().getId()+ " Output file : " + fileName + suffix); - } -} diff --git a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/main/ClientRunner.java b/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/main/ClientRunner.java deleted file mode 100644 index c8da17e79..000000000 --- a/appc-client/client-simulator/src/main/java/org/openecomp/appc/simulator/client/main/ClientRunner.java +++ /dev/null @@ -1,100 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.simulator.client.main; - -import org.apache.commons.io.filefilter.WildcardFileFilter; -import org.onap.appc.simulator.client.RequestHandler; -import org.onap.appc.simulator.client.impl.JsonRequestHandler; - -import java.io.*; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Properties; - -public class ClientRunner { - - public static void main(String ... args) throws Exception { - String folder = args[0]; - if (folder == null) { - folder = System.getProperty("user.dir"); - } - System.out.println("== THR#" +Thread.currentThread().getId()+ " Reading files under the folder : " + folder); - - String inputType = args[1]; - if (inputType != null && !inputType.matches("JSON")) { - throw new RuntimeException("### ERROR ### - Unsupported file type <" + inputType + "> was provided"); - } - - Properties properties = getProperties(folder); - RequestHandler reqHandler = new JsonRequestHandler(properties); - List sources = getJsonFiles(folder); - File log = new File(folder + "/output.txt"); - int filesNum = 0; - for (File source: sources) { - reqHandler.proceedFile(source, log); - System.out.println("== THR#" +Thread.currentThread().getId()+ " File <" + source.getName() + "> processed."); - ++filesNum; - } - System.out.println("DONE with " + filesNum + " files under the folder : " + folder); - Thread.sleep(30); - System.out.println("Shutdown ..."); - reqHandler.shutdown(Boolean.parseBoolean(properties.getProperty("client.force.shutdown"))); -// System.exit(0); - } - - private static Properties getProperties(String folder) { - Properties prop = new Properties(); - - InputStream conf = null; - try { - conf = new FileInputStream(folder + "client-simulator.properties"); - } catch (FileNotFoundException e) { - - } - if (conf != null) { - try { - prop.load(conf); - } catch (IOException e) { - e.printStackTrace(); - } - } else { - try { - prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("client-simulator.properties")); - } catch (Exception e) { - throw new RuntimeException("### ERROR ### - Could not load properties to test"); - } - } - return prop; - } - - private static List getJsonFiles(String folder) throws FileNotFoundException { - Path dir = Paths.get(folder); - FileFilter fileFilter = new WildcardFileFilter("*.json"); - return new ArrayList(Arrays.asList(dir.toFile().listFiles(fileFilter))); - } -} diff --git a/appc-client/client-simulator/src/test/java/org/onap/appc/simulator/client/impl/TestJsonRequestHandler.java b/appc-client/client-simulator/src/test/java/org/onap/appc/simulator/client/impl/TestJsonRequestHandler.java new file mode 100644 index 000000000..ba3696f26 --- /dev/null +++ b/appc-client/client-simulator/src/test/java/org/onap/appc/simulator/client/impl/TestJsonRequestHandler.java @@ -0,0 +1,53 @@ +package org.onap.appc.simulator.client.impl; + +import org.apache.commons.io.filefilter.WildcardFileFilter; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.onap.appc.client.lcm.api.LifeCycleManagerStateful; +import org.onap.appc.client.lcm.exceptions.AppcClientException; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.io.*; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({LifeCycleManagerStateful.class}) + +public class TestJsonRequestHandler { + + JsonResponseHandler jsonResponseHandler=new JsonResponseHandler(); + @Before + public void init(){ + jsonResponseHandler= Mockito.mock(JsonResponseHandler.class); + } + + + @Test + public void testProceedFiles() throws AppcClientException,java.io.IOException{ + String folder="src/test/resources/data"; + List sources = getJsonFiles(folder); + File source=sources.get(0); + File log = new File(folder + "/output.txt"); + JsonRequestHandler requestHandler = new JsonRequestHandler(); + Mockito.doNothing().when(jsonResponseHandler).onResponse(Matchers.anyBoolean()); + requestHandler.proceedFile(source,log); + + Assert.assertNotNull(log); + + } + + private static List getJsonFiles(String folder) throws FileNotFoundException { + Path dir = Paths.get(folder); + FileFilter fileFilter = new WildcardFileFilter("*.json"); + return new ArrayList(Arrays.asList(dir.toFile().listFiles(fileFilter))); + } + + +} \ No newline at end of file diff --git a/appc-client/client-simulator/src/test/java/org/onap/appc/simulator/client/impl/TestJsonResponseHandler.java b/appc-client/client-simulator/src/test/java/org/onap/appc/simulator/client/impl/TestJsonResponseHandler.java new file mode 100644 index 000000000..59ffe1b31 --- /dev/null +++ b/appc-client/client-simulator/src/test/java/org/onap/appc/simulator/client/impl/TestJsonResponseHandler.java @@ -0,0 +1,46 @@ +package org.onap.appc.simulator.client.impl; + +import org.apache.commons.io.filefilter.WildcardFileFilter; +import org.junit.Assert; +import org.junit.Ignore; +import java.io.*; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + +public class TestJsonResponseHandler { + String folder="/data/output/error.json"; + JsonResponseHandler responseHandler=new JsonResponseHandler(); + + @Ignore + public void testOnResponse() throws URISyntaxException, IOException{ + responseHandler.onResponse(getNode()); + List files=getJsonFiles(folder); + Assert.assertNotNull(files); + + } + + private String readData(String inputFile) throws URISyntaxException, IOException { + File file = new File(this.getClass().getResource(inputFile).toURI()); + + byte[] bFile = new byte[(int) file.length()]; + FileInputStream fileInputStream = new FileInputStream(file); + fileInputStream.read(bFile); + fileInputStream.close(); + return new String(bFile); + } + + private String getNode() throws java.io.IOException{ + String jsonSring="{\"status\": {\"code\": \"200\"}}"; + return jsonSring; +} + public List getJsonFiles(String folder) throws FileNotFoundException { + Path dir = Paths.get(folder); + FileFilter fileFilter = new WildcardFileFilter("*.error"); + return new ArrayList(Arrays.asList(dir.toFile().listFiles(fileFilter))); + } +} diff --git a/appc-client/client-simulator/src/test/java/org/onap/appc/simulator/client/main/TestClientRunner.java b/appc-client/client-simulator/src/test/java/org/onap/appc/simulator/client/main/TestClientRunner.java new file mode 100644 index 000000000..db648f957 --- /dev/null +++ b/appc-client/client-simulator/src/test/java/org/onap/appc/simulator/client/main/TestClientRunner.java @@ -0,0 +1,102 @@ +package org.onap.appc.simulator.client.main; + +import org.apache.commons.io.filefilter.WildcardFileFilter; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.onap.appc.client.lcm.exceptions.AppcClientException; +import org.onap.appc.simulator.client.impl.JsonRequestHandler; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.io.*; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({JsonRequestHandler.class,ClientRunner.class}) + +public class TestClientRunner { + + JsonRequestHandler jsonRequestHandler; + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + + @Before + public void init() throws AppcClientException{ + System.setOut(new PrintStream(outContent)); + jsonRequestHandler= Mockito.mock(JsonRequestHandler.class); + + } + + @After + public void cleanUpStreams() { + System.setOut(null); + } + + @Test + public void testMain() throws java.io.IOException,java.lang.Exception{ + String []arguments=new String[]{"src/test/resources/data","JSON"}; + PowerMockito.whenNew(JsonRequestHandler.class).withArguments(Mockito.anyObject()).thenReturn(jsonRequestHandler); + Mockito.doNothing().when(jsonRequestHandler).proceedFile(Matchers.anyObject(), Matchers.anyObject()); + + ClientRunner.main(arguments); + String expectedOutput=outContent.toString(); + Assert.assertEquals(expectedOutput,outContent.toString()); + } + + @Test + public void testGetPrperties(){ + String folder="src/test/resources/data"; + Properties properties=new Properties(); + properties=getProperties(folder); + Assert.assertNotNull(properties); + } + + @Test + public void testGetJsonFIles() throws FileNotFoundException{ + String folder="src/test/resources/data"; + List sources = getJsonFiles(folder); + Assert.assertNotNull(sources); + } + + private static Properties getProperties(String folder) { + Properties prop = new Properties(); + + InputStream conf = null; + try { + conf = new FileInputStream(folder + "client-simulator.properties"); + } catch (FileNotFoundException e) { + + } + if (conf != null) { + try { + prop.load(conf); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + try { + prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("client-simulator.properties")); + } catch (Exception e) { + throw new RuntimeException("### ERROR ### - Could not load properties to test"); + } + } + return prop; + } + + private static List getJsonFiles(String folder) throws FileNotFoundException { + Path dir = Paths.get(folder); + FileFilter fileFilter = new WildcardFileFilter("*.json"); + return new ArrayList(Arrays.asList(dir.toFile().listFiles(fileFilter))); + } + +} diff --git a/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonRequestHandler.java b/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonRequestHandler.java deleted file mode 100644 index ba3696f26..000000000 --- a/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonRequestHandler.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.onap.appc.simulator.client.impl; - -import org.apache.commons.io.filefilter.WildcardFileFilter; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Matchers; -import org.mockito.Mockito; -import org.onap.appc.client.lcm.api.LifeCycleManagerStateful; -import org.onap.appc.client.lcm.exceptions.AppcClientException; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import java.io.*; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.*; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({LifeCycleManagerStateful.class}) - -public class TestJsonRequestHandler { - - JsonResponseHandler jsonResponseHandler=new JsonResponseHandler(); - @Before - public void init(){ - jsonResponseHandler= Mockito.mock(JsonResponseHandler.class); - } - - - @Test - public void testProceedFiles() throws AppcClientException,java.io.IOException{ - String folder="src/test/resources/data"; - List sources = getJsonFiles(folder); - File source=sources.get(0); - File log = new File(folder + "/output.txt"); - JsonRequestHandler requestHandler = new JsonRequestHandler(); - Mockito.doNothing().when(jsonResponseHandler).onResponse(Matchers.anyBoolean()); - requestHandler.proceedFile(source,log); - - Assert.assertNotNull(log); - - } - - private static List getJsonFiles(String folder) throws FileNotFoundException { - Path dir = Paths.get(folder); - FileFilter fileFilter = new WildcardFileFilter("*.json"); - return new ArrayList(Arrays.asList(dir.toFile().listFiles(fileFilter))); - } - - -} \ No newline at end of file diff --git a/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonResponseHandler.java b/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonResponseHandler.java deleted file mode 100644 index 59ffe1b31..000000000 --- a/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/impl/TestJsonResponseHandler.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.onap.appc.simulator.client.impl; - -import org.apache.commons.io.filefilter.WildcardFileFilter; -import org.junit.Assert; -import org.junit.Ignore; -import java.io.*; -import java.net.URISyntaxException; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - - -public class TestJsonResponseHandler { - String folder="/data/output/error.json"; - JsonResponseHandler responseHandler=new JsonResponseHandler(); - - @Ignore - public void testOnResponse() throws URISyntaxException, IOException{ - responseHandler.onResponse(getNode()); - List files=getJsonFiles(folder); - Assert.assertNotNull(files); - - } - - private String readData(String inputFile) throws URISyntaxException, IOException { - File file = new File(this.getClass().getResource(inputFile).toURI()); - - byte[] bFile = new byte[(int) file.length()]; - FileInputStream fileInputStream = new FileInputStream(file); - fileInputStream.read(bFile); - fileInputStream.close(); - return new String(bFile); - } - - private String getNode() throws java.io.IOException{ - String jsonSring="{\"status\": {\"code\": \"200\"}}"; - return jsonSring; -} - public List getJsonFiles(String folder) throws FileNotFoundException { - Path dir = Paths.get(folder); - FileFilter fileFilter = new WildcardFileFilter("*.error"); - return new ArrayList(Arrays.asList(dir.toFile().listFiles(fileFilter))); - } -} diff --git a/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/main/TestClientRunner.java b/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/main/TestClientRunner.java deleted file mode 100644 index db648f957..000000000 --- a/appc-client/client-simulator/src/test/java/org/openecomp/appc/simulator/client/main/TestClientRunner.java +++ /dev/null @@ -1,102 +0,0 @@ -package org.onap.appc.simulator.client.main; - -import org.apache.commons.io.filefilter.WildcardFileFilter; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Matchers; -import org.mockito.Mockito; -import org.onap.appc.client.lcm.exceptions.AppcClientException; -import org.onap.appc.simulator.client.impl.JsonRequestHandler; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import java.io.*; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Properties; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({JsonRequestHandler.class,ClientRunner.class}) - -public class TestClientRunner { - - JsonRequestHandler jsonRequestHandler; - private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); - - @Before - public void init() throws AppcClientException{ - System.setOut(new PrintStream(outContent)); - jsonRequestHandler= Mockito.mock(JsonRequestHandler.class); - - } - - @After - public void cleanUpStreams() { - System.setOut(null); - } - - @Test - public void testMain() throws java.io.IOException,java.lang.Exception{ - String []arguments=new String[]{"src/test/resources/data","JSON"}; - PowerMockito.whenNew(JsonRequestHandler.class).withArguments(Mockito.anyObject()).thenReturn(jsonRequestHandler); - Mockito.doNothing().when(jsonRequestHandler).proceedFile(Matchers.anyObject(), Matchers.anyObject()); - - ClientRunner.main(arguments); - String expectedOutput=outContent.toString(); - Assert.assertEquals(expectedOutput,outContent.toString()); - } - - @Test - public void testGetPrperties(){ - String folder="src/test/resources/data"; - Properties properties=new Properties(); - properties=getProperties(folder); - Assert.assertNotNull(properties); - } - - @Test - public void testGetJsonFIles() throws FileNotFoundException{ - String folder="src/test/resources/data"; - List sources = getJsonFiles(folder); - Assert.assertNotNull(sources); - } - - private static Properties getProperties(String folder) { - Properties prop = new Properties(); - - InputStream conf = null; - try { - conf = new FileInputStream(folder + "client-simulator.properties"); - } catch (FileNotFoundException e) { - - } - if (conf != null) { - try { - prop.load(conf); - } catch (IOException e) { - e.printStackTrace(); - } - } else { - try { - prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("client-simulator.properties")); - } catch (Exception e) { - throw new RuntimeException("### ERROR ### - Could not load properties to test"); - } - } - return prop; - } - - private static List getJsonFiles(String folder) throws FileNotFoundException { - Path dir = Paths.get(folder); - FileFilter fileFilter = new WildcardFileFilter("*.json"); - return new ArrayList(Arrays.asList(dir.toFile().listFiles(fileFilter))); - } - -} -- cgit 1.2.3-korg