Skip to content

Commit d33c54c

Browse files
Modified generate's endpoint generate to accept array of events
1 parent 6eb83b3 commit d33c54c

File tree

6 files changed

+367
-60
lines changed

6 files changed

+367
-60
lines changed

service/src/main/java/com/ericsson/eiffel/remrem/generate/constants/RemremGenerateServiceConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public final class RemremGenerateServiceConstants {
2525
public static final String NO_TEMPLATE_ERROR = "{\"status_code\": 404, \"result\": \"FAIL\", "
2626
+ "\"message\":\"Requested template is not available\"}";
2727

28+
public static final String TEMPLATE_ERROR = "{\"status_code\": 400, \"result\": \"FAIL\", "
29+
+ "\"message\":\"Template is not correct format or something is missing in the template, please check\"}";
30+
2831
public static final String INTERNAL_SERVER_ERROR = "{\"status_code\": 500, \"result\": \"FAIL\", "
2932
+ "\"message\":\"Internal server error\"}";
3033

service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
119119

120120
} catch (JsonSyntaxException e) {
121121
JsonObject errorResponse = new JsonObject();
122-
log.error("Unexpected exception caught due to parse json data", e.getMessage());
123122
String exceptionMessage = e.getMessage();
123+
log.error(exceptionMessage, e.getMessage());
124124
errorResponse.addProperty("Status code", HttpStatus.BAD_REQUEST.value());
125-
errorResponse.addProperty("result", "Fatal");
125+
errorResponse.addProperty("result", "fatal");
126126
errorResponse.addProperty("message", "Invalid JSON parse data format due to: " + exceptionMessage);
127127
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
128128
}
@@ -133,35 +133,87 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
133133
JsonArray results = new JsonArray();
134134
JsonObject errorResponse = new JsonObject();
135135
try {
136+
if (lookupLimit <= 0) {
137+
return new ResponseEntity<>("LookupLimit must be greater than or equals to 1", HttpStatus.valueOf(HttpStatus.BAD_REQUEST.value()));
138+
}
136139

137140
if (bodyJson.isJsonArray()) {
138141

139142
JsonArray jsonArray = bodyJson.getAsJsonArray();
140143
for (JsonElement element : jsonArray) {
141-
results.add(processEvent(element.getAsJsonObject(), msgProtocol, msgType,
144+
results.add(processEvent(msgProtocol, msgType,
142145
failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit,
143-
okToLeaveOutInvalidOptionalFields));
146+
okToLeaveOutInvalidOptionalFields, element.getAsJsonObject()));
147+
}
148+
for (int i = 0; i < results.size(); i++) {
149+
JsonObject event = results.get(i).getAsJsonObject();
150+
if (event.has("meta")) {
151+
return new ResponseEntity<>(results, HttpStatus.OK);
152+
} else if (event.has("status code") && "400".equals(event.get("status code").toString())) {
153+
return new ResponseEntity<>(results, HttpStatus.BAD_REQUEST);
154+
} else {
155+
return new ResponseEntity<>(results, HttpStatus.SERVICE_UNAVAILABLE);
156+
}
144157
}
145-
return new ResponseEntity<>(results, HttpStatus.OK);
158+
return new ResponseEntity<>(results, HttpStatus.ACCEPTED);
146159

147160
} else if (bodyJson.isJsonObject()) {
148161
JsonObject jsonObject = bodyJson.getAsJsonObject();
149-
JsonObject jsonObject1 = processEvent(jsonObject, msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
150-
lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields);
151-
return new ResponseEntity<>(jsonObject1, HttpStatus.OK);
162+
JsonObject processedJson = processEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
163+
lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, jsonObject);
164+
165+
if (processedJson.has("meta")) {
166+
return new ResponseEntity<>(processedJson, HttpStatus.OK);
167+
}
168+
if (processedJson.has("status code") && "400".equals(processedJson.get("status code").toString())) {
169+
return new ResponseEntity<>(processedJson, HttpStatus.BAD_REQUEST);
170+
} else {
171+
return new ResponseEntity<>(processedJson, HttpStatus.SERVICE_UNAVAILABLE);
172+
}
152173
} else {
153174
errorResponse.addProperty("Status code", HttpStatus.BAD_REQUEST.value());
154175
errorResponse.addProperty("result", "fail");
155-
errorResponse.addProperty("message", "Invalid JSON format");
176+
errorResponse.addProperty("message", "Invalid JSON format,expected either single template or array of templates");
156177
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
157178
}
179+
} catch (REMGenerateException e) {
180+
if (e.getMessage().contains(Integer.toString(HttpStatus.NOT_ACCEPTABLE.value()))) {
181+
errorResponse.addProperty("message", e.getMessage());
182+
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_ACCEPTABLE);
183+
} else if (e.getMessage().contains(Integer.toString(HttpStatus.EXPECTATION_FAILED.value()))) {
184+
errorResponse.addProperty("message", e.getMessage());
185+
return new ResponseEntity<>(errorResponse, HttpStatus.EXPECTATION_FAILED);
186+
} else if (e.getMessage().contains(Integer.toString(HttpStatus.EXPECTATION_FAILED.value()))) {
187+
errorResponse.addProperty("status code", HttpStatus.EXPECTATION_FAILED.value());
188+
189+
} else if (e.getMessage()
190+
.contains(Integer.toString(HttpStatus.SERVICE_UNAVAILABLE.value()))) {
191+
errorResponse.addProperty("message", e.getMessage());
192+
return new ResponseEntity<>(errorResponse, HttpStatus.SERVICE_UNAVAILABLE);
193+
} else {
194+
errorResponse.addProperty("status code", HttpStatus.UNPROCESSABLE_ENTITY.value());
195+
errorResponse.addProperty("message", e.getMessage());
196+
return new ResponseEntity<>(errorResponse, HttpStatus.UNPROCESSABLE_ENTITY);
197+
}
198+
errorResponse.addProperty("result", "fail");
199+
errorResponse.add("message", parser.parse(e.getMessage()));
200+
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
201+
202+
} catch (JsonSyntaxException e) {
203+
log.error("Failed to parse JSON: ", e.getMessage());
204+
String exceptionMessage = e.getMessage();
205+
errorResponse.addProperty("status code", HttpStatus.INTERNAL_SERVER_ERROR.value());
206+
errorResponse.addProperty("result", "fail");
207+
errorResponse.addProperty("message", exceptionMessage);
208+
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
209+
158210
} catch (Exception e) {
159211
log.error("Unexpected exception caught", e);
160-
errorResponse.addProperty("Status code", HttpStatus.BAD_REQUEST.value());
161-
errorResponse.addProperty("Result", "Fail");
162-
errorResponse.addProperty("Message", "Invalid JSON format");
212+
String exceptionMessage = e.getMessage();
213+
errorResponse.addProperty("status code", HttpStatus.BAD_REQUEST.value());
214+
errorResponse.addProperty("result", "fail");
215+
errorResponse.addProperty("message", exceptionMessage);
163216
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
164-
165217
}
166218
}
167219

@@ -170,57 +222,28 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
170222
* @return JsonObject generated event
171223
*/
172224

173-
public JsonObject processEvent(JsonObject jsonElement, String msgProtocol, String msgType, Boolean failIfMultipleFound,
174-
Boolean okToLeaveOutInvalidOptionalFields,
175-
Boolean failIfNoneFound, int lookupLimit, Boolean lookupInExternalERs) {
225+
public JsonObject processEvent(String msgProtocol, String msgType, Boolean failIfMultipleFound,
226+
Boolean failIfNoneFound,Boolean lookupInExternalERs, int lookupLimit,
227+
Boolean okToLeaveOutInvalidOptionalFields, JsonObject jsonObject) throws REMGenerateException, JsonSyntaxException{
176228
JsonObject eventResponse = new JsonObject();
177229
JsonElement parsedResponse = null;
178-
try {
179-
JsonObject event = jsonElement.getAsJsonObject();
180-
event = erLookup(event, failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit);
230+
231+
JsonObject event = erLookup(jsonObject, failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit);
181232
MsgService msgService = getMessageService(msgProtocol);
182233

183234
if (msgService != null) {
184235
String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
185236
parsedResponse = parser.parse(response);
186237

187-
if (lookupLimit <= 0) {
188-
eventResponse.addProperty("status code", HttpStatus.BAD_REQUEST.value());
189-
} else if (!parsedResponse.getAsJsonObject().has(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD)) {
238+
if (!parsedResponse.getAsJsonObject().has(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD)) {
190239
return parsedResponse.getAsJsonObject();
191240
} else {
192-
eventResponse.addProperty("Status code", HttpStatus.BAD_REQUEST.value());
193-
eventResponse.addProperty("Result", "Fail");
194-
eventResponse.addProperty("Message", RemremGenerateServiceConstants.NOT_ACCEPTABLE);
241+
eventResponse.addProperty("status code", HttpStatus.BAD_REQUEST.value());
242+
eventResponse.addProperty("result", "fail");
243+
eventResponse.addProperty("message", RemremGenerateServiceConstants.TEMPLATE_ERROR);
195244
return eventResponse;
196245
}
197-
198246
}
199-
} catch (REMGenerateException e1) {
200-
if (e1.getMessage().contains(Integer.toString(HttpStatus.NOT_ACCEPTABLE.value()))) {
201-
eventResponse.addProperty("Status code", HttpStatus.NOT_ACCEPTABLE.value());
202-
} else if (e1.getMessage().contains(Integer.toString(HttpStatus.EXPECTATION_FAILED.value()))) {
203-
eventResponse.addProperty("Status code", HttpStatus.EXPECTATION_FAILED.value());
204-
} else if (e1.getMessage().contains(Integer.toString(HttpStatus.EXPECTATION_FAILED.value()))) {
205-
eventResponse.addProperty("Status code", HttpStatus.EXPECTATION_FAILED.value());
206-
207-
} else if (e1.getMessage()
208-
.contains(Integer.toString(HttpStatus.SERVICE_UNAVAILABLE.value()))) {
209-
eventResponse.addProperty("Status code", HttpStatus.SERVICE_UNAVAILABLE.value());
210-
eventResponse.addProperty("Message", RemremGenerateServiceConstants.NO_SERVICE_ERROR);
211-
} else {
212-
eventResponse.addProperty("Status code", HttpStatus.UNPROCESSABLE_ENTITY.value());
213-
}
214-
eventResponse.addProperty("Result", "Fail");
215-
eventResponse.add("Message", parser.parse(e1.getMessage()));
216-
return eventResponse;
217-
} catch (Exception e) {
218-
log.error("Unexpected error caught", e);
219-
eventResponse.addProperty("Status code", HttpStatus.INTERNAL_SERVER_ERROR.value());
220-
eventResponse.addProperty("Result", "Fail");
221-
eventResponse.addProperty("Message", RemremGenerateServiceConstants.INTERNAL_SERVER_ERROR);
222-
return eventResponse;
223-
}
224247
return eventResponse;
225248
}
226249

service/src/test/java/com/ericsson/eiffel/remrem/generate/integrationtest/EiffelRemremControllerIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void testDuplicateKeyInBody() throws IOException {
173173
.contentType("application/json")
174174
.body(activityFinishedDuplicateKeysBody)
175175
.when()
176-
.post("/eiffelsemantics?msgType=EiffelActivityFinishedEvent")
176+
.post("/eiffelsemantics?msgType=EiffelActivityFinishedEven")
177177
.then()
178178
.statusCode(HttpStatus.SC_BAD_REQUEST);
179179
}

service/src/test/java/com/ericsson/eiffel/remrem/generate/service/EiffelRemremControllerUnitTest.java

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import java.util.ArrayList;
2626
import java.util.List;
2727

28+
import com.google.gson.JsonArray;
29+
import com.google.gson.JsonObject;
30+
import com.google.gson.JsonParser;
2831
import org.junit.Before;
2932
import org.junit.Test;
3033
import org.junit.runner.RunWith;
@@ -33,6 +36,7 @@
3336
import org.mockito.Mockito;
3437
import org.mockito.MockitoAnnotations;
3538
import org.mockito.Spy;
39+
import org.springframework.beans.factory.annotation.Autowired;
3640
import org.springframework.http.HttpStatus;
3741
import org.springframework.http.ResponseEntity;
3842
import org.springframework.test.context.junit4.SpringRunner;
@@ -53,11 +57,9 @@ public class EiffelRemremControllerUnitTest {
5357

5458
@Mock
5559
MsgService service2;
56-
5760

5861
@Spy
5962
private List<MsgService> msgServices = new ArrayList<MsgService>();
60-
6163
@Mock
6264
JsonElement body;
6365

@@ -111,41 +113,104 @@ public void setUp() throws Exception {
111113

112114
@Test
113115
public void testSemanticsSuccessEvent() throws Exception {
114-
ResponseEntity<?> elem = unit.generate("eiffelsemantics", "eiffelactivityfinished", false, false, true, 1, false, body.getAsJsonObject());
116+
File file = new File("src/test/resources/ErlookupConfidenceLevelOutput.json");
117+
JsonParser parser = new JsonParser();
118+
JsonObject json = parser.parse(new FileReader(file)).getAsJsonObject();
119+
ResponseEntity<?> elem = unit.generate("eiffelsemantics", "eiffelactivityfinished", false, false, true, 1, false, json);
115120
assertEquals(elem.getStatusCode(), HttpStatus.OK);
116121
}
117122

123+
@Test
124+
public void testSemanticsSuccessArrayEvent() throws Exception {
125+
File file = new File("src/test/resources/ErlookupConfidenceLevelArrayOutput.json");
126+
JsonParser parser = new JsonParser();
127+
JsonArray json = parser.parse(new FileReader(file)).getAsJsonArray();
128+
ResponseEntity<?> elem = unit.generate("eiffelsemantics", "eiffelactivityfinished", false, false, true, 1, false, json);
129+
assertEquals(elem.getStatusCode(), HttpStatus.OK);
130+
}
131+
132+
118133
@Test
119134
public void testSemanticsFailureEvent() throws Exception {
120-
ResponseEntity<?> elem = unit.generate("eiffelsemantics", "EiffelActivityFinished", false, false, true, 1, false, body.getAsJsonObject());
135+
File file = new File("src/test/resources/ErlookupConfidenceLevelOutput.json");
136+
JsonParser parser = new JsonParser();
137+
JsonObject json = parser.parse(new FileReader(file)).getAsJsonObject();
138+
ResponseEntity<?> elem = unit.generate("eiffelsemantics", "EiffelActivityFinished", false, false, true, 1, false, json);
139+
assertEquals(elem.getStatusCode(), HttpStatus.BAD_REQUEST);
140+
}
141+
142+
@Test
143+
public void testSemanticsFailureEventArray() throws Exception {
144+
File file = new File("src/test/resources/ErlookupConfidenceLevelArrayOutput.json");
145+
JsonParser parser = new JsonParser();
146+
JsonArray json = parser.parse(new FileReader(file)).getAsJsonArray();
147+
ResponseEntity<?> elem = unit.generate("eiffelsemantics", "EiffelActivityFinished", false, false, true, 1, false, json);
121148
assertEquals(elem.getStatusCode(), HttpStatus.BAD_REQUEST);
122149
}
123150

124151
@Test
125152
public void testEiffel3SuccessEvent() throws Exception {
126-
ResponseEntity<?> elem = unit.generate("eiffel3", "eiffelartifactnew", false, false, true, 1, false, body.getAsJsonObject());
153+
File file = new File("src/test/resources/ErlookupConfidenceLevelOutput.json");
154+
JsonParser parser = new JsonParser();
155+
JsonObject json = parser.parse(new FileReader(file)).getAsJsonObject();
156+
ResponseEntity<?> elem = unit.generate("eiffel3", "eiffelartifactnew", false, false, true, 1, false, json);
157+
assertEquals(elem.getStatusCode(), HttpStatus.OK);
158+
}
159+
160+
@Test
161+
public void testEiffel3SuccessEventArray() throws Exception {
162+
File file = new File("src/test/resources/ErlookupConfidenceLevelArrayOutput.json");
163+
JsonParser parser = new JsonParser();
164+
JsonArray json = parser.parse(new FileReader(file)).getAsJsonArray();
165+
ResponseEntity<?> elem = unit.generate("eiffel3", "eiffelartifactnew", false, false, true, 1, false, json);
127166
assertEquals(elem.getStatusCode(), HttpStatus.OK);
128167
}
129168

130169
@Test
131170
public void testEiffel3FailureEvent() throws Exception {
132-
ResponseEntity<?> elem = unit.generate("eiffel3", "eiffelartifactnewevent", false, false, true, 1, false, body.getAsJsonObject());
171+
File file = new File("src/test/resources/ErlookupConfidenceLevelOutput.json");
172+
JsonParser parser = new JsonParser();
173+
JsonObject json = parser.parse(new FileReader(file)).getAsJsonObject();
174+
ResponseEntity<?> elem = unit.generate("eiffel3", "eiffelartifactnewevent", false, false, true, 1, false, json);
133175
assertEquals(elem.getStatusCode(), HttpStatus.BAD_REQUEST);
134176
}
135177

178+
@Test
179+
public void testEiffel3FailureEventArray() throws Exception {
180+
File file = new File("src/test/resources/ErlookupConfidenceLevelArrayOutput.json");
181+
JsonParser parser = new JsonParser();
182+
JsonArray json = parser.parse(new FileReader(file)).getAsJsonArray();
183+
ResponseEntity<?> elem = unit.generate("eiffel3", "eiffelartifactnewevent", false, false, true, 1, false, json);
184+
assertEquals(elem.getStatusCode(), HttpStatus.BAD_REQUEST);
185+
}
136186
@Test
137187
public void testMessageServiceUnavailableEvent() throws Exception {
138-
ResponseEntity<?> elem = unit.generate("other", "EiffelActivityFinishedEvent", false, false, true, 1, false, body.getAsJsonObject());
188+
File file = new File("src/test/resources/ArtifactCreated.json");
189+
JsonParser parser = new JsonParser();
190+
JsonObject json = parser.parse(new FileReader(file)).getAsJsonObject();
191+
ResponseEntity<?> elem = unit.generate("other", "EiffelActivityFinishedEvent", false, false, true, 1, false, json);
139192
assertEquals(elem.getStatusCode(), HttpStatus.SERVICE_UNAVAILABLE);
140193
}
141194

142195
@Test
143196
public void testlenientValidation() throws Exception {
197+
File file = new File("src/test/resources/ArtifactCreated.json");
198+
JsonParser parser = new JsonParser();
199+
JsonObject json = parser.parse(new FileReader(file)).getAsJsonObject();
144200
unit.setLenientValidationEnabledToUsers(true);
145-
ResponseEntity<?> elem = unit.generate("eiffelsemantics", "EiffelArtifactCreatedEvent", false, false, true, 1, true, body.getAsJsonObject());
201+
ResponseEntity<?> elem = unit.generate("eiffelsemantics", "EiffelArtifactCreatedEvent", false, false, true, 1, true, json);
146202
assertEquals(elem.getStatusCode(), HttpStatus.OK);
147203
}
148204

205+
@Test
206+
public void testlenientValidationEventArray() throws Exception {
207+
File file = new File("src/test/resources/ArtifactCreatedEventArray.json");
208+
JsonParser parser = new JsonParser();
209+
JsonArray json = parser.parse(new FileReader(file)).getAsJsonArray();
210+
unit.setLenientValidationEnabledToUsers(true);
211+
ResponseEntity<?> elem = unit.generate("eiffelsemantics", "EiffelArtifactCreatedEvent", false, false, true, 1, true, json);
212+
assertEquals(elem.getStatusCode(), HttpStatus.OK);
213+
}
149214
@Test
150215
public void testJasyptFileSuccess() throws IOException {
151216
String jasyptPath = "src/test/resources/jasypt.key";

0 commit comments

Comments
 (0)