From 3e4f983cdc2d4bb92a47f589c6e465895e495ae7 Mon Sep 17 00:00:00 2001 From: SantoshNC68 Date: Mon, 27 Feb 2017 19:48:50 +0530 Subject: [PATCH] Added status codes to the output in Remrem generate service (#60) * Added status codes to the output in Remrem generate service * Added changes for code review * Added changes for Review comments * Added changes for Remrem-Semantics latest version --- CHANGELOG.md | 3 + build.gradle | 6 +- cli/build.gradle | 6 +- .../remrem/generate/EiffelSemanticsCli.java | 4 +- .../eiffel/remrem/generate/cli/CLI.java | 3 +- .../remrem/generate/cli/CliUnitTests.java | 10 +-- .../generate/EiffelSemanticsController.java | 8 +- .../resources/ActivityFinished.json | 18 ++++- .../resources/ArtifactPublished.json | 8 +- .../RemremGenerateServiceConstants.java | 8 ++ .../controller/RemremGenerateController.java | 30 +++++-- .../EiffelRemremControllerUnitTest.java | 80 +++++++++++-------- service/src/test/resources/errorInput.json | 1 + service/src/test/resources/successInput.json | 1 + 14 files changed, 122 insertions(+), 64 deletions(-) create mode 100644 service/src/main/java/com/ericsson/eiffel/remrem/generate/constants/RemremGenerateServiceConstants.java create mode 100644 service/src/test/resources/errorInput.json create mode 100644 service/src/test/resources/successInput.json diff --git a/CHANGELOG.md b/CHANGELOG.md index a2b0a37a..d700af89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.7.8 +- Added HttpStatus codes for generated ouput + ## 0.7.7 - Changed the project structure to build seperate binaries for CLI and Service. diff --git a/build.gradle b/build.gradle index 4c489786..d5ad352e 100644 --- a/build.gradle +++ b/build.gradle @@ -18,6 +18,8 @@ repositories { maven { url "https://jitpack.io" } } + +//Adding dependencies and sub projects common build script subprojects { buildscript { repositories { @@ -38,7 +40,7 @@ subprojects { targetCompatibility = 1.8 //Latest version for generate - version = "0.7.7" + version = "0.7.8" repositories { mavenCentral() @@ -48,7 +50,7 @@ subprojects { dependencies { //Injectable Message Library and its Implementation compile ('com.github.Ericsson:eiffel-remrem-shared:0.3.0') - compile ('com.github.Ericsson:eiffel-remrem-semantics:0.2.0') + compile ('com.github.Ericsson:eiffel-remrem-semantics:0.2.3') compile ('com.github.Ericsson:eiffel-remrem-protocol-interface:0.0.1') //Authentication diff --git a/cli/build.gradle b/cli/build.gradle index 005c35c7..2c8836a0 100644 --- a/cli/build.gradle +++ b/cli/build.gradle @@ -1,3 +1,4 @@ +//Plugins used for CLI plugins{ id 'java' id 'maven' @@ -63,6 +64,7 @@ apply plugin: 'spring-boot' apply plugin: 'java' apply plugin: 'eclipse' +//this is used to build the generate-cli binary jar{ baseName = 'generate-cli' manifest { @@ -92,15 +94,13 @@ repositories { mavenCentral() } +//Dependencies for remrem-generate-cli dependencies { compile 'org.slf4j:slf4j-api:1.7.13' //commons CLI compile 'commons-cli:commons-cli:1.3.1' - - // https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api compile 'javax.servlet:javax.servlet-api:3.0.1' - // Will be used to package contents of third party libs runtime fileTree(dir: 'libs', include: '*.jar') } diff --git a/cli/src/integration-test/java/com/ericsson/eiffel/remrem/generate/EiffelSemanticsCli.java b/cli/src/integration-test/java/com/ericsson/eiffel/remrem/generate/EiffelSemanticsCli.java index 6e0c960f..e135182f 100644 --- a/cli/src/integration-test/java/com/ericsson/eiffel/remrem/generate/EiffelSemanticsCli.java +++ b/cli/src/integration-test/java/com/ericsson/eiffel/remrem/generate/EiffelSemanticsCli.java @@ -74,7 +74,7 @@ public void testWrongMessageTypeFail() throws Exception { @Test public void testIncompleteMessageContentFail() throws Exception { String jsonContent = "{\"msgParams\": {\"meta\":{\"fakseContent\":\"yes\"}}, \"eventParams\": {\"falseKey\" : \"none\"}}"; - String[] args = {"-t", "EiffelArtifactPublishedEvent", "-json", jsonContent}; + String[] args = {"-t", "eiffelartifactpublished", "-json", jsonContent}; CLIOptions.parse(args); cli.main(args); String message = bytes.toString(); @@ -85,7 +85,7 @@ public void testIncompleteMessageContentFail() throws Exception { @Test public void testMalformedJsonFail() throws Exception { String jsonContent = "{\"someKey\":\"someValue\"}"; - String[] args = {"-t", "EiffelArtifactPublishedEvent", "-json", jsonContent}; + String[] args = {"-t", "eiffelactivityfinished", "-json", jsonContent}; CLIOptions.parse(args); cli.main(args); String message = bytes.toString(); diff --git a/cli/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java b/cli/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java index 8856c6cd..1c5f3cd1 100644 --- a/cli/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java +++ b/cli/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java @@ -7,6 +7,7 @@ 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; @@ -178,7 +179,7 @@ private void handleJsonString(String jsonString, CommandLine commandLine) { } private String handleMsgTypeArgs(CommandLine commandLine) { - String msgType = commandLine.getOptionValue("t"); + String msgType = commandLine.getOptionValue("t").toLowerCase(Locale.ROOT); Pattern p = Pattern.compile("(.*)event"); Matcher m = p.matcher(msgType); if (m.matches()) { diff --git a/cli/src/test/java/com/ericsson/eiffel/remrem/generate/cli/CliUnitTests.java b/cli/src/test/java/com/ericsson/eiffel/remrem/generate/cli/CliUnitTests.java index 335ea0da..cdedebe7 100644 --- a/cli/src/test/java/com/ericsson/eiffel/remrem/generate/cli/CliUnitTests.java +++ b/cli/src/test/java/com/ericsson/eiffel/remrem/generate/cli/CliUnitTests.java @@ -57,7 +57,7 @@ public void tearDown() { @Test public void testHandleFileArgsFail() throws Exception { - String[] args = {"-t", "EiffelArtifactPublishedEvent", "-f", "filename"}; + String[] args = {"-t", "eiffelactivityfinished", "-f", "filename"}; CLIOptions.parse(args); cli.run(args); int code = CLIExitCodes.CLI_READ_FILE_FAILED; @@ -71,7 +71,7 @@ public void testHandleFileArgsPass() throws Exception { File file = new File(path); String filePath = file.getAbsolutePath(); - String[] args = {"-t", "EiffelArtifactPublishedEvent", "-f", filePath}; + String[] args = {"-t", "eiffelactivityfinished", "-f", filePath}; CLIOptions.parse(args); cli.run(args); assertTrue(CLIOptions.getErrorCodes().isEmpty()); @@ -79,7 +79,7 @@ public void testHandleFileArgsPass() throws Exception { @Test public void testHandleJsonArgsPass() throws Exception { - String[] args = {"-t", "EiffelArtifactPublishedEvent", "-json", "{someKey:someValue}"}; + String[] args = {"-t", "eiffelactivityfinished", "-json", "{someKey:someValue}"}; CLIOptions.parse(args); cli.run(args); assertTrue(CLIOptions.getErrorCodes().isEmpty()); @@ -87,7 +87,7 @@ public void testHandleJsonArgsPass() throws Exception { @Test public void testHandleJsonArgsFail() throws Exception { - String[] args = {"-t", "EiffelArtifactPublishedEvent", "-json", "filename"}; + String[] args = {"-t", "eiffelactivityfinished", "-json", "filename"}; CLIOptions.parse(args); cli.run(args); int code = CLIExitCodes.HANDLE_JSON_STRING_FAILED; @@ -96,7 +96,7 @@ public void testHandleJsonArgsFail() throws Exception { @Test public void testHandleMsgTypeEventArgsPass() throws Exception { - String[] args = {"-t", "EiffelArtifactPublishedEvent", "-json", "{someKey:someValue}"}; + String[] args = {"-t", "eiffelactivityfinished", "-json", "{someKey:someValue}"}; CLIOptions.parse(args); cli.run(args); assertTrue(CLIOptions.getErrorCodes().isEmpty()); diff --git a/service/src/integration-test/java/com/ericsson/eiffel/remrem/generate/EiffelSemanticsController.java b/service/src/integration-test/java/com/ericsson/eiffel/remrem/generate/EiffelSemanticsController.java index 2c763042..329ff14c 100644 --- a/service/src/integration-test/java/com/ericsson/eiffel/remrem/generate/EiffelSemanticsController.java +++ b/service/src/integration-test/java/com/ericsson/eiffel/remrem/generate/EiffelSemanticsController.java @@ -111,10 +111,10 @@ public void testUnauthenticatedNotAllowed() throws Exception { .contentType("application/json") .body(artifactPublishedBody) .when() - .post("/eiffelsemantics?msgType=EiffelArtifactPublishedEvent") + .post("/eiffelsemantics?msgType=eiffelartifactpublished") .then() .statusCode(HttpStatus.SC_OK) - .body("meta.type", Matchers.is("EiffelArtifactPublishedEvent")) + .body("meta.type", Matchers.is("eiffelartifactpublished")) .body("meta.version", Matchers.is(version)); } @@ -124,10 +124,10 @@ public void testUnauthenticatedNotAllowed() throws Exception { .contentType("application/json") .body(activityFinishedBody) .when() - .post("/eiffelsemantics?msgType=EiffelActivityFinishedEvent") + .post("/eiffelsemantics?msgType=eiffelactivityfinished") .then() .statusCode(HttpStatus.SC_OK) - .body("meta.type", Matchers.is("EiffelActivityFinishedEvent")) + .body("meta.type", Matchers.is("eiffelactivityfinished")) .body("meta.version", Matchers.is(version)); } diff --git a/service/src/integration-test/resources/ActivityFinished.json b/service/src/integration-test/resources/ActivityFinished.json index 54734806..d7cfb67d 100644 --- a/service/src/integration-test/resources/ActivityFinished.json +++ b/service/src/integration-test/resources/ActivityFinished.json @@ -1,7 +1,7 @@ { "msgParams": { "meta": { - "type": "EiffelActivityFinishedEvent", + "type": "eiffelactivityfinished", "version": "1.0.0", "tags": [ "tag1", @@ -11,7 +11,7 @@ "domainId": "domainID", "host": "host", "name": "name", - "uri": "http://java.sun.com/j2se/1.3/", + "uri": "http:\/\/java.sun.com\/j2se\/1.3\/", "serializer": { "groupId": "G", "artifactId": "A", @@ -22,6 +22,16 @@ }, "eventParams": { "data": { + "customData": [ + { + "key": "firstLog", + "value": "http:\/\/myHost.com\/firstLog" + }, + { + "key": "otherLog", + "value": "http:\/\/myHost.com\/firstLog33" + } + ], "outcome": { "conclusion": "TIMED_OUT", "description": "Compilation timed out." @@ -29,11 +39,11 @@ "persistentLogs": [ { "name": "firstLog", - "uri": "http://myHost.com/firstLog" + "uri": "http:\/\/myHost.com\/firstLog" }, { "name": "otherLog", - "uri": "http://myHost.com/firstLog33" + "uri": "http:\/\/myHost.com\/firstLog33" } ] }, diff --git a/service/src/integration-test/resources/ArtifactPublished.json b/service/src/integration-test/resources/ArtifactPublished.json index 6aeff463..09a3e175 100644 --- a/service/src/integration-test/resources/ArtifactPublished.json +++ b/service/src/integration-test/resources/ArtifactPublished.json @@ -1,7 +1,7 @@ { "msgParams": { "meta": { - "type": "EiffelArtifactPublishedEvent", + "type": "eiffelartifactpublished", "version": "1.0.0", "tags": [ "tag1", @@ -11,7 +11,7 @@ "domainId": "example.domain", "host": "host", "name": "name", - "uri": "http://java.sun.com/j2se/1.3/", + "uri": "http:\/\/java.sun.com\/j2se\/1.3\/", "serializer": { "groupId": "G", "artifactId": "A", @@ -25,11 +25,11 @@ "locations": [ { "type": "ARTIFACTORY", - "uri": "https://one.place" + "uri": "https:\/\/one.place" }, { "type": "PLAIN", - "uri": "http://another.com" + "uri": "http:\/\/another.com" } ] }, diff --git a/service/src/main/java/com/ericsson/eiffel/remrem/generate/constants/RemremGenerateServiceConstants.java b/service/src/main/java/com/ericsson/eiffel/remrem/generate/constants/RemremGenerateServiceConstants.java new file mode 100644 index 00000000..c3e388b0 --- /dev/null +++ b/service/src/main/java/com/ericsson/eiffel/remrem/generate/constants/RemremGenerateServiceConstants.java @@ -0,0 +1,8 @@ +package com.ericsson.eiffel.remrem.generate.constants; + +public final class RemremGenerateServiceConstants { + + public static final String NO_SERVICE_ERROR = "{\"message\":\"No protocol service has been found registered.\"}"; + public static final String JSON_ERROR_MESSAGE_FIELD = "message"; + +} diff --git a/service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java b/service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java index c331623e..657a381b 100644 --- a/service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java +++ b/service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java @@ -3,6 +3,8 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -10,10 +12,11 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import com.ericsson.eiffel.remrem.generate.constants.RemremGenerateServiceConstants; import com.ericsson.eiffel.remrem.protocol.MsgService; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.JsonParser; +import com.google.gson.JsonParser;; @RestController @RequestMapping("/*") public class RemremGenerateController { @@ -36,16 +39,31 @@ public class RemremGenerateController { * */ @RequestMapping(value = "/{mp}", method = RequestMethod.POST) - public JsonElement generate(@PathVariable String mp, @RequestParam("msgType") String msgType, + public ResponseEntity 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; + String response= ""; + try{ + if (msgService != null) { + response = msgService.generateMsg(msgType, bodyJson); + JsonElement parsedResponse = parser.parse(response); + if(!parsedResponse.getAsJsonObject().has(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD)) { + return new ResponseEntity<>(parsedResponse,HttpStatus.OK); + } + else { + return new ResponseEntity<>(parsedResponse,HttpStatus.BAD_REQUEST); + } + } + else { + return new ResponseEntity<>(parser.parse(RemremGenerateServiceConstants.NO_SERVICE_ERROR),HttpStatus.SERVICE_UNAVAILABLE); + } + } + catch(Exception e) { + return new ResponseEntity<>(null,HttpStatus.INTERNAL_SERVER_ERROR); } } + private MsgService getMessageService(String messageProtocol) { for (MsgService service : msgServices) { if (service.getServiceName().equals(messageProtocol)) { diff --git a/service/src/test/java/com/ericsson/eiffel/remrem/generate/service/EiffelRemremControllerUnitTest.java b/service/src/test/java/com/ericsson/eiffel/remrem/generate/service/EiffelRemremControllerUnitTest.java index 156a2a2e..48d75c05 100644 --- a/service/src/test/java/com/ericsson/eiffel/remrem/generate/service/EiffelRemremControllerUnitTest.java +++ b/service/src/test/java/com/ericsson/eiffel/remrem/generate/service/EiffelRemremControllerUnitTest.java @@ -3,6 +3,10 @@ import static org.junit.Assert.assertEquals; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -14,6 +18,8 @@ import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.mockito.Spy; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; import com.ericsson.eiffel.remrem.generate.controller.RemremGenerateController; @@ -40,7 +46,8 @@ public class EiffelRemremControllerUnitTest { JsonElement body; - @Before + @SuppressWarnings("resource") + @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); msgServices.add(service); @@ -48,56 +55,63 @@ public void setUp() throws Exception { Mockito.when(service.getServiceName()).thenReturn("eiffelsemantics"); Mockito.when(service2.getServiceName()).thenReturn("eiffel3"); + URL jsonInputURL = getClass().getClassLoader().getResource("successInput.json"); + String inputFilePath = jsonInputURL.getPath().replace("%20"," "); + File jsonFile = new File(inputFilePath); + String successOutput = new BufferedReader(new FileReader(jsonFile)).readLine(); + + + jsonInputURL = getClass().getClassLoader().getResource("errorInput.json"); + inputFilePath = jsonInputURL.getPath().replace("%20"," "); + jsonFile = new File(inputFilePath); + String errorOutput = new BufferedReader(new FileReader(jsonFile)).readLine(); + Mockito.when(service.generateMsg( - Mockito.eq("eiffelactivityfinished"), - Mockito.anyObject() - )).thenReturn("{ \"result\":\"SUCCESS\" }"); + Mockito.eq("eiffelactivityfinished"), + Mockito.anyObject())).thenReturn(successOutput); Mockito.when(service.generateMsg( - Mockito.eq("eiffeljobfinished"), - Mockito.anyObject() - )).thenReturn("{ \"result\":\"FAILURE\" }"); + Mockito.eq("EiffelActivityFinished"), + Mockito.anyObject())).thenReturn(errorOutput); Mockito.when(service2.generateMsg( - Mockito.eq("eiffeljobfinished"), - Mockito.anyObject() - )).thenReturn("{ \"result\":\"SUCCESS\" }"); - + Mockito.eq("eiffelartifactnew"), + Mockito.anyObject())).thenReturn(successOutput); Mockito.when(service2.generateMsg( - Mockito.eq("eiffelactivityfinished"), - Mockito.anyObject() - )).thenReturn("{ \"result\":\"FAILURE\" }"); - + Mockito.eq("eiffelartifactnewevent"), + Mockito.anyObject())).thenReturn(errorOutput); + } - @Test - public void testSemanticsEvent() throws Exception { - JsonElement elem = unit.generate("eiffelsemantics", "eiffelactivityfinished", body.getAsJsonObject()); - assertEquals(elem.getAsJsonObject().get("result").getAsString(), "SUCCESS"); + @Test + public void testSemanticsSuccessEvent() throws Exception { + ResponseEntity elem = unit.generate("eiffelsemantics", "eiffelactivityfinished", body.getAsJsonObject()); + assertEquals(elem.getStatusCode(), HttpStatus.OK); } @Test - public void testSemanticsFailureEvent() throws Exception{ - JsonElement elem = unit.generate("eiffelsemantics", "eiffeljobfinished", body.getAsJsonObject()); - assertEquals(elem.getAsJsonObject().get("result").getAsString(), "FAILURE"); + public void testSemanticsFailureEvent() throws Exception { + ResponseEntity elem = unit.generate("eiffelsemantics", "EiffelActivityFinished", body.getAsJsonObject()); + assertEquals(elem.getStatusCode(), HttpStatus.BAD_REQUEST); } - @Test - public void testEiffel3Event() throws Exception { - JsonElement elem = unit.generate("eiffel3", "eiffeljobfinished", body.getAsJsonObject()); - assertEquals(elem.getAsJsonObject().get("result").getAsString(), "SUCCESS"); + @Test + public void testEiffel3SuccessEvent() throws Exception { + ResponseEntity elem = unit.generate("eiffel3", "eiffelartifactnew", body.getAsJsonObject()); + assertEquals(elem.getStatusCode(), HttpStatus.OK); } @Test - public void testEiffel3FailureEvent() throws Exception{ - JsonElement elem = unit.generate("eiffel3", "eiffelactivityfinished", body.getAsJsonObject()); - assertEquals(elem.getAsJsonObject().get("result").getAsString(), "FAILURE"); + public void testEiffel3FailureEvent() throws Exception { + ResponseEntity elem = unit.generate("eiffel3", "eiffelartifactnewevent", body.getAsJsonObject()); + assertEquals(elem.getStatusCode(), HttpStatus.BAD_REQUEST); } @Test - public void testOtherEvent() throws Exception{ - JsonElement elem = unit.generate("other", "eiffelactivityfinished", body.getAsJsonObject()); - assertEquals(elem,null); - } + public void testMessageServiceUnavailableEvent() throws Exception { + ResponseEntity elem = unit.generate("other", "EiffelActivityFinishedEvent", body.getAsJsonObject()); + assertEquals(elem.getStatusCode(), HttpStatus.SERVICE_UNAVAILABLE); + } + } diff --git a/service/src/test/resources/errorInput.json b/service/src/test/resources/errorInput.json new file mode 100644 index 00000000..d5c32a9d --- /dev/null +++ b/service/src/test/resources/errorInput.json @@ -0,0 +1 @@ +{"message":"Unknown message type requested","cause":"\u0027EiffelActivityFinis\u0027 is not in the vocabulary of this service"} \ No newline at end of file diff --git a/service/src/test/resources/successInput.json b/service/src/test/resources/successInput.json new file mode 100644 index 00000000..b3bbeb15 --- /dev/null +++ b/service/src/test/resources/successInput.json @@ -0,0 +1 @@ +{"meta":{"id":"dd595536-e8e4-4526-b959-8bb7e105d40d","type":"EiffelActivityFinishedEvent","version":"1.0.0","time":1481281099889,"tags":["tag1","tag2"],"source":{"domainId":"domainID","host":"host","name":"name","serializer":{"groupId":"G","artifactId":"A","version":"V"},"uri":"http://java.sun.com/j2se/1.3/"}},"data":{"outcome":{"conclusion":"TIMED_OUT","description":"Compilation timed out."},"persistentLogs":[{"name":"firstLog","uri":"http://myHost.com/firstLog"},{"name":"otherLog","uri":"http://myHost.com/firstLog33"}],"customData":[]},"links":[{"type":"LinkTargetType","target":"LinkTarget"}]} \ No newline at end of file