From 856bbcdd69aa16c3ec4ade3c9558d7f96d37ca0e Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Fri, 7 Jul 2017 09:32:21 +0200 Subject: [PATCH 1/6] index controller --- .../boxitory/controller/BoxController.java | 5 +++-- .../boxitory/controller/IndexController.java | 22 +++++++++++++++++++ .../boxitory/service/BoxRepository.java | 3 +++ .../service/FilesystemBoxRepository.java | 8 +++++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/main/java/cz/sparko/boxitory/controller/IndexController.java diff --git a/src/main/java/cz/sparko/boxitory/controller/BoxController.java b/src/main/java/cz/sparko/boxitory/controller/BoxController.java index e0144bc..1eed035 100644 --- a/src/main/java/cz/sparko/boxitory/controller/BoxController.java +++ b/src/main/java/cz/sparko/boxitory/controller/BoxController.java @@ -6,8 +6,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RestController -@RequestMapping(value = "/", method = RequestMethod.GET) public class BoxController { private BoxRepository boxRepository; @@ -16,7 +17,7 @@ public BoxController(BoxRepository boxRepository) { this.boxRepository = boxRepository; } - @RequestMapping("{boxName}") + @RequestMapping(value = "/{boxName}", method = RequestMethod.GET) @ResponseBody public Box getBoxes(@PathVariable String boxName) { return boxRepository.getBox(boxName) diff --git a/src/main/java/cz/sparko/boxitory/controller/IndexController.java b/src/main/java/cz/sparko/boxitory/controller/IndexController.java new file mode 100644 index 0000000..e79f6ac --- /dev/null +++ b/src/main/java/cz/sparko/boxitory/controller/IndexController.java @@ -0,0 +1,22 @@ +package cz.sparko.boxitory.controller; + +import cz.sparko.boxitory.service.BoxRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Controller +public class IndexController { + private BoxRepository boxRepository; + + @Autowired + public IndexController(BoxRepository boxRepository) { + this.boxRepository = boxRepository; + } + + @RequestMapping(value = "/", method = RequestMethod.GET) + public String getBoxes() { + return "index"; + } +} diff --git a/src/main/java/cz/sparko/boxitory/service/BoxRepository.java b/src/main/java/cz/sparko/boxitory/service/BoxRepository.java index 6b6e061..93bdac4 100644 --- a/src/main/java/cz/sparko/boxitory/service/BoxRepository.java +++ b/src/main/java/cz/sparko/boxitory/service/BoxRepository.java @@ -2,6 +2,7 @@ import cz.sparko.boxitory.domain.Box; +import java.util.List; import java.util.Optional; public interface BoxRepository { @@ -12,4 +13,6 @@ public interface BoxRepository { * @return {@link Box} when found, {@link Optional#empty()} when not found */ Optional getBox(String boxName); + + List getBoxes(); } diff --git a/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java b/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java index 3132f92..8a645c3 100644 --- a/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java +++ b/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java @@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory; import java.io.File; +import java.io.FileFilter; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -30,6 +31,13 @@ public FilesystemBoxRepository(AppProperties appProperties) { LOG.info("setting BOX_HOME as [{}] and HOST_PREFIX as [{}]", boxHome.getAbsolutePath(), hostPrefix); } + @Override + public List getBoxes() { + return Arrays.stream(boxHome.listFiles(File::isDirectory)) + .map(File::getName) + .collect(Collectors.toList()); + } + @Override public Optional getBox(String boxName) { Map> groupedBoxFiles = new HashMap<>(); From f55f9b2cbde4a1b9e7f1fc1e738319f19e09772f Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Fri, 7 Jul 2017 10:55:35 +0200 Subject: [PATCH 2/6] really simple index html, some cleanup --- pom.xml | 4 ++++ .../boxitory/controller/IndexController.java | 5 ++++- .../boxitory/service/FilesystemBoxRepository.java | 15 ++++++++------- src/main/resources/templates/index.html | 12 ++++++++++++ 4 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 src/main/resources/templates/index.html diff --git a/pom.xml b/pom.xml index df82e78..d83945d 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-thymeleaf + org.springframework.boot spring-boot-configuration-processor diff --git a/src/main/java/cz/sparko/boxitory/controller/IndexController.java b/src/main/java/cz/sparko/boxitory/controller/IndexController.java index e79f6ac..9ac02d2 100644 --- a/src/main/java/cz/sparko/boxitory/controller/IndexController.java +++ b/src/main/java/cz/sparko/boxitory/controller/IndexController.java @@ -3,8 +3,10 @@ import cz.sparko.boxitory.service.BoxRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Controller public class IndexController { @@ -16,7 +18,8 @@ public IndexController(BoxRepository boxRepository) { } @RequestMapping(value = "/", method = RequestMethod.GET) - public String getBoxes() { + public String getBoxes(Model model) { + model.addAttribute("boxes", boxRepository.getBoxes()); return "index"; } } diff --git a/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java b/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java index 8a645c3..60dc032 100644 --- a/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java +++ b/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java @@ -8,14 +8,14 @@ import org.slf4j.LoggerFactory; import java.io.File; -import java.io.FileFilter; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static java.lang.Integer.compare; -import static java.lang.Integer.parseInt; -import static java.util.Comparator.comparingInt; public class FilesystemBoxRepository implements BoxRepository { private static final Logger LOG = LoggerFactory.getLogger(FilesystemBoxRepository.class); @@ -35,6 +35,7 @@ public FilesystemBoxRepository(AppProperties appProperties) { public List getBoxes() { return Arrays.stream(boxHome.listFiles(File::isDirectory)) .map(File::getName) + .sorted() .collect(Collectors.toList()); } diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 0000000..f951981 --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,12 @@ + + + + + + +

Available boxes

+
    +
  • +
+ + \ No newline at end of file From 21d4bf1b50c7d1a5e346da1fca9ba7345cc1036b Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Fri, 7 Jul 2017 11:09:58 +0200 Subject: [PATCH 3/6] some javadoc, improved NotFoundException message --- .../java/cz/sparko/boxitory/controller/BoxController.java | 2 +- src/main/java/cz/sparko/boxitory/domain/Box.java | 3 +++ src/main/java/cz/sparko/boxitory/service/BoxRepository.java | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/cz/sparko/boxitory/controller/BoxController.java b/src/main/java/cz/sparko/boxitory/controller/BoxController.java index 1eed035..5c0c929 100644 --- a/src/main/java/cz/sparko/boxitory/controller/BoxController.java +++ b/src/main/java/cz/sparko/boxitory/controller/BoxController.java @@ -21,6 +21,6 @@ public BoxController(BoxRepository boxRepository) { @ResponseBody public Box getBoxes(@PathVariable String boxName) { return boxRepository.getBox(boxName) - .orElseThrow(() -> new NotFoundException("[" + boxName + "] does not exist")); + .orElseThrow(() -> new NotFoundException("box [" + boxName + "] does not exist")); } } diff --git a/src/main/java/cz/sparko/boxitory/domain/Box.java b/src/main/java/cz/sparko/boxitory/domain/Box.java index d4c686f..75a670b 100644 --- a/src/main/java/cz/sparko/boxitory/domain/Box.java +++ b/src/main/java/cz/sparko/boxitory/domain/Box.java @@ -3,6 +3,9 @@ import java.util.List; import java.util.Objects; +/** + * Full description of Vagrant's box as needed in http API. + */ public class Box { private final String name; private final String description; diff --git a/src/main/java/cz/sparko/boxitory/service/BoxRepository.java b/src/main/java/cz/sparko/boxitory/service/BoxRepository.java index 93bdac4..4171661 100644 --- a/src/main/java/cz/sparko/boxitory/service/BoxRepository.java +++ b/src/main/java/cz/sparko/boxitory/service/BoxRepository.java @@ -14,5 +14,11 @@ public interface BoxRepository { */ Optional getBox(String boxName); + /** + * Returns {@link List} of names of available {@link Box}es. Call {@link BoxRepository#getBox(String)} with any of + * returned name should get valid result. + * + * @return names of available {@link Box}es + */ List getBoxes(); } From 8450dded2d9baf1d21420616922e15cd89def946 Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Tue, 11 Jul 2017 09:52:48 +0200 Subject: [PATCH 4/6] unused import --- src/main/java/cz/sparko/boxitory/controller/BoxController.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/cz/sparko/boxitory/controller/BoxController.java b/src/main/java/cz/sparko/boxitory/controller/BoxController.java index 5c0c929..ee759a9 100644 --- a/src/main/java/cz/sparko/boxitory/controller/BoxController.java +++ b/src/main/java/cz/sparko/boxitory/controller/BoxController.java @@ -6,8 +6,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import java.util.List; - @RestController public class BoxController { private BoxRepository boxRepository; From fffded4a654e3f31c917685891e6db5c3316bf0e Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Tue, 11 Jul 2017 10:02:56 +0200 Subject: [PATCH 5/6] merged IndexController into BoxController --- .../boxitory/controller/BoxController.java | 12 +++++++-- .../boxitory/controller/IndexController.java | 25 ------------------- 2 files changed, 10 insertions(+), 27 deletions(-) delete mode 100644 src/main/java/cz/sparko/boxitory/controller/IndexController.java diff --git a/src/main/java/cz/sparko/boxitory/controller/BoxController.java b/src/main/java/cz/sparko/boxitory/controller/BoxController.java index ee759a9..977a939 100644 --- a/src/main/java/cz/sparko/boxitory/controller/BoxController.java +++ b/src/main/java/cz/sparko/boxitory/controller/BoxController.java @@ -4,9 +4,11 @@ import cz.sparko.boxitory.domain.Box; import cz.sparko.boxitory.service.BoxRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; -@RestController +@Controller public class BoxController { private BoxRepository boxRepository; @@ -17,8 +19,14 @@ public BoxController(BoxRepository boxRepository) { @RequestMapping(value = "/{boxName}", method = RequestMethod.GET) @ResponseBody - public Box getBoxes(@PathVariable String boxName) { + public Box box(@PathVariable String boxName) { return boxRepository.getBox(boxName) .orElseThrow(() -> new NotFoundException("box [" + boxName + "] does not exist")); } + + @RequestMapping(value = "/", method = RequestMethod.GET) + public String index(Model model) { + model.addAttribute("boxes", boxRepository.getBoxes()); + return "index"; + } } diff --git a/src/main/java/cz/sparko/boxitory/controller/IndexController.java b/src/main/java/cz/sparko/boxitory/controller/IndexController.java deleted file mode 100644 index 9ac02d2..0000000 --- a/src/main/java/cz/sparko/boxitory/controller/IndexController.java +++ /dev/null @@ -1,25 +0,0 @@ -package cz.sparko.boxitory.controller; - -import cz.sparko.boxitory.service.BoxRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; - -@Controller -public class IndexController { - private BoxRepository boxRepository; - - @Autowired - public IndexController(BoxRepository boxRepository) { - this.boxRepository = boxRepository; - } - - @RequestMapping(value = "/", method = RequestMethod.GET) - public String getBoxes(Model model) { - model.addAttribute("boxes", boxRepository.getBoxes()); - return "index"; - } -} From f8342af65c2a47845bb6cad0950f7e6f433e30ed Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Tue, 11 Jul 2017 10:15:23 +0200 Subject: [PATCH 6/6] test index for FilesystemBoxRepository --- .../boxitory/service/FilesystemBoxRepositoryTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/java/cz/sparko/boxitory/service/FilesystemBoxRepositoryTest.java b/src/test/java/cz/sparko/boxitory/service/FilesystemBoxRepositoryTest.java index 1eaba51..1c8022c 100644 --- a/src/test/java/cz/sparko/boxitory/service/FilesystemBoxRepositoryTest.java +++ b/src/test/java/cz/sparko/boxitory/service/FilesystemBoxRepositoryTest.java @@ -19,6 +19,7 @@ import java.util.Optional; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; @SpringBootTest public class FilesystemBoxRepositoryTest { @@ -162,6 +163,14 @@ public void givenSortDescending_whenGetBox_thenVersionsSortedDesc() { assertEquals(versions.get(2).getVersion(), "1"); } + @Test + public void givenValidRepositoryWithBoxes_whenIndex_thenGetValidBoxes() { + BoxRepository boxRepository = new FilesystemBoxRepository(testAppProperties, new NoopHashService()); + + List boxes = boxRepository.getBoxes(); + assertTrue(boxes.containsAll(Arrays.asList("f25", "f26", "f27", "f28", "f29"))); + } + private String composePath(String boxName, String version, String provider) { return String.format("%s%s/%s/%s_%s_%s.box", TEST_BOX_PREFIX, testHomeDir.getAbsolutePath(), boxName, boxName, version, provider);