Skip to content

Commit

Permalink
Refactor ForTestingController
Browse files Browse the repository at this point in the history
  • Loading branch information
hokwaichan committed Nov 30, 2024
1 parent 256114a commit ef8af2f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 124 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
package edu.hawaii.its.groupings.controller;

import java.util.Map;

import jakarta.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import edu.hawaii.its.groupings.exceptions.ExceptionForTesting;
import edu.hawaii.its.groupings.type.Feedback;

@RestController
public class ErrorRestController {

private static final Log logger = LogFactory.getLog(ErrorRestController.class);

@PreAuthorize("isAuthenticated()")
@PostMapping("/feedback/error")
public ResponseEntity<Void> feedbackError(@RequestBody Map<String, String> body, HttpSession session) {
Expand All @@ -29,4 +27,12 @@ public ResponseEntity<Void> feedbackError(@RequestBody Map<String, String> body,
session.setAttribute("feedback", new Feedback(exceptionMessage));
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}

@PreAuthorize("hasRole('ADMIN')")
@GetMapping(value = "/api/groupings/testing/exception")
public ResponseEntity<String> throwException() {
logger.info("Entered REST throwException...");

throw new ExceptionForTesting("Exception thrown intentionally");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package edu.hawaii.its.groupings.exceptions;

public class ExceptionForTesting extends RuntimeException {

public ExceptionForTesting(String message) {
super(message);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

import jakarta.servlet.http.HttpSession;

Expand All @@ -14,33 +17,39 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.web.context.WebApplicationContext;

import edu.hawaii.its.api.controller.GroupingsRestController;
import edu.hawaii.its.groupings.configuration.SpringBootWebApplication;
import edu.hawaii.its.groupings.type.Feedback;

@ActiveProfiles("localTest")
@SpringBootTest(classes = { SpringBootWebApplication.class })
public class ErrorRestControllerTest {

@Autowired
private ErrorRestController restController;

@Autowired
private WebApplicationContext context;

private MockMvc mockMvc;

private static final String REST_CONTROLLER_BASE = "/api/groupings/testing/";

@Autowired
GroupingsRestController groupingsRestController;

@BeforeEach
public void setUp() {
mockMvc = webAppContextSetup(context).build();
}

@Test
public void testConstruction() {
assertNotNull(restController);
}

@Test
public void httpPostFeedbackError() throws Exception {
HttpSession session = mockMvc.perform(post("/feedback/error")
Expand All @@ -50,11 +59,27 @@ public void httpPostFeedbackError() throws Exception {
.andReturn()
.getRequest()
.getSession();

assert session != null;
assertNotNull(session.getAttribute("feedback"));
Feedback feedback = (Feedback) session.getAttribute("feedback");
assertThat(feedback.getExceptionMessage(), is("exception"));
}

}
@Test
@WithMockUhUser(uid = "admin", roles = { "ROLE_UH", "ROLE_ADMIN" })
public void throwExceptionTest() throws Exception {
mockMvc = webAppContextSetup(context)
.apply(springSecurity())
.build();

String uri = REST_CONTROLLER_BASE + "exception";

MvcResult result = mockMvc.perform(get(uri))
.andExpect(status().isInternalServerError())
.andReturn();

String responseBody = result.getResponse().getContentAsString();
assertNotNull(responseBody);
assertThat(responseBody, containsString("Exception thrown intentionally"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package edu.hawaii.its.groupings.controller;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;
import edu.hawaii.its.groupings.exceptions.ExceptionForTesting;

public class ExceptionForTestingTest {

@Test
public void construction() {
ExceptionForTesting ex = new ExceptionForTesting("Test Exception");
assertNotNull(ex);
assertThat(ex.getMessage(), equalTo("Test Exception"));
}
}

0 comments on commit ef8af2f

Please sign in to comment.