Skip to content

Commit fe19807

Browse files
Converted in a Spring Boot product
1 parent 993c223 commit fe19807

13 files changed

Lines changed: 297 additions & 51 deletions

File tree

pom.xml

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,66 @@
66
<artifactId>calculator</artifactId>
77
<version>0.0.1-SNAPSHOT</version>
88
<properties>
9-
<dep.junit.version>5.5.1</dep.junit.version>
9+
<dep.junit.version>5.5.2</dep.junit.version>
1010
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1111
<maven.compiler.source>11</maven.compiler.source>
1212
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
13+
<bootstrap.version>4.3.1</bootstrap.version>
14+
<springboot.version>2.2.0.RELEASE</springboot.version>
1315
</properties>
16+
17+
<parent>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-parent</artifactId>
20+
<version>2.2.0.RELEASE</version>
21+
</parent>
22+
1423
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-test</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-devtools</artifactId>
39+
<scope>runtime</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.webjars</groupId>
43+
<artifactId>bootstrap</artifactId>
44+
<version>${bootstrap.version}</version>
45+
</dependency>
1546
<dependency>
1647
<groupId>org.junit.jupiter</groupId>
1748
<artifactId>junit-jupiter-api</artifactId>
18-
<version>${dep.junit.version}</version>
1949
<scope>test</scope>
2050
</dependency>
2151
<dependency>
2252
<groupId>org.junit.jupiter</groupId>
2353
<artifactId>junit-jupiter</artifactId>
24-
<version>${dep.junit.version}</version>
2554
<scope>test</scope>
2655
</dependency>
2756
<dependency>
2857
<groupId>org.mockito</groupId>
2958
<artifactId>mockito-junit-jupiter</artifactId>
30-
<version>3.1.0</version>
3159
<scope>test</scope>
3260
</dependency>
3361
<dependency>
3462
<groupId>org.assertj</groupId>
3563
<artifactId>assertj-core</artifactId>
36-
<version>3.13.2</version>
3764
<scope>test</scope>
3865
</dependency>
3966
<dependency>
4067
<groupId>org.apache.logging.log4j</groupId>
4168
<artifactId>log4j-core</artifactId>
42-
<version>2.12.1</version>
4369
</dependency>
4470
<dependency>
4571
<groupId>javax.inject</groupId>
@@ -50,8 +76,16 @@
5076
<build>
5177
<plugins>
5278
<plugin>
53-
<artifactId>maven-surefire-plugin</artifactId>
54-
<version>2.22.2</version>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<artifactId>maven-failsafe-plugin</artifactId>
81+
<executions>
82+
<execution>
83+
<goals>
84+
<goal>integration-test</goal>
85+
<goal>verify</goal>
86+
</goals>
87+
</execution>
88+
</executions>
5589
</plugin>
5690
</plugins>
5791
</build>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.openclassrooms.testing.calcul;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class CalculatorApp {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(CalculatorApp.class, args);
11+
}
12+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.openclassrooms.testing.calcul.controller;
2+
3+
import javax.inject.Inject;
4+
import javax.validation.Valid;
5+
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.Model;
8+
import org.springframework.validation.BindingResult;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.PostMapping;
11+
12+
import com.openclassrooms.testing.calcul.domain.model.Calculation;
13+
import com.openclassrooms.testing.calcul.domain.model.CalculationModel;
14+
import com.openclassrooms.testing.calcul.domain.model.CalculationType;
15+
import com.openclassrooms.testing.calcul.service.CalculatorService;
16+
17+
@Controller
18+
public class CalculatorController {
19+
20+
public static final String CALCULATOR_TEMPLATE = "calculator";
21+
22+
@Inject
23+
CalculatorService calculatorService;
24+
25+
@GetMapping("/")
26+
public String index(Calculation calculation) {
27+
return "redirect:/calculator";
28+
}
29+
30+
@GetMapping("/calculator")
31+
public String root(Calculation calculation) {
32+
return CALCULATOR_TEMPLATE; // cf. resources/templates/calculator.html
33+
}
34+
35+
@PostMapping("/calculator")
36+
public String calculate(@Valid Calculation calculation, BindingResult bindingResult, Model model) {
37+
38+
final CalculationType type = CalculationType.valueOf(calculation.getCalculationType());
39+
final CalculationModel calculationModel = new CalculationModel(
40+
type,
41+
calculation.getLeftArgument(),
42+
calculation.getRightArgument());
43+
44+
final CalculationModel response = calculatorService.calculate(calculationModel);
45+
46+
model.addAttribute("response", response);
47+
return CALCULATOR_TEMPLATE; // cf. resources/templates/calculator.html
48+
}
49+
}

src/main/java/com/openclassrooms/testing/calcul/domain/Calculator.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,64 @@
33
import java.util.HashSet;
44
import java.util.Set;
55

6+
import javax.inject.Named;
7+
8+
@Named
69
public class Calculator {
710

811
public int add(int a, int b) {
912
return a + b;
1013
}
11-
14+
1215
public int sub(int a, int b) {
1316
return a - b;
1417
}
1518

1619
public int multiply(int a, int b) {
1720
return a * b;
1821
}
19-
22+
2023
public int divide(int a, int b) {
2124
return a / b;
2225
}
23-
26+
2427
public double add(double a, double b) {
2528
return a + b;
2629
}
27-
30+
2831
public double sub(double a, double b) {
2932
return a - b;
3033
}
3134

3235
public double multiply(double a, double b) {
3336
return a * b;
3437
}
35-
38+
3639
public double divide(double a, double b) {
3740
return a / b;
3841
}
39-
42+
4043
public int fact(int a) {
4144
if (a < 0 || a > 12) {
4245
throw new IllegalArgumentException("Doit être compris entre 0 et 12.");
4346
}
4447
if (a <= 1) {
4548
return a;
4649
}
47-
return a*fact(a-1);
50+
return a * fact(a - 1);
4851
}
4952

5053
public void longCalculation() {
5154
try {
5255
Thread.sleep(500);
53-
} catch (InterruptedException e) {
56+
} catch (final InterruptedException e) {
5457
e.printStackTrace();
5558
}
5659
}
5760

5861
public Set<Integer> digitsSet(int number) {
59-
Set<Integer> integers = new HashSet<Integer>();
60-
String numberString = String.valueOf(number);
62+
final Set<Integer> integers = new HashSet<Integer>();
63+
final String numberString = String.valueOf(number);
6164

6265
for (int i = 0; i < numberString.length(); i++) {
6366
if (numberString.charAt(i) != '-') {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.openclassrooms.testing.calcul.domain.model;
2+
3+
public class Calculation {
4+
5+
private String calculationType;
6+
7+
private Integer leftArgument;
8+
9+
private Integer rightArgument;
10+
11+
public String getCalculationType() {
12+
return calculationType;
13+
}
14+
15+
public void setCalculationType(String calculationType){
16+
this.calculationType = calculationType;
17+
}
18+
19+
public Integer getLeftArgument() {
20+
return leftArgument;
21+
}
22+
23+
public void setLeftArgument(Integer leftArgument) {
24+
this.leftArgument = leftArgument;
25+
}
26+
27+
public Integer getRightArgument() {
28+
return rightArgument;
29+
}
30+
31+
public void setRightArgument(Integer rightArgument) {
32+
this.rightArgument = rightArgument;
33+
}
34+
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package com.openclassrooms.testing.calcul.service;
22

33
import java.io.IOException;
4-
import java.util.List;
54
import java.util.stream.Stream;
65

76
/**
87
* Enables interacting with a batch file for calculations
98
*/
109
public interface BatchCalculationFileService {
1110

12-
/**
13-
* Reads the calculations from a batch file and returns a stream
14-
* of written calculations.
15-
* @param file path of the batchfile
16-
* @return stream of calculations in the form "2 + 2"
17-
*/
18-
Stream<String> read(String file) throws IOException;
11+
/**
12+
* Reads the calculations from a batch file and returns a stream of written
13+
* calculations.
14+
*
15+
* @param file path of the batchfile
16+
* @return stream of calculations in the form "2 + 2"
17+
*/
18+
Stream<String> read(String file) throws IOException;
1919

2020
}

src/main/java/com/openclassrooms/testing/calcul/service/BatchCalculationFileServiceImpl.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,32 @@
33
import java.io.IOException;
44
import java.nio.file.Files;
55
import java.nio.file.Paths;
6-
import java.util.List;
76
import java.util.stream.Stream;
87

8+
import javax.inject.Named;
9+
910
/**
1011
* Reads a text file and returns a stream of calculations
1112
*/
13+
@Named
1214
public class BatchCalculationFileServiceImpl implements BatchCalculationFileService {
1315

14-
/**
15-
* Returns a stream of the rows in a file
16-
* with file. The file format should have a calculation
17-
* with a mathematical operator and an argument on each side,
18-
* separated by a spaces. Eg:
19-
*
20-
* <pre>
21-
* 2 + 2
22-
* 3 - 2
23-
* 2022 / 4
24-
* </pre>
25-
* @param file path of the batch file
26-
* @return Stream of calculations
27-
*/
28-
@Override
29-
public Stream<String> read(String file) throws IOException {
30-
return Files.lines(Paths.get(file));
31-
}
16+
/**
17+
* Returns a stream of the rows in a file with file. The file format should have
18+
* a calculation with a mathematical operator and an argument on each side,
19+
* separated by a spaces. Eg:
20+
*
21+
* <pre>
22+
* 2 + 2
23+
* 3 - 2
24+
* 2022 / 4
25+
* </pre>
26+
*
27+
* @param file path of the batch file
28+
* @return Stream of calculations
29+
*/
30+
@Override
31+
public Stream<String> read(String file) throws IOException {
32+
return Files.lines(Paths.get(file));
33+
}
3234
}

src/main/java/com/openclassrooms/testing/calcul/service/BatchCalculatorServiceImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
import java.util.stream.Collectors;
55
import java.util.stream.Stream;
66

7+
import javax.inject.Named;
8+
79
import com.openclassrooms.testing.calcul.domain.model.CalculationModel;
810

11+
@Named
912
public class BatchCalculatorServiceImpl implements BatchCalculatorService {
1013

1114
private final CalculatorService calculatorService;

src/main/java/com/openclassrooms/testing/calcul/service/CalculatorServiceImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.openclassrooms.testing.calcul.service;
22

3+
import javax.inject.Named;
4+
35
import com.openclassrooms.testing.calcul.domain.Calculator;
46
import com.openclassrooms.testing.calcul.domain.model.CalculationModel;
57
import com.openclassrooms.testing.calcul.domain.model.CalculationType;
68

9+
@Named
710
public class CalculatorServiceImpl implements CalculatorService {
811

912
private final Calculator calculator;

src/main/java/com/openclassrooms/testing/calcul/service/SolutionFormatterImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import java.util.Locale;
44

5+
import javax.inject.Named;
6+
7+
@Named
58
public class SolutionFormatterImpl implements SolutionFormatter {
69

710
@Override

0 commit comments

Comments
 (0)