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: added PUT operation to the API #23

Closed
wants to merge 1 commit into from
Closed
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,38 @@
package com.liatrio.dojo.devopsknowledgeshareapi.functional;

import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
// import org.springframework.test.annotation.DirtiesContext;
// import org.springframework.test.context.TestMethodOrder;
// import org.springframework.test.context.junit.jupiter.MethodOrderer;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;

import io.restassured.specification.RequestSpecification;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;

@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
public class ApplicationInfoTest {

/**
* Test case for verifying that the /info page shows the correct API version information
* without requiring the user to be logged in.
*/
@Test
@Order(1)
public void getInfoPageAsUnauthenticatedUser() {
// Declare and initialize the request variable
RequestSpecification request = given();

request
.when()
.get("/info")
.then()
.statusCode(200)
.body(containsString("Welcome to the API version 1.0"))
.body(not(containsString("error")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.liatrio.dojo.devopsknowledgeshareapi.functional;

import MethodOrderer.OrderAnnotation;

public @interface TestMethodOrder {

Class<org.junit.jupiter.api.MethodOrderer.OrderAnnotation> value();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.liatrio.dojo.devopsknowledgeshareapi;

import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AuthorController {

@Autowired
private AuthorService authorService;

@GetMapping("/authors")
public ResponseEntity<?> getAllAuthors() {
List<Author> authors = authorService.findAllAuthors();
if (authors.isEmpty()) {
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(authors);
}

@GetMapping("/authors/{id}")
public ResponseEntity<?> getAuthorById(@PathVariable Long id) {
Optional<Author> author = authorService.findAuthorById(id);
return author.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
}
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
@@ -1,10 +1,13 @@
package com.liatrio.dojo.devopsknowledgeshareapi;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.Date;
import java.util.stream.Collectors;

@CrossOrigin(origins = "*", maxAge = 3600)
Expand Down Expand Up @@ -36,4 +39,42 @@ public void deletePost(@PathVariable("id") String id) {
log.info("{}: recieved a DELETE request", deploymentType);
repository.deleteById(Long.parseLong(id));
}

@PutMapping("/posts/{id}")
public ResponseEntity<Post> putPost(@PathVariable Long id, @RequestBody Post updatedPost) {
return repository.findById(id)
.map(post -> {
post.setTitle(updatedPost.getTitle());
post.setFirstName(updatedPost.getFirstName());
try {
post.setLink(updatedPost.getLink());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
post.setImageUrl(updatedPost.getImageUrl());
post.setDatePosted(new Date()); // Assuming you want to update the datePosted to the current date
Post savedPost = repository.save(post);
return new ResponseEntity<>(savedPost, HttpStatus.OK);
})
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
}

@GetMapping("/posts/title/{title}")
public Collection<Post> getPostsByTitle(@PathVariable String title) {
log.info("{}: received a GET request for posts by title", deploymentType);
return repository.findByTitleContainingIgnoreCase(title);
}

@GetMapping("/posts/firstName/{firstName}")
public Collection<Post> getPostsByFirstName(@PathVariable String firstName) {
log.info("{}: received a GET request for posts by first name", deploymentType);
return repository.findByFirstNameContainingIgnoreCase(firstName);
}

@GetMapping("/posts/link/{link}")
public Collection<Post> getPostsByLink(@PathVariable String link) {
log.info("{}: received a GET request for posts by link", deploymentType);
return repository.findByLinkContainingIgnoreCase(link);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;

import java.util.List;

@RepositoryRestResource
@Repository
interface PostRepository extends JpaRepository<Post, Long> {
List<Post> findByTitleContainingIgnoreCase(String title);
List<Post> findByFirstNameContainingIgnoreCase(String firstName);
List<Post> findByLinkContainingIgnoreCase(String link);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.liatrio.dojo.devopsknowledgeshareapi;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@WebMvcTest(AuthorController.class)
public class AuthorControllerTest {

@Autowired
private MockMvc mockMvc;

// Existing test
@Test
public void testGetAllAuthors() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/authors"))
.andExpect(MockMvcResultMatchers.status().isOk());
}

// New test for the pseudo code comment
@Test
public void testGetAuthorById() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/authors/{id}", 1))
.andExpect(MockMvcResultMatchers.status().isOk());
}

// New test for the pseudo code comment
@Test
public void testGetAuthorByIdNotFound() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/authors/{id}", 999))
.andExpect(MockMvcResultMatchers.status().isNotFound());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.liatrio.dojo.devopsknowledgeshareapi;

public class DateFormat {

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
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 static org.junit.jupiter.api.Assertions.assertNotEquals;

import java.util.*;
import java.text.SimpleDateFormat;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
Expand Down Expand Up @@ -106,4 +109,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();
SimpleDateFormat 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();
SimpleDateFormat 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