acf72cc22b33292a168814266708d0c4c7a92e6a
[msb/apigateway.git] /
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at 
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package org.onap.msb.apiroute.wrapper.consulextend.util;
17
18 import java.io.InputStream;
19 import java.math.BigInteger;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.http.HttpEntity;
24 import org.apache.http.ProtocolVersion;
25 import org.apache.http.StatusLine;
26 import org.apache.http.entity.BasicHttpEntity;
27 import org.apache.http.message.BasicHttpResponse;
28 import org.apache.http.message.BasicStatusLine;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.msb.apiroute.wrapper.consulextend.util.Http;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.fasterxml.jackson.core.type.TypeReference;
36 import com.orbitz.consul.option.ConsistencyMode;
37 import com.orbitz.consul.option.ImmutableCatalogOptions;
38 import com.orbitz.consul.option.ImmutableQueryOptions;
39 import com.orbitz.consul.option.QueryOptions;
40
41 public class HttpTest {
42         private static final Logger LOGGER = LoggerFactory
43                         .getLogger(HttpTest.class);
44
45         @Before
46         public void init() {
47
48         }
49
50         @Test
51         public void testoptionsFrom() {
52                 ImmutableCatalogOptions catalogs = ImmutableCatalogOptions.builder()
53                                 .build();
54                 catalogs = catalogs.withDatacenter("datacenter").withTag("tag");
55
56                 BigInteger index = new BigInteger("1");
57                 ImmutableQueryOptions querys = QueryOptions.blockSeconds(10, index)
58                                 .build();
59                 querys = querys.withConsistencyMode(ConsistencyMode.STALE)
60                                 .withDatacenter("datacenter").withNear("near")
61                                 .withToken("taoken");
62                 String url = Http.optionsFrom(catalogs, querys);
63                 LOGGER.info(url);
64         }
65
66         @Test
67         public void testconsulResponse() {
68                 
69                 TypeReference<Map<String, List<String>>> TYPE_SERVICES_MAP = new TypeReference<Map<String, List<String>>>() {};
70                 
71                 ProtocolVersion version = new ProtocolVersion("HTTP",1,1);
72                 StatusLine status= new BasicStatusLine(version,200,"ok");
73                 BasicHttpResponse response = new BasicHttpResponse(status);
74                 
75                 response.setHeader("X-Consul-Index", "1");
76                 response.setHeader("X-Consul-Lastcontact", "1");
77                 response.setHeader("X-Consul-Knownleader", "true");
78                 
79                 BasicHttpEntity entity = new BasicHttpEntity();
80                 InputStream content = HttpTest.class.getResourceAsStream("serviceslist.json");
81                 entity.setContent(content);
82                 response.setEntity(entity);
83                 
84                 Http.consulResponse(TYPE_SERVICES_MAP, response);
85                 
86                 TypeReference<String> TYPE_SERVICES_MAP_STRING = new TypeReference<String>() {};
87                 InputStream content1 = HttpTest.class.getResourceAsStream("serviceslist.json");
88                 entity.setContent(content1);
89                 
90                 Http.consulResponse(TYPE_SERVICES_MAP_STRING, response);
91
92                 TypeReference<HttpEntity> TYPE_SERVICES_MAP_ENTITY = new TypeReference<HttpEntity>() {};
93                 Http.consulResponse(TYPE_SERVICES_MAP_ENTITY, response);
94         }
95 }