1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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;
18 import java.io.InputStream;
19 import java.math.BigInteger;
20 import java.util.List;
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;
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;
41 public class HttpTest {
42 private static final Logger LOGGER = LoggerFactory
43 .getLogger(HttpTest.class);
51 public void testoptionsFrom() {
52 ImmutableCatalogOptions catalogs = ImmutableCatalogOptions.builder()
54 catalogs = catalogs.withDatacenter("datacenter").withTag("tag");
56 BigInteger index = new BigInteger("1");
57 ImmutableQueryOptions querys = QueryOptions.blockSeconds(10, index)
59 querys = querys.withConsistencyMode(ConsistencyMode.STALE)
60 .withDatacenter("datacenter").withNear("near")
62 String url = Http.optionsFrom(catalogs, querys);
67 public void testconsulResponse() {
69 TypeReference<Map<String, List<String>>> TYPE_SERVICES_MAP = new TypeReference<Map<String, List<String>>>() {};
71 ProtocolVersion version = new ProtocolVersion("HTTP",1,1);
72 StatusLine status= new BasicStatusLine(version,200,"ok");
73 BasicHttpResponse response = new BasicHttpResponse(status);
75 response.setHeader("X-Consul-Index", "1");
76 response.setHeader("X-Consul-Lastcontact", "1");
77 response.setHeader("X-Consul-Knownleader", "true");
79 BasicHttpEntity entity = new BasicHttpEntity();
80 InputStream content = HttpTest.class.getResourceAsStream("serviceslist.json");
81 entity.setContent(content);
82 response.setEntity(entity);
84 Http.consulResponse(TYPE_SERVICES_MAP, response);
86 TypeReference<String> TYPE_SERVICES_MAP_STRING = new TypeReference<String>() {};
87 InputStream content1 = HttpTest.class.getResourceAsStream("serviceslist.json");
88 entity.setContent(content1);
90 Http.consulResponse(TYPE_SERVICES_MAP_STRING, response);
92 TypeReference<HttpEntity> TYPE_SERVICES_MAP_ENTITY = new TypeReference<HttpEntity>() {};
93 Http.consulResponse(TYPE_SERVICES_MAP_ENTITY, response);