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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.db.request.data.repository;
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;
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;
51 @RunWith(SpringRunner.class)
52 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
53 @ActiveProfiles("test")
54 public class InfraActiveRequestsRepositoryImplTest {
57 * January 1, 2019 2:00:00 PM
59 private static final long END_TIME = 1546351200000l;
62 * January 1, 2019 12:45:00 PM
64 private static final long START_TIME = 1546346700000l;
65 private static final int MAX_LIMIT = 1;
68 * December 23, 2016 23:59 PM
70 private static final long END_TIME_IN_MILISEC = 1482580740000l;
73 * December 21, 2016 01:00 AM
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";
80 private InfraActiveRequestsRepositoryImpl objUnderTest;
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());
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());
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());
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());
115 assertEquals(SERVICE_INSTANCE_ID_VALUE, actualRequests.get(0).getServiceInstanceId());
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());
126 assertEquals(REQUEST_ID_VALUE, actualRequests.get(0).getRequestId());
127 assertEquals(SERVICE_INSTANCE_ID_VALUE, actualRequests.get(0).getServiceInstanceId());
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());
140 assertEquals(REQUEST_ID_VALUE, actualRequests.get(0).getRequestId());
141 assertEquals(SERVICE_INSTANCE_ID_VALUE, actualRequests.get(0).getServiceInstanceId());
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());
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");
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());
169 assertEquals(3, actualRequests.size());
170 assertEquals("ShouldReturnInSearchQuery_1,ShouldReturnInSearchQuery_2,ShouldReturnInSearchQuery_3",
171 actualRequests.stream().map(item -> item.getServiceInstanceName()).collect(Collectors.joining(",")));