b89985755eb0bcdc9feca41787c02f93d12b7a99
[dcaegen2/services.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : DataLake
4  * ================================================================================
5  * Copyright 2019 China Mobile
6  *=================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.datalake.feeder.util;
22
23 import org.apache.http.HttpResponse;
24 import org.apache.http.client.HttpClient;
25 import org.apache.http.client.methods.HttpPost;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import static org.mockito.Mockito.when;
29
30 import java.io.IOException;
31
32 import static org.junit.Assert.*;
33
34 /**
35  * Test HtpClient
36  *
37  * @author guochunmeng
38  */
39
40 public class HttpClientUtilTest {
41
42     @Mock
43     private HttpClient  httpClient;
44
45     @Mock
46     private HttpPost httpPost;
47
48     @Mock
49     private HttpResponse httpResponse;
50
51     @Test(expected = RuntimeException.class)
52     public void testSendPostHttpClient() {
53
54         String testUrl = "http://localhost:5601/api/kibana/dashboards/import?exclude=index-pattern";
55         String testJson = "{\n" +
56                 "  \"objects\": [\n" +
57                 "    {\n" +
58                 "      \"id\": \"80b956f0-b2cd-11e8-ad8e-85441f0c2e5c\",\n" +
59                 "      \"type\": \"visualization\",\n" +
60                 "      \"updated_at\": \"2018-09-07T18:40:33.247Z\",\n" +
61                 "      \"version\": 1,\n" +
62                 "      \"attributes\": {\n" +
63                 "        \"title\": \"Count Example\",\n" +
64                 "        \"visState\": \"{\\\"title\\\":\\\"Count Example\\\",\\\"type\\\":\\\"metric\\\",\\\"params\\\":{\\\"addTooltip\\\":true,\\\"addLegend\\\":false,\\\"type\\\":\\\"metric\\\",\\\"metric\\\":{\\\"percentageMode\\\":false,\\\"useRanges\\\":false,\\\"colorSchema\\\":\\\"Green to Red\\\",\\\"metricColorMode\\\":\\\"None\\\",\\\"colorsRange\\\":[{\\\"from\\\":0,\\\"to\\\":10000}],\\\"labels\\\":{\\\"show\\\":true},\\\"invertColors\\\":false,\\\"style\\\":{\\\"bgFill\\\":\\\"#000\\\",\\\"bgColor\\\":false,\\\"labelColor\\\":false,\\\"subText\\\":\\\"\\\",\\\"fontSize\\\":60}}},\\\"aggs\\\":[{\\\"id\\\":\\\"1\\\",\\\"enabled\\\":true,\\\"type\\\":\\\"count\\\",\\\"schema\\\":\\\"metric\\\",\\\"params\\\":{}}]}\",\n" +
65                 "        \"uiStateJSON\": \"{}\",\n" +
66                 "        \"description\": \"\",\n" +
67                 "        \"version\": 1,\n" +
68                 "        \"kibanaSavedObjectMeta\": {\n" +
69                 "          \"searchSourceJSON\": \"{\\\"index\\\":\\\"90943e30-9a47-11e8-b64d-95841ca0b247\\\",\\\"query\\\":{\\\"query\\\":\\\"\\\",\\\"language\\\":\\\"lucene\\\"},\\\"filter\\\":[]}\"\n" +
70                 "        }\n" +
71                 "      }\n" +
72                 "    },\n" +
73                 "    {\n" +
74                 "      \"id\": \"90943e30-9a47-11e8-b64d-95841ca0b247\",\n" +
75                 "      \"type\": \"index-pattern\",\n" +
76                 "      \"updated_at\": \"2018-09-07T18:39:47.683Z\",\n" +
77                 "      \"version\": 1,\n" +
78                 "      \"attributes\": {\n" +
79                 "        \"title\": \"kibana_sample_data_logs\",\n" +
80                 "        \"timeFieldName\": \"timestamp\",\n" +
81                 "        \"fields\": \"<truncated for example>\",\n" +
82                 "        \"fieldFormatMap\": \"{\\\"hour_of_day\\\":{}}\"\n" +
83                 "      }\n" +
84                 "    },\n" +
85                 "    {\n" +
86                 "      \"id\": \"942dcef0-b2cd-11e8-ad8e-85441f0c2e5c\",\n" +
87                 "      \"type\": \"dashboard\",\n" +
88                 "      \"updated_at\": \"2018-09-07T18:41:05.887Z\",\n" +
89                 "      \"version\": 1,\n" +
90                 "      \"attributes\": {\n" +
91                 "        \"title\": \"Example Dashboard\",\n" +
92                 "        \"hits\": 0,\n" +
93                 "        \"description\": \"\",\n" +
94                 "        \"panelsJSON\": \"[{\\\"gridData\\\":{\\\"w\\\":24,\\\"h\\\":15,\\\"x\\\":0,\\\"y\\\":0,\\\"i\\\":\\\"1\\\"},\\\"version\\\":\\\"7.0.0-alpha1\\\",\\\"panelIndex\\\":\\\"1\\\",\\\"type\\\":\\\"visualization\\\",\\\"id\\\":\\\"80b956f0-b2cd-11e8-ad8e-85441f0c2e5c\\\",\\\"embeddableConfig\\\":{}}]\",\n" +
95                 "        \"optionsJSON\": \"{\\\"darkTheme\\\":false,\\\"useMargins\\\":true,\\\"hidePanelTitles\\\":false}\",\n" +
96                 "        \"version\": 1,\n" +
97                 "        \"timeRestore\": false,\n" +
98                 "        \"kibanaSavedObjectMeta\": {\n" +
99                 "          \"searchSourceJSON\": \"{\\\"query\\\":{\\\"query\\\":\\\"\\\",\\\"language\\\":\\\"lucene\\\"},\\\"filter\\\":[]}\"\n" +
100                 "        }\n" +
101                 "      }\n" +
102                 "    }\n" +
103                 "  ]\n" +
104                 "}";
105         String testFlag = "KibanaDashboardImport";
106         try {
107             when(httpClient.execute(httpPost)).thenReturn(httpResponse);
108         } catch (IOException e) {
109             e.printStackTrace();
110         }
111         assertEquals(true, HttpClientUtil.sendPostHttpClient(testUrl, testJson, testFlag));
112
113     }
114 }