Skip to content

Commit

Permalink
Added code for handling single endpoint in generate (#46)
Browse files Browse the repository at this point in the history
* Dynamic loading of Protocol jars

* Added code for single end point in generate

* Added for Dynamic protocol service class loading

* Added single end point as a path param

* Added message protocol as part of the path

* Added change in code for improving the rest calls

* Added mock test for RemremGenerateController

* change array to list to make easier dependency injections during tests

* add working service tests

* replace deprecated assert

* Added some more mock tests for Remrem Generate

* Deleted jar path dependent test case

* Added latest version for generate in build.gradle

* Changes for test cases
  • Loading branch information
SantoshNC68 authored Jan 17, 2017
1 parent 8ec0f96 commit ff5653c
Show file tree
Hide file tree
Showing 19 changed files with 259 additions and 608 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ apply plugin: 'war'

war {
baseName = 'remrem-generate'
version = '0.7.2'
version = '0.7.3'
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,4 @@ public void testMalformedJsonFail() throws Exception {
assertTrue(CLIOptions.getErrorCodes().contains(code));
}

@Test
public void testJarLoading() throws Exception{
String jarFile = "sample.jar";
String[] args = {"-t", "eiffelartifactpublished", "-f", "ActivityFinished.json","-jp",jarFile};
CLIOptions.parse(args);
assertTrue(CLIOptions.getErrorCodes().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ public class EiffelSemanticsController {
@Before
public void setUp() throws FileNotFoundException {
RestAssured.port = port;

File file = new File(getClass().getClassLoader().getResource(artifactPublishedFileName).getFile());
URL url = getClass().getClassLoader().getResource(artifactPublishedFileName);
String path = url.getPath().replace("%20"," ");
File file = new File(path);
artifactPublishedBody = new Scanner(file)
.useDelimiter("\\A").next();

file = new File(getClass().getClassLoader().getResource(activityFinishedFileName).getFile());
url = getClass().getClassLoader().getResource(activityFinishedFileName);
path = url.getPath().replace("%20"," ");
file = new File(path);
activityFinishedBody = new Scanner(file)
.useDelimiter("\\A").next();

Expand Down
38 changes: 27 additions & 11 deletions src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.cli.CommandLine;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.boot.CommandLineRunner;

import com.ericsson.eiffel.remrem.generate.config.PropertiesConfig;
import com.ericsson.eiffel.remrem.protocol.MsgService;
import com.ericsson.eiffel.remrem.semantics.SemanticsService;
Expand All @@ -38,18 +41,32 @@
@Component
@ComponentScan(basePackages = "com.ericsson.eiffel.remrem")
public class CLI implements CommandLineRunner {

@Value("${jar.path}")
private String jarPath;

public void setJarPath(String jarPath) {
this.jarPath = jarPath;
}

public String getJarPath() {
return jarPath;
}

@Autowired
private MsgService[] msgServices;
private List<MsgService> msgServices;
//private MsgService[] msgServices;

public CLI(MsgService[] msgServices) {
public CLI(List<MsgService> msgServices) {
super();
this.msgServices = msgServices;
}

@Override
public void run(String... args) throws Exception {
if (CLIOptions.hasParsedOptions())
if (CLIOptions.hasParsedOptions()){
handleOptions();
}
}

/**
Expand All @@ -75,7 +92,6 @@ private void handleLogging(CommandLine commandLine) {
private void handleOptions() {
CommandLine commandLine = CLIOptions.getCommandLine();
handleLogging(commandLine);
CLIOptions.handleJarPath();
if (commandLine.hasOption("h")) {
System.out.println("You passed help flag.");
CLIOptions.help(1);
Expand Down Expand Up @@ -186,11 +202,10 @@ private String handleMsgTypeArgs(CommandLine commandLine) {
private MsgService getMessageService(CommandLine commandLine) {
if (commandLine.hasOption("mp")) {
String protocol = commandLine.getOptionValue("mp");
for (MsgService service : msgServices) {
boolean isEiffel3 = (protocol.equals("eiffel3"));
boolean isEiffel3Service = service.getClass().getName().endsWith("Eiffel3Service");
if (isEiffel3 && isEiffel3Service)
for(MsgService service: msgServices){
if(service.getServiceName().equals(protocol)){
return service;
}
}
} else {
for (MsgService service : msgServices) {
Expand All @@ -200,8 +215,9 @@ private MsgService getMessageService(CommandLine commandLine) {
}

boolean testMode = Boolean.getBoolean(PropertiesConfig.TEST_MODE);
if (testMode && msgServices.length>0)
return msgServices[0];
if (testMode && msgServices.size()>0)
return msgServices.get(0);
//return msgServices[0];

System.out.println("No protocol service has been found registered.");
CLIOptions.exit(CLIExitCodes.MESSAGE_PROTOCOL_NOT_FOUND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;

import com.ericsson.eiffel.remrem.generate.helper.RemremJarHelper;
import com.ericsson.eiffel.remrem.generate.config.PropertiesConfig;
import com.ericsson.eiffel.remrem.shared.VersionService;

Expand Down Expand Up @@ -59,9 +58,6 @@ public static Options createCLIOptions() {
options.addOption("d", "debug", false, "enable debug traces");
options.addOption("mp", "messaging_protocol", true,
"name of messaging protocol to be used, e.g. eiffel3, semantics");

options.addOption("jp", "jar_path", true,
"path to find protocol definition jar files, e.g. C:/Users/xyz/Desktop/eiffel3messaging.jar");
contentGroup = new OptionGroup();
contentGroup.addOption(new Option("f", "content_file", true, "message content file"));
contentGroup.addOption(new Option("json", "json_content", true, "json content"));
Expand Down Expand Up @@ -173,16 +169,4 @@ public static boolean hasParsedOptions() {
return commandLine.getOptions().length > 0;
}

public static void handleJarPath(){
if (commandLine.hasOption("jp")) {
String jarPath = commandLine.getOptionValue("jp");
System.out.println("JarPath :: "+ jarPath);
try {
RemremJarHelper.addJarsToClassPath(jarPath);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error while loading jars from the path mentioned, MESSAGE :: " + e.getMessage());
}
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.ericsson.eiffel.remrem.generate.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ericsson.eiffel.remrem.protocol.MsgService;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

@RestController @RequestMapping("/*")
public class RemremGenerateController {

@Autowired
private List<MsgService> msgServices;
private JsonParser parser = new JsonParser();

/**
* Returns event information as json element based on the message protocol, taking message type and json body as input.
*
*
* Parameters:
* mp - The message protocol , which tells us which service to invoke.
* msgType - The type of message that needs to be generated.
* bodyJson - The content of the message which is used in creating the event details.
*
* Returns:
* The event information as a json element
*
*/
@RequestMapping(value = "/{mp}", method = RequestMethod.POST)
public JsonElement generate(@PathVariable String mp, @RequestParam("msgType") String msgType,
@RequestBody JsonObject bodyJson) {
MsgService msgService = getMessageService(mp);
if (msgService != null) {
return parser.parse(msgService.generateMsg(msgType, bodyJson));
} else {
return null;
}
}

private MsgService getMessageService(String messageProtocol) {
for (MsgService service : msgServices) {
if (service.getServiceName().equals(messageProtocol)) {
return service;
}
}
return null;
}
}

This file was deleted.

Loading

0 comments on commit ff5653c

Please sign in to comment.