8122928d3d94e7cb1621e10c0804a90dc0e81410
[aai/sparky-be.git] /
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2024 Deutsche Telekom Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.sparky.aggregatevnf;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.mockito.Mockito.when;
24
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.onap.aai.restclient.client.OperationResult;
35 import org.onap.aai.sparky.entities.Aggregation;
36 import org.onap.aai.sparky.entities.AggregationFilter;
37 import org.onap.aai.sparky.entities.AggregationResult;
38 import org.onap.aai.sparky.entities.AggregationsResponse;
39 import org.onap.aai.sparky.entities.SearchServiceAggregationsResponse;
40 import org.onap.aai.sparky.entities.Bucket;
41 import org.onap.aai.sparky.entities.BucketResponse;
42 import org.onap.aai.sparky.entities.FilterAggregationRequest;
43 import org.onap.aai.sparky.entities.SearchResult;
44 import org.onap.aai.sparky.search.SearchServiceAdapter;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.beans.factory.annotation.Value;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
49 import org.springframework.boot.test.mock.mockito.MockBean;
50 import org.springframework.boot.test.web.client.TestRestTemplate;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.test.context.ActiveProfiles;
53 import org.springframework.test.context.junit4.SpringRunner;
54 import org.springframework.web.client.RestClientException;
55
56 import com.fasterxml.jackson.core.JsonProcessingException;
57 import com.fasterxml.jackson.databind.ObjectMapper;
58
59 @ActiveProfiles({"test","oxm-default","oxm-schema-dev", "camel","fe-dev"})
60 @RunWith(SpringRunner.class)
61 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
62 public class AggregateSummaryProcessorTest {
63
64   ObjectMapper objectMapper = new ObjectMapper();
65
66   @MockBean SearchServiceAdapter searchServiceAdapter;
67   @Mock OperationResult operationResult;
68
69   @Autowired
70   private TestRestTemplate restTemplate;
71
72   @Value("${schema.ingest.file}") String schemaIngestFileLocation;
73
74   @Test
75   public void thatFilterAggregationWorks() throws RestClientException, JsonProcessingException {
76     when(searchServiceAdapter.doPost(Mockito.any(), Mockito.any())).thenReturn(operationResult);
77     when(operationResult.wasSuccessful()).thenReturn(true);
78
79     List<Aggregation> aggregationList = Arrays.asList(new Aggregation("someAggregation", Arrays.asList(new Bucket(1L,"someKey"))));
80     SearchServiceAggregationsResponse searchServiceAggregationsResponse = new SearchServiceAggregationsResponse(new SearchResult(1L), new AggregationResult(aggregationList), 1L);
81     when(operationResult.getResult()).thenReturn(objectMapper.writeValueAsString(searchServiceAggregationsResponse));
82
83     Map<String,String> params = new HashMap<String,String>();
84     AggregationFilter filter = new AggregationFilter("1","someOrchestrationStatus");
85     FilterAggregationRequest request = new FilterAggregationRequest(Arrays.asList(filter));
86
87     ResponseEntity<AggregationsResponse> response = restTemplate.postForEntity("/rest/search/filterAggregation", objectMapper.writeValueAsString(request), AggregationsResponse.class);
88     AggregationsResponse aggregationsResponse = response.getBody();
89     assertEquals(1L, aggregationsResponse.getTotal());
90     BucketResponse bucket = aggregationsResponse.getAggregations().get("someAggregation").get(0);
91     assertEquals("someKey", bucket.getKey());
92     assertEquals(0, bucket.getDocCount());
93
94   }
95 }