1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
13 ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.consulextend.util;
16 import java.io.InputStream;
17 import java.math.BigInteger;
18 import java.util.List;
21 import org.apache.http.HttpEntity;
22 import org.apache.http.ProtocolVersion;
23 import org.apache.http.StatusLine;
24 import org.apache.http.entity.BasicHttpEntity;
25 import org.apache.http.message.BasicHttpResponse;
26 import org.apache.http.message.BasicStatusLine;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.fasterxml.jackson.core.type.TypeReference;
33 import com.orbitz.consul.option.ConsistencyMode;
34 import com.orbitz.consul.option.ImmutableCatalogOptions;
35 import com.orbitz.consul.option.ImmutableQueryOptions;
36 import com.orbitz.consul.option.QueryOptions;
38 public class HttpTest {
39 private static final Logger LOGGER = LoggerFactory.getLogger(HttpTest.class);
47 public void testoptionsFrom() {
48 ImmutableCatalogOptions catalogs = ImmutableCatalogOptions.builder().build();
49 catalogs = catalogs.withDatacenter("datacenter").withTag("tag");
51 BigInteger index = new BigInteger("1");
52 ImmutableQueryOptions querys = QueryOptions.blockSeconds(10, index).build();
53 querys = querys.withConsistencyMode(ConsistencyMode.STALE).withDatacenter("datacenter").withNear("near")
55 String url = Http.optionsFrom(catalogs, querys);
60 public void testconsulResponse() {
62 TypeReference<Map<String, List<String>>> TYPE_SERVICES_MAP = new TypeReference<Map<String, List<String>>>() {};
64 ProtocolVersion version = new ProtocolVersion("HTTP", 1, 1);
65 StatusLine status = new BasicStatusLine(version, 200, "ok");
66 BasicHttpResponse response = new BasicHttpResponse(status);
68 response.setHeader("X-Consul-Index", "1");
69 response.setHeader("X-Consul-Lastcontact", "1");
70 response.setHeader("X-Consul-Knownleader", "true");
72 BasicHttpEntity entity = new BasicHttpEntity();
73 InputStream content = HttpTest.class.getResourceAsStream("serviceslist.json");
74 entity.setContent(content);
75 response.setEntity(entity);
77 Http.consulResponse(TYPE_SERVICES_MAP, response);
79 TypeReference<String> TYPE_SERVICES_MAP_STRING = new TypeReference<String>() {};
80 InputStream content1 = HttpTest.class.getResourceAsStream("serviceslist.json");
81 entity.setContent(content1);
83 Http.consulResponse(TYPE_SERVICES_MAP_STRING, response);
85 TypeReference<HttpEntity> TYPE_SERVICES_MAP_ENTITY = new TypeReference<HttpEntity>() {};
86 Http.consulResponse(TYPE_SERVICES_MAP_ENTITY, response);