Skip to content

Commit

Permalink
#186: Refactoring - move next http calls to the new architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
vgv committed Sep 6, 2019
1 parent b664c4b commit ce1d547
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/ecwid/consul/v1/ConsulRawClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ public HttpResponse makeGetRequest(String endpoint, List<UrlParameters> urlParam
return httpTransport.makeGetRequest(request);
}

public HttpResponse makeGetRequest(Request request) {
String url = prepareUrl(agentAddress + request.getEndpoint());
url = Utils.generateUrl(url, request.getUrlParameters());

HttpRequest httpRequest = HttpRequest.Builder.newBuilder()
.setUrl(url)
.addHeaders(Utils.createTokenMap(request.getToken()))
.build();

return httpTransport.makeGetRequest(httpRequest);
}

public HttpResponse makePutRequest(String endpoint, String content, UrlParameters... urlParams) {
String url = prepareUrl(agentAddress + endpoint);
url = Utils.generateUrl(url, urlParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import com.ecwid.consul.json.GsonFactory;
import com.ecwid.consul.transport.HttpResponse;
import com.ecwid.consul.transport.TLSConfig;
import com.ecwid.consul.v1.ConsulRawClient;
import com.ecwid.consul.v1.OperationException;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.*;
import com.ecwid.consul.v1.catalog.model.*;
import com.google.gson.reflect.TypeToken;

Expand Down Expand Up @@ -110,12 +107,17 @@ public Response<List<Node>> getCatalogNodes(QueryParams queryParams) {

@Override
public Response<List<Node>> getCatalogNodes(CatalogNodesRequest catalogNodesRequest) {
HttpResponse httpResponse = rawClient.makeGetRequest("/v1/catalog/nodes", catalogNodesRequest.asUrlParameters());
Request request = Request.Builder.newBuilder()
.setEndpoint("/v1/catalog/nodes")
.addUrlParameters(catalogNodesRequest.asUrlParameters())
.build();

HttpResponse httpResponse = rawClient.makeGetRequest(request);

if (httpResponse.getStatusCode() == 200) {
List<Node> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Node>>() {
}.getType());
return new Response<List<Node>>(value, httpResponse);
return new Response<>(value, httpResponse);
} else {
throw new OperationException(httpResponse);
}
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/ecwid/consul/ConsulTestConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.ecwid.consul;

public class ConsulTestConstants {

public static final String CONSUL_VERSION = "1.6.0";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.ecwid.consul.v1.catalog;

import com.ecwid.consul.ConsulTestConstants;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.catalog.model.Node;
import com.ecwid.consul.v1.kv.KeyValueConsulClient;
import com.pszymczyk.consul.ConsulProcess;
import com.pszymczyk.consul.ConsulStarterBuilder;
import com.pszymczyk.consul.infrastructure.Ports;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Random;

import static org.junit.jupiter.api.Assertions.*;

class CatalogConsulClientTest {

private static final Random rnd = new Random();

private ConsulProcess consul;
private int port = Ports.nextAvailable();

private CatalogConsulClient consulClient = new CatalogConsulClient("localhost", port);

@BeforeEach
void setUp() {
consul = ConsulStarterBuilder.consulStarter()
.withConsulVersion(ConsulTestConstants.CONSUL_VERSION)
.withHttpPort(port)
.build()
.start();
}

@AfterEach
void tearDown() {
consul.close();
}

@Test
void testGetCatalogNodes() {
CatalogNodesRequest request = CatalogNodesRequest.newBuilder().build();
Response<List<Node>> response = consulClient.getCatalogNodes(request);

// We should find only one node – this
assertEquals(1, response.getValue().size());
}


}

0 comments on commit ce1d547

Please sign in to comment.