3df30c0f49658d1b4e84dede9dcaf48f29650ed3
[so.git] / mso-api-handlers / mso-requests-db-repositories / src / test / java / org / onap / so / db / request / data / repository / InfraActiveRequestsRepositoryImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.db.request.data.repository;
21
22 import static org.hamcrest.CoreMatchers.containsString;
23 import static org.hamcrest.CoreMatchers.not;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertThat;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Mockito.mock;
31 import static org.onap.so.db.request.data.repository.InfraActiveRequestsRepositoryImpl.REQUEST_ID;
32 import static org.onap.so.db.request.data.repository.InfraActiveRequestsRepositoryImpl.SERVICE_INSTANCE_ID;
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.concurrent.TimeUnit;
38 import java.util.stream.Collectors;
39 import javax.persistence.criteria.CriteriaBuilder;
40 import javax.persistence.criteria.Predicate;
41 import javax.persistence.criteria.Root;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.onap.so.TestApplication;
45 import org.onap.so.db.request.beans.InfraActiveRequests;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.test.context.ActiveProfiles;
49 import org.springframework.test.context.junit4.SpringRunner;
50
51 @RunWith(SpringRunner.class)
52 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
53 @ActiveProfiles("test")
54 public class InfraActiveRequestsRepositoryImplTest {
55
56     /**
57      * January 1, 2019 2:00:00 PM
58      */
59     private static final long END_TIME = 1546351200000l;
60
61     /**
62      * January 1, 2019 12:45:00 PM
63      */
64     private static final long START_TIME = 1546346700000l;
65     private static final int MAX_LIMIT = 1;
66
67     /**
68      * December 23, 2016 23:59 PM
69      */
70     private static final long END_TIME_IN_MILISEC = 1482580740000l;
71
72     /**
73      * December 21, 2016 01:00 AM
74      */
75     private static final long START_TIME_IN_MILISEC = 1482282000000l;
76     private static final String REQUEST_ID_VALUE = "00032ab7-3fb3-42e5-965d-8ea592502017";
77     private static final String SERVICE_INSTANCE_ID_VALUE = "e3b5744d-2ad1-4cdd-8390-c999a38829bc";
78
79     @Autowired
80     private InfraActiveRequestsRepositoryImpl objUnderTest;
81
82     @Test
83     public void test_GetInfraActiveRequests_emptyFiltersMap() {
84         final List<InfraActiveRequests> actualRequests = objUnderTest.getInfraActiveRequests(Collections.emptyMap(),
85                 START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, null);
86         assertFalse(actualRequests.isEmpty());
87     }
88
89     @Test
90     public void test_GetInfraActiveRequests_invalidFiltersMap() {
91         final Map<String, String[]> filters = new HashMap<>();
92         filters.put("OverTheMoon", new String[] {"Humpty Dumpty Sat On The Wall"});
93         final List<Predicate> predicates = objUnderTest.getPredicates(filters, mock(CriteriaBuilder.class),
94                 (Root<InfraActiveRequests>) mock(Root.class));
95         assertTrue(predicates.isEmpty());
96     }
97
98     @Test
99     public void test_GetInfraActiveRequests_invalidFiltersMapWithInvalidKey() {
100         final Map<String, String[]> filters = new HashMap<>();
101         filters.put("OverTheMoon", new String[] {"Avengers", "Humpty Dumpty Sat On The Wall"});
102         final List<InfraActiveRequests> actualRequests =
103                 objUnderTest.getInfraActiveRequests(filters, START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, null);
104         assertFalse(actualRequests.isEmpty());
105     }
106
107     @Test
108     public void test_GetInfraActiveRequestsData_withEqualServiceInstanceId() {
109         final Map<String, String[]> values = new HashMap<>();
110         values.put(SERVICE_INSTANCE_ID, new String[] {QueryOperationType.EQ.name(), SERVICE_INSTANCE_ID_VALUE});
111         final List<InfraActiveRequests> actualRequests =
112                 objUnderTest.getInfraActiveRequests(values, START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, 1);
113         assertFalse(actualRequests.isEmpty());
114
115         assertEquals(SERVICE_INSTANCE_ID_VALUE, actualRequests.get(0).getServiceInstanceId());
116     }
117
118     @Test
119     public void test_GetInfraActiveRequestsData_withLikeRequestID() {
120         final Map<String, String[]> values = new HashMap<>();
121         values.put(REQUEST_ID, new String[] {QueryOperationType.LIKE.name(), "00032ab7"});
122         final List<InfraActiveRequests> actualRequests =
123                 objUnderTest.getInfraActiveRequests(values, START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, 1);
124         assertFalse(actualRequests.isEmpty());
125
126         assertEquals(REQUEST_ID_VALUE, actualRequests.get(0).getRequestId());
127         assertEquals(SERVICE_INSTANCE_ID_VALUE, actualRequests.get(0).getServiceInstanceId());
128     }
129
130
131     @Test
132     public void test_GetInfraActiveRequestsData_withLikeRequestIDAndEqualToServiceInstanceId() {
133         final Map<String, String[]> values = new HashMap<>();
134         values.put(REQUEST_ID, new String[] {QueryOperationType.LIKE.name(), "00032ab7"});
135         values.put(SERVICE_INSTANCE_ID, new String[] {QueryOperationType.EQ.name(), SERVICE_INSTANCE_ID_VALUE});
136         final List<InfraActiveRequests> actualRequests =
137                 objUnderTest.getInfraActiveRequests(values, START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, 1);
138         assertFalse(actualRequests.isEmpty());
139
140         assertEquals(REQUEST_ID_VALUE, actualRequests.get(0).getRequestId());
141         assertEquals(SERVICE_INSTANCE_ID_VALUE, actualRequests.get(0).getServiceInstanceId());
142     }
143
144     @Test
145     public void test_GetInfraActiveRequestsData_withNullFilters() {
146         final List<InfraActiveRequests> actualRequests =
147                 objUnderTest.getInfraActiveRequests(null, START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, MAX_LIMIT);
148         assertTrue(actualRequests.isEmpty());
149     }
150
151     @Test
152     public void checkInstanceNameDuplicateNullInstanceNameTest() {
153         final Map<String, String> instanceIdMap = new HashMap<>();
154         instanceIdMap.put("serviceInstanceId", "e05864f0-ab35-47d0-8be4-56fd9619ba3b");
155         final InfraActiveRequests results =
156                 objUnderTest.checkInstanceNameDuplicate((HashMap<String, String>) instanceIdMap, null, "vnf");
157         assertNull(results);
158     }
159
160     @Test
161     public void test_GetInfraActiveRequestsData_returnRecordWithNullEndTime() {
162         final Map<String, String[]> values = new HashMap<>();
163         values.put(SERVICE_INSTANCE_ID,
164                 new String[] {QueryOperationType.EQ.name(), "f7712652-b516-4925-a243-64550d26fd84"});
165         final List<InfraActiveRequests> actualRequests =
166                 objUnderTest.getInfraActiveRequests(values, START_TIME, END_TIME, null);
167         assertFalse(actualRequests.isEmpty());
168
169         assertEquals(3, actualRequests.size());
170         assertEquals("ShouldReturnInSearchQuery_1,ShouldReturnInSearchQuery_2,ShouldReturnInSearchQuery_3",
171                 actualRequests.stream().map(item -> item.getServiceInstanceName()).collect(Collectors.joining(",")));
172     }
173
174 }