Skip to content

Commit ebd10ea

Browse files
EI Subscription REST POST supporting authentication headers (#100)
* JMESPath REST API * JMESPath API with fixed reviews * partial work jmespath rest * JMESPath API with RAML 0.8 specification * remove componet comment * replace tab with spaces * fix usage examples * prepare for new asserts in flow test * fix unit tests * Subscription for individual events * pom file * subscription individual objects * pom * error removal * Subscription Individual Object * deleting unused import * jmespath comment correction * space removal and new clean build * review changes * review change file deleted * review change * review changes * log.info to log.error * required changes * raml * raml files * resolving conflict * self generated code raml * controllers auto generated * auto generated * removing ttl * adding ttl * copied pom * changes in app.properties * resolving conflict with upstream * adding more files * added time lapse variable * rebased... * new clone and update * reviewed changes * review changes-evasiba * review changes 2:evasiba * handling of authorization by Subscription REST API: 9584 * changing property name * pom updated * method change * reviewed changes-1 * some review changes regarding comments * some changes ´to get pass throguh tests * review changes * Update application.properties * Fix test * development * changes * code changes related to story 9561 * error * error removal * requested changes * reviewed change * reviewed changes
1 parent ecb6527 commit ebd10ea

File tree

7 files changed

+135
-35
lines changed

7 files changed

+135
-35
lines changed

src/main/java/com/ericsson/ei/subscriptionhandler/InformSubscription.java

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.mongodb.BasicDBObject;
2424
import com.mongodb.util.JSON;
2525
import lombok.Getter;
26+
2627
import org.slf4j.Logger;
2728
import org.slf4j.LoggerFactory;
2829
import org.springframework.beans.factory.annotation.Autowired;
@@ -50,7 +51,7 @@
5051
public class InformSubscription {
5152

5253
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(InformSubscription.class);
53-
//Regular expression for replacement unexpected character like \"|
54+
// Regular expression for replacement unexpected character like \"|
5455
private static final String REGEX = "^\"|\"$";
5556

5657
@Getter
@@ -90,28 +91,42 @@ public class InformSubscription {
9091
* @param subscriptionJson
9192
*/
9293
public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson) {
93-
String subscriptionName = subscriptionJson.get("subscriptionName").toString().replaceAll(REGEX, "");
94-
LOGGER.debug("SubscriptionName : " + subscriptionName);
95-
String notificationType = subscriptionJson.get("notificationType").toString().replaceAll(REGEX, "");
96-
LOGGER.debug("NotificationType : " + notificationType);
97-
String notificationMeta = subscriptionJson.get("notificationMeta").toString().replaceAll(REGEX, "");
98-
LOGGER.debug("NotificationMeta : " + notificationMeta);
94+
String subscriptionName = getSubscriptionField("subscriptionName", subscriptionJson);
95+
String notificationType = getSubscriptionField("notificationType", subscriptionJson);
96+
String notificationMeta = getSubscriptionField("notificationMeta", subscriptionJson);
97+
98+
String key = "";
99+
String val = "";
99100
MultiValueMap<String, String> mapNotificationMessage = new LinkedMultiValueMap<>();
100101
ArrayNode arrNode = (ArrayNode) subscriptionJson.get("notificationMessageKeyValues");
101102
if (arrNode.isArray()) {
102103
for (final JsonNode objNode : arrNode) {
103-
mapNotificationMessage.add(objNode.get("formkey").toString().replaceAll(REGEX, ""), jmespath
104-
.runRuleOnEvent(objNode.get("formvalue").toString().replaceAll(REGEX, ""), aggregatedObject)
105-
.toString().replaceAll(REGEX, ""));
104+
if (objNode.get("formkey").toString().replaceAll(REGEX, "").equals("Authorization")) {
105+
key = "Authorization";
106+
val = objNode.get("formvalue").toString().replaceAll(REGEX, "");
107+
108+
} else {
109+
110+
mapNotificationMessage.add(objNode.get("formkey").toString().replaceAll(REGEX, ""), jmespath
111+
.runRuleOnEvent(objNode.get("formvalue").toString().replaceAll(REGEX, ""), aggregatedObject)
112+
.toString().replaceAll(REGEX, ""));
113+
}
106114
}
107115
}
108116
if (notificationType.trim().equals("REST_POST")) {
109117
LOGGER.debug("Notification through REST_POST");
110118
int result;
111-
String headerContentMediaType = subscriptionJson.get("restPostBodyMediaType").toString()
112-
.replaceAll(REGEX, "");
119+
String headerContentMediaType = subscriptionJson.get("restPostBodyMediaType").toString().replaceAll(REGEX,
120+
"");
113121
LOGGER.debug("headerContentMediaType : " + headerContentMediaType);
114-
result = restTemplate.postDataMultiValue(notificationMeta, mapNotificationMessage, headerContentMediaType);
122+
if (!key.isEmpty() && !val.isEmpty()) {
123+
result = restTemplate.postDataMultiValue(notificationMeta, mapNotificationMessage,
124+
headerContentMediaType, key, val);
125+
} else {
126+
result = restTemplate.postDataMultiValue(notificationMeta, mapNotificationMessage,
127+
headerContentMediaType);
128+
}
129+
115130
if (result == HttpStatus.OK.value() || result == HttpStatus.CREATED.value()
116131
|| result == HttpStatus.NO_CONTENT.value()) {
117132
LOGGER.debug("The result is : " + result);
@@ -141,12 +156,12 @@ public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson)
141156
} else if (notificationType.trim().equals("MAIL")) {
142157
LOGGER.debug("Notification through EMAIL");
143158
try {
144-
sendMail.sendMail(notificationMeta,
145-
String.valueOf((mapNotificationMessage.get("")).get(0)));
159+
sendMail.sendMail(notificationMeta, String.valueOf((mapNotificationMessage.get("")).get(0)));
146160
} catch (MessagingException e) {
147161
e.printStackTrace();
148162
LOGGER.error(e.getMessage());
149163
}
164+
150165
}
151166
}
152167

@@ -159,7 +174,8 @@ public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson)
159174
* @param notificationMeta
160175
* @return String
161176
*/
162-
private String prepareMissedNotification(String aggregatedObject, String subscriptionName, String notificationMeta) {
177+
private String prepareMissedNotification(String aggregatedObject, String subscriptionName,
178+
String notificationMeta) {
163179
Date date = new Date();
164180
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
165181
String time = dateFormat.format(date);
@@ -175,6 +191,19 @@ private String prepareMissedNotification(String aggregatedObject, String subscri
175191
document.put("AggregatedObject", JSON.parse(aggregatedObject));
176192
return document.toString();
177193
}
194+
195+
/**
196+
* This method, given the field name, returns its value
197+
*
198+
* @param subscriptionJson
199+
* @param fieldName
200+
* @return field value
201+
*/
202+
private String getSubscriptionField(String fieldName, JsonNode subscriptionJson) {
203+
String value = subscriptionJson.get(fieldName).toString().replaceAll(REGEX, "");
204+
LOGGER.debug("Extracted field name and value from subscription json:" + fieldName + " : " + value);
205+
return value;
206+
}
178207

179208
/**
180209
* This method is responsible to display the configurable application properties

src/main/java/com/ericsson/ei/subscriptionhandler/SpringRestTemplate.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.ericsson.ei.subscriptionhandler;
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
20+
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
2223
import org.springframework.boot.web.client.RestTemplateBuilder;
@@ -42,23 +43,34 @@ public SpringRestTemplate(RestTemplateBuilder builder) {
4243
}
4344

4445
/**
45-
* This method is responsible to notify the subscriber through REST POST With raw body and form parameters.
46+
* This method is responsible to notify the subscriber through REST POST With
47+
* raw body and form parameters.
4648
*
4749
* @param notificationMeta
4850
* @param mapNotificationMessage
4951
* @param headerContentMediaType
5052
* @return integer
5153
*/
52-
public int postDataMultiValue(String notificationMeta, MultiValueMap<String, String> mapNotificationMessage, String headerContentMediaType) {
54+
public int postDataMultiValue(String notificationMeta, MultiValueMap<String, String> mapNotificationMessage,
55+
String headerContentMediaType, String... args) {
5356
ResponseEntity<JsonNode> response;
57+
5458
try {
5559
HttpHeaders headers = new HttpHeaders();
5660
headers.setContentType(MediaType.valueOf(headerContentMediaType));
57-
if (headerContentMediaType.equals(MediaType.APPLICATION_FORM_URLENCODED.toString())) { //"application/x-www-form-urlencoded"
61+
if (headerContentMediaType.equals(MediaType.APPLICATION_FORM_URLENCODED.toString())) { // "application/x-www-form-urlencoded"
62+
63+
if (args.length != 0) {
64+
String key = args[0];
65+
String val = args[1];
66+
headers.add(key, val);
67+
}
68+
5869
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(mapNotificationMessage, headers);
5970
response = rest.postForEntity(notificationMeta, request, JsonNode.class);
6071
} else {
61-
HttpEntity<String> request = new HttpEntity<>(String.valueOf((mapNotificationMessage.get("")).get(0)), headers);
72+
HttpEntity<String> request = new HttpEntity<>(String.valueOf((mapNotificationMessage.get("")).get(0)),
73+
headers);
6274
response = rest.postForEntity(notificationMeta, request, JsonNode.class);
6375
}
6476
} catch (Exception e) {

src/main/resources/application.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ spring.mail.properties.mail.smtp.auth: false
7272
er.url: http://localhost:8080/search/
7373

7474
ldap.enabled:false
75-
7675
ldap.url:
7776
ldap.base.dn:
7877
ldap.username:

src/test/java/com/ericsson/ei/subscriptionhandler/test/SubscriptionHandlerTest.java

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
5252
import org.springframework.util.LinkedMultiValueMap;
5353
import org.springframework.util.MultiValueMap;
54-
5554
import com.ericsson.ei.App;
5655
import com.ericsson.ei.controller.model.QueryResponse;
5756
import com.ericsson.ei.exception.SubscriptionValidationException;
@@ -71,26 +70,30 @@
7170
import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory;
7271

7372
@RunWith(SpringJUnit4ClassRunner.class)
74-
@SpringBootTest(classes = {
75-
App.class
76-
})
73+
@SpringBootTest(classes = { App.class })
7774
@AutoConfigureMockMvc
7875
public class SubscriptionHandlerTest {
7976

8077
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(SubscriptionHandlerTest.class);
8178
private static final String aggregatedPath = "src/test/resources/AggregatedObject.json";
8279
private static final String subscriptionPath = "src/test/resources/SubscriptionObject.json";
80+
private static final String subscriptionPathForAuthorization = "src/test/resources/SubscriptionObjectForAuthorization.json";
8381
private static final String DB_NAME = "MissedNotification";
8482
private static final String COLLECTION_NAME = "Notification";
8583
private static final String REGEX = "^\"|\"$";
8684
private static final String MISSED_NOTIFICATION_URL = "/queryMissedNotifications";
8785
private static final int STATUS_OK = 200;
8886
private static String aggregatedObject;
8987
private static String subscriptionData;
88+
private static String subscriptionDataForAuthorization;
9089
private static String url;
9190
private static String headerContentMediaType;
91+
private static String urlAuthorization;
92+
private static String headerContentMediaTypeAuthorization;
9293
private static MongodForTestsFactory testsFactory;
9394
private static MongoClient mongoClient = null;
95+
private static final String formkey = "Authorization";
96+
private static final String formvalue = "Basic XX0=";
9497

9598
@Autowired
9699
private RunSubscription runSubscription;
@@ -144,6 +147,7 @@ public static void setUpEmbeddedMongo() throws JSONException, IOException {
144147
subscriptionData = FileUtils.readFileToString(new File(subscriptionPath), "UTF-8");
145148
subscriptionRepeatFlagTrueData = FileUtils.readFileToString(new File(subscriptionRepeatFlagTruePath),
146149
"UTF-8");
150+
subscriptionDataForAuthorization = FileUtils.readFileToString(new File(subscriptionPathForAuthorization), "UTF-8");
147151
subscriptionDataEmail = FileUtils.readFileToString(new File(subscriptionPathForEmail), "UTF-8");
148152
} catch (Exception e) {
149153
LOGGER.error(e.getMessage(), e);
@@ -152,6 +156,8 @@ public static void setUpEmbeddedMongo() throws JSONException, IOException {
152156

153157
url = new JSONObject(subscriptionData).getString("notificationMeta").replaceAll(REGEX, "");
154158
headerContentMediaType = new JSONObject(subscriptionData).getString("restPostBodyMediaType");
159+
urlAuthorization = new JSONObject(subscriptionDataForAuthorization).getString("notificationMeta").replaceAll(REGEX, "");
160+
headerContentMediaTypeAuthorization = new JSONObject(subscriptionDataForAuthorization).getString("restPostBodyMediaType");
155161
}
156162

157163
@BeforeClass
@@ -288,16 +294,27 @@ public void sendMailTest() {
288294

289295
@Test
290296
public void testRestPostTrigger() throws IOException {
291-
when(springRestTemplate.postDataMultiValue(url, mapNotificationMessage(), headerContentMediaType))
292-
.thenReturn(STATUS_OK);
297+
when(springRestTemplate.postDataMultiValue(url, mapNotificationMessage(subscriptionData),
298+
headerContentMediaType)).thenReturn(STATUS_OK);
293299
subscription.informSubscriber(aggregatedObject, new ObjectMapper().readTree(subscriptionData));
294-
verify(springRestTemplate, times(1)).postDataMultiValue(url, mapNotificationMessage(), headerContentMediaType);
300+
verify(springRestTemplate, times(1)).postDataMultiValue(url, mapNotificationMessage(subscriptionData),
301+
headerContentMediaType);
302+
}
303+
304+
@Test
305+
public void testRestPostTriggerForAuthorization() throws IOException {
306+
when(springRestTemplate.postDataMultiValue(urlAuthorization, mapNotificationMessage(subscriptionDataForAuthorization),
307+
headerContentMediaTypeAuthorization, formkey, formvalue)).thenReturn(STATUS_OK);
308+
subscription.informSubscriber(aggregatedObject, new ObjectMapper().readTree(subscriptionDataForAuthorization));
309+
verify(springRestTemplate, times(1)).postDataMultiValue(urlAuthorization,
310+
mapNotificationMessage(subscriptionDataForAuthorization), headerContentMediaTypeAuthorization, formkey, formvalue);
295311
}
296312

297313
@Test
298314
public void testRestPostTriggerFailure() throws IOException {
299315
subscription.informSubscriber(aggregatedObject, new ObjectMapper().readTree(subscriptionData));
300-
verify(springRestTemplate, times(4)).postDataMultiValue(url, mapNotificationMessage(), headerContentMediaType);
316+
verify(springRestTemplate, times(4)).postDataMultiValue(url, mapNotificationMessage(subscriptionData),
317+
headerContentMediaType);
301318
assertFalse(mongoDBHandler.getAllDocuments(DB_NAME, COLLECTION_NAME).isEmpty());
302319
}
303320

@@ -315,16 +332,18 @@ public void testQueryMissedNotificationEndPoint() throws Exception {
315332
assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus());
316333
}
317334

318-
private MultiValueMap<String, String> mapNotificationMessage() throws IOException {
335+
private MultiValueMap<String, String> mapNotificationMessage(String data) throws IOException {
319336
MultiValueMap<String, String> mapNotificationMessage = new LinkedMultiValueMap<>();
320337

321-
ArrayNode arrNode = (ArrayNode) new ObjectMapper().readTree(subscriptionData)
322-
.get("notificationMessageKeyValues");
338+
ArrayNode arrNode = (ArrayNode) new ObjectMapper().readTree(data).get("notificationMessageKeyValues");
323339
if (arrNode.isArray()) {
324340
for (final JsonNode objNode : arrNode) {
325-
mapNotificationMessage.add(objNode.get("formkey").toString().replaceAll(REGEX, ""), jmespath
326-
.runRuleOnEvent(objNode.get("formvalue").toString().replaceAll(REGEX, ""), aggregatedObject)
327-
.toString().replaceAll(REGEX, ""));
341+
if (!objNode.get("formkey").toString().replaceAll(REGEX, "").equals("Authorization")) {
342+
343+
mapNotificationMessage.add(objNode.get("formkey").toString().replaceAll(REGEX, ""), jmespath
344+
.runRuleOnEvent(objNode.get("formvalue").toString().replaceAll(REGEX, ""), aggregatedObject)
345+
.toString().replaceAll(REGEX, ""));
346+
}
328347
}
329348
}
330349
return mapNotificationMessage;

src/test/resources/SubscriptionForMail.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"subscriptionName": "Subscription_1",
3+
"userName" : "DEF",
34
"repeat": false,
45
"created": "data-time",
56
"notificationType": "MAIL",

src/test/resources/SubscriptionObject.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"subscriptionName": "Subscription_1",
3+
"userName" : "DEF",
34
"repeat": false,
45
"created": "data-time",
56
"notificationType": "REST_POST",
@@ -11,6 +12,7 @@
1112
"formvalue" : "@"
1213
}
1314
],
15+
1416
"requirements": [
1517
{
1618
"type": "ARTIFACT_1",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"subscriptionName": "Subscription_1",
3+
"userName" : "DEF",
4+
"repeat": false,
5+
"created": "data-time",
6+
"notificationType": "REST_POST",
7+
"notificationMeta": "https://jenkins.domain.com:8100/jenkins/job/test_params/build",
8+
"restPostBodyMediaType": "application/x-www-form-urlencoded",
9+
"notificationMessageKeyValues" : [
10+
{
11+
"formkey" : "json",
12+
"formvalue" : "{parameter: [{ name: 'parameter', value : 'parameterValue' }]}"
13+
},
14+
{
15+
"formkey" : "Authorization",
16+
"formvalue" : "Basic XX0="
17+
}
18+
],
19+
20+
"requirements": [
21+
{
22+
"type": "ARTIFACT_1",
23+
"conditions": [
24+
{"jmespath": "gav.groupId=='com.mycompany.myproduct'"},
25+
{"jmespath": "testCaseExecutions[?testCase.conclusion == 'SUCCESSFUL' && testCase.id=='TC5']"}
26+
]
27+
28+
},
29+
{
30+
"type": "ARTIFACT_1",
31+
"conditions": [
32+
{"jmespath": "gav.groupId=='com.mycompany.myproduct'"},
33+
{"jmespath": "testCaseExecutions[?testCaseStartedEventId == '13af4a14-f951-4346-a1ba-624c79f10e98']"}
34+
]
35+
36+
}
37+
]
38+
}

0 commit comments

Comments
 (0)