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