From 9c04a024c9f75c39fe4d0d8d98f2f7aa6bffb6c0 Mon Sep 17 00:00:00 2001 From: Sugui Date: Mon, 16 Dec 2024 19:03:14 +0100 Subject: [PATCH] feat: added image endpoint basic functionality --- .../siesta/twochi_verifier_be/Controller.java | 23 ++++++++ .../twochi_verifier_be/ImageRepository.java | 7 +++ .../ImageRetrieverService.java | 21 +++++++ .../twochi_verifier_be/TokenGenerator.java | 24 ++++++++ .../twochi_verifier_be/rest/HomeResource.java | 15 ----- src/main/resources/application.yml | 2 + .../ImageRetrieverTest.java | 55 +++++++++++++++++++ 7 files changed, 132 insertions(+), 15 deletions(-) create mode 100644 src/main/java/cat/siesta/twochi_verifier_be/Controller.java create mode 100644 src/main/java/cat/siesta/twochi_verifier_be/ImageRepository.java create mode 100644 src/main/java/cat/siesta/twochi_verifier_be/ImageRetrieverService.java create mode 100644 src/main/java/cat/siesta/twochi_verifier_be/TokenGenerator.java delete mode 100644 src/main/java/cat/siesta/twochi_verifier_be/rest/HomeResource.java create mode 100644 src/test/java/cat/siesta/twochi_verifier_be/ImageRetrieverTest.java diff --git a/src/main/java/cat/siesta/twochi_verifier_be/Controller.java b/src/main/java/cat/siesta/twochi_verifier_be/Controller.java new file mode 100644 index 0000000..0d0eebb --- /dev/null +++ b/src/main/java/cat/siesta/twochi_verifier_be/Controller.java @@ -0,0 +1,23 @@ +package cat.siesta.twochi_verifier_be; + +import java.util.Map; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +import lombok.AllArgsConstructor; + +@RestController +@AllArgsConstructor +public class Controller { + private ImageRetrieverService imageRetrieverService; + + @GetMapping("/image") + @ResponseStatus(HttpStatus.OK) + public Map image() { + var imageAndToken = imageRetrieverService.getImage(); + return Map.of("url", imageAndToken.getLeft(), "token", imageAndToken.getRight()); + } +} diff --git a/src/main/java/cat/siesta/twochi_verifier_be/ImageRepository.java b/src/main/java/cat/siesta/twochi_verifier_be/ImageRepository.java new file mode 100644 index 0000000..30fd13d --- /dev/null +++ b/src/main/java/cat/siesta/twochi_verifier_be/ImageRepository.java @@ -0,0 +1,7 @@ +package cat.siesta.twochi_verifier_be; + +import java.net.URL; + +public interface ImageRepository { + public URL getImage(); +} diff --git a/src/main/java/cat/siesta/twochi_verifier_be/ImageRetrieverService.java b/src/main/java/cat/siesta/twochi_verifier_be/ImageRetrieverService.java new file mode 100644 index 0000000..0497e5c --- /dev/null +++ b/src/main/java/cat/siesta/twochi_verifier_be/ImageRetrieverService.java @@ -0,0 +1,21 @@ +package cat.siesta.twochi_verifier_be; + +import java.net.URL; + +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.stereotype.Service; + +import lombok.AllArgsConstructor; + +@Service +@AllArgsConstructor +public class ImageRetrieverService { + private final TokenGenerator tokenGenerator; + private final ImageRepository imageRepository; + + public Pair getImage() { + var url = imageRepository.getImage(); + return Pair.of(url, tokenGenerator.generate(url.toString())); + } + +} diff --git a/src/main/java/cat/siesta/twochi_verifier_be/TokenGenerator.java b/src/main/java/cat/siesta/twochi_verifier_be/TokenGenerator.java new file mode 100644 index 0000000..78442c6 --- /dev/null +++ b/src/main/java/cat/siesta/twochi_verifier_be/TokenGenerator.java @@ -0,0 +1,24 @@ +package cat.siesta.twochi_verifier_be; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.ToString; + +@EqualsAndHashCode +@ToString +@Getter +@Component +public class TokenGenerator { + private final String secret; + + public TokenGenerator(@Value("${twochi.secret}") String secret) { + this.secret = secret; + } + + public String generate(String url) { + return url + "\n" + secret; + } +} diff --git a/src/main/java/cat/siesta/twochi_verifier_be/rest/HomeResource.java b/src/main/java/cat/siesta/twochi_verifier_be/rest/HomeResource.java deleted file mode 100644 index f61733e..0000000 --- a/src/main/java/cat/siesta/twochi_verifier_be/rest/HomeResource.java +++ /dev/null @@ -1,15 +0,0 @@ -package cat.siesta.twochi_verifier_be.rest; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - - -@RestController -public class HomeResource { - - @GetMapping("/") - public String index() { - return "\"Hello World!\""; - } - -} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 860f067..7499cbf 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -11,3 +11,5 @@ spring: lifecycle-management: start-only springdoc: pathsToMatch: / +twochi: + secret: ${TWOCHI_SECRET} diff --git a/src/test/java/cat/siesta/twochi_verifier_be/ImageRetrieverTest.java b/src/test/java/cat/siesta/twochi_verifier_be/ImageRetrieverTest.java new file mode 100644 index 0000000..db373fe --- /dev/null +++ b/src/test/java/cat/siesta/twochi_verifier_be/ImageRetrieverTest.java @@ -0,0 +1,55 @@ +package cat.siesta.twochi_verifier_be; + +import static org.mockito.Mockito.when; + +import java.net.URI; +import java.net.URL; + +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.bean.override.mockito.MockitoBean; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +@SpringBootTest(properties = { + "twochi.secret = mysecret" +}) +@AutoConfigureMockMvc +public class ImageRetrieverTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private TokenGenerator tokenGenerator; + + @MockitoBean + private ImageRepository imageRepository; + + @Test + void retrieves_image_with_valid_token() throws Exception { + var url = randomUrl(); + var expectedToken = tokenGenerator.generate(url.toString()); + var expectedJson = String.format("{'url': '%s', 'token': '%s'}", url, expectedToken); + when(imageRepository.getImage()).thenReturn(url); + + this.mockMvc.perform(MockMvcRequestBuilders.get("/image")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.content().json(expectedJson)); + } + + private URL randomUrl() { + try { + return URI + .create(String.format("https://%s.com", RandomStringUtils.insecure().nextAlphabetic(10))).toURL(); + } catch (Exception e) { + throw new RuntimeException("failed to create random url"); + } + } +}