From 95fc3a727ffbeac7d93c7bae2cc89f70ce458f0f Mon Sep 17 00:00:00 2001 From: mitchell-liatrio Date: Tue, 30 Jul 2024 10:44:46 -0500 Subject: [PATCH] feat: Add dateUpdated field to Post model and create InfoController --- .../InfoController.java | 20 ++++++++ .../dojo/devopsknowledgeshareapi/Post.java | 11 +++++ .../PostController.java | 15 +++++- .../PostRepository.java | 4 ++ .../InfoControllerTest.java | 46 ++++++++++++++++++ .../devopsknowledgeshareapi/PostTest.java | 47 ++++++++++++++++++- 6 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/InfoController.java create mode 100644 src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/InfoControllerTest.java diff --git a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/InfoController.java b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/InfoController.java new file mode 100644 index 0000000..f790a08 --- /dev/null +++ b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/InfoController.java @@ -0,0 +1,20 @@ +package com.liatrio.dojo.devopsknowledgeshareapi; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.CrossOrigin; +import lombok.extern.slf4j.Slf4j; + +@CrossOrigin(origins = "*", maxAge = 3600) +@RestController +@Slf4j +public class InfoController { + + private String deploymentType = System.getenv("DEPLOYMENT_TYPE") != null ? System.getenv("DEPLOYMENT_TYPE") : "blue"; + + @GetMapping("/info") + public String getInfo() { + log.info("{}: received a GET request for /info", deploymentType); + return "Welcome to the API version 1.0"; + } +} \ No newline at end of file diff --git a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/Post.java b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/Post.java index a7429f2..d9d4844 100644 --- a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/Post.java +++ b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/Post.java @@ -92,4 +92,15 @@ public boolean validatePostLink(String postLink) { String pattern = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; return postLink.matches(pattern); } + + private String dateUpdated; + + public void setDateUpdated(Date dateAsDate) { + DateFormat dateFormat = new SimpleDateFormat(dateFormat()); + this.dateUpdated = dateFormat.format(dateAsDate); + } + + public String getDateUpdated() { + return dateUpdated; + } } diff --git a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostController.java b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostController.java index 1fb7569..462e351 100644 --- a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostController.java +++ b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostController.java @@ -21,16 +21,29 @@ public PostController(PostRepository repository) { @GetMapping("/posts") public Collection posts() { - log.info("{}: recieved a GET request", deploymentType); + log.info("{}: received a GET request", deploymentType); return repository.findAll().stream().collect(Collectors.toList()); } + @GetMapping("/posts/title/{title}") + public Collection getPostsByTitle(@PathVariable("title") String title) { + log.info("{}: received a GET request for posts with title: {}", deploymentType, title); + return repository.findByTitle(title); + } + @PostMapping("/posts") public Post post(@RequestBody Post post, HttpServletResponse resp) { log.info("{}: recieved a POST request", deploymentType); return repository.save(post); } + @PutMapping("/posts/{id}") + public Post putPost(@PathVariable("id") String id, @RequestBody Post post) { + log.info("{}: received a PUT request", deploymentType); + post.setId(Long.parseLong(id)); + return repository.save(post); + } + @DeleteMapping("/posts/{id}") public void deletePost(@PathVariable("id") String id) { log.info("{}: recieved a DELETE request", deploymentType); diff --git a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostRepository.java b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostRepository.java index 1b28be7..0688720 100644 --- a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostRepository.java +++ b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostRepository.java @@ -1,5 +1,7 @@ package com.liatrio.dojo.devopsknowledgeshareapi; +import java.util.Collection; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; import org.springframework.stereotype.Repository; @@ -7,4 +9,6 @@ @RepositoryRestResource @Repository interface PostRepository extends JpaRepository { + + Collection findByTitle(String title); } diff --git a/src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/InfoControllerTest.java b/src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/InfoControllerTest.java new file mode 100644 index 0000000..e2f5881 --- /dev/null +++ b/src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/InfoControllerTest.java @@ -0,0 +1,46 @@ +package com.liatrio.dojo.devopsknowledgeshareapi; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +public class InfoControllerTest { + // User visits the /info URL of the service + // Given the user is not logged in + // When the user visits the /info page + // Then the user should see the phrase "Welcome to the API version 1.0"" + // And they should not see an error message + + @WebMvcTest(InfoController.class) + public class InfoControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void testInfoPageNotLoggedIn() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/info")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.content().string("Welcome to the API version 1.0")) + .andExpect(MockMvcResultMatchers.content().string(Matchers.not(Matchers.containsString("error")))); + } + + @Test + public void testInfoPageLoggedIn() throws Exception { + // Additional test case for when the user is logged in + // Implement the test logic here + } + + @Test + public void testInfoPageError() throws Exception { + // Additional test case for when an error occurs on the /info page + // Implement the test logic here + } + + // Add more test cases as needed + + } +} diff --git a/src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/PostTest.java b/src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/PostTest.java index df33dd8..85665d4 100644 --- a/src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/PostTest.java +++ b/src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/PostTest.java @@ -1,7 +1,13 @@ package com.liatrio.dojo.devopsknowledgeshareapi; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Date; +import java.text.DateFormat; +import java.text.SimpleDateFormat; + import static org.junit.jupiter.api.Assertions.assertNotEquals; import org.junit.jupiter.api.Test; @@ -17,7 +23,6 @@ public class PostTest { String link = "https://devops.com/blog/post-1"; String imageUrl = "https://devops.com/images/image1.png"; - @Test public void getIdTest() throws Exception { Post hc = new Post(); @@ -106,4 +111,42 @@ public void getImageUrlTest() throws Exception { String test = hc.getImageUrl(); assertEquals(imageUrl, test); } -} + + @Test + public void setDateUpdatedTest() throws Exception { + Post hc = new Post(); + Date date = new Date(); + hc.setDateUpdated(date); + String test = hc.getDateUpdated(); + DateFormat dateFormat = new SimpleDateFormat(hc.dateFormat()); + String expected = dateFormat.format(date); + assertEquals(expected, test); + } + + @Test + public void getDateUpdatedTest() throws Exception { + Post hc = new Post(); + Date date = new Date(); + hc.setDateUpdated(date); + String test = hc.getDateUpdated(); + DateFormat dateFormat = new SimpleDateFormat(hc.dateFormat()); + String expected = dateFormat.format(date); + assertEquals(expected, test); + } + + @Test + public void validatePostLinkValidTest() { + Post hc = new Post(); + String validLink = "https://example.com"; + boolean result = hc.validatePostLink(validLink); + assertTrue(result); + } + + @Test + public void validatePostLinkInvalidTest() { + Post hc = new Post(); + String invalidLink = "example.com"; + boolean result = hc.validatePostLink(invalidLink); + assertFalse(result); + } +} \ No newline at end of file