Selaa lähdekoodia

Set up Spring Boot and 'add' controller endpoint

Harry de Boer 5 vuotta sitten
vanhempi
commit
d1eec7e484

+ 15 - 5
README.md

@@ -4,16 +4,26 @@ This calculator project contains a simple Angular based frontend a Spring based
 
 ## Environment
 
-To compile and run this project the following software needs to be installed. Versions used for
-development are listed.
+To compile and run this project the following software needs to be installed and available on the
+path. Versions used for development are listed.
 
 - Maven (3.6.3)
 - Java JDK (OpenJDK 11.0.9.1)
  
-## Compiling and running the backend
+## Backend
 
-TODO
+To (somewhat) stay in line with RESTful principles, the backend api models a number as a resource 
+on which calculations can be applied. This makes it possible to avoid hardcoding arithmetic 
+operations in the frontend and use HATEOAS links instead. The latter is not implemented. 
+
+### Compiling and running
+
+Navigate into the `backend` directory and run `mvn spring-boot:run`
+
+### Example api call
+
+Navigate to `http://localhost:8080/numbers/5/add/15` in a browser and observe the response.
 
-## Compiling and running the frontend
+## Frontend
 
 TODO

+ 28 - 16
backend/pom.xml

@@ -8,35 +8,47 @@
     <artifactId>aegoncalc</artifactId>
     <version>1.0-SNAPSHOT</version>
 
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.4.0</version>
+        <relativePath/>
+    </parent>
+
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <maven.compiler.source>11</maven.compiler.source>
         <maven.compiler.target>11</maven.compiler.target>
     </properties>
 
-    <dependencyManagement>
-        <dependencies>
-            <dependency>
-                <groupId>org.junit</groupId>
-                <artifactId>junit-bom</artifactId>
-                <version>5.7.0</version>
-                <type>pom</type>
-                <scope>import</scope>
-            </dependency>
-        </dependencies>
-    </dependencyManagement>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
 
     <dependencies>
+
         <dependency>
-            <groupId>org.junit.jupiter</groupId>
-            <artifactId>junit-jupiter</artifactId>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
+
         <dependency>
-            <groupId>org.hamcrest</groupId>
-            <artifactId>hamcrest</artifactId>
-            <version>2.2</version>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter</artifactId>
+            <scope>test</scope>
         </dependency>
+
     </dependencies>
 
 </project>

+ 13 - 0
backend/src/main/java/nl/quintor/hdboer/aegoncalc/Application.java

@@ -0,0 +1,13 @@
+package nl.quintor.hdboer.aegoncalc;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class Application {
+
+    public static void main(String[] args) {
+        SpringApplication.run(Application.class, args);
+    }
+
+}

+ 25 - 0
backend/src/main/java/nl/quintor/hdboer/aegoncalc/controllers/SimpleCalculatorController.java

@@ -0,0 +1,25 @@
+package nl.quintor.hdboer.aegoncalc.controllers;
+
+import nl.quintor.hdboer.aegoncalc.services.SimpleCalculator;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * Controller that models a number as a resource on which calculator operations can be applied.
+ */
+@RestController
+public class SimpleCalculatorController {
+
+    private final SimpleCalculator calculator;
+
+    public SimpleCalculatorController(SimpleCalculator c) {
+        this.calculator = c;
+    }
+
+    @GetMapping("/numbers/{val}/add/{addend}")
+    Double add(@PathVariable int val, @PathVariable int addend) {
+        return calculator.add(val, addend);
+    }
+
+}

+ 3 - 0
backend/src/main/java/nl/quintor/hdboer/aegoncalc/services/SimpleCalculator.java

@@ -1,8 +1,11 @@
 package nl.quintor.hdboer.aegoncalc.services;
 
+import org.springframework.stereotype.Service;
+
 /**
  * Class SimpleCalculator implements the basic arithmetic functions needed by this project.
  */
+@Service
 public class SimpleCalculator {
 
     /**

+ 44 - 0
backend/src/test/java/nl/quintor/hdboer/aegoncalc/controllers/SimpleCalculatorControllerIT.java

@@ -0,0 +1,44 @@
+package nl.quintor.hdboer.aegoncalc.controllers;
+
+import nl.quintor.hdboer.aegoncalc.Application;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+
+@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+class SimpleCalculatorControllerIT {
+
+    @LocalServerPort
+    private int port;
+
+    TestRestTemplate restTemplate = new TestRestTemplate();
+
+    @Test
+    public void testAdd() {
+        ResponseEntity<Double> response = get("/numbers/1/add/15");
+
+        Assertions.assertAll(
+                () -> Assertions.assertEquals(HttpStatus.OK, response.getStatusCode()),
+                () -> Assertions.assertEquals(16.0, response.getBody())
+        );
+    }
+
+    @Test
+    public void testAddLargeNumber() {
+        ResponseEntity<Double> response = get("/numbers/2147483647/add/37");
+
+        Assertions.assertAll(
+                () -> Assertions.assertEquals(HttpStatus.OK, response.getStatusCode()),
+                () -> Assertions.assertEquals(2147483684.0, response.getBody())
+        );
+    }
+
+    private ResponseEntity<Double> get(String path) {
+        return restTemplate.getForEntity("http://localhost:" + port + path, Double.class);
+    }
+
+}