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

Refactor ForTestingController #1047

Merged
merged 1 commit into from
Dec 5, 2024
Merged
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

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 = "/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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@
* Throws an exception
*/
throwException(onSuccess, onError) {
let endpoint = BASE_URL + "testing/" + "exception";
let endpoint = "/testing/" + "exception";
dataProvider.loadData(endpoint, onSuccess, onError);
},
};
Expand Down

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,18 +17,20 @@
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.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;

Expand All @@ -35,12 +40,10 @@ public class ErrorRestControllerTest {
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 +53,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 = "/testing/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"));
}
}
Loading