Skip to content

Commit

Permalink
Merge pull request #9 from sparkoo/index
Browse files Browse the repository at this point in the history
Index page
  • Loading branch information
sparkoo authored Jul 11, 2017
2 parents bcb2c61 + f8342af commit 2b372a3
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 6 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/cz/sparko/boxitory/controller/BoxController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +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
@RequestMapping(value = "/", method = RequestMethod.GET)
@Controller
public class BoxController {
private BoxRepository boxRepository;

Expand All @@ -16,10 +17,16 @@ public BoxController(BoxRepository boxRepository) {
this.boxRepository = boxRepository;
}

@RequestMapping("{boxName}")
@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("[" + boxName + "] does not exist"));
.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";
}
}
3 changes: 3 additions & 0 deletions src/main/java/cz/sparko/boxitory/domain/Box.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/cz/sparko/boxitory/service/BoxRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cz.sparko.boxitory.domain.Box;

import java.util.List;
import java.util.Optional;

public interface BoxRepository {
Expand All @@ -12,4 +13,12 @@ public interface BoxRepository {
* @return {@link Box} when found, {@link Optional#empty()} when not found
*/
Optional<Box> 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<String> getBoxes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
import org.slf4j.LoggerFactory;

import java.io.File;
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;

public class FilesystemBoxRepository implements BoxRepository {
Expand All @@ -27,6 +33,14 @@ public FilesystemBoxRepository(AppProperties appProperties, HashService hashServ
LOG.info("setting BOX_HOME as [{}] and HOST_PREFIX as [{}]", boxHome.getAbsolutePath(), hostPrefix);
}

@Override
public List<String> getBoxes() {
return Arrays.stream(boxHome.listFiles(File::isDirectory))
.map(File::getName)
.sorted()
.collect(Collectors.toList());
}

@Override
public Optional<Box> getBox(String boxName) {
Map<String, List<File>> groupedBoxFiles = new HashMap<>();
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text="Boxitory"></title>
</head>
<body>
<h2>Available boxes</h2>
<ul th:each="box : ${boxes}">
<li><a th:href="${box}" th:text="${box}"></a></li>
</ul>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Optional;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

@SpringBootTest
public class FilesystemBoxRepositoryTest {
Expand Down Expand Up @@ -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<String> 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);
Expand Down

0 comments on commit 2b372a3

Please sign in to comment.