PAN-52 Finished creating the ProofController and writing all unit tests

This commit is contained in:
atusa
2019-03-15 15:58:23 -06:00
parent bd7455c679
commit 4b866017a1
7 changed files with 333 additions and 10 deletions
@@ -2,13 +2,18 @@ package edu.msudenver.tsp.persistence.controller;
import edu.msudenver.tsp.persistence.dto.ProofDto;
import edu.msudenver.tsp.persistence.repository.ProofRepository;
import edu.msudenver.tsp.utilities.PersistenceUtilities;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StopWatch;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.groups.Default;
import java.util.List;
import java.util.Optional;
@@ -97,4 +102,140 @@ public class ProofController {
LOG.info("Returning list of proofs with branch {}", branch);
return new ResponseEntity<>(listOfProofs, HttpStatus.OK);
}
@GetMapping("/{theorem_name}")
public @ResponseBody
ResponseEntity<List<ProofDto>> getAllProofsByTheoremName(@PathVariable("theorem_name") final String theoremName) {
LOG.info("Received request to query for proofs of the theorem {}", theoremName);
if (theoremName == null) {
LOG.error("ERROR: theorem name was null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Querying for proofs of the theorem {}", theoremName);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final List<ProofDto> listOfProofs = proofRepository.findByTheoremName(theoremName);
stopWatch.stop();
LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
LOG.info("Returning list of all proofs with size " + listOfProofs.size());
if (listOfProofs.isEmpty()) {
LOG.warn("No proofs were found of the theorem {}", theoremName);
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
LOG.info("Returning list of proofs for the theorem {}", theoremName);
return new ResponseEntity<>(listOfProofs, HttpStatus.OK);
}
@PostMapping("/")
@Validated({ProofDto.Insert.class, Default.class})
public @ResponseBody ResponseEntity<ProofDto> insertProof(
@Valid @RequestBody final ProofDto proofDto,
final BindingResult bindingResult) {
LOG.info("Received request to insert a new proof");
if (bindingResult.hasErrors()) {
LOG.error("Binding result is unprocessable");
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
}
if (proofDto == null) {
LOG.error("Passed entity is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Saving new proof");
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final ProofDto savedProof = proofRepository.save(proofDto);
stopWatch.stop();
LOG.debug("Received response from server: query took {}ms to complete", stopWatch.getTotalTimeMillis());
LOG.info("Returning the newly created proof with id {}", savedProof.getId());
return new ResponseEntity<>(savedProof, HttpStatus.CREATED);
}
@PatchMapping("/{id}")
public @ResponseBody ResponseEntity<ProofDto> updateProof(
@PathVariable("id") final Integer id,
@RequestBody final ProofDto proofDto, final BindingResult bindingResult) {
LOG.info("Received request to update a proof");
if (bindingResult.hasErrors()) {
LOG.error("Binding result is unprocessable");
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
}
if (proofDto == null) {
LOG.error("Passed entity is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
if (id == null) {
LOG.error("Proof ID must be specified");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Checking for existence of proof with id " + id);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final Optional<ProofDto> existingProof = proofRepository.findById(id);
stopWatch.stop();
LOG.debug("Received response from server: query took {}ms to complete", stopWatch.getTotalTimeMillis());
if (!existingProof.isPresent()) {
LOG.error("No proof associated with id {}", id);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
PersistenceUtilities.copyNonNullProperties(proofDto, existingProof.get());
existingProof.get().setVersion(existingProof.get().getVersion()+ 1);
LOG.info("Updating proof with id {}", id);
LOG.debug("Querying for proof with ID {}", id);
stopWatch.start();
final ProofDto updatedProof = proofRepository.save(existingProof.get());
stopWatch.stop();
LOG.debug("Received response from server: query took {}ms to complete", stopWatch.getTotalTimeMillis());
return new ResponseEntity<>(updatedProof, HttpStatus.OK);
}
@DeleteMapping("/{id}")
public @ResponseBody ResponseEntity<Void> deleteProofById(@PathVariable("id") final Integer id) {
LOG.info("Received request to delete proof with id {}", id);
if (id == null) {
LOG.error("Specified id is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Deleting proof with id {}", id);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
proofRepository.deleteById(id);
stopWatch.stop();
LOG.debug("Received response from server: query took {}ms to complete", stopWatch.getTotalTimeMillis());
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
@@ -9,9 +9,11 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StopWatch;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.groups.Default;
import java.util.List;
import java.util.Optional;
@@ -162,6 +164,7 @@ public class TheoremController {
}
@PostMapping("/")
@Validated({TheoremDto.Insert.class, Default.class})
public @ResponseBody ResponseEntity<TheoremDto> insertTheorem(
@Valid @RequestBody final TheoremDto theoremDto,
final BindingResult bindingResult) {
@@ -195,7 +198,7 @@ public class TheoremController {
@PathVariable("id") final Integer id,
@RequestBody final TheoremDto theoremDto, final BindingResult bindingResult) {
LOG.info("Received request to update an account");
LOG.info("Received request to update a theorem");
if (bindingResult.hasErrors()) {
LOG.error("Binding result is unprocessable");
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
@@ -20,7 +20,8 @@ import java.util.List;
public class ProofDto extends BaseDto implements Serializable {
@NotBlank(groups = Insert.class)
@Size(min = 1, max = 512, message = "The name must be at least 1 character and at most 512 characters")
private String name;
@Column(name = "theorem_name")
private String theoremName;
@NotBlank(groups = Insert.class)
@Size(min = 1, max = 512, message = "The branch must be at least 1 character and at most 512 characters")
private String branch;
@@ -29,6 +30,15 @@ public class ProofDto extends BaseDto implements Serializable {
@Temporal(TemporalType.DATE) @Column(name = "date_created") private Date dateCreated;
@Temporal(TemporalType.DATE) @Column(name = "last_updated") private Date lastUpdated;
@JsonProperty("theorem_name")
public String getTheoremName() {
return theoremName;
}
@JsonProperty("theorem_name")
public void setTheoremName(final String theoremName) {
this.theoremName = theoremName;
}
@JsonProperty("referenced_definitions")
public List<String> getReferencedDefinitions() {
@@ -20,12 +20,12 @@ import java.util.List;
@Data
@EqualsAndHashCode(callSuper = true)
public class TheoremDto extends BaseDto implements Serializable {
@NotBlank @Size(min = 1, max = 512, message = "theorem name must be between 1 and 512 characters") private String name;
@NotNull @Column(name = "theorem_type") private TheoremType theoremType;
@NotNull(message = "a branch of mathematics that this theorem is associated with must be specified") private String branch;
@NotBlank(groups = Insert.class) @Size(min = 1, max = 512, message = "theorem name must be between 1 and 512 characters") private String name;
@NotNull(groups = Insert.class) @Column(name = "theorem_type") private TheoremType theoremType;
@NotNull(groups = Insert.class, message = "a branch of mathematics that this theorem is associated with must be specified") private String branch;
@Type(type = "json") @Column(name = "referenced_definitions", columnDefinition = "jsonb") private List<String> referencedDefinitions;
@Type(type = "json") @Column(name = "referenced_theorems", columnDefinition = "jsonb") private List<String> referencedTheorems;
@NotNull @Column(name = "proven_status") private boolean provenStatus;
@NotNull(groups = Insert.class) @Column(name = "proven_status") private boolean provenStatus;
@JsonProperty("theorem_type")
public TheoremType getTheoremType() {
@@ -68,4 +68,7 @@ public class TheoremDto extends BaseDto implements Serializable {
}
private static final long serialVersionUID = 1545568391140364425L;
public interface Insert {}
}
@@ -9,5 +9,5 @@ public interface ProofRepository extends JpaRepository<ProofDto, Integer> {
List<ProofDto> findByBranch(String branch);
List<ProofDto> findByName(String name);
List<ProofDto> findByTheoremName(String name);
}