Skip to content

Commit

Permalink
feat: implement updatePost endpoint and modify datePosted handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchell-liatrio committed Nov 19, 2024
1 parent 2be3407 commit fc2eea1
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public String dateFormat() {
return "EE MM/dd/yyyy H:m:s:S z";
}

public void setDatePosted(Date dateAsDate) {
public void setDatePosted(String dateAsDate) {
DateFormat dateFormat = new SimpleDateFormat(dateFormat());
this.datePosted = dateFormat.format(dateAsDate);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.liatrio.dojo.devopsknowledgeshareapi;

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

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

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
Expand Down Expand Up @@ -36,4 +41,32 @@ 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> updatePost(@PathVariable("id") Long id, @RequestBody Post postDetails) {
log.info("{}: received a PUT request", deploymentType);
Optional<Post> optionalPost = repository.findById(id);
if (optionalPost.isPresent()) {
Post post = optionalPost.get();
post.setFirstName(postDetails.getFirstName());
post.setTitle(postDetails.getTitle());
try {
post.setLink(postDetails.getLink());
} catch (Exception e) {
return ResponseEntity.badRequest().body(null);
}
try {
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(postDetails.getDatePosted());
post.setDatePosted(date);
} catch (ParseException e) {
return ResponseEntity.badRequest().body(null);
}
post.setImageUrl(postDetails.getImageUrl());
post.setDateAsDate(postDetails.getDateAsDate());
final Post updatedPost = repository.save(post);
return ResponseEntity.ok(updatedPost);
} else {
return ResponseEntity.notFound().build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.any;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;

import java.util.Optional;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;

Expand Down Expand Up @@ -68,4 +73,69 @@ public void deletePostsFirstName() throws Exception {
.andDo(print()).andExpect(status().isOk());
this.mockMvc.perform(delete("/posts/1/").with(csrf())).andDo(print()).andExpect(status().isOk());
}

@Test
public void updatePostSuccess() throws Exception {
Post updatedPost = new Post();
updatedPost.setFirstName("Jane");
updatedPost.setTitle("Updated Post");
updatedPost.setLink("https://www.example.com/blog/updated-post");
updatedPost.setDatePosted("2023-10-10");
updatedPost.setImageUrl("https://www.example.com/images/updated-post.jpg");

when(mockPr.findById(1L)).thenReturn(Optional.of(new Post()));
when(mockPr.save(any(Post.class))).thenReturn(updatedPost);

JSONObject updatedPostJson = new JSONObject();
updatedPostJson.put("firstName", "Jane");
updatedPostJson.put("title", "Updated Post");
updatedPostJson.put("link", "https://www.example.com/blog/updated-post");
updatedPostJson.put("datePosted", "2023-10-10");
updatedPostJson.put("imageUrl", "https://www.example.com/images/updated-post.jpg");

this.mockMvc.perform(put("/posts/1")
.content(updatedPostJson.toString())
.with(csrf())
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk());
}

@Test
public void updatePostNotFound() throws Exception {
JSONObject updatedPostJson = new JSONObject();
updatedPostJson.put("firstName", "Jane");
updatedPostJson.put("title", "Updated Post");
updatedPostJson.put("link", "https://www.example.com/blog/updated-post");
updatedPostJson.put("datePosted", "2023-10-10");
updatedPostJson.put("imageUrl", "https://www.example.com/images/updated-post.jpg");

when(mockPr.findById(1L)).thenReturn(Optional.empty());

this.mockMvc.perform(put("/posts/1")
.content(updatedPostJson.toString())
.with(csrf())
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isNotFound());
}

@Test
public void updatePostBadRequest() throws Exception {
JSONObject updatedPostJson = new JSONObject();
updatedPostJson.put("firstName", "Jane");
updatedPostJson.put("title", "Updated Post");
updatedPostJson.put("link", "https://www.example.com/blog/updated-post");
updatedPostJson.put("datePosted", "invalid-date");
updatedPostJson.put("imageUrl", "https://www.example.com/images/updated-post.jpg");

when(mockPr.findById(1L)).thenReturn(Optional.of(new Post()));

this.mockMvc.perform(put("/posts/1")
.content(updatedPostJson.toString())
.with(csrf())
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isBadRequest());
}
}

0 comments on commit fc2eea1

Please sign in to comment.