Skip to content

Commit

Permalink
Added status codes to the output in Remrem generate service (#60)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
SantoshNC68 authored Feb 27, 2017
1 parent 59e6e25 commit 3e4f983
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 64 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ repositories {
maven { url "https://jitpack.io" }
}


//Adding dependencies and sub projects common build script
subprojects {
buildscript {
repositories {
Expand All @@ -38,7 +40,7 @@ subprojects {
targetCompatibility = 1.8

//Latest version for generate
version = "0.7.7"
version = "0.7.8"

repositories {
mavenCentral()
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cli/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//Plugins used for CLI
plugins{
id 'java'
id 'maven'
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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')
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -71,23 +71,23 @@ 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());
}

@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());
}

@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;
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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));
}

Expand Down
18 changes: 14 additions & 4 deletions service/src/integration-test/resources/ActivityFinished.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"msgParams": {
"meta": {
"type": "EiffelActivityFinishedEvent",
"type": "eiffelactivityfinished",
"version": "1.0.0",
"tags": [
"tag1",
Expand All @@ -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",
Expand All @@ -22,18 +22,28 @@
},
"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."
},
"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"
}
]
},
Expand Down
8 changes: 4 additions & 4 deletions service/src/integration-test/resources/ArtifactPublished.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"msgParams": {
"meta": {
"type": "EiffelArtifactPublishedEvent",
"type": "eiffelartifactpublished",
"version": "1.0.0",
"tags": [
"tag1",
Expand All @@ -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",
Expand All @@ -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"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -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";

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
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;
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.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 {
Expand All @@ -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)) {
Expand Down
Loading

0 comments on commit 3e4f983

Please sign in to comment.