Compare commits
10 Commits
5c25e7c9f8
...
1d7e77b37f
Author | SHA1 | Date |
---|---|---|
Greg Gauthier | 1d7e77b37f | |
Greg Gauthier | a8feab9d4d | |
Greg Gauthier | 1b6b1a50db | |
Greg Gauthier | 137b1ecfc9 | |
Greg Gauthier | 65dfd81d83 | |
Greg Gauthier | c209f3d1be | |
Greg Gauthier | 633e7a059e | |
Greg Gauthier | ba7a44886b | |
Greg Gauthier | b019a3c9b3 | |
Greg Gauthier | 51f9390cec |
63
pom.xml
63
pom.xml
|
@ -23,6 +23,18 @@
|
|||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.bytebuddy</groupId>
|
||||
<artifactId>byte-buddy</artifactId>
|
||||
<version>1.14.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.bytebuddy</groupId>
|
||||
<artifactId>byte-buddy-agent</artifactId>
|
||||
<version>1.14.4</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
@ -67,6 +79,29 @@
|
|||
<scope>test</scope>
|
||||
</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>5.3.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.bytebuddy</groupId>
|
||||
<artifactId>byte-buddy</artifactId>
|
||||
<version>1.14.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.bytebuddy</groupId>
|
||||
<artifactId>byte-buddy-agent</artifactId>
|
||||
<version>1.14.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
@ -85,6 +120,7 @@
|
|||
<version>1.0.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -99,6 +135,19 @@
|
|||
</pluginManagement>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>**/CalculatorClientTests.java</include>
|
||||
<include>**/CalculatorControllerTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
|
@ -111,6 +160,20 @@
|
|||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.11.0</version>
|
||||
<configuration>
|
||||
<source>11</source>
|
||||
<target>11</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>3.3.1</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
package com.gmgauthier;
|
||||
|
||||
import com.gmgauthier.client.CalculatorClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public class CalculatorApp {
|
||||
|
||||
public static void main(String[] args)
|
||||
throws URISyntaxException, IOException, InterruptedException {
|
||||
CalculatorClient cc = new CalculatorClient();
|
||||
|
||||
System.out.println(cc.getSum(10,20));
|
||||
System.out.println(cc.getProduct(123, 444));
|
||||
System.out.println(cc.getDifference(846, 233));
|
||||
System.out.println(cc.getQuotient(8, 2));
|
||||
System.out.println(cc.getQuotient(19, 5));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
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 CalculatorClient {
|
||||
|
||||
private String DEFAULT_ROOT_URL = "http://localhost:8000";
|
||||
static HttpClient httpClient;
|
||||
|
||||
public CalculatorClient() {
|
||||
httpClient = HttpClient.newHttpClient();
|
||||
}
|
||||
|
||||
public CalculatorClient(String serverUrl) {
|
||||
this.DEFAULT_ROOT_URL = serverUrl;
|
||||
httpClient = HttpClient.newHttpClient();
|
||||
}
|
||||
|
||||
public Integer getSum(Integer operanda, Integer operandb)
|
||||
throws URISyntaxException, IOException, InterruptedException {
|
||||
URI addUrl = new URI(DEFAULT_ROOT_URL + "/sum");
|
||||
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
||||
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("sum");
|
||||
}
|
||||
|
||||
public Integer getDifference(Integer operanda, Integer operandb)
|
||||
throws URISyntaxException, IOException, InterruptedException {
|
||||
URI addUrl = new URI(DEFAULT_ROOT_URL + "/difference");
|
||||
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
||||
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("difference");
|
||||
}
|
||||
|
||||
public Integer getProduct(Integer operanda, Integer operandb)
|
||||
throws URISyntaxException, IOException, InterruptedException {
|
||||
URI addUrl = new URI(DEFAULT_ROOT_URL + "/product");
|
||||
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
||||
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("product");
|
||||
}
|
||||
|
||||
public Double getQuotient(Integer operanda, Integer operandb)
|
||||
throws URISyntaxException, IOException, InterruptedException {
|
||||
URI addUrl = new URI(DEFAULT_ROOT_URL + "/quotient");
|
||||
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
|
||||
JSONObject response = new JSONObject(makeRequest(addUrl, postJson).body());
|
||||
String val = response.get("quotient").toString();
|
||||
return Double.valueOf(val);
|
||||
}
|
||||
|
||||
private 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());
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.gmgauthier;
|
||||
package com.gmgauthier.server;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
@ -8,8 +8,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||
* Spring Boot application starter class
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
public class CalculatorServer {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
SpringApplication.run(CalculatorServer.class, args);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.gmgauthier.controllers;
|
||||
package com.gmgauthier.server.controllers;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.http.HttpStatus;
|
|
@ -6,3 +6,7 @@ server.port=8000
|
|||
#Open "http://0.0.0.0:8000/h2-console" and hit "Connect" button
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2-console
|
||||
|
||||
spring.main.banner-mode=off
|
||||
logging.level.org.springframework=OFF
|
||||
logging.level.root=OFF
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package com.gmgauthier;
|
||||
|
||||
import com.gmgauthier.client.requests.CalculatorClientTests;
|
||||
import com.gmgauthier.server.requests.CalculatorControllerTest;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@Suite.SuiteClasses({
|
||||
CalculatorClientTests.class,
|
||||
CalculatorControllerTest.class
|
||||
})
|
||||
@SpringBootTest
|
||||
public class Alltests {
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
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 org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@SpringBootTest
|
||||
@RunWith(OrderedTestRunner.class)
|
||||
public class CalculatorClientTests {
|
||||
|
||||
@Rule
|
||||
public WireMockRule wireMockRule = new WireMockRule(8000);
|
||||
//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
|
||||
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
|
||||
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
|
||||
assertEquals(4, resp.intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWholeNumberDivision() 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();
|
||||
Double resp = calc.getQuotient(8,2);//eight divided by two
|
||||
assertEquals(Double.valueOf(4), resp);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecimalDivision() 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\": 3.8}")));
|
||||
CalculatorClient calc = new CalculatorClient();
|
||||
Double resp = calc.getQuotient(19,5);
|
||||
assertEquals(Double.valueOf(3.8), resp);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.gmgauthier.requests;
|
||||
package com.gmgauthier.server.requests;
|
||||
|
||||
import com.hackerrank.test.utility.OrderedTestRunner;
|
||||
import com.hackerrank.test.utility.TestWatchman;
|
||||
|
@ -145,4 +145,19 @@ public class CalculatorControllerTest {
|
|||
|
||||
Assert.assertEquals("{\"quotient\":2}", response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecimalQuotients() throws Exception {
|
||||
String response = mockMvc.perform(
|
||||
MockMvcRequestBuilders.post("/quotient")
|
||||
.content("{\"values\":[19,5]}")
|
||||
.contentType("application/json")
|
||||
)
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
|
||||
Assert.assertEquals("{\"quotient\":3.8}", response);
|
||||
}
|
||||
}
|
|
@ -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