4097aee7d51cda9a4a8f932e0e10939ebe614127
[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.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotEquals;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.onap.so.db.request.data.repository.InfraActiveRequestsRepositoryImpl.ACTION;
28 import static org.onap.so.db.request.data.repository.InfraActiveRequestsRepositoryImpl.REQUEST_ID;
29 import static org.onap.so.db.request.data.repository.InfraActiveRequestsRepositoryImpl.SERVICE_INSTANCE_ID;
30
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.concurrent.TimeUnit;
36
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.onap.so.TestApplication;
40 import org.onap.so.db.request.beans.InfraActiveRequests;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.test.context.ActiveProfiles;
44 import org.springframework.test.context.junit4.SpringRunner;
45
46
47 @RunWith(SpringRunner.class)
48 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
49 @ActiveProfiles("test")
50 public class InfraActiveRequestsRepositoryImplTest {
51
52     private static final int MAX_LIMIT = 1;
53     private static final long END_TIME_IN_MILISEC = 1482580740000l;      // December 23, 2016 23:59 PM
54     private static final long START_TIME_IN_MILISEC = 1482282000000l;    // December 21, 2016 01:00 AM
55     private static final String REQUEST_ID_VALUE = "00032ab7-3fb3-42e5-965d-8ea592502017";
56     private static final String SERVICE_INSTANCE_ID_VALUE = "e3b5744d-2ad1-4cdd-8390-c999a38829bc";
57
58     @Autowired
59     private InfraActiveRequestsRepository objUnderTest;
60
61     @Test
62     public void test_GetInfraActiveRequests_emptyFiltersMap() {
63         final List<InfraActiveRequests> actualRequests = objUnderTest.getInfraActiveRequests(Collections.emptyMap(),
64                 START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, null);
65         assertFalse(actualRequests.isEmpty());
66     }
67
68     @Test
69     public void test_GetInfraActiveRequests_invalidFiltersMap() {
70         final Map<String, String[]> filters = new HashMap<>();
71         filters.put("OverTheMoon", new String[] {"Humpty Dumpty Sat On The Wall"});
72         final long startTime = START_TIME_IN_MILISEC - TimeUnit.DAYS.toMillis(20);
73         final long endTime = END_TIME_IN_MILISEC - TimeUnit.DAYS.toMillis(20);
74         final List<InfraActiveRequests> actualRequests =
75                 objUnderTest.getInfraActiveRequests(filters, startTime, endTime, null);
76         assertTrue(actualRequests.isEmpty());
77     }
78
79     @Test
80     public void test_GetInfraActiveRequests_invalidFiltersMapWithInvalidKey() {
81         final Map<String, String[]> filters = new HashMap<>();
82         filters.put("OverTheMoon", new String[] {"Avengers", "Humpty Dumpty Sat On The Wall"});
83         final List<InfraActiveRequests> actualRequests =
84                 objUnderTest.getInfraActiveRequests(filters, START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, null);
85         assertFalse(actualRequests.isEmpty());
86     }
87
88     @Test
89     public void test_GetInfraActiveRequestsData_withEqualServiceInstanceId() {
90         final Map<String, String[]> values = new HashMap<>();
91         values.put(SERVICE_INSTANCE_ID, new String[] {QueryOperationType.EQ.name(), SERVICE_INSTANCE_ID_VALUE});
92         final List<InfraActiveRequests> actualRequests =
93                 objUnderTest.getInfraActiveRequests(values, START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, 1);
94         assertFalse(actualRequests.isEmpty());
95
96         assertEquals(SERVICE_INSTANCE_ID_VALUE, actualRequests.get(0).getServiceInstanceId());
97     }
98
99
100     @Test
101     public void test_GetInfraActiveRequestsData_withLikeRequestID() {
102         final Map<String, String[]> values = new HashMap<>();
103         values.put(REQUEST_ID, new String[] {QueryOperationType.LIKE.name(), "00032ab7"});
104         final List<InfraActiveRequests> actualRequests =
105                 objUnderTest.getInfraActiveRequests(values, START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, 1);
106         assertFalse(actualRequests.isEmpty());
107
108         assertEquals(REQUEST_ID_VALUE, actualRequests.get(0).getRequestId());
109         assertEquals(SERVICE_INSTANCE_ID_VALUE, actualRequests.get(0).getServiceInstanceId());
110     }
111
112
113     @Test
114     public void test_GetInfraActiveRequestsData_withLikeRequestIDAndEqualToServiceInstanceId() {
115         final Map<String, String[]> values = new HashMap<>();
116         values.put(REQUEST_ID, new String[] {QueryOperationType.LIKE.name(), "00032ab7"});
117         values.put(SERVICE_INSTANCE_ID, new String[] {QueryOperationType.EQ.name(), SERVICE_INSTANCE_ID_VALUE});
118         final List<InfraActiveRequests> actualRequests =
119                 objUnderTest.getInfraActiveRequests(values, START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, 1);
120         assertFalse(actualRequests.isEmpty());
121
122         assertEquals(REQUEST_ID_VALUE, actualRequests.get(0).getRequestId());
123         assertEquals(SERVICE_INSTANCE_ID_VALUE, actualRequests.get(0).getServiceInstanceId());
124     }
125
126
127     @Test
128     public void test_GetInfraActiveRequestsData_withNotEqualAction() {
129         final Map<String, String[]> values = new HashMap<>();
130         values.put(ACTION, new String[] {QueryOperationType.NEQ.name(), "createInstance"});
131         final List<InfraActiveRequests> actualRequests =
132                 objUnderTest.getInfraActiveRequests(values, START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, null);
133         assertFalse(actualRequests.isEmpty());
134         for (final InfraActiveRequests actualActiveRequests : actualRequests) {
135             assertNotEquals("createInstance", actualActiveRequests.getAction());
136         }
137     }
138
139
140     @Test
141     public void test_GetInfraActiveRequestsData_withNotEqualToServiceInstanceNameAndServiceInstanceIdNul() {
142         final Map<String, String[]> values = new HashMap<>();
143         values.put(ACTION, new String[] {QueryOperationType.NEQ.name(), "createInstance"});
144         values.put(SERVICE_INSTANCE_ID, new String[] {QueryOperationType.EQ.name(), SERVICE_INSTANCE_ID_VALUE});
145
146         final List<InfraActiveRequests> actualRequests =
147                 objUnderTest.getInfraActiveRequests(values, START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, null);
148         assertFalse(actualRequests.isEmpty());
149         for (final InfraActiveRequests actualActiveRequests : actualRequests) {
150             assertNotEquals("createInstance", actualActiveRequests.getAction());
151             assertEquals(SERVICE_INSTANCE_ID_VALUE, actualActiveRequests.getServiceInstanceId());
152         }
153     }
154
155     @Test
156     public void test_GetInfraActiveRequestsData_withStartEndDateTimeNotEqualAction() {
157         final Map<String, String[]> values = new HashMap<>();
158         values.put(ACTION, new String[] {QueryOperationType.NEQ.name(), "createInstance"});
159         final List<InfraActiveRequests> actualRequests =
160                 objUnderTest.getInfraActiveRequests(values, START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, null);
161         assertFalse(actualRequests.isEmpty());
162         for (final InfraActiveRequests actualActiveRequests : actualRequests) {
163             assertNotEquals("createInstance", actualActiveRequests.getAction());
164         }
165     }
166
167     @Test
168     public void test_GetInfraActiveRequestsData_withLimitNotEqualAction() {
169         final Map<String, String[]> values = new HashMap<>();
170         values.put(ACTION, new String[] {QueryOperationType.NEQ.name(), "createInstance"});
171         final List<InfraActiveRequests> actualRequests =
172                 objUnderTest.getInfraActiveRequests(values, START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, MAX_LIMIT);
173         assertFalse(actualRequests.isEmpty());
174         for (final InfraActiveRequests actualActiveRequests : actualRequests) {
175             assertNotEquals("createInstance", actualActiveRequests.getAction());
176         }
177     }
178
179     @Test
180     public void test_GetInfraActiveRequestsData_withNullFilters() {
181         final List<InfraActiveRequests> actualRequests =
182                 objUnderTest.getInfraActiveRequests(null, START_TIME_IN_MILISEC, END_TIME_IN_MILISEC, MAX_LIMIT);
183         assertTrue(actualRequests.isEmpty());
184     }
185     
186     @Test
187     public void checkInstanceNameDuplicateNullInstanceNameTest(){
188         Map<String, String> instanceIdMap = new HashMap<>();
189         instanceIdMap.put("serviceInstanceId", "e05864f0-ab35-47d0-8be4-56fd9619ba3b");
190         InfraActiveRequests results = objUnderTest.checkInstanceNameDuplicate((HashMap<String, String>)instanceIdMap, null, "vnf");
191         assertNull(results);
192     }
193 }