Merge branch 'master' of https://github.com/atusa17/ptp into PAN-16

This commit is contained in:
-
2019-03-14 17:15:42 -06:00
12 changed files with 436 additions and 20 deletions
@@ -22,4 +22,14 @@ name varchar(200) not null,
definition json not null, definition json not null,
notation json, notation json,
version int default 1 version int default 1
); );
create table theorems (
id int not null auto_increment primary key unique,
name varchar(512) not null,
theorem_type enum ('THEOREM', 'PROPOSITION', 'LEMMA', 'COROLLARY') not null,
branch varchar(512) not null,
referenced_definitions json,
referenced_theorems json,
proven_status boolean default false,
version int default 1
)
@@ -56,7 +56,7 @@ public class PersistenceTestConfig {
.create() .create()
.username("panda") .username("panda")
.password("secret") .password("secret")
.url("jdbc:mysql://127.0.0.1:3306/pandamonium?autoReconnect=true&useSSL=false") .url("jdbc:mysql://127.0.0.1:3306/pandamonium?autoReconnect=true&useSSL=false&serverTimezone=UTC")
.driverClassName("com.mysql.cj.jdbc.Driver") .driverClassName("com.mysql.cj.jdbc.Driver")
.build(); .build();
} }
@@ -105,5 +105,6 @@ public class PersistenceApi {
properties.setProperty("hibernate.id.new_generator_mappings","false"); properties.setProperty("hibernate.id.new_generator_mappings","false");
return properties; return properties;
} }
} }
@@ -14,6 +14,8 @@ import org.springframework.web.bind.annotation.*;
import javax.validation.Valid; import javax.validation.Valid;
import javax.validation.groups.Default; import javax.validation.groups.Default;
import java.time.Duration;
import java.time.Instant;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@@ -73,6 +75,35 @@ public class AccountController {
); );
} }
@GetMapping("/{username}")
public @ResponseBody
ResponseEntity<AccountDto> getAccountByUsername(@PathVariable("username") final String username) {
LOG.info("Received request to query for account with username " + username);
if (username == null) {
LOG.error("ERROR: username was null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Querying for account with username " + username);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final Optional<AccountDto> account = accountsRepository.findByUsername(username);
stopWatch.stop();
LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
return account.map(accountDto -> {
LOG.info("Returning account with username " + username);
return new ResponseEntity<>(accountDto, HttpStatus.OK);
}).orElseGet(
() -> {
LOG.warn("No account was found with username " + username);
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
);
}
@PostMapping("/") @PostMapping("/")
@Validated({AccountDto.Insert.class, Default.class}) @Validated({AccountDto.Insert.class, Default.class})
public @ResponseBody ResponseEntity<AccountDto> insertAccount( public @ResponseBody ResponseEntity<AccountDto> insertAccount(
@@ -89,6 +120,21 @@ public class AccountController {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} }
LOG.info("Checking for any existing users with username {}", accountDto.getUsername());
final Instant start = Instant.now();
LOG.debug("Querying for existing accounts");
final Optional<AccountDto> existingAccount = accountsRepository.findByUsername(accountDto.getUsername());
LOG.debug("Received response from the server: query took {} ms", Duration.between(start, Instant.now()).toMillis());
if (existingAccount.isPresent()) {
LOG.warn("An account already exists with username {}", accountDto.getUsername());
return new ResponseEntity<>(HttpStatus.CONFLICT);
}
LOG.debug("Saving new account"); LOG.debug("Saving new account");
final StopWatch stopWatch = new StopWatch(); final StopWatch stopWatch = new StopWatch();
@@ -20,7 +20,7 @@ import java.util.Optional;
@Slf4j @Slf4j
@RestController @RestController
@AllArgsConstructor @AllArgsConstructor
@RequestMapping(path = "/definitions/") @RequestMapping(path = "/definitions")
public class DefinitionController { public class DefinitionController {
private final DefinitionRepository definitionRepository; private final DefinitionRepository definitionRepository;
@@ -85,7 +85,7 @@ public class DefinitionController {
} }
if (definitionDto == null) { if (definitionDto == null) {
LOG.error("Passed entity is unprocessable"); LOG.error("Passed entity is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} }
@@ -1,11 +1,238 @@
package edu.msudenver.tsp.persistence.controller; package edu.msudenver.tsp.persistence.controller;
import edu.msudenver.tsp.persistence.dto.TheoremDto;
import edu.msudenver.tsp.persistence.repository.TheoremRepository; import edu.msudenver.tsp.persistence.repository.TheoremRepository;
import edu.msudenver.tsp.utilities.PersistenceUtilities;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component; 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.web.bind.annotation.*;
@Component import javax.validation.Valid;
import java.util.List;
import java.util.Optional;
@Slf4j
@RestController
@AllArgsConstructor @AllArgsConstructor
@RequestMapping(path = "/theorems")
public class TheoremController { public class TheoremController {
private final TheoremRepository theoremRepository; private final TheoremRepository theoremRepository;
@GetMapping("/")
public @ResponseBody
ResponseEntity<Iterable<TheoremDto>> getAllTheorems() {
LOG.info("Received request to list all theorems");
LOG.debug("Querying for list of all theorems");
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final List<TheoremDto> listOfTheorems = theoremRepository.findAll();
stopWatch.stop();
LOG.debug("Successfully completed query. Query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
LOG.info("Returning list of all theorems with size " + listOfTheorems.size());
return new ResponseEntity<>(listOfTheorems, HttpStatus.OK);
}
@GetMapping("/{branch}")
public @ResponseBody
ResponseEntity<List<TheoremDto>> getAllTheoremsByBranch(@PathVariable("branch") final String branch) {
LOG.info("Received request to query for theorems related to the " + branch + " branch of mathematics");
if (branch == null) {
LOG.error("ERROR: branch was null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Querying for theorems with branch " + branch);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final List<TheoremDto> listOfTheorems = theoremRepository.findByBranch(branch);
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 for branch {}", branch);
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
LOG.info("Returning list of theorems with branch {}", branch);
return new ResponseEntity<>(listOfTheorems, HttpStatus.OK);
}
@GetMapping("/{proven_status}")
public @ResponseBody
ResponseEntity<List<TheoremDto>> getAllTheoremsByProvenStatus(@PathVariable("proven_status") final Boolean provenStatus) {
LOG.info("Received request to query for theorems whose proven status is " + provenStatus);
if (provenStatus == null) {
LOG.error("ERROR: status was null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Querying for theorems with proven status " + provenStatus);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final List<TheoremDto> listOfTheorems = theoremRepository.findByProvenStatus(provenStatus);
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 for proven status {}", provenStatus);
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
LOG.info("Returning list of theorems with proven status {}", provenStatus);
return new ResponseEntity<>(listOfTheorems, HttpStatus.OK);
}
@GetMapping("/{id}")
public @ResponseBody
ResponseEntity<TheoremDto> getTheoremById(@PathVariable("id") final Integer id) {
LOG.info("Received request to query for theorem with id " + id);
if (id == null) {
LOG.error("ERROR: ID was null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Querying for theorem with id " + id);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final Optional<TheoremDto> theorem = theoremRepository.findById(id);
stopWatch.stop();
LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
return theorem.map(theoremDto -> {
LOG.info("Returning theorem with id " + id);
return new ResponseEntity<>(theoremDto, HttpStatus.OK);
}).orElseGet(
() -> {
LOG.warn("No theorem was found with id " + id);
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
});
}
@PostMapping("/")
public @ResponseBody ResponseEntity<TheoremDto> insertTheorem(
@Valid @RequestBody final TheoremDto theoremDto,
final BindingResult bindingResult) {
LOG.info("Received request to insert a new theorem");
if (bindingResult.hasErrors()) {
LOG.error("Binding result is unprocessable");
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
}
if (theoremDto == null) {
LOG.error("Passed entity is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Saving new theorem");
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final TheoremDto savedTheorem = theoremRepository.save(theoremDto);
stopWatch.stop();
LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
LOG.info("Returning the newly created theorem with id " + savedTheorem.getId());
return new ResponseEntity<>(savedTheorem, HttpStatus.CREATED);
}
@PatchMapping("/{id}")
public @ResponseBody ResponseEntity<TheoremDto> updateTheorem(
@PathVariable("id") final Integer id,
@RequestBody final TheoremDto theoremDto, final BindingResult bindingResult) {
LOG.info("Received request to update an account");
if (bindingResult.hasErrors()) {
LOG.error("Binding result is unprocessable");
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
}
if (theoremDto == null) {
LOG.error("Passed entity is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
if (id == null) {
LOG.error("Theorem ID must be specified");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Checking for existence of theorem with id " + id);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final Optional<TheoremDto> existingTheorem = theoremRepository.findById(id);
stopWatch.stop();
LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
if (!existingTheorem.isPresent()) {
LOG.error("No theorem associated with id " + id);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
PersistenceUtilities.copyNonNullProperties(theoremDto, existingTheorem.get());
existingTheorem.get().setVersion(existingTheorem.get().getVersion()+ 1);
LOG.info("Updating theorem with id " + id);
LOG.debug("Querying for theorem with ID " + id);
stopWatch.start();
final TheoremDto updatedTheorem = theoremRepository.save(existingTheorem.get());
stopWatch.stop();
LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
return new ResponseEntity<>(updatedTheorem, HttpStatus.OK);
}
@DeleteMapping("/{id}")
public @ResponseBody ResponseEntity<Void> deleteTheoremById(@PathVariable("id") final Integer id) {
LOG.info("Received request to delete theorem with id " + id);
if (id == null) {
LOG.error("Specified id is null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Deleting theorem with id " + id);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
theoremRepository.deleteById(id);
stopWatch.stop();
LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
} }
@@ -5,10 +5,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import org.springframework.data.jpa.domain.support.AuditingEntityListener; import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.Entity; import javax.persistence.*;
import javax.persistence.EntityListeners;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
@@ -22,8 +19,8 @@ import java.util.Date;
public class AccountDto extends BaseDto implements Serializable { public class AccountDto extends BaseDto implements Serializable {
@NotBlank(groups = Insert.class, message = "A username must be specified") @Size(max = 50) private String username; @NotBlank(groups = Insert.class, message = "A username must be specified") @Size(max = 50) private String username;
@NotBlank(groups = Insert.class, message = "A password must be specified") @Size(max = 256) private String password; @NotBlank(groups = Insert.class, message = "A password must be specified") @Size(max = 256) private String password;
@NotNull private boolean administratorStatus; @NotNull @Column(name = "administrator_status") private boolean administratorStatus;
@Temporal(TemporalType.DATE) private Date lastLogin; @Temporal(TemporalType.DATE) @Column(name = "last_login") private Date lastLogin;
private static final long serialVersionUID = 7095627971593953734L; private static final long serialVersionUID = 7095627971593953734L;
@@ -48,4 +45,9 @@ public class AccountDto extends BaseDto implements Serializable {
} }
public interface Insert {} public interface Insert {}
@PrePersist
public void prePersist() {
lastLogin = new Date();
}
} }
@@ -1,4 +1,71 @@
package edu.msudenver.tsp.persistence.dto; package edu.msudenver.tsp.persistence.dto;
public class TheoremDto extends BaseDto { import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Type;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;
@Entity(name = "theorems")
@EntityListeners(AuditingEntityListener.class)
@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;
@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;
@JsonProperty("theorem_type")
public TheoremType getTheoremType() {
return theoremType;
}
@JsonProperty("theorem_type")
public void setTheoremType(final TheoremType theoremType) {
this.theoremType = theoremType;
}
@JsonProperty("referenced_definitions")
public List<String> getReferencedDefinitions() {
return referencedDefinitions;
}
@JsonProperty("referenced_definitions")
public void setReferencedDefinitions(final List<String> referencedDefinitions) {
this.referencedDefinitions = referencedDefinitions;
}
@JsonProperty("referenced_theorems")
public List<String> getReferencedTheorems() {
return referencedTheorems;
}
@JsonProperty("referenced_theorems")
public void setReferencedTheorems(final List<String> referencedTheorems) {
this.referencedTheorems = referencedTheorems;
}
@JsonProperty("proven_status")
public boolean getProvenStatus() {
return provenStatus;
}
@JsonProperty("proven_status")
public void setProvenStatus(final boolean provenStatus) {
this.provenStatus = provenStatus;
}
private static final long serialVersionUID = 1545568391140364425L;
} }
@@ -4,6 +4,9 @@ import edu.msudenver.tsp.persistence.dto.AccountDto;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository @Repository
public interface AccountsRepository extends CrudRepository<AccountDto, Integer> { public interface AccountsRepository extends CrudRepository<AccountDto, Integer> {
Optional<AccountDto> findByUsername(String username);
} }
@@ -1,7 +1,15 @@
package edu.msudenver.tsp.persistence.repository; package edu.msudenver.tsp.persistence.repository;
import edu.msudenver.tsp.persistence.dto.BaseDto; import edu.msudenver.tsp.persistence.dto.TheoremDto;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
public interface TheoremRepository extends CrudRepository<BaseDto, Long> { import java.util.List;
@Repository
public interface TheoremRepository extends JpaRepository<TheoremDto, Integer> {
List<TheoremDto> findByBranch(String branch);
List<TheoremDto> findByProvenStatus(Boolean provenStatus);
} }
@@ -47,7 +47,7 @@ public class AccountControllerTest {
assertTrue(responseEntity.hasBody()); assertTrue(responseEntity.hasBody());
assertNotNull(responseEntity.getBody()); assertNotNull(responseEntity.getBody());
responseEntity.getBody().forEach(account -> assertEquals(account, accountDto)); responseEntity.getBody().forEach(account -> assertEquals(accountDto, account));
} }
@Test @Test
@@ -87,10 +87,48 @@ public class AccountControllerTest {
verify(accountsRepository).findById(anyInt()); verify(accountsRepository).findById(anyInt());
} }
@Test
public void testGetAccountByUsername() {
final AccountDto accountDto = createAccount();
when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.ofNullable(accountDto));
final ResponseEntity<AccountDto> responseEntity = accountController.getAccountByUsername("Test username");
assertNotNull(responseEntity);
assertTrue(responseEntity.hasBody());
assertNotNull(responseEntity.getBody());
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
assertEquals(accountDto, responseEntity.getBody());
verify(accountsRepository).findByUsername(anyString());
}
@Test
public void testGetAccountById_nullUsername() {
final ResponseEntity responseEntity = accountController.getAccountByUsername(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(accountsRepository);
}
@Test
public void testGetAccountByUsername_noAccountFound() {
when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.empty());
final ResponseEntity responseEntity = accountController.getAccountByUsername("Test username");
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
verify(accountsRepository).findByUsername(anyString());
}
@Test @Test
public void testInsertAccount() { public void testInsertAccount() {
final AccountDto accountDto = createAccount(); final AccountDto accountDto = createAccount();
when(accountsRepository.save(any(AccountDto.class))).thenReturn(accountDto); when(accountsRepository.save(any(AccountDto.class))).thenReturn(accountDto);
when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.empty());
final ResponseEntity<AccountDto> responseEntity = accountController.insertAccount(accountDto, bindingResult); final ResponseEntity<AccountDto> responseEntity = accountController.insertAccount(accountDto, bindingResult);
@@ -102,6 +140,20 @@ public class AccountControllerTest {
verify(accountsRepository).save(any(AccountDto.class)); verify(accountsRepository).save(any(AccountDto.class));
} }
@Test
public void testInsertAccount_usernameAlreadyExists() {
final AccountDto accountDto = createAccount();
when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.of(accountDto));
final ResponseEntity<AccountDto> responseEntity = accountController.insertAccount(accountDto, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
verify(accountsRepository).findByUsername(anyString());
verify(accountsRepository, times(0)).save(any(AccountDto.class));
}
@Test @Test
public void testInsertAccount_accountsDtoIsNull() { public void testInsertAccount_accountsDtoIsNull() {
final ResponseEntity responseEntity = accountController.insertAccount(null, bindingResult); final ResponseEntity responseEntity = accountController.insertAccount(null, bindingResult);
@@ -114,10 +166,10 @@ public class AccountControllerTest {
@Test @Test
public void testInsertAccount_bindingResultHasErrors() { public void testInsertAccount_bindingResultHasErrors() {
final AccountDto definitionDto = createAccount(); final AccountDto accountDto = createAccount();
when(bindingResult.hasErrors()).thenReturn(true); when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity responseEntity = accountController.insertAccount(definitionDto, bindingResult); final ResponseEntity responseEntity = accountController.insertAccount(accountDto, bindingResult);
assertNotNull(responseEntity); assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody()); assertFalse(responseEntity.hasBody());
@@ -46,7 +46,7 @@ public class DefinitionControllerTest {
assertTrue(responseEntity.hasBody()); assertTrue(responseEntity.hasBody());
assertNotNull(responseEntity.getBody()); assertNotNull(responseEntity.getBody());
responseEntity.getBody().forEach(definition -> assertEquals(definition, definitionDto)); responseEntity.getBody().forEach(definition -> assertEquals(definitionDto, definition));
} }
@Test @Test