PAN-52 Added findByName to the TheoremRepository, and to the TheoremController as well as the corresponding tests, and a foreign key mapping from the proofs table to the corresponding theorem in the theorems table

This commit is contained in:
2019-03-14 12:28:59 -06:00
parent d560f5831f
commit 4d0658a268
4 changed files with 83 additions and 9 deletions
@@ -101,6 +101,36 @@ public class TheoremController {
return new ResponseEntity<>(listOfTheorems, HttpStatus.OK);
}
@GetMapping("/{name}")
public @ResponseBody
ResponseEntity<List<TheoremDto>> getAllTheoremsByName(@PathVariable("name") final String name) {
LOG.info("Received request to query for theorems whose name is " + name);
if (name == null) {
LOG.error("ERROR: name was null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Querying for theorems with name " + name);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final List<TheoremDto> listOfTheorems = theoremRepository.findByName(name);
stopWatch.stop();
LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
LOG.info("Returning list of all theorems with size " + listOfTheorems.size());
if (listOfTheorems.isEmpty()) {
LOG.warn("No theorems were found with name {}", name);
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
LOG.info("Returning list of theorems with name {}", name);
return new ResponseEntity<>(listOfTheorems, HttpStatus.OK);
}
@GetMapping("/{id}")
public @ResponseBody
ResponseEntity<TheoremDto> getTheoremById(@PathVariable("id") final Integer id) {
@@ -12,4 +12,6 @@ public interface TheoremRepository extends JpaRepository<TheoremDto, Integer> {
List<TheoremDto> findByBranch(String branch);
List<TheoremDto> findByProvenStatus(Boolean provenStatus);
List<TheoremDto> findByName(String name);
}
@@ -166,6 +166,46 @@ public class TheoremControllerTest {
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
}
@Test
public void testGetAllTheoremsByName() {
final TheoremDto theoremDto = createTheorem();
final List<TheoremDto> listOfTheorems = new ArrayList<>();
listOfTheorems.add(theoremDto);
listOfTheorems.add(theoremDto);
when(theoremRepository.findByName(anyString())).thenReturn(listOfTheorems);
final ResponseEntity<List<TheoremDto>> responseEntity = theoremController.getAllTheoremsByName("Test Theorem");
assertNotNull(responseEntity);
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
assertTrue(responseEntity.hasBody());
assertNotNull(responseEntity.getBody());
responseEntity.getBody().forEach(theorem -> assertEquals(theoremDto, theorem));
}
@Test
public void testGetAllTheoremsByName_nullName() {
final ResponseEntity<List<TheoremDto>> responseEntity = theoremController.getAllTheoremsByName(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
}
@Test
public void testGetAllTheoremsByName_noNameFound() {
when(theoremRepository.findByName(anyString())).thenReturn(Collections.emptyList());
final ResponseEntity<List<TheoremDto>> responseEntity = theoremController.getAllTheoremsByName("No name");
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
}
@Test
public void testInsertTheorem() {
final TheoremDto theoremDto = createTheorem();