Skip to content

Commit ff5653c

Browse files
author
SantoshNC68
authored
Added code for handling single endpoint in generate (#46)
* 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
1 parent 8ec0f96 commit ff5653c

File tree

19 files changed

+259
-608
lines changed

19 files changed

+259
-608
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ apply plugin: 'war'
1919

2020
war {
2121
baseName = 'remrem-generate'
22-
version = '0.7.2'
22+
version = '0.7.3'
2323
}
2424

2525

src/integration-test/java/com/ericsson/eiffel/remrem/generate/EiffelSemanticsCli.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,4 @@ public void testMalformedJsonFail() throws Exception {
9494
assertTrue(CLIOptions.getErrorCodes().contains(code));
9595
}
9696

97-
@Test
98-
public void testJarLoading() throws Exception{
99-
String jarFile = "sample.jar";
100-
String[] args = {"-t", "eiffelartifactpublished", "-f", "ActivityFinished.json","-jp",jarFile};
101-
CLIOptions.parse(args);
102-
assertTrue(CLIOptions.getErrorCodes().isEmpty());
103-
}
10497
}

src/integration-test/java/com/ericsson/eiffel/remrem/generate/EiffelSemanticsController.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ public class EiffelSemanticsController {
4848
@Before
4949
public void setUp() throws FileNotFoundException {
5050
RestAssured.port = port;
51-
52-
File file = new File(getClass().getClassLoader().getResource(artifactPublishedFileName).getFile());
51+
URL url = getClass().getClassLoader().getResource(artifactPublishedFileName);
52+
String path = url.getPath().replace("%20"," ");
53+
File file = new File(path);
5354
artifactPublishedBody = new Scanner(file)
5455
.useDelimiter("\\A").next();
5556

56-
file = new File(getClass().getClassLoader().getResource(activityFinishedFileName).getFile());
57+
url = getClass().getClassLoader().getResource(activityFinishedFileName);
58+
path = url.getPath().replace("%20"," ");
59+
file = new File(path);
5760
activityFinishedBody = new Scanner(file)
5861
.useDelimiter("\\A").next();
5962

src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
import java.io.PrintWriter;
77
import java.nio.file.Files;
88
import java.nio.file.Paths;
9+
import java.util.List;
910
import java.util.Locale;
1011
import java.util.regex.Matcher;
1112
import java.util.regex.Pattern;
1213

1314
import org.apache.commons.cli.CommandLine;
1415
import org.slf4j.LoggerFactory;
1516
import org.springframework.beans.factory.annotation.Autowired;
17+
import org.springframework.beans.factory.annotation.Value;
18+
import org.springframework.boot.CommandLineRunner;
1619
import org.springframework.context.annotation.ComponentScan;
1720
import org.springframework.stereotype.Component;
18-
import org.springframework.boot.CommandLineRunner;
21+
1922
import com.ericsson.eiffel.remrem.generate.config.PropertiesConfig;
2023
import com.ericsson.eiffel.remrem.protocol.MsgService;
2124
import com.ericsson.eiffel.remrem.semantics.SemanticsService;
@@ -38,18 +41,32 @@
3841
@Component
3942
@ComponentScan(basePackages = "com.ericsson.eiffel.remrem")
4043
public class CLI implements CommandLineRunner {
44+
45+
@Value("${jar.path}")
46+
private String jarPath;
47+
48+
public void setJarPath(String jarPath) {
49+
this.jarPath = jarPath;
50+
}
51+
52+
public String getJarPath() {
53+
return jarPath;
54+
}
55+
4156
@Autowired
42-
private MsgService[] msgServices;
57+
private List<MsgService> msgServices;
58+
//private MsgService[] msgServices;
4359

44-
public CLI(MsgService[] msgServices) {
60+
public CLI(List<MsgService> msgServices) {
4561
super();
4662
this.msgServices = msgServices;
4763
}
4864

4965
@Override
5066
public void run(String... args) throws Exception {
51-
if (CLIOptions.hasParsedOptions())
67+
if (CLIOptions.hasParsedOptions()){
5268
handleOptions();
69+
}
5370
}
5471

5572
/**
@@ -75,7 +92,6 @@ private void handleLogging(CommandLine commandLine) {
7592
private void handleOptions() {
7693
CommandLine commandLine = CLIOptions.getCommandLine();
7794
handleLogging(commandLine);
78-
CLIOptions.handleJarPath();
7995
if (commandLine.hasOption("h")) {
8096
System.out.println("You passed help flag.");
8197
CLIOptions.help(1);
@@ -186,11 +202,10 @@ private String handleMsgTypeArgs(CommandLine commandLine) {
186202
private MsgService getMessageService(CommandLine commandLine) {
187203
if (commandLine.hasOption("mp")) {
188204
String protocol = commandLine.getOptionValue("mp");
189-
for (MsgService service : msgServices) {
190-
boolean isEiffel3 = (protocol.equals("eiffel3"));
191-
boolean isEiffel3Service = service.getClass().getName().endsWith("Eiffel3Service");
192-
if (isEiffel3 && isEiffel3Service)
205+
for(MsgService service: msgServices){
206+
if(service.getServiceName().equals(protocol)){
193207
return service;
208+
}
194209
}
195210
} else {
196211
for (MsgService service : msgServices) {
@@ -200,8 +215,9 @@ private MsgService getMessageService(CommandLine commandLine) {
200215
}
201216

202217
boolean testMode = Boolean.getBoolean(PropertiesConfig.TEST_MODE);
203-
if (testMode && msgServices.length>0)
204-
return msgServices[0];
218+
if (testMode && msgServices.size()>0)
219+
return msgServices.get(0);
220+
//return msgServices[0];
205221

206222
System.out.println("No protocol service has been found registered.");
207223
CLIOptions.exit(CLIExitCodes.MESSAGE_PROTOCOL_NOT_FOUND);

src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLIOptions.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.apache.commons.cli.OptionGroup;
1313
import org.apache.commons.cli.Options;
1414

15-
import com.ericsson.eiffel.remrem.generate.helper.RemremJarHelper;
1615
import com.ericsson.eiffel.remrem.generate.config.PropertiesConfig;
1716
import com.ericsson.eiffel.remrem.shared.VersionService;
1817

@@ -59,9 +58,6 @@ public static Options createCLIOptions() {
5958
options.addOption("d", "debug", false, "enable debug traces");
6059
options.addOption("mp", "messaging_protocol", true,
6160
"name of messaging protocol to be used, e.g. eiffel3, semantics");
62-
63-
options.addOption("jp", "jar_path", true,
64-
"path to find protocol definition jar files, e.g. C:/Users/xyz/Desktop/eiffel3messaging.jar");
6561
contentGroup = new OptionGroup();
6662
contentGroup.addOption(new Option("f", "content_file", true, "message content file"));
6763
contentGroup.addOption(new Option("json", "json_content", true, "json content"));
@@ -173,16 +169,4 @@ public static boolean hasParsedOptions() {
173169
return commandLine.getOptions().length > 0;
174170
}
175171

176-
public static void handleJarPath(){
177-
if (commandLine.hasOption("jp")) {
178-
String jarPath = commandLine.getOptionValue("jp");
179-
System.out.println("JarPath :: "+ jarPath);
180-
try {
181-
RemremJarHelper.addJarsToClassPath(jarPath);
182-
} catch (Exception e) {
183-
e.printStackTrace();
184-
System.out.println("Error while loading jars from the path mentioned, MESSAGE :: " + e.getMessage());
185-
}
186-
}
187-
}
188172
}

src/main/java/com/ericsson/eiffel/remrem/generate/controller/Eiffel3Controller.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/main/java/com/ericsson/eiffel/remrem/generate/controller/EiffelSemanticsController.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.ericsson.eiffel.remrem.generate.controller;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestMethod;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import com.ericsson.eiffel.remrem.protocol.MsgService;
14+
import com.google.gson.JsonElement;
15+
import com.google.gson.JsonObject;
16+
import com.google.gson.JsonParser;
17+
18+
@RestController @RequestMapping("/*")
19+
public class RemremGenerateController {
20+
21+
@Autowired
22+
private List<MsgService> msgServices;
23+
private JsonParser parser = new JsonParser();
24+
25+
/**
26+
* Returns event information as json element based on the message protocol, taking message type and json body as input.
27+
*
28+
*
29+
* Parameters:
30+
* mp - The message protocol , which tells us which service to invoke.
31+
* msgType - The type of message that needs to be generated.
32+
* bodyJson - The content of the message which is used in creating the event details.
33+
*
34+
* Returns:
35+
* The event information as a json element
36+
*
37+
*/
38+
@RequestMapping(value = "/{mp}", method = RequestMethod.POST)
39+
public JsonElement generate(@PathVariable String mp, @RequestParam("msgType") String msgType,
40+
@RequestBody JsonObject bodyJson) {
41+
MsgService msgService = getMessageService(mp);
42+
if (msgService != null) {
43+
return parser.parse(msgService.generateMsg(msgType, bodyJson));
44+
} else {
45+
return null;
46+
}
47+
}
48+
49+
private MsgService getMessageService(String messageProtocol) {
50+
for (MsgService service : msgServices) {
51+
if (service.getServiceName().equals(messageProtocol)) {
52+
return service;
53+
}
54+
}
55+
return null;
56+
}
57+
}

src/main/java/com/ericsson/eiffel/remrem/generate/helper/RemremJarHelper.java

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)