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 controller endpoint functionality. #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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,49 @@
// package com.liatrio.dojo.devopsknowledgeshareapi;

// import io.restassured.path.json.JsonPath;
// import io.restassured.response.Response;
// import org.junit.jupiter.api.*;
// import io.restassured.specification.RequestSpecification;
// import io.restassured.builder.RequestSpecBuilder;
// import io.restassured.filter.log.RequestLoggingFilter;
// import io.restassured.filter.log.ResponseLoggingFilter;
// import io.restassured.filter.log.ErrorLoggingFilter;
// import org.springframework.boot.test.context.SpringBootTest;

// import static io.restassured.RestAssured.given;
// import static org.junit.jupiter.api.Assertions.assertEquals;

// @SpringBootTest
// @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
// public class InfoControllerTest {

// private static RequestSpecification request;

// @BeforeAll
// public static void setup() {
// request = new RequestSpecBuilder()
// .setBaseUri("http://localhost:8080")
// .addFilter(new RequestLoggingFilter())
// .addFilter(new ResponseLoggingFilter())
// .addFilter(new ErrorLoggingFilter())
// .build();
// }

// /**
// * Scenario: 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
// */
// @Test
// @Order(1)
// public void userVisitsInfoPage() {
// Response response = given().spec(request).get("/info");
// assertEquals(200, response.getStatusCode());

// JsonPath jsonPath = new JsonPath(response.asString());
// String message = jsonPath.getString("message");
// assertEquals("Welcome to the API version 1.0", message);
// }
// }
28 changes: 28 additions & 0 deletions src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.liatrio.dojo.devopsknowledgeshareapi;

public class Author {
private Long id;
private String name;

public Author(Long id, String name) {
this.id = id;
this.name = name;
}

// Getters and Setters
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.liatrio.dojo.devopsknowledgeshareapi;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Arrays;

@RestController
public class AuthorController {

@GetMapping("/authors")
public List<Author> getAuthors() {
Author author1 = new Author(1L, "Author One");
Author author2 = new Author(2L, "Author Two");
return Arrays.asList(author1, author2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.liatrio.dojo.devopsknowledgeshareapi;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class InfoController {

@GetMapping(value = "/info", produces = "application/json")
public Map<String, String> getInfo() {
Map<String, String> response = new HashMap<>();
response.put("message", "Welcome to the API version 1.0");
return response;
}
}
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 @@ -26,6 +26,17 @@ public class Post {
private @NonNull String imageUrl;
private @NonNull Date dateAsDate;

private String dateUpdated;

public void setDateUpdated(Date dateAsDate) {
DateFormat dateFormat = new SimpleDateFormat(dateFormat());
this.dateUpdated = dateFormat.format(dateAsDate);
}

public String getDateUpdated() {
return dateUpdated;
}

public Post() {
this.dateAsDate = Calendar.getInstance().getTime();
setDatePosted(dateAsDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.Collection;
import java.util.stream.Collectors;

import java.util.List;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@Slf4j
Expand All @@ -25,12 +27,52 @@ public Collection<Post> posts() {
return repository.findAll().stream().collect(Collectors.toList());
}

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

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

@GetMapping("/posts/link")
public List<Post> getPostsByLink(@RequestParam String link) {
log.info("{}: received a GET request for posts by link", deploymentType);
return repository.findByLink(link);
}

@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") Long id, @RequestBody Post updatedPost) throws Exception {
log.info("{}: recieved a PUT request", deploymentType);
return repository.findById(id)
.map(post -> {
post.setFirstName(updatedPost.getFirstName());
post.setTitle(updatedPost.getTitle());
try {
post.setLink(updatedPost.getLink());
} catch (Exception e) {
e.printStackTrace();
}
post.setDatePosted(updatedPost.getDateAsDate());
post.setImageUrl(updatedPost.getImageUrl());
return repository.save(post);
})
.orElseGet(() -> {
updatedPost.setId(id);
return repository.save(updatedPost);
});
}

@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
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> findByTitle(String title);
List<Post> findByFirstName(String firstName);
List<Post> findByLink(String link);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.liatrio.dojo.devopsknowledgeshareapi;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
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.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;

@ExtendWith(MockitoExtension.class)
@SpringBootTest
@AutoConfigureMockMvc()
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class AuthorControllerTest {
@Autowired
private MockMvc mockMvc;

@InjectMocks
AuthorController mockAuthorController;

@BeforeAll
public void setup() throws Exception {
this.mockMvc = standaloneSetup(mockAuthorController).build();
}

@Test
public void getAuthorsResponse() throws Exception {
this.mockMvc.perform(get("/authors"))
.andDo(print())
.andExpect(status().isOk());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.text.SimpleDateFormat;
import java.util.Date;

@ExtendWith(SpringExtension.class)
public class PostTest {
Expand Down Expand Up @@ -106,4 +108,22 @@ 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 SimpleDateFormat("yyyy-MM-dd").parse("2023-10-01");
hc.setDateUpdated(date);
String expectedDate = new SimpleDateFormat(hc.dateFormat()).format(date);
assertEquals(expectedDate, hc.getDateUpdated());
}

@Test
public void getDateUpdatedTest() throws Exception {
Post hc = new Post();
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2023-10-01");
hc.setDateUpdated(date);
String expectedDate = new SimpleDateFormat(hc.dateFormat()).format(date);
assertEquals(expectedDate, hc.getDateUpdated());
}
}
Loading