got client tests working, which immediately broke the controller tests
This commit is contained in:
parent
ba7a44886b
commit
633e7a059e
48
pom.xml
48
pom.xml
@ -23,6 +23,17 @@
|
|||||||
<type>pom</type>
|
<type>pom</type>
|
||||||
<scope>import</scope>
|
<scope>import</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.bytebuddy</groupId>
|
||||||
|
<artifactId>byte-buddy</artifactId>
|
||||||
|
<version>1.12.9</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.bytebuddy</groupId>
|
||||||
|
<artifactId>byte-buddy-agent</artifactId>
|
||||||
|
<version>1.12.9</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
@ -67,6 +78,30 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.tomakehurst</groupId>
|
||||||
|
<artifactId>wiremock</artifactId>
|
||||||
|
<version>2.27.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<version>2.23.4</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.bytebuddy</groupId>
|
||||||
|
<artifactId>byte-buddy</artifactId>
|
||||||
|
<version>1.12.9</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.bytebuddy</groupId>
|
||||||
|
<artifactId>byte-buddy-agent</artifactId>
|
||||||
|
<version>1.12.9</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
@ -85,6 +120,14 @@
|
|||||||
<version>1.0.1</version>
|
<version>1.0.1</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>5.9.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -120,6 +163,11 @@
|
|||||||
<target>11</target>
|
<target>11</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-resources-plugin</artifactId>
|
||||||
|
<version>3.3.1</version>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</project>
|
</project>
|
||||||
|
67
src/main/java/com/gmgauthier/client/CalculatorApp.java
Normal file
67
src/main/java/com/gmgauthier/client/CalculatorApp.java
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
package com.gmgauthier.client;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
|
||||||
|
public class CalculatorApp {
|
||||||
|
|
||||||
|
private static final String ROOT_URL = "http://localhost:8000/";
|
||||||
|
static HttpClient httpClient;
|
||||||
|
|
||||||
|
public static void main(String[] args)
|
||||||
|
throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
httpClient = HttpClient.newHttpClient();
|
||||||
|
|
||||||
|
System.out.println(getSum(10,20));
|
||||||
|
System.out.println(getProduct(123, 444));
|
||||||
|
System.out.println(getDifference(846, 233));
|
||||||
|
System.out.println(getQuotient(999, 4));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer getSum(Integer operanda, Integer operandb)
|
||||||
|
throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
URI addUrl = new URI(ROOT_URL + "sum");
|
||||||
|
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
||||||
|
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("sum");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer getDifference(Integer operanda, Integer operandb)
|
||||||
|
throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
URI addUrl = new URI(ROOT_URL + "difference");
|
||||||
|
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
||||||
|
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("difference");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer getProduct(Integer operanda, Integer operandb)
|
||||||
|
throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
URI addUrl = new URI(ROOT_URL + "product");
|
||||||
|
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
||||||
|
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("product");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BigDecimal getQuotient(Integer operanda, Integer operandb)
|
||||||
|
throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
URI addUrl = new URI(ROOT_URL + "quotient");
|
||||||
|
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
||||||
|
return (BigDecimal) new JSONObject(makeRequest(addUrl, postJson).body()).get("quotient");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HttpResponse<String> makeRequest(URI uri, JSONObject jsonBody)
|
||||||
|
throws IOException, InterruptedException {
|
||||||
|
HttpRequest httpRequest = HttpRequest.newBuilder()
|
||||||
|
.header("Content-Type","application/json")
|
||||||
|
.uri(uri)
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofString(String.valueOf(jsonBody)))
|
||||||
|
.build();
|
||||||
|
return httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -12,49 +12,55 @@ import java.net.http.HttpResponse;
|
|||||||
|
|
||||||
public class CalculatorClient {
|
public class CalculatorClient {
|
||||||
|
|
||||||
private static final String ROOT_URL = "http://localhost:8000/";
|
private String DEFAULT_ROOT_URL = "http://localhost:8000";
|
||||||
static HttpClient httpClient;
|
static HttpClient httpClient;
|
||||||
|
|
||||||
public static void main(String[] args)
|
public CalculatorClient() {
|
||||||
throws URISyntaxException, IOException, InterruptedException {
|
|
||||||
httpClient = HttpClient.newHttpClient();
|
httpClient = HttpClient.newHttpClient();
|
||||||
|
|
||||||
System.out.println(getSum(10,20));
|
|
||||||
System.out.println(getProduct(123, 444));
|
|
||||||
System.out.println(getDifference(846, 233));
|
|
||||||
System.out.println(getQuotient(999, 4));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Integer getSum(Integer operanda, Integer operandb)
|
public CalculatorClient(String serverUrl) {
|
||||||
|
this.DEFAULT_ROOT_URL = serverUrl;
|
||||||
|
httpClient = HttpClient.newHttpClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getSum(Integer operanda, Integer operandb)
|
||||||
throws URISyntaxException, IOException, InterruptedException {
|
throws URISyntaxException, IOException, InterruptedException {
|
||||||
URI addUrl = new URI(ROOT_URL + "sum");
|
URI addUrl = new URI(DEFAULT_ROOT_URL + "/sum");
|
||||||
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
||||||
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("sum");
|
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("sum");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Integer getDifference(Integer operanda, Integer operandb)
|
public Integer getDifference(Integer operanda, Integer operandb)
|
||||||
throws URISyntaxException, IOException, InterruptedException {
|
throws URISyntaxException, IOException, InterruptedException {
|
||||||
URI addUrl = new URI(ROOT_URL + "difference");
|
URI addUrl = new URI(DEFAULT_ROOT_URL + "/difference");
|
||||||
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
||||||
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("difference");
|
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("difference");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Integer getProduct(Integer operanda, Integer operandb)
|
public Integer getProduct(Integer operanda, Integer operandb)
|
||||||
throws URISyntaxException, IOException, InterruptedException {
|
throws URISyntaxException, IOException, InterruptedException {
|
||||||
URI addUrl = new URI(ROOT_URL + "product");
|
URI addUrl = new URI(DEFAULT_ROOT_URL + "/product");
|
||||||
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
||||||
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("product");
|
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("product");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BigDecimal getQuotient(Integer operanda, Integer operandb)
|
public BigDecimal getQuotient(Integer operanda, Integer operandb)
|
||||||
throws URISyntaxException, IOException, InterruptedException {
|
throws URISyntaxException, IOException, InterruptedException {
|
||||||
URI addUrl = new URI(ROOT_URL + "quotient");
|
URI addUrl = new URI(DEFAULT_ROOT_URL + "/quotient");
|
||||||
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
||||||
return (BigDecimal) new JSONObject(makeRequest(addUrl, postJson).body()).get("quotient");
|
JSONObject response = new JSONObject(makeRequest(addUrl, postJson).body());
|
||||||
|
String val = response.get("quotient").toString();
|
||||||
|
BigDecimal quotient;
|
||||||
|
try {
|
||||||
|
quotient = BigDecimal.valueOf(Long.parseLong(val));
|
||||||
|
} catch (Exception e){
|
||||||
|
quotient = BigDecimal.valueOf(Integer.parseInt(val));
|
||||||
|
}
|
||||||
|
return quotient;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static HttpResponse<String> makeRequest(URI uri, JSONObject jsonBody)
|
private HttpResponse<String> makeRequest(URI uri, JSONObject jsonBody)
|
||||||
throws IOException, InterruptedException {
|
throws IOException, InterruptedException {
|
||||||
HttpRequest httpRequest = HttpRequest.newBuilder()
|
HttpRequest httpRequest = HttpRequest.newBuilder()
|
||||||
.header("Content-Type","application/json")
|
.header("Content-Type","application/json")
|
||||||
@ -63,5 +69,4 @@ public class CalculatorClient {
|
|||||||
.build();
|
.build();
|
||||||
return httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
return httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,3 +6,7 @@ server.port=8000
|
|||||||
#Open "http://0.0.0.0:8000/h2-console" and hit "Connect" button
|
#Open "http://0.0.0.0:8000/h2-console" and hit "Connect" button
|
||||||
spring.h2.console.enabled=true
|
spring.h2.console.enabled=true
|
||||||
spring.h2.console.path=/h2-console
|
spring.h2.console.path=/h2-console
|
||||||
|
|
||||||
|
spring.main.banner-mode=off
|
||||||
|
logging.level.org.springframework=OFF
|
||||||
|
logging.level.root=OFF
|
@ -1,18 +1,88 @@
|
|||||||
package com.gmgauthier.client.requests;
|
package com.gmgauthier.client.requests;
|
||||||
|
|
||||||
|
import com.github.tomakehurst.wiremock.client.WireMock;
|
||||||
|
import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
||||||
|
import com.gmgauthier.client.CalculatorClient;
|
||||||
import com.hackerrank.test.utility.OrderedTestRunner;
|
import com.hackerrank.test.utility.OrderedTestRunner;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
|
import static com.github.tomakehurst.wiremock.client.WireMock.*;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@RunWith(OrderedTestRunner.class)
|
@RunWith(OrderedTestRunner.class)
|
||||||
public class CalculatorClientTests {
|
public class CalculatorClientTests {
|
||||||
|
|
||||||
@Test
|
@Rule
|
||||||
public void testDummy() throws Exception {
|
public WireMockRule wireMockRule = new WireMockRule(8000);
|
||||||
assert true;
|
//public WireMockServer wms = new WireMockServer(9999);
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void reset() {
|
||||||
|
WireMock.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddition() throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
stubFor(
|
||||||
|
post(urlEqualTo("/sum"))
|
||||||
|
.withHeader("Content-Type", equalTo("application/json"))
|
||||||
|
.willReturn(aResponse()
|
||||||
|
.withStatus(200)
|
||||||
|
.withHeader("Content-Type", "application/json")
|
||||||
|
.withBody("{\"sum\": 4}")));
|
||||||
|
CalculatorClient calc = new CalculatorClient();
|
||||||
|
Integer resp = calc.getSum(2,2);//two plus two
|
||||||
|
Assertions.assertEquals(4, resp.intValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSubtraction() throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
stubFor(
|
||||||
|
post(urlEqualTo("/difference"))
|
||||||
|
.withHeader("Content-Type", equalTo("application/json"))
|
||||||
|
.willReturn(aResponse()
|
||||||
|
.withStatus(200)
|
||||||
|
.withHeader("Content-Type", "application/json")
|
||||||
|
.withBody("{\"difference\": 4}")));
|
||||||
|
CalculatorClient calc = new CalculatorClient();
|
||||||
|
Integer resp = calc.getDifference(6,2); //six minus two
|
||||||
|
Assertions.assertEquals(4, resp.intValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMultiplication() throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
stubFor(
|
||||||
|
post(urlEqualTo("/product"))
|
||||||
|
.withHeader("Content-Type", equalTo("application/json"))
|
||||||
|
.willReturn(aResponse()
|
||||||
|
.withStatus(200)
|
||||||
|
.withHeader("Content-Type", "application/json")
|
||||||
|
.withBody("{\"product\": 4}")));
|
||||||
|
CalculatorClient calc = new CalculatorClient();
|
||||||
|
Integer resp = calc.getProduct(2,2);//two times two
|
||||||
|
Assertions.assertEquals(4, resp.intValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDivision() throws URISyntaxException, IOException, InterruptedException {
|
||||||
|
stubFor(
|
||||||
|
post(urlEqualTo("/quotient"))
|
||||||
|
.withHeader("Content-Type", equalTo("application/json"))
|
||||||
|
.willReturn(aResponse()
|
||||||
|
.withStatus(200)
|
||||||
|
.withHeader("Content-Type", "application/json")
|
||||||
|
.withBody("{\"quotient\": 4}")));
|
||||||
|
CalculatorClient calc = new CalculatorClient();
|
||||||
|
BigDecimal resp = calc.getQuotient(8,2);//eight divided by two
|
||||||
|
Assertions.assertEquals(BigDecimal.valueOf(4), resp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
6
src/test/resources/logback-test.xml
Normal file
6
src/test/resources/logback-test.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<include resource="org/springframework/boot/logging/logback/base.xml" />
|
||||||
|
<logger name="org.springframework" level="ERROR"/>
|
||||||
|
<logger name="org.eclipse.jetty" level="ERROR" />
|
||||||
|
</configuration>
|
Loading…
Reference in New Issue
Block a user