no longer writable:
+ * DayModel() is wrapper class for String answer
+ */
+ private static class DayModel {
+ private final Integer day;
+ private final Integer part;
+ private final String answer;
+
+ public DayModel(final Integer day, final Integer part, final String answer) {
+ this.day = day;
+ this.part = part;
+ this.answer = answer;
+ }
+
+ public Integer getDay() {
+ return day;
+ }
+
+ public Integer getPart() {
+ return part;
+ }
+
+ public String getAnswer() {
+ return answer;
+ }
+ }
+
/**
* Handles a GET-Request with the day of the advent calendar and the part to be solved and returns a HATEOAS
- * {@code Resource<>} with the corresponding solution.
+ * {@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
*
- * @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 Resource<>} with the corresponding solution
+ * @return a HATEOAS-{@code EntityModel<>} with the corresponding solution
*/
- @GetMapping
- public Resource getResultForASpecificDayAndPuzzlePart(@RequestParam(value = "day", defaultValue = "") String day, @RequestParam(value = "part", defaultValue = "") String part) {
+ @GetMapping(value = "/")
+ public EntityModel getResultForASpecificDayAndPuzzlePart(@RequestParam(value = "day", defaultValue = "1") final Integer day,
+ @RequestParam(value = "part", defaultValue = "1") final Integer part) {
- logger.info("The results for day " + day + ", part " + part + " have been requested.");
+ logger.info("The results for day {}, part {} have been requested.", day, part);
- return new Resource<>(
- adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(day, part),
- linkTo(methodOn(AdventOfCodeController.class).getResultForASpecificDayAndPuzzlePart(day, part)).withSelfRel()
- );
+ final String answer = adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(day, part);
+ final Link link = linkTo(methodOn(AdventOfCodeController.class).getResultForASpecificDayAndPuzzlePart(day, part)).withSelfRel();
+ final DayModel dayModel = new DayModel(day, part, answer);
+ return EntityModel.of(dayModel, link);
}
/**
- * Returns a HATEOAS {@code Resources<>} with an integer list of all days that have been implemented
+ * Returns a HATEOAS {@code CollectionModel<>} with an integer list of all days that have been implemented
*
- * @return a HATEOAS-{@code Resources<>} 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 Resources daysImplemented() {
+ public CollectionModel daysImplemented() {
+
+ logger.info("A list of implemented days (sorted) has been requested.");
- logger.info("A list of implemented days has been requested.");
+ final List daysImplemented = adventOfCodeService.getDaysSolutions().stream()
+ .map(Days::getDay)
+ .collect(Collectors.toList());
+ final Link link = linkTo(methodOn(AdventOfCodeController.class).daysImplemented()).withSelfRel();
- return new Resources<>(
- adventOfCodeService.getDaysSolutions().stream()
- .map(Days::getDay)
- .sorted()
- .collect(Collectors.toList()),
- linkTo(methodOn(AdventOfCodeController.class).daysImplemented()).withSelfRel()
- );
+ return CollectionModel.of(daysImplemented, link);
}
}
\ No newline at end of file
diff --git a/src/main/java/org/haffson/adventofcode/days/Days.java b/src/main/java/org/haffson/adventofcode/days/Days.java
index 28948aa5..0aac8b8a 100644
--- a/src/main/java/org/haffson/adventofcode/days/Days.java
+++ b/src/main/java/org/haffson/adventofcode/days/Days.java
@@ -2,7 +2,8 @@
import org.haffson.adventofcode.ProblemStatusEnum;
-import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
/**
* An interface for the individual puzzles,
@@ -38,5 +39,5 @@ public interface Days {
*
* @return the status of each part
*/
- HashMap getProblemStatus();
+ Map getProblemStatus();
}
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 e703d306..18ef4cf5 100644
--- a/src/main/java/org/haffson/adventofcode/days/day01/Day01.java
+++ b/src/main/java/org/haffson/adventofcode/days/day01/Day01.java
@@ -3,10 +3,11 @@
import org.haffson.adventofcode.ProblemStatusEnum;
import org.haffson.adventofcode.days.Days;
import org.haffson.adventofcode.utils.FileReaders;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.haffson.adventofcode.utils.ProblemStatus;
import org.springframework.stereotype.Component;
-import java.util.HashMap;
+import java.util.Map;
+
/**
* Implementation for Day 1: Chronal Calibration.
@@ -14,19 +15,11 @@
@Component
public class Day01 implements Days {
- /** The puzzle status {@code HashMap} */
- private final HashMap problemStatus;
+ private final Map problemStatus;
- /**
- * Causes the input file to be parsed into the frequencies array ({@code frequencies}).
- *
- * @param fileReaders {@code @Autowired} fileReader //TODO: inject what you need
- */
- @Autowired
- Day01(FileReaders fileReaders) {
- this.problemStatus = new HashMap<>();
- this.problemStatus.put("1", ProblemStatusEnum.UNSOLVED);
- this.problemStatus.put("2", ProblemStatusEnum.UNSOLVED);
+ public Day01(FileReaders fileReaders) {
+ this.problemStatus = ProblemStatus.getProblemStatusMap(1, 2,
+ ProblemStatusEnum.SOLVED, ProblemStatusEnum.SOLVED);
}
@Override
@@ -35,7 +28,7 @@ public int getDay() {
}
@Override
- public HashMap getProblemStatus() {
+ public Map getProblemStatus() {
return problemStatus;
}
diff --git a/src/main/java/org/haffson/adventofcode/exceptions/PuzzleNotSolvedYetException.java b/src/main/java/org/haffson/adventofcode/exceptions/PuzzleNotSolvedYetException.java
index 85735ac1..55c7573f 100644
--- a/src/main/java/org/haffson/adventofcode/exceptions/PuzzleNotSolvedYetException.java
+++ b/src/main/java/org/haffson/adventofcode/exceptions/PuzzleNotSolvedYetException.java
@@ -11,7 +11,7 @@
*/
@ResponseStatus(value = HttpStatus.NOT_IMPLEMENTED, reason = "This puzzle has not been solved yet. Please contact your local nerd to implement a solution for that first.")
public class PuzzleNotSolvedYetException extends RuntimeException {
- public PuzzleNotSolvedYetException(Throwable e) {
+ public PuzzleNotSolvedYetException(final Throwable e) {
super(e);
}
}
\ No newline at end of file
diff --git a/src/main/java/org/haffson/adventofcode/service/AdventOfCodeService.java b/src/main/java/org/haffson/adventofcode/service/AdventOfCodeService.java
index 7eb7d4bd..58194a64 100644
--- a/src/main/java/org/haffson/adventofcode/service/AdventOfCodeService.java
+++ b/src/main/java/org/haffson/adventofcode/service/AdventOfCodeService.java
@@ -4,9 +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;
@@ -20,17 +21,18 @@
@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) {
+
+ public AdventOfCodeService(final List daysSolutions) {
this.daysSolutions = Objects.requireNonNull(daysSolutions);
}
@@ -38,18 +40,23 @@ 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.
*/
- public String getResultsForASpecificDayAndPuzzlePart(String day, String part) {
- Days thisDaysClass = findDayForDay(Integer.parseInt(day));
+ public String getResultsForASpecificDayAndPuzzlePart(@NonNull final Integer day, @NonNull final Integer part) {
+
+ Objects.requireNonNull(day, "day is null");
+ Objects.requireNonNull(part, "part is null");
+
+
+ final Days thisDaysClass = findDayForDay(day);
if (!isProblemSolvedForPart(thisDaysClass, part)) {
throw new PuzzleNotSolvedYetException(new Throwable());
- } else if (("1").equals(part)) {
+ } else if (Objects.equals(part, 1)) {
return thisDaysClass.firstPart();
- } else if (("2").equals(part)) {
+ } else if (Objects.equals(part, 2)) {
return thisDaysClass.secondPart();
} else {
return "This puzzle has not been solved yet.";
@@ -60,10 +67,10 @@ public String getResultsForASpecificDayAndPuzzlePart(String day, String part) {
* 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, String part) {
+ private boolean isProblemSolvedForPart(final Days thisDaysClass, final Integer part) {
return thisDaysClass.getProblemStatus().containsKey(part) && thisDaysClass.getProblemStatus().get(part) == ProblemStatusEnum.SOLVED;
}
@@ -74,7 +81,7 @@ private boolean isProblemSolvedForPart(Days thisDaysClass, String part) {
* @param day the day for which an implementation should be retrieved
* @return the {@code Day} implementation for the requested day
*/
- private Days findDayForDay(int day) {
+ private Days findDayForDay(final int day) {
return daysSolutions.stream()
.filter(solution -> solution.getDay() == day)
.findFirst()
@@ -84,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() {
+ final 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(final Days day1, final 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/ProblemStatus.java b/src/main/java/org/haffson/adventofcode/utils/ProblemStatus.java
new file mode 100644
index 00000000..d39b6e11
--- /dev/null
+++ b/src/main/java/org/haffson/adventofcode/utils/ProblemStatus.java
@@ -0,0 +1,26 @@
+package org.haffson.adventofcode.utils;
+
+import org.haffson.adventofcode.ProblemStatusEnum;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ProblemStatus {
+
+ private ProblemStatus() {
+ }
+
+ public static Map getProblemStatusMap(
+ final int part1,
+ final int part2,
+ final ProblemStatusEnum status1,
+ final ProblemStatusEnum status2
+ ) {
+ final Map problemStatus = new HashMap<>();
+
+ 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 383e1788..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,39 +29,54 @@
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";
private MediaType contentType = new MediaType("application", "hal+json", Charset.forName("UTF-8"));
- private String day1 = "1";
- private String part1 = "1";
- private String resultDay1Part1 = "Part 1 - Frequency: 599";
+ 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 4ef0c170..86db3335 100644
--- a/src/test/java/org/haffson/adventofcode/days/day01/Day01Test.java
+++ b/src/test/java/org/haffson/adventofcode/days/day01/Day01Test.java
@@ -1,38 +1,34 @@
package org.haffson.adventofcode.days.day01;
import org.haffson.adventofcode.utils.FileReaders;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
-import org.springframework.test.context.junit4.SpringRunner;
+import static org.junit.jupiter.api.Assertions.assertEquals;
-@RunWith(SpringRunner.class)
-public class Day01Test {
+class Day01Test {
@MockBean
private FileReaders fileReaders;
@Test
- public void testGetDay() {
- Day01 day01 = new Day01(fileReaders);
- int expectedResult = 1;
- int actualResult = day01.getDay();
- Assert.assertEquals(expectedResult, actualResult);
+ void testGetDay() {
+ final Day01 day01 = new Day01(fileReaders);
+ final int expectedResult = 1;
+ final int actualResult = day01.getDay();
+ assertEquals(expectedResult, actualResult);
}
@Test
- public void test_firstPart_returnsExpectedResult() {
+ void test_firstPart_returnsExpectedResult() {
//arrange
- Day01 day01 = new Day01(fileReaders);
+ final Day01 day01 = new Day01(fileReaders);
- String expectedResult = "Part 1 - Frequency: " + 0;
+ final String expectedResult = "Part 1 - Frequency: " + 0;
//act
- String actualResult = day01.firstPart();
+ final String actualResult = day01.firstPart();
//assert
- Assert.assertEquals(expectedResult, actualResult);
+ assertEquals(expectedResult, actualResult);
}
-
}
diff --git a/src/test/java/org/haffson/adventofcode/service/AdventOfCodeServiceTest.java b/src/test/java/org/haffson/adventofcode/service/AdventOfCodeServiceTest.java
index 92c53bb9..5c7a5819 100644
--- a/src/test/java/org/haffson/adventofcode/service/AdventOfCodeServiceTest.java
+++ b/src/test/java/org/haffson/adventofcode/service/AdventOfCodeServiceTest.java
@@ -4,34 +4,30 @@
import org.haffson.adventofcode.days.Days;
import org.haffson.adventofcode.days.day01.Day01;
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.HashMap;
import java.util.LinkedList;
import java.util.List;
-@RunWith(SpringRunner.class)
-public class AdventOfCodeServiceTest {
+import static org.junit.jupiter.api.Assertions.assertEquals;
- private final List daysSolutions = new LinkedList<>();
- private HashMap problemStatus = new HashMap<>();
+class AdventOfCodeServiceTest {
- private AdventOfCodeService adventOfCodeService;
+ private final List daysSolutions = new LinkedList<>();
+ private final HashMap problemStatus = new HashMap<>();
- @MockBean
- private Day01 day01;
+ private AdventOfCodeService adventOfCodeService;
- @Before
+ @BeforeEach
public void setup() {
- problemStatus.put("1", ProblemStatusEnum.SOLVED);
- problemStatus.put("2", ProblemStatusEnum.UNSOLVED);
+ final Day01 day01 = Mockito.mock(Day01.class);
+ problemStatus.put(1, ProblemStatusEnum.SOLVED);
+ problemStatus.put(2, ProblemStatusEnum.UNSOLVED);
daysSolutions.add(day01);
Mockito.when(day01.getDay()).thenReturn(1);
Mockito.when(day01.getProblemStatus()).thenReturn(problemStatus);
@@ -40,25 +36,26 @@ public void setup() {
}
@Test
- public void getResultsForASpecificDayAndPuzzlePartTest() {
- String actualResult = adventOfCodeService.getResultsForASpecificDayAndPuzzlePart("1", "1");
-
- Assert.assertEquals("Part 1 - Frequency: 599", actualResult);
+ void getResultsForASpecificDayAndPuzzlePartTest() {
+ final String actualResult = adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(1, 1);
+ assertEquals("Part 1 - Frequency: 599", actualResult);
}
- @Test(expected = PuzzleNotSolvedYetException.class)
- public void tryingToGetResultsForANotYetImplementedPartThrowsExceptionTest() {
- adventOfCodeService.getResultsForASpecificDayAndPuzzlePart("1", "2");
+ @Test
+ void tryingToGetResultsForANotYetImplementedPartThrowsExceptionTest() {
+ Assertions.assertThrows(PuzzleNotSolvedYetException.class, () ->
+ adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(1, 2));
}
- @Test(expected = PuzzleNotSolvedYetException.class)
- public void tryingToGetResultsForANotYetImplementedDayThrowsException() {
- adventOfCodeService.getResultsForASpecificDayAndPuzzlePart("2", "1");
+ @Test
+ void tryingToGetResultsForANotYetImplementedDayThrowsException() {
+ Assertions.assertThrows(PuzzleNotSolvedYetException.class, () ->
+ adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(10, 1));
}
- @Test(expected = PuzzleNotSolvedYetException.class)
- public void tryingToGetResultsForAnyOtherPartThrowsException() {
- adventOfCodeService.getResultsForASpecificDayAndPuzzlePart("1", "3");
+ @Test
+ void tryingToGetResultsForAnyOtherPartThrowsException() {
+ Assertions.assertThrows(PuzzleNotSolvedYetException.class, () ->
+ adventOfCodeService.getResultsForASpecificDayAndPuzzlePart(2, 3));
}
-
-}
\ 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/src/test/java/org/haffson/adventofcode/utils/ProblemStatusTest.java b/src/test/java/org/haffson/adventofcode/utils/ProblemStatusTest.java
new file mode 100644
index 00000000..7885de6d
--- /dev/null
+++ b/src/test/java/org/haffson/adventofcode/utils/ProblemStatusTest.java
@@ -0,0 +1,4 @@
+package org.haffson.adventofcode.utils;
+
+public class ProblemStatusTest {
+}
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 5846f875..bde01e7e 100644
--- a/target/snippets/daysImplemented/http-response.adoc
+++ b/target/snippets/daysImplemented/http-response.adoc
@@ -1,8 +1,8 @@
[source,http,options="nowrap"]
----
HTTP/1.1 200 OK
-Content-Length: 176
-Content-Type: application/json;charset=UTF-8
+Content-Type: application/hal+json
+Content-Length: 167
{
"_embedded" : {
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 dc0f182a..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-Length: 160
-Content-Type: application/json;charset=UTF-8
+Content-Type: application/hal+json
+Content-Length: 159
{
- "content" : "Part 1 - Frequency: 599",
+ "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 e8a2c14e..7d66651b 100644
--- a/target/snippets/getResultForASpecificDayAndPuzzlePart/response-body.adoc
+++ b/target/snippets/getResultForASpecificDayAndPuzzlePart/response-body.adoc
@@ -1,10 +1,12 @@
[source,options="nowrap"]
----
{
- "content" : "Part 1 - Frequency: 599",
+ "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+`