791e9bad0ec596d3a921a6696d99f2b9be36446e
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.apigateway.test;
19
20 import static org.junit.Assert.*;
21 import static org.mockito.Mockito.mock;
22 import static org.junit.Assert.assertTrue;
23 import static org.mockito.Mockito.when;
24
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.junit.Test;
29 import org.onap.ccsdk.features.sdnr.wt.apigateway.database.DatabaseEntryProvider;
30 import org.onap.ccsdk.features.sdnr.wt.apigateway.database.DatabaseHttpClient;
31 import org.onap.ccsdk.features.sdnr.wt.apigateway.database.MediatorServerInfo;
32 import org.onap.ccsdk.features.sdnr.wt.apigateway.database.http.BaseHTTPResponse;
33
34 public class TestDatabaseHttpClient {
35
36         private static final String ID_1="AWwscPepjf5-TrAFEdUD";
37         private static final String HOSTNAME_1="http://192.168.178.89:7070";
38         private static final String NAME_1="internal mediatorserver";
39         private static final String ID_2="AWwscSCWjf5-TrAFEdUE";
40         private static final String HOSTNAME_2="http://192.168.178.89:7071";
41         private static final String NAME_2="internal mediatorserver2";
42         private static final String ID_3="AWwscSCWjf5-TrAFEdsE";
43         private static final String HOSTNAME_3="http://192.168.178.88:7371";
44         private static final String NAME_3="test mediatorserver2";
45         
46         private static final String RESPONSE_VALID="{\n" + 
47                         "\"took\": 3,\n" + 
48                         "\"timed_out\": false,\n" + 
49                         "\"_shards\": {\n" + 
50                         "\"total\": 5,\n" + 
51                         "\"successful\": 5,\n" + 
52                         "\"failed\": 0\n" + 
53                         "},\n" + 
54                         "\"hits\": {\n" + 
55                         "\"total\": 2,\n" + 
56                         "\"max_score\": 1,\n" + 
57                         "\"hits\": [\n" + 
58                         "{\n" + 
59                         "\"_index\": \"mwtn_v1\",\n" + 
60                         "\"_type\": \"mediator-server\",\n" + 
61                         "\"_id\": \"AWwscPepjf5-TrAFEdUD\",\n" + 
62                         "\"_score\": 1,\n" + 
63                         "\"_source\": {\n" + 
64                         "\"name\": \"internal mediatorserver\",\n" + 
65                         "\"url\": \"http://192.168.178.89:7070\"\n" + 
66                         "}\n" + 
67                         "}\n" + 
68                         ",\n" + 
69                         "{\n" + 
70                         "\"_index\": \"mwtn_v1\",\n" + 
71                         "\"_type\": \"mediator-server\",\n" + 
72                         "\"_id\": \"AWwscSCWjf5-TrAFEdUE\",\n" + 
73                         "\"_score\": 1,\n" + 
74                         "\"_source\": {\n" + 
75                         "\"name\": \"internal mediatorserver2\",\n" + 
76                         "\"url\": \"http://192.168.178.89:7071\"\n" + 
77                         "}\n" + 
78                         "}\n" + 
79                         "]\n" + 
80                         "}\n" + 
81                         "}";
82         
83         private class PublicDatabaseHttpClient extends DatabaseHttpClient{
84
85                 public PublicDatabaseHttpClient(String base, boolean trustAllCerts) {
86                         super(base, trustAllCerts);
87                 }
88                 @Override
89                 public BaseHTTPResponse sendRequest(String uri, String method, String body, Map<String, String> headers) {
90                         return new BaseHTTPResponse(200, RESPONSE_VALID);
91                 }
92         }
93         public class PublicDatabaseEntryProvider extends DatabaseEntryProvider {
94
95                 protected PublicDatabaseEntryProvider() {
96                         super(new PublicDatabaseHttpClient("http://localhost/",false), 60);
97                 }
98
99         }
100         @Test
101         public void test() {
102                 
103                 final PublicDatabaseHttpClient client = new PublicDatabaseHttpClient("http://localhost/",false);
104                 Map<String, MediatorServerInfo> entries = client.requestEntries();
105                 assertEquals("result size is not correct",2,entries.size());
106                 assertEquals("hostname 1 is not correct",HOSTNAME_1,entries.get(ID_1).getHost());
107                 assertEquals("name 1 is not correct",NAME_1,entries.get(ID_1).getName());
108                 assertEquals("hostname 2 is not correct",HOSTNAME_2,entries.get(ID_2).getHost());
109                 assertEquals("name 2 is not correct",NAME_2,entries.get(ID_2).getName());
110         }
111         @Test
112         public void test2() {
113                 final PublicDatabaseEntryProvider provider  = new PublicDatabaseEntryProvider();
114                 boolean reloaded = provider.triggerReloadSync();
115                 assertTrue("data were not reloaded",reloaded);
116                 try {
117                         Thread.sleep(1500);
118                 } catch (InterruptedException e) {
119                         Thread.interrupted();
120                 }                       
121         
122                 System.out.println(provider.printEntries());
123                 assertEquals("provider has not loaded data",HOSTNAME_1,provider.getHostUrl(ID_1));
124                 assertEquals("provider has not loaded data",HOSTNAME_2,provider.getHostUrl(ID_2));
125                 Map<String, MediatorServerInfo> entries2 = new HashMap<String,MediatorServerInfo>();
126                 entries2.put(ID_3, new MediatorServerInfo(NAME_3, HOSTNAME_3));
127                 provider.setEntries(entries2);  
128                 assertEquals("provider has not loaded data",HOSTNAME_3,provider.getHostUrl(ID_3));
129                 try {
130                         provider.close();
131                 } catch (Exception e) {
132                         
133                 }
134                 
135                 
136         }
137 }