Added CorsConfigurer class and cleaned up typos
[so.git] / so-monitoring / so-monitoring-handler / src / test / java / org / onap / so / montoring / 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.Matchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.UUID;
34
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;
42
43 import com.google.common.base.Optional;
44
45
46 /**
47  * @author waqas.ikram@ericsson.com
48  */
49 public class DatabaseServiceProviderTest {
50
51     private final static DatabaseUrlProvider URL_PROVIDER =
52             new DatabaseUrlProvider("http://localhost:8081/infraActiveRequests/");
53
54     @Test
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[] {});
59
60         when(mockServiceProvider.postHttpRequest(eq(Collections.emptyMap()), eq(searchUrl),
61                 eq(SoActiveInfraRequests[].class))).thenReturn(response);
62
63         final DatabaseServiceProvider objUnderTest = new DatabaseServiceProviderImpl(URL_PROVIDER, mockServiceProvider);
64
65         assertTrue(objUnderTest.getSoInfraRequest(Collections.emptyMap(), 0, 0, null).isEmpty());
66     }
67
68     @Test
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();
73
74         when(mockServiceProvider.postHttpRequest(eq(Collections.emptyMap()), eq(searchUrl),
75                 eq(SoActiveInfraRequests[].class))).thenReturn(response);
76
77         final DatabaseServiceProvider objUnderTest = new DatabaseServiceProviderImpl(URL_PROVIDER, mockServiceProvider);
78
79         assertTrue(objUnderTest.getSoInfraRequest(Collections.emptyMap(), 0, 0, null).isEmpty());
80     }
81
82
83     @Test
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});
89
90         SoActiveInfraRequests soActiveInfraRequests = new SoActiveInfraRequests();
91         soActiveInfraRequests.setRequestId(requestID);
92
93         final Optional<SoActiveInfraRequests[]> response =
94                 Optional.of(new SoActiveInfraRequests[] {soActiveInfraRequests});
95
96         final HttpRestServiceProvider mockServiceProvider = mock(HttpRestServiceProvider.class);
97
98         when(mockServiceProvider.postHttpRequest(eq(filters), eq(searchUrl), eq(SoActiveInfraRequests[].class)))
99                 .thenReturn(response);
100
101         final DatabaseServiceProvider objUnderTest = new DatabaseServiceProviderImpl(URL_PROVIDER, mockServiceProvider);
102
103         final List<SoInfraRequest> actualList = objUnderTest.getSoInfraRequest(filters, 0, 0, null);
104         assertFalse(actualList.isEmpty());
105         assertEquals(requestID, actualList.get(0).getRequestId());
106
107     }
108 }