Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add dateUpdated field to Post model and create InfoController #27

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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";
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,29 @@

@GetMapping("/posts")
public Collection<Post> 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<Post> getPostsByTitle(@PathVariable("title") String title) {
log.info("{}: received a GET request for posts with title: {}", deploymentType, title);

Check failure

Code scanning / CodeQL

Log Injection High

This log entry depends on a
user-provided value
.
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
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;

@RepositoryRestResource
@Repository
interface PostRepository extends JpaRepository<Post, Long> {

Collection<Post> findByTitle(String title);
}
Original file line number Diff line number Diff line change
@@ -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

}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}
Loading