1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
package org.onap.aai.sparky.search;
import static org.junit.Assert.assertEquals;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.component.restlet.RestletConstants;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.onap.aai.restclient.client.OperationResult;
import org.onap.aai.sparky.dal.ElasticSearchAdapter;
import org.onap.aai.sparky.search.EntityCountHistoryProcessor;
import org.onap.aai.sparky.util.RestletUtils;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.ClientInfo;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class EntityCountHistoryProcessorTest {
private EntityCountHistoryProcessor entityCountHistoryProcessor;
private Exchange mockExchange;
private Message mockRequestMessage;
private Message mockResponseMessage;
private Request mockRestletRequest;
private Response mockRestletResponse;
private ClientInfo requestClientInfo;
private RestletUtils mockRestletUtils;
private ElasticSearchAdapter mockElasticSearchAdapter;
@Before
public void init() throws Exception {
mockExchange = Mockito.mock(Exchange.class);
mockRequestMessage = Mockito.mock(Message.class);
mockResponseMessage = Mockito.mock(Message.class);
mockRestletRequest = Mockito.mock(Request.class);
mockRestletResponse = Mockito.mock(Response.class);
mockRestletUtils = Mockito.mock(RestletUtils.class);
mockElasticSearchAdapter = Mockito.mock(ElasticSearchAdapter.class);
entityCountHistoryProcessor = new EntityCountHistoryProcessor(mockElasticSearchAdapter,
"pserver", "vnf", "entityCount-index");
entityCountHistoryProcessor.setRestletUtils(mockRestletUtils);
requestClientInfo = new ClientInfo();
Mockito.when(mockRestletRequest.getClientInfo()).thenReturn(requestClientInfo);
Mockito.when(mockRequestMessage.getHeader(RestletConstants.RESTLET_REQUEST, Request.class))
.thenReturn(mockRestletRequest);
Mockito.when(mockRequestMessage.getHeader(RestletConstants.RESTLET_RESPONSE, Response.class))
.thenReturn(mockRestletResponse);
Mockito.when(mockExchange.getIn()).thenReturn(mockRequestMessage);
Mockito.when(mockExchange.getOut()).thenReturn(mockResponseMessage);
}
public String getStubbedData_getEntityCountHistory_success() {
ObjectMapper mapper = new ObjectMapper();
ObjectNode entityCountEntry = mapper.createObjectNode();
entityCountEntry.put("doc_count", 2069);
entityCountEntry.put("key", "complex");
ArrayNode arrayNode = mapper.createArrayNode();
arrayNode.add(entityCountEntry);
ObjectNode resultNode = mapper.createObjectNode();
resultNode.set("result", arrayNode);
return resultNode.toString();
}
@Test
public void testGetEntityCountHistory_success() {
EntityCountHistoryProcessor spyEntityCountHistoryProcessor =
Mockito.spy(entityCountHistoryProcessor);
OperationResult operationResult = new OperationResult();
String result = getStubbedData_getEntityCountHistory_success();
operationResult.setResult(Status.SUCCESS_OK.getCode(), result);
Mockito.when(mockExchange.getIn().getHeader("CamelHttpQuery", String.class)).thenReturn("");
Mockito.doReturn("table").when(spyEntityCountHistoryProcessor).getTypeParameter(Mockito.any());
Mockito.doReturn(operationResult).when(spyEntityCountHistoryProcessor).getResults(Mockito.any(),
Mockito.any());
}
@Test
public void testGetEntityCountHistory_failure_noTypeParameter() {
Mockito.when(mockRestletRequest.getOriginalRef()).thenReturn(new Reference());
EntityCountHistoryProcessor spyEntityCountHistoryProcessor =
Mockito.spy(entityCountHistoryProcessor);
OperationResult operationResult = new OperationResult();
String result = getStubbedData_getEntityCountHistory_success();
operationResult.setResult(Status.SUCCESS_OK.getCode(), result);
Mockito.when(mockExchange.getIn().getHeader("CamelHttpQuery", String.class)).thenReturn("");
Mockito.doReturn("").when(spyEntityCountHistoryProcessor).getTypeParameter(Mockito.any());
Mockito.doReturn(operationResult).when(spyEntityCountHistoryProcessor).getResults(Mockito.any(),
Mockito.any());
}
}
|