Remove mso-oof-adapter from SO
[so.git] / so-monitoring / so-monitoring-handler / src / test / java / org / onap / so / monitoring / db / api / DatabaseServiceProviderTest.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.monitoring.db.api;
21
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.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.UUID;
33 import org.junit.Test;
34 import org.onap.so.monitoring.camunda.model.SoActiveInfraRequests;
35 import org.onap.so.monitoring.configuration.database.DatabaseUrlProvider;
36 import org.onap.so.monitoring.db.service.DatabaseServiceProvider;
37 import org.onap.so.monitoring.db.service.DatabaseServiceProviderImpl;
38 import org.onap.so.monitoring.model.SoInfraRequest;
39 import org.onap.so.rest.service.HttpRestServiceProvider;
40 import com.google.common.base.Optional;
41
42
43 /**
44  * @author waqas.ikram@ericsson.com
45  */
46 public class DatabaseServiceProviderTest {
47
48     private final static DatabaseUrlProvider URL_PROVIDER =
49             new DatabaseUrlProvider("http://localhost:8081/infraActiveRequests/");
50
51     @Test
52     public void test_GetSoInfraRequest_WithEmptyFilters_EmptyList() {
53         final HttpRestServiceProvider mockServiceProvider = mock(HttpRestServiceProvider.class);
54         final String searchUrl = URL_PROVIDER.getSearchUrl(0, 0, null);
55         final Optional<SoActiveInfraRequests[]> response = Optional.of(new SoActiveInfraRequests[] {});
56
57         when(mockServiceProvider.post(eq(Collections.emptyMap()), eq(searchUrl), eq(SoActiveInfraRequests[].class)))
58                 .thenReturn(response);
59
60         final DatabaseServiceProvider objUnderTest = new DatabaseServiceProviderImpl(URL_PROVIDER, mockServiceProvider);
61
62         assertTrue(objUnderTest.getSoInfraRequest(Collections.emptyMap(), 0, 0, null).isEmpty());
63     }
64
65     @Test
66     public void test_GetSoInfraRequest_OptionalAbsent_EmptyList() {
67         final HttpRestServiceProvider mockServiceProvider = mock(HttpRestServiceProvider.class);
68         final String searchUrl = URL_PROVIDER.getSearchUrl(0, 0, null);
69         final Optional<SoActiveInfraRequests[]> response = Optional.absent();
70
71         when(mockServiceProvider.post(eq(Collections.emptyMap()), eq(searchUrl), eq(SoActiveInfraRequests[].class)))
72                 .thenReturn(response);
73
74         final DatabaseServiceProvider objUnderTest = new DatabaseServiceProviderImpl(URL_PROVIDER, mockServiceProvider);
75
76         assertTrue(objUnderTest.getSoInfraRequest(Collections.emptyMap(), 0, 0, null).isEmpty());
77     }
78
79     @Test
80     public void test_GetSoInfraRequest_WithFilters_InfraActiveRequestsList() {
81         final String searchUrl = URL_PROVIDER.getSearchUrl(0, 0, null);
82         final String requestID = UUID.randomUUID().toString();
83         final Map<String, String[]> filters = new HashMap<>();
84         filters.put("requestId", new String[] {"EQ", requestID});
85
86         final SoActiveInfraRequests soActiveInfraRequests = new SoActiveInfraRequests();
87         soActiveInfraRequests.setRequestId(requestID);
88
89         final Optional<SoActiveInfraRequests[]> response =
90                 Optional.of(new SoActiveInfraRequests[] {soActiveInfraRequests});
91
92         final HttpRestServiceProvider mockServiceProvider = mock(HttpRestServiceProvider.class);
93
94         when(mockServiceProvider.post(eq(filters), eq(searchUrl), eq(SoActiveInfraRequests[].class)))
95                 .thenReturn(response);
96
97         final DatabaseServiceProvider objUnderTest = new DatabaseServiceProviderImpl(URL_PROVIDER, mockServiceProvider);
98
99         final List<SoInfraRequest> actualList = objUnderTest.getSoInfraRequest(filters, 0, 0, null);
100         assertFalse(actualList.isEmpty());
101         assertEquals(requestID, actualList.get(0).getRequestId());
102
103     }
104 }