PAN-50 added findByUsername to the AccountController class and tests

This commit is contained in:
2019-03-10 23:04:29 -06:00
18 changed files with 396 additions and 19 deletions
@@ -29,7 +29,7 @@ public class AccountsIntegrationTest {
assertEquals("Test username", savedAccount.getUsername());
assertEquals("test password", savedAccount.getPassword());
assertTrue(savedAccount.isAdministratorStatus());
assertTrue(savedAccount.getAdministratorStatus());
savedAccount.setPassword("Test Update");
@@ -37,7 +37,7 @@ public class AccountsIntegrationTest {
assertEquals("Test username", savedAccount.getUsername());
assertEquals("Test Update", savedAccount.getPassword());
assertTrue(savedAccount.isAdministratorStatus());
assertTrue(savedAccount.getAdministratorStatus());
assertEquals(updatedAccount.getId(), id);
accountsRepository.delete(accountDto);
@@ -14,6 +14,8 @@ import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.groups.Default;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
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("/")
@Validated({AccountDto.Insert.class, Default.class})
public @ResponseBody ResponseEntity<AccountDto> insertAccount(
@@ -89,6 +120,21 @@ public class AccountController {
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");
final StopWatch stopWatch = new StopWatch();
@@ -7,5 +7,5 @@ import org.springframework.stereotype.Component;
@Component
@AllArgsConstructor
public class NotationController {
final private NotationRepository notationRepository;
private final NotationRepository notationRepository;
}
@@ -7,5 +7,5 @@ import org.springframework.stereotype.Component;
@Component
@AllArgsConstructor
public class ProofController {
final private ProofRepository proofRepository;
private final ProofRepository proofRepository;
}
@@ -67,19 +67,19 @@ public class TheoremController {
@GetMapping("/{proven_status}")
public @ResponseBody
ResponseEntity<List<TheoremDto>> getAllTheoremsByBranch(@PathVariable("proven_status") final Boolean proven_status) {
LOG.info("Received request to query for theorems whose proven status is " + proven_status);
if (proven_status == null) {
ResponseEntity<List<TheoremDto>> getAllTheoremsByBranch(@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: branch was null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Querying for theorems with proven status " + proven_status);
LOG.debug("Querying for theorems with proven status " + provenStatus);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final List<TheoremDto> listOfTheorems = theoremRepository.findByProven_status(proven_status);
final List<TheoremDto> listOfTheorems = theoremRepository.findByProvenStatus(provenStatus);
stopWatch.stop();
@@ -1,5 +1,6 @@
package edu.msudenver.tsp.persistence.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@@ -18,15 +19,35 @@ import java.util.Date;
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 password must be specified") @Size(max = 256) private String password;
@NotNull private boolean administrator_status;
@Temporal(TemporalType.DATE) private Date last_login;
@NotNull private boolean administratorStatus;
@Temporal(TemporalType.DATE) private Date lastLogin;
private static final long serialVersionUID = 7095627971593953734L;
@JsonProperty("administrator_status")
public boolean getAdministratorStatus() {
return administratorStatus;
}
@JsonProperty("administrator_status")
public void setAdministratorStatus(final boolean administratorStatus) {
this.administratorStatus = administratorStatus;
}
@JsonProperty("last_login")
public Date getLastLogin() {
return lastLogin;
}
@JsonProperty("last_login")
public void setLastLogin(final Date lastLogin) {
this.lastLogin = lastLogin;
}
public interface Insert {}
@PrePersist
public void prePersist() {
last_login = new Date();
lastLogin = new Date();
}
}
@@ -1,5 +1,6 @@
package edu.msudenver.tsp.persistence.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Type;
@@ -20,11 +21,41 @@ import java.util.List;
@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 private TheoremType theorem_type;
@NotNull 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(columnDefinition = "jsonb") private List<String> referenced_definitions;
@Type(type = "json") @Column(columnDefinition = "jsonb") private List<String> referenced_theorems;
@NotNull private boolean proven_status;
@Type(type = "json") @Column(columnDefinition = "jsonb") private List<String> referencedDefinitions;
@Type(type = "json") @Column(columnDefinition = "jsonb") private List<String> referencedTheorems;
@NotNull 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;
}
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.stereotype.Repository;
import java.util.Optional;
@Repository
public interface AccountsRepository extends CrudRepository<AccountDto, Integer> {
Optional<AccountDto> findByUsername(String username);
}
@@ -11,5 +11,5 @@ public interface TheoremRepository extends JpaRepository<TheoremDto, Integer> {
List<TheoremDto> findByBranch(String branch);
List<TheoremDto> findByProven_status(Boolean proven_status);
List<TheoremDto> findByProvenStatus(Boolean provenStatus);
}
@@ -1,6 +1,6 @@
spring.jpa.hibernate.ddl-auto = none
spring.jpa.database=mysql
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/pandamonium?autoReconnect=true&useSSL=false
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/pandamonium?autoReconnect=true&useSSL=false&serverTimezone=UTC
spring.datasource.username=panda
spring.datasource.password=secret
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
@@ -87,10 +87,48 @@ public class AccountControllerTest {
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
public void testInsertAccount() {
final AccountDto accountDto = createAccount();
when(accountsRepository.save(any(AccountDto.class))).thenReturn(accountDto);
when(accountsRepository.findByUsername(anyString())).thenReturn(Optional.empty());
final ResponseEntity<AccountDto> responseEntity = accountController.insertAccount(accountDto, bindingResult);
@@ -102,6 +140,20 @@ public class AccountControllerTest {
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
public void testInsertAccount_accountsDtoIsNull() {
final ResponseEntity responseEntity = accountController.insertAccount(null, bindingResult);