Skip to content

Commit 3ed9200

Browse files
Jibandeepvasile-baluta
authored andcommitted
Implement Historical ER Query Service (#6)
*Implement historical Event Repository query Service * add posibility to query downstream and upstreamfrom ER and test for ER query method * modify unit test
1 parent 916114b commit 3ed9200

File tree

3 files changed

+333
-1
lines changed

3 files changed

+333
-1
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
Copyright 2017 Ericsson AB.
3+
For a full list of individual contributors, please see the commit history.
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
package com.ericsson.ei.erqueryservice;
15+
16+
import java.io.IOException;
17+
import java.util.ArrayList;
18+
import java.util.HashMap;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import javax.annotation.PostConstruct;
23+
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
import org.springframework.beans.factory.annotation.Value;
27+
import org.springframework.boot.web.client.RestTemplateBuilder;
28+
import org.springframework.http.HttpEntity;
29+
import org.springframework.http.HttpHeaders;
30+
import org.springframework.http.HttpMethod;
31+
import org.springframework.http.MediaType;
32+
import org.springframework.http.ResponseEntity;
33+
import org.springframework.stereotype.Component;
34+
import org.springframework.util.LinkedMultiValueMap;
35+
import org.springframework.web.client.RestOperations;
36+
import org.springframework.web.util.UriComponentsBuilder;
37+
38+
import com.fasterxml.jackson.core.JsonProcessingException;
39+
import com.fasterxml.jackson.databind.JsonNode;
40+
import com.fasterxml.jackson.databind.ObjectMapper;
41+
import com.fasterxml.jackson.databind.node.ArrayNode;
42+
import com.fasterxml.jackson.databind.node.ObjectNode;
43+
44+
/**
45+
* @author evasiba
46+
*
47+
*/
48+
@Component
49+
public class ERQueryService {
50+
51+
static Logger log = (Logger) LoggerFactory.getLogger(ERQueryService.class);
52+
53+
private RestOperations rest;
54+
55+
public final static int DOWNSTREAM = 0;
56+
public final static int UPSTREAM = 1;
57+
public final static int DOWNANDUPSTREAM = 2;
58+
59+
@Value("${er.url}")
60+
private String url;
61+
62+
public String getUrl() {
63+
return url;
64+
}
65+
66+
public ERQueryService(RestTemplateBuilder builder) {
67+
rest = builder.build();
68+
}
69+
70+
public void setRest(RestOperations rest) {
71+
this.rest = rest;
72+
}
73+
74+
/**
75+
* This method only extracts the event information from ER2.0 based on the
76+
* eventID.
77+
*
78+
* @param eventId
79+
* @return ResponseEntity
80+
*/
81+
public ResponseEntity getEventDataById(String eventId) {
82+
String erUrl = url.trim() + "{id}";
83+
log.info("The url is : " + erUrl);
84+
Map<String, String> params = new HashMap<String, String>();
85+
params.put("id", eventId);
86+
ResponseEntity<String> response = null;
87+
log.info("The ID parameter is set");
88+
try {
89+
response = rest.getForEntity(erUrl, String.class, params);
90+
log.info("The response is : " + response.toString());
91+
} catch (Exception e) {
92+
log.error(e.getMessage(), e);
93+
}
94+
return response;
95+
}
96+
97+
/**
98+
* This method is used to fetch only the upstream or downstream or both
99+
* event information from ER2.0 based on the eventID and searchAction
100+
* conditions.
101+
*
102+
* @param eventId
103+
* @param searchAction
104+
* @param limitParam
105+
* @param levelsParam
106+
* @param tree
107+
* @return ResponseEntity
108+
*/
109+
110+
public ResponseEntity getEventStreamDataById(String eventId, int searchAction, int limitParam,
111+
int levelsParam, boolean tree) {
112+
113+
String erUrl = url.trim() + eventId;
114+
log.info("The url is : " + erUrl);
115+
116+
// Request Body parameters
117+
JsonNode uriParams = getSearchParameters(searchAction);
118+
119+
// Add query parameter
120+
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(erUrl).queryParam("limit", limitParam)
121+
.queryParam("levels", levelsParam).queryParam("tree", tree);
122+
123+
HttpHeaders headers = new HttpHeaders();
124+
headers.setContentType(MediaType.APPLICATION_JSON);
125+
126+
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity(uriParams, headers);
127+
log.info("The request is : " + builder.buildAndExpand(uriParams).toUri().toString());
128+
129+
ResponseEntity response = rest.exchange(builder.buildAndExpand(uriParams).toUri(), HttpMethod.POST,
130+
requestEntity, JsonNode.class);
131+
return response;
132+
}
133+
134+
/** Generates the json object used as body for downstream/upstream
135+
* query requests
136+
* @param searchAction - one of DOWNSTREAM, UPSTREAM or DOWNANDUPSTREAM
137+
* @return
138+
*/
139+
public JsonNode getSearchParameters(int searchAction) {
140+
JsonNode uriParams = null;
141+
ObjectMapper objectmapper = new ObjectMapper();
142+
143+
String[] linkTypes = {"ALL"};
144+
145+
try {
146+
uriParams = objectmapper.readTree("{}");
147+
if (searchAction == DOWNSTREAM) {
148+
putSearchParameter(uriParams, "dlt", linkTypes);
149+
} else if (searchAction == UPSTREAM) {
150+
putSearchParameter(uriParams, "ult", linkTypes);
151+
} else if (searchAction == DOWNANDUPSTREAM) {
152+
putSearchParameter(uriParams, "dlt", linkTypes);
153+
putSearchParameter(uriParams, "ult", linkTypes);
154+
}
155+
} catch (Exception e) {
156+
log.error(e.getMessage(), e);
157+
}
158+
return uriParams;
159+
}
160+
161+
/** Create an array node with link types for upstream or downstream query
162+
* @param params
163+
* @param actionString
164+
* @param linkTypes
165+
*/
166+
public void putSearchParameter(JsonNode params, String actionString, String[] linkTypes) {
167+
ArrayNode node =((ObjectNode) params).putArray(actionString);
168+
for (String string : linkTypes) {
169+
node.add(string);
170+
}
171+
}
172+
173+
@PostConstruct
174+
public void init() {
175+
log.debug("The url parameter is : " + url);
176+
}
177+
}

src/main/resources/application.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ subscriptionDataBaseName: subscription
4343
email.sender: noreply@ericsson.com
4444
email.subject: Email Subscription Notification
4545
notification.failAttempt: 3
46-
notification.ttl.value: 600
46+
notification.ttl.value: 600
47+
48+
er.url: http://localhost:8080/search/
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
Copyright 2017 Ericsson AB.
3+
For a full list of individual contributors, please see the commit history.
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
package com.ericsson.ei.erqueryservice.test;
15+
16+
import static org.junit.Assert.assertEquals;
17+
18+
import java.net.URI;
19+
import java.net.URISyntaxException;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.mockito.InjectMocks;
25+
import org.mockito.Mock;
26+
import org.mockito.Mockito;
27+
import org.mockito.MockitoAnnotations;
28+
29+
import static org.mockito.BDDMockito.*;
30+
import org.mockito.invocation.Invocation;
31+
import org.mockito.invocation.InvocationOnMock;
32+
import org.mockito.stubbing.Answer;
33+
import org.springframework.beans.factory.annotation.Autowired;
34+
import org.springframework.boot.test.context.SpringBootTest;
35+
import org.springframework.boot.test.mock.mockito.MockBean;
36+
import org.springframework.http.HttpEntity;
37+
import org.springframework.http.HttpMethod;
38+
import org.springframework.http.HttpRequest;
39+
import org.springframework.http.HttpStatus;
40+
import org.springframework.http.ResponseEntity;
41+
import org.springframework.http.server.ServletServerHttpRequest;
42+
import org.springframework.mock.web.MockHttpServletRequest;
43+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
44+
import org.springframework.util.LinkedMultiValueMap;
45+
import org.springframework.util.MultiValueMap;
46+
import org.springframework.web.client.RestOperations;
47+
import org.springframework.web.util.UriComponents;
48+
import org.springframework.web.util.UriComponentsBuilder;
49+
50+
import com.ericsson.ei.controller.model.Subscription;
51+
import com.ericsson.ei.erqueryservice.ERQueryService;
52+
import com.fasterxml.jackson.databind.JsonNode;
53+
import com.fasterxml.jackson.databind.node.ObjectNode;
54+
55+
@RunWith(SpringJUnit4ClassRunner.class)
56+
@SpringBootTest
57+
public class ERQueryServiceTest {
58+
59+
@Autowired
60+
ERQueryService erQueryService;
61+
62+
@Mock
63+
RestOperations rest;
64+
65+
String eventId = "01";
66+
int searchAction = 0;
67+
int limitParam = 85;
68+
int levels = 2;
69+
boolean isTree = true;
70+
71+
@Before public void setUp() throws Exception {
72+
MockitoAnnotations.initMocks(this);
73+
}
74+
75+
@Test
76+
public void testErQueryUpstream() {
77+
erQueryService.setRest(rest);
78+
searchAction = ERQueryService.UPSTREAM;
79+
given(rest.exchange(Mockito.any(URI.class), Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class), Mockito.any(Class.class))).willAnswer(returnRestExchange(Mockito.any(URI.class), Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class), Mockito.any(Class.class)));
80+
ResponseEntity result = erQueryService.getEventStreamDataById(eventId, searchAction, limitParam, levels, isTree);
81+
}
82+
83+
Answer<ResponseEntity> returnRestExchange(URI url, HttpMethod method, HttpEntity<?> requestEntity,
84+
Class responseType) {
85+
return new Answer<ResponseEntity>() {
86+
@Override
87+
public ResponseEntity answer(InvocationOnMock invocation) throws Throwable {
88+
URI arg0 = invocation.getArgumentAt(0, URI.class);
89+
String expectedUri = buildUri();
90+
assertEquals(expectedUri, arg0.toString());
91+
HttpEntity arg2 = invocation.getArgumentAt(2, HttpEntity.class);
92+
ObjectNode body = (ObjectNode) arg2.getBody();
93+
assertBody(body);
94+
boolean firstStop = true;
95+
return new ResponseEntity(HttpStatus.OK);
96+
}
97+
};
98+
}
99+
100+
public String buildUri() {
101+
String uri = "";
102+
// example uri
103+
// http://localhost:8080/search/01?limit=85&levels=2&tree=true
104+
uri += erQueryService.getUrl().trim() + eventId + "?limit=" + limitParam + "&levels=" + levels + "&tree=" + isTree;
105+
return uri;
106+
}
107+
108+
public void assertBody(ObjectNode body) {
109+
// example body
110+
// {"ult":["ALL"]}
111+
boolean bodyNotNull = body != null;
112+
assertEquals(bodyNotNull, true);
113+
boolean searchActionIsRight = false;
114+
if (searchAction == ERQueryService.DOWNSTREAM) {
115+
searchActionIsRight = body.has("dlt");
116+
} else if (searchAction == ERQueryService.UPSTREAM) {
117+
searchActionIsRight = body.has("ult");
118+
} else if (searchAction == ERQueryService.DOWNANDUPSTREAM) {
119+
searchActionIsRight = body.has("dlt") && body.has("ult");
120+
}
121+
assertEquals(searchActionIsRight, true);
122+
}
123+
124+
@Test
125+
public void queryParamsTest() throws URISyntaxException {
126+
MultiValueMap<String, String> expectedQueryParams = new LinkedMultiValueMap(3);
127+
expectedQueryParams.add("limit", "10");
128+
expectedQueryParams.add("levels", "5");
129+
expectedQueryParams.add("tree", "true");
130+
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
131+
UriComponents result = builder.queryParams(expectedQueryParams).build();
132+
assertEquals("limit=10&levels=5&tree=true", result.getQuery());
133+
assertEquals(expectedQueryParams, result.getQueryParams());
134+
}
135+
136+
@Test
137+
public void uriParamAndHeaderTest() throws Exception {
138+
MockHttpServletRequest request = new MockHttpServletRequest();
139+
request.addHeader("Forwarded", "proto=https; host=127.0.0.1");
140+
request.setScheme("http");
141+
request.setServerName("localhost");
142+
request.setRequestURI("/search/6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43");
143+
144+
HttpRequest httpRequest = new ServletServerHttpRequest(request);
145+
UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
146+
147+
assertEquals("https", result.getScheme());
148+
assertEquals("127.0.0.1", result.getHost());
149+
assertEquals("/search/6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43", result.getPath());
150+
assertEquals("https://127.0.0.1/search/6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43", result.toUriString());
151+
}
152+
153+
}

0 commit comments

Comments
 (0)