no longer writable:
+ * DayModel() is wrapper class for String answer
*/
- @GetMapping
- public Resource getResultForASpecificDayAndPuzzlePart(@RequestParam(value = "day", defaultValue = "1") Integer day,
- @RequestParam(value = "part", defaultValue = "1") Integer part){
+ private static class DayModel {
+ private final Integer day;
+ private final Integer part;
+ private final String answer;
+
+ public DayModel(Integer day, Integer part, String answer) {
+ this.day = day;
+ this.part = part;
+ this.answer = answer;
+ }
+
+ public Integer getDay() {
+ return day;
+ }
- logger.info("The results for day " + day + ", part " + part + " have been requested.");
+ public Integer getPart() {
+ return part;
+ }
- return new Resource<>(
- adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(day, part),
- linkTo(methodOn(AdventOfCodeController.class).getResultForASpecificDayAndPuzzlePart(day, part)).withSelfRel()
- );
+ public String getAnswer() {
+ return answer;
+ }
}
/**
- * Returns a HATEOAS {@code Resources<>} with an integer list of all days that have been implemented
+ * Handles a GET-Request with the day of the advent calendar and the part to be solved and returns a HATEOAS
+ * {@code EntityModel<>} with the corresponding solution.
+ *
+ * // * @param day the simple day of the advent calendar to be solved
+ * // * @param part the part of the puzzle for that day
*
- * @return a HATEOAS-{@code Resources<>} with an integer list of all days that have been implemented
+ * @return a HATEOAS-{@code EntityModel<>} with the corresponding solution
*/
- @GetMapping("/daysimplemented")
- public Resources daysImplemented() {
+ @GetMapping(value = "/")
+ public EntityModel getResultForASpecificDayAndPuzzlePart(@RequestParam(value = "day", defaultValue = "1") Integer day,
+ @RequestParam(value = "part", defaultValue = "1") Integer part) {
- logger.info("A list of implemented days has been requested.");
+ logger.info("The results for day {}, part {} have been requested.", day, part);
- return new Resources<>(
- adventOfCodeService.getDaysSolutions().stream()
- .map(Days::getDay)
- .sorted()
- .collect(Collectors.toList()),
- linkTo(methodOn(AdventOfCodeController.class).daysImplemented()).withSelfRel()
- );
+ String answer = adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(day, part);
+ Link link = linkTo(methodOn(AdventOfCodeController.class).getResultForASpecificDayAndPuzzlePart(day, part)).withSelfRel();
+ DayModel dayModel = new DayModel(day, part, answer);
+ return EntityModel.of(dayModel, link);
}
- @GetMapping("/test")
- public String test () {
- logger.info("A list of implemented days has been requested.");
- return "Hallo";
- }
+ /**
+ * Returns a HATEOAS {@code CollectionModel<>} with an integer list of all days that have been implemented
+ *
+ * @return a HATEOAS-{@code CollectionModel<>} with an integer list of all days that have been implemented
+ */
+ @GetMapping("/daysimplemented")
+ public CollectionModel daysImplemented() {
- @GetMapping("/test11")
- public String test11 () {
+ logger.info("A list of implemented days (sorted) has been requested.");
- logger.info("A list of implemented days has been requested.");
+ List daysImplemented = adventOfCodeService.getDaysSolutions().stream()
+ .map(Days::getDay)
+ .collect(Collectors.toList());
+ Link link = linkTo(methodOn(AdventOfCodeController.class).daysImplemented()).withSelfRel();
- return "Hallo11";
+ return CollectionModel.of(daysImplemented, link);
}
-
}
\ No newline at end of file
diff --git a/src/main/java/org/haffson/adventofcode/days/day01/Day01.java b/src/main/java/org/haffson/adventofcode/days/day01/Day01.java
index b35e0b2d..cb39b421 100644
--- a/src/main/java/org/haffson/adventofcode/days/day01/Day01.java
+++ b/src/main/java/org/haffson/adventofcode/days/day01/Day01.java
@@ -4,6 +4,7 @@
import org.haffson.adventofcode.days.Days;
import org.haffson.adventofcode.utils.DataLoader;
import org.haffson.adventofcode.utils.ProblemStatus;
+import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
@@ -56,7 +57,7 @@ public String secondPart() {
/**
* utility method: subtract numbers from 2020
*/
- private List getSubtractedFrom2020(List numbers) {
+ private List getSubtractedFrom2020(@NonNull List numbers) {
return numbers.stream()
.map(value -> 2020 - value)
.collect(Collectors.toList());
@@ -68,11 +69,13 @@ private List getSubtractedFrom2020(List numbers) {
*
* @return the product
*/
- private int calculateProduct_Part1(final List numbers) {
+ private int calculateProduct_Part1(@NonNull final List numbers) {
+ List listOfNumbers_part1 = new ArrayList<>(numbers);
+
// check for intersection of two lists
- numbers.retainAll(getSubtractedFrom2020(numbers));
+ listOfNumbers_part1.retainAll(getSubtractedFrom2020(numbers));
// product of "intersected" values is the puzzle's answer!
- return numbers.get(0) * numbers.get(1);
+ return listOfNumbers_part1.get(0) * listOfNumbers_part1.get(1);
}
/**
@@ -81,13 +84,15 @@ private int calculateProduct_Part1(final List numbers) {
*
* @return the product
*/
- private int calculateProduct_Part2(final List numbers) {
- List numbersSubtractedBy2020 = getSubtractedFrom2020(numbers);
+ private int calculateProduct_Part2(@NonNull final List numbers) {
+ List listOfNumbers_part2 = new ArrayList<>(numbers);
+
+ List numbersSubtractedBy2020 = getSubtractedFrom2020(listOfNumbers_part2);
List tempData = new ArrayList<>();
- for (int k = 0; k < numbers.size(); k++) {
- for (Integer datum : numbers) {
- tempData.add(numbers.get(k) + datum);
+ for (int k = 0; k < listOfNumbers_part2.size(); k++) {
+ for (Integer datum : listOfNumbers_part2) {
+ tempData.add(listOfNumbers_part2.get(k) + datum);
}
}
// check for intersection of two lists
@@ -95,4 +100,4 @@ private int calculateProduct_Part2(final List numbers) {
// product of "intersected" values is the puzzle's answer!
return (2020 - numbersSubtractedBy2020.get(0)) * (2020 - numbersSubtractedBy2020.get(1)) * (2020 - numbersSubtractedBy2020.get(2));
}
-}
+}
\ No newline at end of file
diff --git a/src/main/java/org/haffson/adventofcode/days/day04/Day04.java b/src/main/java/org/haffson/adventofcode/days/day04/Day04.java
index 7f150e1b..8b2f7a55 100644
--- a/src/main/java/org/haffson/adventofcode/days/day04/Day04.java
+++ b/src/main/java/org/haffson/adventofcode/days/day04/Day04.java
@@ -56,7 +56,7 @@ public String secondPart() {
public int getNumberValidPassports(final List batchFile) {
int numberOfValidPassports = 0;
for (String passport : batchFile) {
- Set validKeys = new HashSet<>(Arrays.asList("byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"));
+ Set validKeys = new HashSet<>(List.of("byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"));
String[] splitPassport = passport.split("[\\s+]");
for (String field : splitPassport) {
String[] parts = field.split(":");
@@ -76,7 +76,7 @@ public int getRestrictedNumberValidPassports(final List batchFile) {
for (String passport : batchFile) {
int count = 0;
Map passports = new HashMap<>();
- Set validKeys = new HashSet<>(Arrays.asList("byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"));
+ Set validKeys = new HashSet<>(List.of("byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"));
String[] splitPassport = passport.split("[\\s+]");
for (String field : splitPassport) {
String[] parts = field.split(":");
@@ -125,7 +125,7 @@ public int getRestrictedNumberValidPassports(final List batchFile) {
}
// check eye color
String eyeColor = passports.get("ecl");
- List colors = Arrays.asList("amb", "blu", "brn", "gry", "grn", "hzl", "oth");
+ List colors = List.of("amb", "blu", "brn", "gry", "grn", "hzl", "oth");
if (colors.contains(eyeColor)) {
count++;
}
diff --git a/src/main/java/org/haffson/adventofcode/days/day05/Day05.java b/src/main/java/org/haffson/adventofcode/days/day05/Day05.java
index c991dd26..f6def1e2 100644
--- a/src/main/java/org/haffson/adventofcode/days/day05/Day05.java
+++ b/src/main/java/org/haffson/adventofcode/days/day05/Day05.java
@@ -58,7 +58,6 @@ private int getRowOrCol(@NonNull String seatName, int maxNumberRowsOrCols, char
rowOrCol = numMax;
}
}
- System.out.println(rowOrCol);
return rowOrCol;
}
diff --git a/src/main/java/org/haffson/adventofcode/service/AdventOfCodeService.java b/src/main/java/org/haffson/adventofcode/service/AdventOfCodeService.java
index 926dcf5e..46721a31 100644
--- a/src/main/java/org/haffson/adventofcode/service/AdventOfCodeService.java
+++ b/src/main/java/org/haffson/adventofcode/service/AdventOfCodeService.java
@@ -4,10 +4,10 @@
import org.haffson.adventofcode.controller.AdventOfCodeController;
import org.haffson.adventofcode.days.Days;
import org.haffson.adventofcode.exceptions.PuzzleNotSolvedYetException;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
+import java.util.Comparator;
import java.util.List;
import java.util.Objects;
@@ -21,16 +21,17 @@
@Component
public class AdventOfCodeService {
- /** A list containing all {@link Days} from the corresponding subpackages. */
+ /**
+ * A list containing all {@link Days} from the corresponding subpackages.
+ */
private final List daysSolutions;
-
/**
* {@code @Autowired} constructor adding all implemented {@link Days} to the list of Days.
*
* @param daysSolutions {@code @Autowired} days solutions
*/
- @Autowired
+
public AdventOfCodeService(List daysSolutions) {
this.daysSolutions = Objects.requireNonNull(daysSolutions);
}
@@ -39,7 +40,7 @@ public AdventOfCodeService(List daysSolutions) {
* Asks for the implementation for the requested day and then checks whether the requested part
* has been solved yet. Then it requests the retrieved implementation to calculate the solution.
*
- * @param day the simple day of the advent calendar to be solved
+ * @param day the simple day of the advent calendar to be solved
* @param part the part of the puzzle for that day
* @return a {@code String} with the result for the puzzle, or in case it has not been implemented,
* an {@link PuzzleNotSolvedYetException} is thrown.
@@ -53,9 +54,9 @@ public String getResultsForASpecificDayAndPuzzlePart(@NonNull Integer day, @NonN
Days thisDaysClass = findDayForDay(day);
if (!isProblemSolvedForPart(thisDaysClass, part)) {
throw new PuzzleNotSolvedYetException(new Throwable());
- } else if (Objects.equals(part,1)) {
+ } else if (Objects.equals(part, 1)) {
return thisDaysClass.firstPart();
- } else if (Objects.equals(part,2)) {
+ } else if (Objects.equals(part, 2)) {
return thisDaysClass.secondPart();
} else {
return "This puzzle has not been solved yet.";
@@ -66,7 +67,7 @@ public String getResultsForASpecificDayAndPuzzlePart(@NonNull Integer day, @NonN
* Checks whether the corresponding part to a day has already been solved.
*
* @param thisDaysClass the implementation for the requested day
- * @param part the part to check for it's solution status
+ * @param part the part to check for it's solution status
* @return if the part has been solved for a specific day
*/
private boolean isProblemSolvedForPart(Days thisDaysClass, Integer part) {
@@ -90,9 +91,23 @@ private Days findDayForDay(int day) {
/**
* Getter for {@code daysSolutions}
*
- * @return a List of all implemented days
+ * @return a sorted (by day!) List of all implemented days
*/
public List getDaysSolutions() {
+ Comparator dayComparator = new DayComparator();
+ daysSolutions.sort(dayComparator);
return daysSolutions;
}
-}
+
+ /**
+ * creation of a Comparator to use the getDay() method of Days to sort the day instances:
+ */
+ @Component
+ public static class DayComparator implements Comparator {
+
+ @Override
+ public int compare(Days day1, Days day2) {
+ return Integer.compare(day1.getDay(), day2.getDay());
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/org/haffson/adventofcode/utils/DataLoader.java b/src/main/java/org/haffson/adventofcode/utils/DataLoader.java
index e01a2063..d4fa25dd 100644
--- a/src/main/java/org/haffson/adventofcode/utils/DataLoader.java
+++ b/src/main/java/org/haffson/adventofcode/utils/DataLoader.java
@@ -10,8 +10,8 @@
@Component
public class DataLoader {
- public DataLoader() {
- }
+// public DataLoader() {
+// }
private List getRawDataAsList(@NonNull String path, @NonNull String delimiter) {
CheckStringIsEmpty.requireNonNullAndNonEmpty(path);
diff --git a/src/main/java/org/haffson/adventofcode/utils/ProblemStatus.java b/src/main/java/org/haffson/adventofcode/utils/ProblemStatus.java
index cf60eb7a..026fe084 100644
--- a/src/main/java/org/haffson/adventofcode/utils/ProblemStatus.java
+++ b/src/main/java/org/haffson/adventofcode/utils/ProblemStatus.java
@@ -18,8 +18,9 @@ public static Map getProblemStatusMap(
) {
final Map problemStatus = new HashMap<>();
- problemStatus.put(part1, status1);
- problemStatus.put(part2, status2);
- return problemStatus;
+ return Map.of(
+ part1, status1,
+ part2, status2
+ );
}
}
\ No newline at end of file
diff --git a/src/test/java/org/haffson/adventofcode/AntiPatternDetectionTest.java b/src/test/java/org/haffson/adventofcode/AntiPatternDetectionTest.java
index def9f076..1993df43 100644
--- a/src/test/java/org/haffson/adventofcode/AntiPatternDetectionTest.java
+++ b/src/test/java/org/haffson/adventofcode/AntiPatternDetectionTest.java
@@ -5,12 +5,12 @@
import com.tngtech.archunit.library.GeneralCodingRules;
import org.haffson.adventofcode.archUnitTestingUtils.MethodCondition;
import org.haffson.adventofcode.archUnitTestingUtils.MethodParameterNumberPredicate;
-import org.haffson.adventofcode.archUnitTestingUtils.MethodTransformer;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import static com.tngtech.archunit.lang.conditions.ArchConditions.callMethod;
-import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.no;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
+import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noMethods;
+
public class AntiPatternDetectionTest {
@@ -33,9 +33,10 @@ public void doNotUseSystemExit() {
@Test
public void doNotUseTooManyMethodParameters() {
- no(MethodTransformer.methods())
+ noMethods()
.should(MethodCondition.have(MethodParameterNumberPredicate.moreParametersThan(4)))
.because("methods with too many parameters are hard to understand and complicated to test. Consider refactoring, e.g. splitting functionality or introducing Parameter Objects.")
.check(allClasses);
}
+
}
diff --git a/src/test/java/org/haffson/adventofcode/ArchUnitRulesFromPlantUML.java b/src/test/java/org/haffson/adventofcode/ArchUnitRulesFromPlantUML.java
index 8186c941..16cfa66e 100644
--- a/src/test/java/org/haffson/adventofcode/ArchUnitRulesFromPlantUML.java
+++ b/src/test/java/org/haffson/adventofcode/ArchUnitRulesFromPlantUML.java
@@ -3,7 +3,7 @@
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import java.net.URL;
@@ -17,13 +17,14 @@ public class ArchUnitRulesFromPlantUML {
URL myDiagram = getClass().getClassLoader().getResource("adventOfCodeArchitecture.puml");
private final JavaClasses allClasses = new ClassFileImporter()
- .withImportOption(new ImportOption.DontIncludeTests())
+ .withImportOption(new ImportOption.DoNotIncludeTests())
.importPackages("org.haffson.adventofcode");
@Test
public void classesShouldAdhereToPlantUmlDiagram() {
classes().should(adhereToPlantUmlDiagram(myDiagram, consideringOnlyDependenciesInDiagram()))
- .check(allClasses);;
+ .check(allClasses);
+ ;
}
-}
+}
\ No newline at end of file
diff --git a/src/test/java/org/haffson/adventofcode/ArchitectureUnitTestSuite.java b/src/test/java/org/haffson/adventofcode/ArchitectureUnitTestSuite.java
deleted file mode 100644
index 7b13367d..00000000
--- a/src/test/java/org/haffson/adventofcode/ArchitectureUnitTestSuite.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package org.haffson.adventofcode;
-
-import org.junit.experimental.categories.Categories;
-import org.junit.runner.RunWith;
-import org.junit.runners.Suite;
-
-@RunWith(Categories.class)
-@Categories.IncludeCategory(ArchitectureUnitTest.class)
-@Suite.SuiteClasses({ArchitectureUnitTesting.class})
-public class ArchitectureUnitTestSuite {
-}
\ No newline at end of file
diff --git a/src/test/java/org/haffson/adventofcode/ArchitectureUnitTesting.java b/src/test/java/org/haffson/adventofcode/ArchitectureUnitTesting.java
index c7b17132..4c74b928 100644
--- a/src/test/java/org/haffson/adventofcode/ArchitectureUnitTesting.java
+++ b/src/test/java/org/haffson/adventofcode/ArchitectureUnitTesting.java
@@ -3,13 +3,10 @@
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
-import org.haffson.adventofcode.days.Days;
-import org.haffson.adventofcode.service.AdventOfCodeService;
import org.haffson.adventofcode.archUnitTestingUtils.ClassesPredicates;
-import org.haffson.adventofcode.archUnitTestingUtils.FieldTypePredicate;
-import org.haffson.adventofcode.archUnitTestingUtils.FieldsCondition;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
+import org.haffson.adventofcode.days.Days;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;
@@ -18,15 +15,16 @@
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.implement;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
-import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.theClass;
import static com.tngtech.archunit.library.Architectures.layeredArchitecture;
+import static org.haffson.adventofcode.archUnitTestingUtils.FieldTypePredicate.areOfType;
+import static org.haffson.adventofcode.archUnitTestingUtils.FieldsCondition.haveFieldsThat;
-@Category(ArchitectureUnitTest.class)
+@Tag("ArchitectureUnitTest")
public class ArchitectureUnitTesting {
private final JavaClasses allClasses = new ClassFileImporter()
- .withImportOption(new ImportOption.DontIncludeTests())
+ .withImportOption(new ImportOption.DoNotIncludeTests())
.importPackages("org.haffson.adventofcode");
@Test
@@ -39,12 +37,12 @@ public void classesImplementingDaysShouldBeAnnotatedAsComponents() {
@Test
public void theListWithTheImplementedDaysShouldOnlyBeHandledInAdventOfCodeService() {
- noClasses().that().dontHaveSimpleName("AdventOfCodeService")
- .should(FieldsCondition.haveFieldsThat(FieldTypePredicate.areOfType(List.class, Days.class)))
+ noClasses().that().doNotHaveSimpleName("AdventOfCodeService")
+ .should(haveFieldsThat(areOfType(List.class, Days.class)))
.because("we want only AdventOfCodeService to handle the access to the implementations of Days.")
.check(allClasses);
- theClass(AdventOfCodeService.class)
- .should(FieldsCondition.haveFieldsThat(FieldTypePredicate.areOfType(List.class, Days.class)))
+ classes().that().haveSimpleName("AdventOfCodeService")
+ .should(haveFieldsThat(areOfType(List.class, Days.class)))
.because("we want only AdventOfCodeService to handle the access to the implementations of Days.")
.check(allClasses);
}
diff --git a/src/test/java/org/haffson/adventofcode/SpringBootConventionsTest.java b/src/test/java/org/haffson/adventofcode/SpringBootConventionsTest.java
index 8ef36e13..be15a502 100644
--- a/src/test/java/org/haffson/adventofcode/SpringBootConventionsTest.java
+++ b/src/test/java/org/haffson/adventofcode/SpringBootConventionsTest.java
@@ -5,8 +5,11 @@
import com.tngtech.archunit.core.domain.properties.CanBeAnnotated;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
-import org.haffson.adventofcode.archUnitTestingUtils.*;
-import org.junit.Test;
+import org.haffson.adventofcode.archUnitTestingUtils.FieldsCondition;
+import org.haffson.adventofcode.archUnitTestingUtils.IsAutowired;
+import org.haffson.adventofcode.archUnitTestingUtils.IsSpringBootTest;
+import org.haffson.adventofcode.archUnitTestingUtils.MethodCondition;
+import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -18,14 +21,12 @@
import static com.tngtech.archunit.core.domain.JavaMember.Predicates.declaredIn;
import static com.tngtech.archunit.core.domain.properties.CanBeAnnotated.Predicates.annotatedWith;
import static com.tngtech.archunit.lang.conditions.ArchPredicates.are;
-import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
-import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.no;
-import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
+import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.*;
public class SpringBootConventionsTest {
private final JavaClasses allClasses = new ClassFileImporter()
- .withImportOption(new ImportOption.DontIncludeTests())
+ .withImportOption(new ImportOption.DoNotIncludeTests())
.importPackages("org.haffson.adventofcode");
/**
@@ -38,7 +39,7 @@ private static DescribedPredicate springBootApplicationClass() {
@Test
public void doNotCreateBeansInApplicationClass() {
- no(MethodTransformer.methods())
+ noMethods()
.that(are(annotatedWith(Bean.class)))
.should(MethodCondition.be(declaredIn(springBootApplicationClass())))
.because("this may lead to problems with missing dependencies in tests that bootstrap the Spring Boot Application only partially (e.g. @DataMongoTest tests). Simply declare your beans in separate @" + Configuration.class.getSimpleName() + " classes.")
@@ -63,4 +64,4 @@ public void doNotAutowireFields() {
.because("autowiring fields is not recommended and makes classes harder to test. Inject necessary objects via constructor, split classes that require too many autowired constructor parameters.")
.check(allClasses);
}
-}
+}
\ No newline at end of file
diff --git a/src/test/java/org/haffson/adventofcode/archUnitTestingUtils/MethodParameterNumberPredicate.java b/src/test/java/org/haffson/adventofcode/archUnitTestingUtils/MethodParameterNumberPredicate.java
index 5dcf9b87..92ae0849 100644
--- a/src/test/java/org/haffson/adventofcode/archUnitTestingUtils/MethodParameterNumberPredicate.java
+++ b/src/test/java/org/haffson/adventofcode/archUnitTestingUtils/MethodParameterNumberPredicate.java
@@ -24,6 +24,6 @@ public MethodParameterNumberPredicate(final int allowedNumberOfParameters) {
@Override
public boolean apply(JavaMethod method) {
- return method.getParameters().size() > allowedNumberOfParameters;
+ return method.getParameterTypes().size() > allowedNumberOfParameters;
}
}
diff --git a/src/test/java/org/haffson/adventofcode/archUnitTestingUtils/MethodTransformer.java b/src/test/java/org/haffson/adventofcode/archUnitTestingUtils/MethodTransformer.java
index 5bda4718..c5697abd 100644
--- a/src/test/java/org/haffson/adventofcode/archUnitTestingUtils/MethodTransformer.java
+++ b/src/test/java/org/haffson/adventofcode/archUnitTestingUtils/MethodTransformer.java
@@ -11,7 +11,7 @@
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
-import static com.tngtech.archunit.core.domain.properties.HasReturnType.Predicates.returnType;
+import static com.tngtech.archunit.core.domain.properties.HasReturnType.Predicates.rawReturnType;
public class MethodTransformer extends AbstractClassesTransformer {
public MethodTransformer() {
@@ -31,6 +31,7 @@ public static MethodTransformer methods() {
/**
* A predicate that handles a {@link JavaMethod}
+ *
* @param className a {@code String} with the name of the Class
* @return if the method is implemented in a specific classname
*/
@@ -62,7 +63,7 @@ private static JavaClass getDeclaringClass(final JavaMethod method) {
}
public static DescribedPredicate string() {
- return returnType(String.class);
+ return rawReturnType(String.class);
}
public Iterable doTransform(final JavaClasses classes) {
@@ -70,4 +71,5 @@ public Iterable doTransform(final JavaClasses classes) {
.flatMap(javaClass -> javaClass.getMethods().stream())
.collect(Collectors.toList());
}
-}
+
+}
\ No newline at end of file
diff --git a/src/test/java/org/haffson/adventofcode/controller/AdventOfCodeControllerTest.java b/src/test/java/org/haffson/adventofcode/controller/AdventOfCodeControllerTest.java
index 4d668fda..336ca2fe 100644
--- a/src/test/java/org/haffson/adventofcode/controller/AdventOfCodeControllerTest.java
+++ b/src/test/java/org/haffson/adventofcode/controller/AdventOfCodeControllerTest.java
@@ -2,9 +2,8 @@
import org.haffson.adventofcode.days.Days;
import org.haffson.adventofcode.service.AdventOfCodeService;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
@@ -12,11 +11,12 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.restdocs.payload.FieldDescriptor;
-import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import java.nio.charset.Charset;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
import java.util.List;
import static org.hamcrest.Matchers.is;
@@ -29,13 +29,12 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-@RunWith(SpringRunner.class)
@WebMvcTest(AdventOfCodeController.class)
@AutoConfigureRestDocs(outputDir = "target/snippets")
public class AdventOfCodeControllerTest {
@MockBean
- private AdventOfCodeService adventOfCodeService;
+ AdventOfCodeService adventOfCodeService;
private String baseUrl = "/api/adventOfCode";
@@ -43,25 +42,41 @@ public class AdventOfCodeControllerTest {
private Integer day1 = 1;
private Integer part1 = 1;
+ private Integer part2 = 2;
private String resultDay1Part1 = "Product 1: " + 326211;
+ private String resultDay1Part2 = "Product 2: " + 326211;
@Autowired
private MockMvc mvc;
- @Before
- public void setup() {
+
+ @BeforeEach
+ void setup() {
+ Days day01Stub = Mockito.mock(Days.class);
+ Days day02Stub = Mockito.mock(Days.class);
+ Mockito.when(day01Stub.getDay()).thenReturn(1);
+ Mockito.when(day02Stub.getDay()).thenReturn(2);
+
+ List daysImplementedList = new LinkedList<>();
+ daysImplementedList.add(day01Stub);
+ daysImplementedList.add(day02Stub);
+
Mockito.when(adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(day1, part1))
.thenReturn(resultDay1Part1);
+ Mockito.when(adventOfCodeService.getDaysSolutions())
+ .thenReturn(daysImplementedList);
}
+
@Test
public void testGetResultForASpecificDayAndPuzzlePart() throws Exception {
-
- mvc.perform(get(baseUrl + "?day=" + day1 + "&part=" + part1)
+ mvc.perform(get(baseUrl + "/" + "?day=" + day1 + "&part=" + part1)
.contentType(contentType))
.andExpect(status().isOk())
- .andExpect(jsonPath("content", is(resultDay1Part1)))
- .andExpect(jsonPath("$._links.self.href", is("http://localhost:8080" + baseUrl + "?day=" + day1 + "&part=" + part1)))
+ .andExpect(jsonPath("day", is(1)))
+ .andExpect(jsonPath("part", is(1)))
+ .andExpect(jsonPath("answer", is("Product 1: " + 326211)))
+ .andExpect(jsonPath("$._links.self.href", is("http://localhost:8080" + baseUrl + "/" + "?day=" + day1 + "&part=" + part1)))
.andDo(document("getResultForASpecificDayAndPuzzlePart",
preprocessResponse(prettyPrint()),
responseFields(getResultForASpecificDayAndPuzzlePart("")))
@@ -70,28 +85,30 @@ public void testGetResultForASpecificDayAndPuzzlePart() throws Exception {
}
@Test
- public void testDaysImplementedReturnsSortedList() throws Exception {
- Days day01Stub = Mockito.mock(Days.class);
- Mockito.when(day01Stub.getDay()).thenReturn(1);
-
- Days day02Stub = Mockito.mock(Days.class);
- Mockito.when(day02Stub.getDay()).thenReturn(2);
+ public void testGetResultForASpecificDayAndPuzzlePart2() throws Exception {
+ mvc.perform(get(baseUrl + "/" + "?day=" + day1 + "&part=" + part2)
+ .contentType(contentType))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("day", is(1)))
+ .andExpect(jsonPath("part", is(2)))
+// .andExpect(jsonPath("answer", is("Product 1: " + 326211)))
+ .andExpect(jsonPath("$._links.self.href", is("http://localhost:8080" + baseUrl + "/" + "?day=" + day1 + "&part=" + part2)))
+ .andDo(document("getResultForASpecificDayAndPuzzlePart",
+ preprocessResponse(prettyPrint()),
+ responseFields(getResultForASpecificDayAndPuzzlePart("")))
+ );
- List daysImplementedList = new ArrayList<>();
- daysImplementedList.add(day02Stub);
- daysImplementedList.add(day01Stub);
+ }
- List daysImplementedIntegerList = new ArrayList<>();
- daysImplementedIntegerList.add(1);
- daysImplementedIntegerList.add(2);
+ @Test
+ public void testDaysImplemented() throws Exception {
- Mockito.when(adventOfCodeService.getDaysSolutions())
- .thenReturn(daysImplementedList);
+ List daysImplementedIntegerList = new LinkedList<>(Arrays.asList(1, 2));
mvc.perform(get(baseUrl + "/daysimplemented")
.contentType(contentType))
.andExpect(status().isOk())
- .andExpect(jsonPath("_embedded.integerList", is(daysImplementedIntegerList)))
+ .andExpect(jsonPath("$._embedded.integerList", is(daysImplementedIntegerList)))
.andExpect(jsonPath("$._links.self.href", is("http://localhost:8080" + baseUrl + "/daysimplemented")))
.andDo(document(
"daysImplemented",
@@ -108,7 +125,11 @@ private ArrayList getResultForASpecificDayAndPuzzlePart(String
} else pathString = path;
ArrayList fieldDescriptorList = new ArrayList<>();
- fieldDescriptorList.add(fieldWithPath(pathString + "content")
+ fieldDescriptorList.add(fieldWithPath(pathString + "day")
+ .description("Specific day of the puzzle of the AdventOfCode calendar"));
+ fieldDescriptorList.add(fieldWithPath(pathString + "part")
+ .description("Specific day's part of the puzzle of the AdventOfCode calendar"));
+ fieldDescriptorList.add(fieldWithPath(pathString + "answer")
.description("Result of the Puzzle for a specific day and part of the AdventOfCode calendar"));
fieldDescriptorList.add(fieldWithPath(pathString + "_links.self.href")
.description("Self link to the query for the specific solution for a day and part"));
diff --git a/src/test/java/org/haffson/adventofcode/days/day01/Day01Test.java b/src/test/java/org/haffson/adventofcode/days/day01/Day01Test.java
index 8a66f54f..88d234d6 100644
--- a/src/test/java/org/haffson/adventofcode/days/day01/Day01Test.java
+++ b/src/test/java/org/haffson/adventofcode/days/day01/Day01Test.java
@@ -13,9 +13,9 @@
public class Day01Test {
- DataLoader dataLoader;
+ DataLoader dataLoader = new DataLoader();
// @Matthias: I think this is Java 8 the command List.of() does not exist, yet.
- List testNumbers = new ArrayList<>(Arrays.asList(1721, 979, 366, 299, 675, 1456));
+ List testNumbers = new ArrayList<>(List.of(1721, 979, 366, 299, 675, 1456));
@BeforeEach
void setup() {
diff --git a/src/test/java/org/haffson/adventofcode/days/day02/Day02Test.java b/src/test/java/org/haffson/adventofcode/days/day02/Day02Test.java
index 62449e84..8afd6508 100644
--- a/src/test/java/org/haffson/adventofcode/days/day02/Day02Test.java
+++ b/src/test/java/org/haffson/adventofcode/days/day02/Day02Test.java
@@ -15,7 +15,7 @@
public class Day02Test {
DataLoader dataLoader = new DataLoader();
- List passwordDatabase = new ArrayList<>(Arrays.asList("1-3 a: abcde", "1-3 b: cdefg", "2-9 c: ccccccccc"));
+ List passwordDatabase = new ArrayList<>(List.of("1-3 a: abcde", "1-3 b: cdefg", "2-9 c: ccccccccc"));
@BeforeEach
void setup() {
diff --git a/src/test/java/org/haffson/adventofcode/days/day03/Day03Test.java b/src/test/java/org/haffson/adventofcode/days/day03/Day03Test.java
index e6ca41a4..4098209a 100644
--- a/src/test/java/org/haffson/adventofcode/days/day03/Day03Test.java
+++ b/src/test/java/org/haffson/adventofcode/days/day03/Day03Test.java
@@ -16,7 +16,7 @@
public class Day03Test {
DataLoader dataLoader = new DataLoader();
- List grid = new ArrayList<>(Arrays.asList("..##.......", "#...#...#..", ".#....#..#.",
+ List grid = new ArrayList<>(List.of("..##.......", "#...#...#..", ".#....#..#.",
"..#.#...#.#", ".#...##..#.", "..#.##.....", ".#.#.#....#", ".#........#",
"#.##...#...", "#...##....#", ".#..#...#.#"));
diff --git a/src/test/java/org/haffson/adventofcode/days/day05/Day05Test.java b/src/test/java/org/haffson/adventofcode/days/day05/Day05Test.java
index 3e2501cf..30168e39 100644
--- a/src/test/java/org/haffson/adventofcode/days/day05/Day05Test.java
+++ b/src/test/java/org/haffson/adventofcode/days/day05/Day05Test.java
@@ -15,7 +15,7 @@
public class Day05Test {
DataLoader dataLoader = new DataLoader();
- List boardingPassesPart1 = new ArrayList<>(Arrays.asList("BFFFBBFRRR", "FFFBBBFRRR", "BBFFBBFRLL"));
+ List boardingPassesPart1 = new ArrayList<>(List.of("BFFFBBFRRR", "FFFBBBFRRR", "BBFFBBFRLL"));
List boardingPassesPart2 = new DataLoader().getDataDay05();
@BeforeEach
diff --git a/src/test/java/org/haffson/adventofcode/service/AdventOfCodeServiceTest.java b/src/test/java/org/haffson/adventofcode/service/AdventOfCodeServiceTest.java
index 4b8f86c0..f0cb2988 100644
--- a/src/test/java/org/haffson/adventofcode/service/AdventOfCodeServiceTest.java
+++ b/src/test/java/org/haffson/adventofcode/service/AdventOfCodeServiceTest.java
@@ -3,37 +3,45 @@
import org.haffson.adventofcode.ProblemStatusEnum;
import org.haffson.adventofcode.days.Days;
import org.haffson.adventofcode.days.day01.Day01;
+import org.haffson.adventofcode.days.day02.Day02;
+import org.haffson.adventofcode.days.day03.Day03;
import org.haffson.adventofcode.exceptions.PuzzleNotSolvedYetException;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
-import org.springframework.boot.test.mock.mockito.MockBean;
-import org.springframework.test.context.junit4.SpringRunner;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.assertj.core.api.Assertions.assertThat;
-@RunWith(SpringRunner.class)
public class AdventOfCodeServiceTest {
private final List daysSolutions = new LinkedList<>();
- private HashMap problemStatus = new HashMap<>();
-
- private AdventOfCodeService adventOfCodeService;
+ private final HashMap problemStatus = new HashMap<>();
- @MockBean
- private Day01 day01;
+ private AdventOfCodeService adventOfCodeService;
- @Before
+ @BeforeEach
public void setup() {
+ Day01 day01 = Mockito.mock(Day01.class);
problemStatus.put(1, ProblemStatusEnum.SOLVED);
problemStatus.put(2, ProblemStatusEnum.UNSOLVED);
+
+ Day02 day02 = Mockito.mock(Day02.class);
+ Day03 day03 = Mockito.mock(Day03.class);
+ daysSolutions.add(day02);
+ daysSolutions.add(day03);
daysSolutions.add(day01);
Mockito.when(day01.getDay()).thenReturn(1);
+ Mockito.when(day02.getDay()).thenReturn(2);
+ Mockito.when(day03.getDay()).thenReturn(3);
+
Mockito.when(day01.getProblemStatus()).thenReturn(problemStatus);
Mockito.when(day01.firstPart()).thenReturn("Product 1: " + 326211);
adventOfCodeService = new AdventOfCodeService(daysSolutions);
@@ -42,23 +50,37 @@ public void setup() {
@Test
public void getResultsForASpecificDayAndPuzzlePartTest() {
String actualResult = adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(1, 1);
-
- Assert.assertEquals("Product 1: " + 326211, actualResult);
+ assertThat(actualResult).isEqualTo("Product 1: " + 326211);
}
- @Test(expected = PuzzleNotSolvedYetException.class)
- public void tryingToGetResultsForANotYetImplementedPartThrowsExceptionTest() {
- adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(1, 2);
+ @Test
+ public void tryingToGetResultsForANotYetImplementedPartThrowsExceptionTest() throws Exception {
+ Assertions.assertThrows(PuzzleNotSolvedYetException.class, () -> {
+ adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(1, 2);
+ });
}
- @Test(expected = PuzzleNotSolvedYetException.class)
- public void tryingToGetResultsForANotYetImplementedDayThrowsException() {
- adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(10, 1);
+ @Test
+ public void tryingToGetResultsForANotYetImplementedDayThrowsException() throws Exception {
+ Assertions.assertThrows(PuzzleNotSolvedYetException.class, () -> {
+ adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(10, 1);
+ });
}
- @Test(expected = PuzzleNotSolvedYetException.class)
- public void tryingToGetResultsForAnyOtherPartThrowsException() {
- adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(2, 3);
+ @Test
+ public void tryingToGetResultsForAnyOtherPartThrowsException() throws Exception {
+ Assertions.assertThrows(PuzzleNotSolvedYetException.class, () -> {
+ adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(2, 3);
+ });
}
+ @Test
+ public void getSortedListOfDaysSolution() {
+ List expectedResult = Arrays.asList(1, 2, 3);
+ final List actualResult = adventOfCodeService.getDaysSolutions().stream()
+ .map(Days::getDay)
+ .collect(Collectors.toList());
+
+ assertThat(actualResult).isEqualTo(expectedResult);
+ }
}
\ No newline at end of file
diff --git a/src/test/java/org/haffson/adventofcode/utils/FileReadersTest.java b/src/test/java/org/haffson/adventofcode/utils/FileReadersTest.java
index 68205feb..2d4d4b61 100644
--- a/src/test/java/org/haffson/adventofcode/utils/FileReadersTest.java
+++ b/src/test/java/org/haffson/adventofcode/utils/FileReadersTest.java
@@ -1,11 +1,12 @@
package org.haffson.adventofcode.utils;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+//import org.junit.Test;
+import org.junit.jupiter.api.Test;
+//import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;
-@RunWith(SpringRunner.class)
+//@RunWith(SpringRunner.class)
public class FileReadersTest {
//TODO: Meaningful tests for readFileIntoArrayOfIntegers and readFileIntoStringList
diff --git a/system.properties b/system.properties
new file mode 100644
index 00000000..a5a6d271
--- /dev/null
+++ b/system.properties
@@ -0,0 +1 @@
+java.runtime.version=15
\ No newline at end of file
diff --git a/target/snippets/daysImplemented/http-response.adoc b/target/snippets/daysImplemented/http-response.adoc
index 467afa92..bde01e7e 100644
--- a/target/snippets/daysImplemented/http-response.adoc
+++ b/target/snippets/daysImplemented/http-response.adoc
@@ -1,7 +1,7 @@
[source,http,options="nowrap"]
----
HTTP/1.1 200 OK
-Content-Type: application/json;charset=UTF-8
+Content-Type: application/hal+json
Content-Length: 167
{
diff --git a/target/snippets/getResultForASpecificDayAndPuzzlePart/curl-request.adoc b/target/snippets/getResultForASpecificDayAndPuzzlePart/curl-request.adoc
index beba26f4..e168993e 100644
--- a/target/snippets/getResultForASpecificDayAndPuzzlePart/curl-request.adoc
+++ b/target/snippets/getResultForASpecificDayAndPuzzlePart/curl-request.adoc
@@ -1,5 +1,5 @@
[source,bash]
----
-$ curl 'http://localhost:8080/api/adventOfCode?day=1&part=1' -i -X GET \
+$ curl 'http://localhost:8080/api/adventOfCode/?day=1&part=2' -i -X GET \
-H 'Content-Type: application/hal+json;charset=UTF-8'
----
\ No newline at end of file
diff --git a/target/snippets/getResultForASpecificDayAndPuzzlePart/http-request.adoc b/target/snippets/getResultForASpecificDayAndPuzzlePart/http-request.adoc
index 923c3684..84a256b2 100644
--- a/target/snippets/getResultForASpecificDayAndPuzzlePart/http-request.adoc
+++ b/target/snippets/getResultForASpecificDayAndPuzzlePart/http-request.adoc
@@ -1,6 +1,6 @@
[source,http,options="nowrap"]
----
-GET /api/adventOfCode?day=1&part=1 HTTP/1.1
+GET /api/adventOfCode/?day=1&part=2 HTTP/1.1
Content-Type: application/hal+json;charset=UTF-8
Host: localhost:8080
diff --git a/target/snippets/getResultForASpecificDayAndPuzzlePart/http-response.adoc b/target/snippets/getResultForASpecificDayAndPuzzlePart/http-response.adoc
index 6647c125..42ae2c01 100644
--- a/target/snippets/getResultForASpecificDayAndPuzzlePart/http-response.adoc
+++ b/target/snippets/getResultForASpecificDayAndPuzzlePart/http-response.adoc
@@ -1,14 +1,16 @@
[source,http,options="nowrap"]
----
HTTP/1.1 200 OK
-Content-Type: application/json;charset=UTF-8
-Content-Length: 147
+Content-Type: application/hal+json
+Content-Length: 159
{
- "content" : "Product 1: 326211",
+ "day" : 1,
+ "part" : 2,
+ "answer" : null,
"_links" : {
"self" : {
- "href" : "http://localhost:8080/api/adventOfCode?day=1&part=1"
+ "href" : "http://localhost:8080/api/adventOfCode/?day=1&part=2"
}
}
}
diff --git a/target/snippets/getResultForASpecificDayAndPuzzlePart/httpie-request.adoc b/target/snippets/getResultForASpecificDayAndPuzzlePart/httpie-request.adoc
index 1f8e0912..71b599a1 100644
--- a/target/snippets/getResultForASpecificDayAndPuzzlePart/httpie-request.adoc
+++ b/target/snippets/getResultForASpecificDayAndPuzzlePart/httpie-request.adoc
@@ -1,5 +1,5 @@
[source,bash]
----
-$ http GET 'http://localhost:8080/api/adventOfCode?day=1&part=1' \
+$ http GET 'http://localhost:8080/api/adventOfCode/?day=1&part=2' \
'Content-Type:application/hal+json;charset=UTF-8'
----
\ No newline at end of file
diff --git a/target/snippets/getResultForASpecificDayAndPuzzlePart/response-body.adoc b/target/snippets/getResultForASpecificDayAndPuzzlePart/response-body.adoc
index 59789b18..7d66651b 100644
--- a/target/snippets/getResultForASpecificDayAndPuzzlePart/response-body.adoc
+++ b/target/snippets/getResultForASpecificDayAndPuzzlePart/response-body.adoc
@@ -1,10 +1,12 @@
[source,options="nowrap"]
----
{
- "content" : "Product 1: 326211",
+ "day" : 1,
+ "part" : 2,
+ "answer" : null,
"_links" : {
"self" : {
- "href" : "http://localhost:8080/api/adventOfCode?day=1&part=1"
+ "href" : "http://localhost:8080/api/adventOfCode/?day=1&part=2"
}
}
}
diff --git a/target/snippets/getResultForASpecificDayAndPuzzlePart/response-fields.adoc b/target/snippets/getResultForASpecificDayAndPuzzlePart/response-fields.adoc
index 2a2141aa..bd6a358c 100644
--- a/target/snippets/getResultForASpecificDayAndPuzzlePart/response-fields.adoc
+++ b/target/snippets/getResultForASpecificDayAndPuzzlePart/response-fields.adoc
@@ -1,8 +1,16 @@
|===
|Path|Type|Description
-|`+content+`
-|`+String+`
+|`+day+`
+|`+Number+`
+|Specific day of the puzzle of the AdventOfCode calendar
+
+|`+part+`
+|`+Number+`
+|Specific day's part of the puzzle of the AdventOfCode calendar
+
+|`+answer+`
+|`+Null+`
|Result of the Puzzle for a specific day and part of the AdventOfCode calendar
|`+_links.self.href+`