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.monitoring.db.api;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.UUID;
35 import org.junit.Test;
36 import org.onap.so.monitoring.camunda.model.SoActiveInfraRequests;
37 import org.onap.so.monitoring.configuration.database.DatabaseUrlProvider;
38 import org.onap.so.monitoring.db.service.DatabaseServiceProvider;
39 import org.onap.so.monitoring.db.service.DatabaseServiceProviderImpl;
40 import org.onap.so.monitoring.model.SoInfraRequest;
41 import org.onap.so.monitoring.rest.service.HttpRestServiceProvider;
43 import com.google.common.base.Optional;
47 * @author waqas.ikram@ericsson.com
49 public class DatabaseServiceProviderTest {
51 private final static DatabaseUrlProvider URL_PROVIDER =
52 new DatabaseUrlProvider("http://localhost:8081/infraActiveRequests/");
55 public void test_GetSoInfraRequest_WithEmptyFilters_EmptyList() {
56 final HttpRestServiceProvider mockServiceProvider = mock(HttpRestServiceProvider.class);
57 final String searchUrl = URL_PROVIDER.getSearchUrl(0, 0, null);
58 final Optional<SoActiveInfraRequests[]> response = Optional.of(new SoActiveInfraRequests[] {});
60 when(mockServiceProvider.postHttpRequest(eq(Collections.emptyMap()), eq(searchUrl),
61 eq(SoActiveInfraRequests[].class))).thenReturn(response);
63 final DatabaseServiceProvider objUnderTest = new DatabaseServiceProviderImpl(URL_PROVIDER, mockServiceProvider);
65 assertTrue(objUnderTest.getSoInfraRequest(Collections.emptyMap(), 0, 0, null).isEmpty());
69 public void test_GetSoInfraRequest_OptionalAbsent_EmptyList() {
70 final HttpRestServiceProvider mockServiceProvider = mock(HttpRestServiceProvider.class);
71 final String searchUrl = URL_PROVIDER.getSearchUrl(0, 0, null);
72 final Optional<SoActiveInfraRequests[]> response = Optional.absent();
74 when(mockServiceProvider.postHttpRequest(eq(Collections.emptyMap()), eq(searchUrl),
75 eq(SoActiveInfraRequests[].class))).thenReturn(response);
77 final DatabaseServiceProvider objUnderTest = new DatabaseServiceProviderImpl(URL_PROVIDER, mockServiceProvider);
79 assertTrue(objUnderTest.getSoInfraRequest(Collections.emptyMap(), 0, 0, null).isEmpty());
84 public void test_GetSoInfraRequest_WithFilters_InfraActiveRequestsList() {
85 final String searchUrl = URL_PROVIDER.getSearchUrl(0, 0, null);
86 final String requestID = UUID.randomUUID().toString();
87 final Map<String, String[]> filters = new HashMap<>();
88 filters.put("requestId", new String[] {"EQ", requestID});
90 SoActiveInfraRequests soActiveInfraRequests = new SoActiveInfraRequests();
91 soActiveInfraRequests.setRequestId(requestID);
93 final Optional<SoActiveInfraRequests[]> response =
94 Optional.of(new SoActiveInfraRequests[] {soActiveInfraRequests});
96 final HttpRestServiceProvider mockServiceProvider = mock(HttpRestServiceProvider.class);
98 when(mockServiceProvider.postHttpRequest(eq(filters), eq(searchUrl), eq(SoActiveInfraRequests[].class)))
99 .thenReturn(response);
101 final DatabaseServiceProvider objUnderTest = new DatabaseServiceProviderImpl(URL_PROVIDER, mockServiceProvider);
103 final List<SoInfraRequest> actualList = objUnderTest.getSoInfraRequest(filters, 0, 0, null);
104 assertFalse(actualList.isEmpty());
105 assertEquals(requestID, actualList.get(0).getRequestId());