initial commit

This commit is contained in:
Greg Gauthier 2023-05-24 21:01:54 +01:00
commit 5c25e7c9f8
6 changed files with 422 additions and 0 deletions

41
.gitignore vendored Normal file
View File

@ -0,0 +1,41 @@
#https://github.com/spring-projects/spring-boot/blob/master/.gitignore
.idea/
*#
*.iml
*.ipr
*.iws
*.jar
*.sw?
*~
.#*
.*.md.html
.DS_Store
.classpath
.factorypath
.gradle
.idea
.metadata
.project
.recommenders
.settings
.springBeans
/build
/code
MANIFEST.MF
_site/
activemq-data
bin
build
build.log
dependency-reduced-pom.xml
dump.rdb
interpolated*.xml
lib/
manifest.yml
overridedb.*
target
transaction-logs
.flattened-pom.xml
secrets.yml
.gradletasknamecache
.sts4-cache

116
pom.xml Normal file
View File

@ -0,0 +1,116 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>calculator-project2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
</dependency>
<dependency>
<groupId>com.hackerrank.applications</groupId>
<artifactId>junit-ordered-test-runner</artifactId>
<version>1.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.1.RELEASE</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,15 @@
package com.gmgauthier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
* Spring Boot application starter class
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,94 @@
package com.gmgauthier.controllers;
import org.json.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpClientErrorException;
import java.util.*;
@RestController
public class CalculatorController {
@RequestMapping(value = "/sum",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public String sums(@RequestBody HashMap<String, List<Integer>> payload) {
List<Integer> values = validateValues(payload);
Integer sum = values.get(0) + values.get(1);
HashMap<String, Object> results = new HashMap<>();
results.put("sum", sum);
JSONObject jsonResults = new JSONObject(results);
return jsonResults.toString();
}
@RequestMapping(value = "/difference",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public String differences(@RequestBody HashMap<String, List<Integer>> payload) {
List<Integer> values = validateValues(payload);
Integer remainder = values.get(0) - values.get(1);
HashMap<String, Object> results = new HashMap<>();
results.put("difference", remainder);
JSONObject jsonResults = new JSONObject(results);
return jsonResults.toString();
}
@RequestMapping(value = "/product",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public String products(@RequestBody HashMap<String, List<Integer>> payload) {
List<Integer> values = validateValues(payload);
Integer product = values.get(0) * values.get(1);
HashMap<String, Object> results = new HashMap<>();
results.put("product", product);
JSONObject jsonResults = new JSONObject(results);
return jsonResults.toString();
}
@RequestMapping(value = "/quotient",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public String quotients(@RequestBody HashMap<String, List<Integer>> payload) {
List<Integer> values = validateValues(payload);
Float quotient = values.get(0).floatValue() / values.get(1).floatValue();
HashMap<String, Object> results = new HashMap<>();
results.put("quotient", quotient);
JSONObject jsonResults = new JSONObject(results);
return jsonResults.toString();
}
private List<Integer> validateValues(HashMap<String, List<Integer>> payload) {
List<Integer> values;
// Make sure the payload is extractable
try {
values = payload.get("values");
} catch (Exception e){
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST,
"operand values could not be extracted from payload");
}
if (values.size() != 2) {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST,
"incorrect number of operands");
}
return values;
}
}

View File

@ -0,0 +1,8 @@
#Spring Boot server configuration
server.address=0.0.0.0
server.port=8000
#H2 console web access configuration
#Open "http://0.0.0.0:8000/h2-console" and hit "Connect" button
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

View File

@ -0,0 +1,148 @@
package com.gmgauthier.requests;
import com.hackerrank.test.utility.OrderedTestRunner;
import com.hackerrank.test.utility.TestWatchman;
import org.junit.*;
import org.junit.rules.TestWatcher;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.web.util.NestedServletException;
import static org.junit.Assert.assertEquals;
@RunWith(OrderedTestRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class CalculatorControllerTest {
@ClassRule
public static final SpringClassRule springClassRule = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Rule
public TestWatcher watchman = TestWatchman.watchman;
@Autowired
private MockMvc mockMvc;
@BeforeClass
public static void setUpClass() {
TestWatchman.watchman.registerClass(CalculatorControllerTest.class);
}
@AfterClass
public static void tearDownClass() {
TestWatchman.watchman.createReport(CalculatorControllerTest.class);
}
@Test
public void testValidSums() throws Exception {
String response = mockMvc.perform(
MockMvcRequestBuilders.post("/sum")
.content("{\"values\":[2,2]}")
.contentType("application/json")
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn()
.getResponse()
.getContentAsString();
Assert.assertEquals("{\"sum\":4}", response);
}
@Test(expected = NestedServletException.class)
public void testTooManyOperands() throws Exception {
String response = mockMvc.perform(
MockMvcRequestBuilders.post("/sum")
.content("{\"values\":[2,2,3]}")
.contentType("application/json")
)
.andExpect(MockMvcResultMatchers.status().is(400))
.andReturn()
.getResponse()
.getContentAsString();
Assert.assertEquals("", response);
}
@Test
public void testNoContentBody() throws Exception {
String response = mockMvc.perform(
MockMvcRequestBuilders.post("/sum")
)
.andExpect(MockMvcResultMatchers.status().is(415)) //mocker does our status check
.andReturn()
.getResponse()
.getContentAsString();
assertEquals("", response);
}
@Test
public void testStringOperands() throws Exception {
String response = mockMvc.perform(
MockMvcRequestBuilders.post("/sum")
.content("{\"values\":[\"apple\",\"orange\",\"banana\"]}")
.contentType("application/json")
)
.andExpect(MockMvcResultMatchers.status().is(400))
.andReturn()
.getResponse()
.getContentAsString();
Assert.assertEquals("", response);
}
@Test
public void testValidDifferences() throws Exception {
String response = mockMvc.perform(
MockMvcRequestBuilders.post("/difference")
.content("{\"values\":[4,2]}")
.contentType("application/json")
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn()
.getResponse()
.getContentAsString();
Assert.assertEquals("{\"difference\":2}", response);
}
@Test
public void testValidProducts() throws Exception {
String response = mockMvc.perform(
MockMvcRequestBuilders.post("/product")
.content("{\"values\":[4,2]}")
.contentType("application/json")
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn()
.getResponse()
.getContentAsString();
Assert.assertEquals("{\"product\":8}", response);
}
@Test
public void testValidQuotients() throws Exception {
String response = mockMvc.perform(
MockMvcRequestBuilders.post("/quotient")
.content("{\"values\":[4,2]}")
.contentType("application/json")
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn()
.getResponse()
.getContentAsString();
Assert.assertEquals("{\"quotient\":2}", response);
}
}