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:
@@ -35,13 +35,15 @@ version int default 1
|
||||
);
|
||||
CREATE TABLE proofs
|
||||
(
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
name VARCHAR(512) NOT NULL,
|
||||
branch VARCHAR(512) NOT NULL,
|
||||
referenced_definitions JSON,
|
||||
referenced_theorems JSON,
|
||||
date_added DATE,
|
||||
last_updated DATE,
|
||||
version INT DEFAULT 1,
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
name VARCHAR(512) NOT NULL,
|
||||
branch VARCHAR(512) NOT NULL,
|
||||
theorem INT NOT NULL,
|
||||
FOREIGN KEY fk_theorem (theorem) REFERENCES theorems (id) ON DELETE NO ACTION ON UPDATE NO ACTION,
|
||||
referenced_definitions JSON,
|
||||
referenced_theorems JSON,
|
||||
date_added DATE,
|
||||
last_updated DATE,
|
||||
version INT DEFAULT 1,
|
||||
PRIMARY KEY (id)
|
||||
)
|
||||
);
|
||||
+30
@@ -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) {
|
||||
|
||||
+2
@@ -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);
|
||||
}
|
||||
|
||||
+40
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user