Unit test for MSB JAVA SDK
[msb/java-sdk.git] / src / test / java / org / onap / msb / sdk / discovery / util / HttpClientUtilTest.java
1 /**
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14
15 package org.onap.msb.sdk.discovery.util;
16
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.onap.msb.sdk.discovery.common.RouteException;
20
21
22
23 public class HttpClientUtilTest {
24
25     private String testIp = "http://127.0.0.1:8500";
26     private String errorIp = "http://127.0.0.3:8500";
27
28     @Test
29     public void test_httpGet_fail() {
30         try {
31             Assert.assertNull(HttpClientUtil.httpGet(errorIp));
32         } catch (Exception e) {
33             Assert.assertTrue(e instanceof RouteException);
34         }
35     }
36
37     @Test
38     public void test_httpdelete() {
39         try {
40             HttpClientUtil.delete(testIp, "service");
41
42         } catch (Exception e) {
43             Assert.assertTrue(e instanceof RouteException);
44         }
45     }
46
47     @Test
48     public void test_httpPostWithJSON() {
49         String url = testIp + "/v1/catalog/service";
50         String json = "[{\"Node\":\"server\",\"Address\":\"127.0.0.1\",\"TaggedAddresses\":{\"lan\":\"127.0.0.1\",\"wan\":\"127.0.0.1\"},\"ServiceID\":\"_CJ-SNMP_10.74.216.65_12005\",\"ServiceName\":\"CJ-SNMP\",\"ServiceAddress\":\"10.74.216.65\",\"ServicePort\":12005,\"ServiceEnableTagOverride\":false,\"CreateIndex\":1813280,\"ModifyIndex\":1815062}]";
51
52         try {
53             HttpClientUtil.httpPostWithJSON(url, json);
54             Assert.assertEquals("Consul Agent", HttpClientUtil.httpGet(testIp));
55         } catch (Exception e) {
56             Assert.assertTrue(e instanceof RouteException);
57         }
58     }
59
60     @Test
61     public void test_httpPutWithJSON() {
62         String url = testIp + "/v1/catalog/service";
63         String json = "[{\"Node\":\"server\",\"Address\":\"127.0.0.1\",\"TaggedAddresses\":{\"lan\":\"127.0.0.1\",\"wan\":\"127.0.0.1\"},\"ServiceID\":\"_CJ-SNMP_10.74.216.65_12005\",\"ServiceName\":\"CJ-SNMP\",\"ServiceAddress\":\"10.74.216.65\",\"ServicePort\":12005,\"ServiceEnableTagOverride\":false,\"CreateIndex\":1813280,\"ModifyIndex\":1815062}]";
64
65         try {
66             HttpClientUtil.httpPutWithJSON(url, json);
67             Assert.assertEquals("Consul Agent", HttpClientUtil.httpGet(testIp));
68         } catch (Exception e) {
69             Assert.assertTrue(e instanceof RouteException);
70         }
71     }
72 }