I am trying to cover Junit for Elasticsearch ParsedFilter.
Code:
private void returnDeviceColumnValues(List<String> uniqueColumnValues, NativeSearchQuery searchQuery) {
SearchHits<ESDashboardEntity> searchHits = operations.search(searchQuery, ESDashboardEntity.class);
ElasticsearchAggregations aggregations = (ElasticsearchAggregations) searchHits.getAggregations();
List<Aggregation> aggregationList = aggregations.aggregations().asList();
aggregationList.forEach(agg -> {
if (agg instanceof ParsedNested nestedAgg) {
Aggregations subAggs = nestedAgg.getAggregations();
ParsedFilter parseFilter = (ParsedFilter) subAggs.get(DashboardConstant.FILTER_AGG);
ParsedStringTerms terms = parseFilter.getAggregations().get(DashboardConstant.UNIQUE_VALUES_AGG);
for (Terms.Bucket bucket : terms.getBuckets()) {
uniqueColumnValues.add(bucket.getKeyAsString());
}
} else if (agg instanceof Terms terms) {
for (Terms.Bucket bucket : terms.getBuckets()) {
uniqueColumnValues.add(bucket.getKeyAsString());
}
}
});
}
Junit Code:
@Test
void testReturnDeviceColumnValues_WithParsedFilter() throws IOException, MTValidationException {
TypeReference<FilterColumnValuesDTO> typeReference = new TypeReference<>() {
};
FilterColumnValuesDTO deviceInput = BaseConverter.build("deviceFilterRequest.json", typeReference);
NativeSearchQuery searchQuery = mock(NativeSearchQuery.class);
TypeReference<ESDashboardEntity> dashboardTypeReference = new TypeReference<>() {
};
ESDashboardEntity deviceOutput = BaseConverter.build("ESDashboardEntity.json", dashboardTypeReference);
String deviceInputString = mapper.writeValueAsString(deviceInput);
ParsedNested nestedAgg = mock(ParsedNested.class);
ParsedFilter parsedFilter = mock(ParsedFilter.class);
ParsedStringTerms terms = mock(ParsedStringTerms.class);
Aggregations subAggs = mock(Aggregations.class);
when(aggregations.aggregations()).thenReturn(subAggs);
when(nestedAgg.getAggregations()).thenReturn(subAggs);
when(subAggs.get(DashboardConstant.FILTER_AGG)).thenReturn(parsedFilter);
Aggregations filterSubAggs = mock(Aggregations.class);
when(parsedFilter.getAggregations()).thenReturn(filterSubAggs);
List buckets = new ArrayList<>();
Terms.Bucket bucket1 = mock(Terms.Bucket.class);
when(bucket1.getKeyAsString()).thenReturn("value1");
buckets.add(bucket1);
when(terms.getBuckets()).thenReturn(buckets); // Executing the method
List<ParsedNested> aggregationList = Collections.singletonList(nestedAgg);
when(operations.search(Mockito.any(NativeSearchQuery.class), Mockito.eq(ESDashboardEntity.class)))
.thenReturn(searchHits);
List<SearchHit<ESDashboardEntity>> searchHit = new ArrayList<>();
searchHit.add(new SearchHit("1", "1", null, 0, null, null, null, null, null, null, deviceOutput));
when(searchHits.getSearchHits()).thenReturn(searchHit);
when(searchHits.getTotalHits()).thenReturn(1l);
when(searchHits.getMaxScore()).thenReturn(1.0f);
when(searchHits.getAggregations()).thenReturn(aggregations);
when(aggregations.aggregations()).thenReturn(new Aggregations((List<? extends Aggregation>) aggregationList));
when(searchHits.getAggregations()).thenReturn(aggregations);
String encodeToString = Base64.getEncoder().encodeToString(deviceInputString.getBytes());
DeviceColumnFilterValuesDTO deviceColumnData = deviceService.getDeviceColumnData(encodeToString);
assertNotNull(deviceColumnData);
}
I am getting below exception. The subAgg is null
java.lang.NullPointerException: Cannot invoke "java.util.List.size()" because "this.aggregations" is null
at org.elasticsearch.search.aggregations.Aggregations.getAsMap(Aggregations.java:74)
at org.elasticsearch.search.aggregations.Aggregations.asMap(Aggregations.java:66)
at org.elasticsearch.search.aggregations.Aggregations.get(Aggregations.java:88)
at com.mt.retail.dashboardservice.service.DeviceServiceTest.testReturnDeviceColumnValues_WithParsedFilter(DeviceServiceTest.java:374)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)