From ba37ffeb5793efd6be3f2aa0a869e5e27c5e5cb2 Mon Sep 17 00:00:00 2001 From: dantanxiaotian Date: Sun, 10 Mar 2019 15:55:43 -0600 Subject: [PATCH 01/15] PAN-15 Created initial UserService implementation and initial integration tests --- .../tsp/services/ServicesTestConfig.java | 17 ++++++++ .../services/UserServiceIntegrationTest.java | 43 +++++++++++++++++++ .../integrationTest/resources/test.properties | 3 ++ .../msudenver/tsp/services/ServiceConfig.java | 14 ++++++ .../msudenver/tsp/services/UserService.java | 4 +- .../msudenver/tsp/services/dto/Account.java | 8 ++-- .../tsp/services/parser/ParserService.java | 18 -------- .../src/main/resources/application.properties | 6 +-- 8 files changed, 86 insertions(+), 27 deletions(-) create mode 100644 services/src/integrationTest/java/edu/msudenver/tsp/services/ServicesTestConfig.java create mode 100644 services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java create mode 100644 services/src/integrationTest/resources/test.properties create mode 100644 services/src/main/java/edu/msudenver/tsp/services/ServiceConfig.java diff --git a/services/src/integrationTest/java/edu/msudenver/tsp/services/ServicesTestConfig.java b/services/src/integrationTest/java/edu/msudenver/tsp/services/ServicesTestConfig.java new file mode 100644 index 0000000..4c9e2b4 --- /dev/null +++ b/services/src/integrationTest/java/edu/msudenver/tsp/services/ServicesTestConfig.java @@ -0,0 +1,17 @@ +package edu.msudenver.tsp.services; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan +public class ServicesTestConfig { + + @Bean + @Autowired + public UserService userService(final RestService restService) { + return new UserService(restService); + } +} diff --git a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java new file mode 100644 index 0000000..e434d64 --- /dev/null +++ b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java @@ -0,0 +1,43 @@ +package edu.msudenver.tsp.services; + +import edu.msudenver.tsp.services.dto.Account; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.text.ParseException; +import java.util.Date; +import java.util.Optional; + +import static org.junit.Assert.assertTrue; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = ServicesTestConfig.class) +@TestPropertySource("classpath:test.properties") +public class UserServiceIntegrationTest { + @Autowired + @Qualifier("userService") + private UserService userService; + + @Test + public void testCreateNewUser() throws ParseException { + final Account testAccount = new Account(); + testAccount.setUsername("test user"); + testAccount.setPassword("test password"); + testAccount.setAdministratorStatus(false); + testAccount.setLastLogin(new Date()); + + final Optional testCreatedAccount = userService.createNewAccount(testAccount); + + assertTrue(testCreatedAccount.isPresent()); + final Account returnedAccount = testCreatedAccount.get(); + Assert.assertEquals("test user", returnedAccount.getUsername()); + Assert.assertEquals("test password", returnedAccount.getPassword()); + Assert.assertEquals(false, returnedAccount.isAdministratorStatus()); + } +} diff --git a/services/src/integrationTest/resources/test.properties b/services/src/integrationTest/resources/test.properties new file mode 100644 index 0000000..3690a57 --- /dev/null +++ b/services/src/integrationTest/resources/test.properties @@ -0,0 +1,3 @@ +persistence.api.connection.timeout.milliseconds=5000 +persistence.api.socket.timeout.milliseconds=10000 +persistence.api.base.url=http://localhost:8090/ diff --git a/services/src/main/java/edu/msudenver/tsp/services/ServiceConfig.java b/services/src/main/java/edu/msudenver/tsp/services/ServiceConfig.java new file mode 100644 index 0000000..db65cc6 --- /dev/null +++ b/services/src/main/java/edu/msudenver/tsp/services/ServiceConfig.java @@ -0,0 +1,14 @@ +package edu.msudenver.tsp.services; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; +import org.springframework.boot.devtools.autoconfigure.DevToolsDataSourceAutoConfiguration; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableAutoConfiguration(exclude = {DevToolsDataSourceAutoConfiguration.class, + HibernateJpaAutoConfiguration.class, + DataSourceAutoConfiguration.class}) +public class ServiceConfig { +} diff --git a/services/src/main/java/edu/msudenver/tsp/services/UserService.java b/services/src/main/java/edu/msudenver/tsp/services/UserService.java index f119441..0862ae5 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/UserService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/UserService.java @@ -34,8 +34,8 @@ public class UserService { try { final TypeToken typeToken = new TypeToken() {}; - final Optional persistenceApiResponse = restService.post(persistenceApiBaseUrl + "/accounts/", - new GsonBuilder().create().toJson(account), + final Optional persistenceApiResponse = restService.post(persistenceApiBaseUrl + "accounts/", + new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account), typeToken, connectionTimeoutMilliseconds, socketTimeoutMilliseconds); diff --git a/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java b/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java index c82731a..b7135ec 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java +++ b/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java @@ -1,19 +1,19 @@ package edu.msudenver.tsp.services.dto; import com.google.gson.annotations.SerializedName; -import edu.msudenver.tsp.persistence.dto.AccountDto; +import lombok.Data; import javax.persistence.Temporal; import javax.persistence.TemporalType; -import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import java.io.Serializable; import java.util.Date; +@Data public class Account extends BaseDto implements Serializable { - @NotBlank(groups = AccountDto.Insert.class, message = "A username must be specified") @Size(max = 50) private String username; - @NotBlank(groups = AccountDto.Insert.class, message = "A password must be specified") @Size(max = 256) private String password; + @Size(max = 50) private String username; + @Size(max = 256) private String password; @NotNull @SerializedName("administrator_status") private boolean administratorStatus; @Temporal(TemporalType.DATE) @SerializedName("last_login") private Date lastLogin; diff --git a/services/src/main/java/edu/msudenver/tsp/services/parser/ParserService.java b/services/src/main/java/edu/msudenver/tsp/services/parser/ParserService.java index 0106b8b..5bebbc1 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/parser/ParserService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/parser/ParserService.java @@ -1,10 +1,5 @@ package edu.msudenver.tsp.services.parser; -import edu.msudenver.tsp.persistence.controller.DefinitionController; -import edu.msudenver.tsp.persistence.controller.NotationController; -import edu.msudenver.tsp.persistence.controller.ProofController; -import edu.msudenver.tsp.persistence.controller.TheoremController; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -12,21 +7,8 @@ import java.util.List; @Service class ParserService { - private final DefinitionController definitionController; - private final TheoremController theoremController; - private final NotationController notationController; - private final ProofController proofController; private Node root; - @Autowired - public ParserService(final DefinitionController definitionController, final TheoremController theoremController, - final NotationController notationController, final ProofController proofController) { - this.definitionController = definitionController; - this.theoremController = theoremController; - this.notationController = notationController; - this.proofController = proofController; - } - public boolean parseUserInput(final String userInput) { try { diff --git a/services/src/main/resources/application.properties b/services/src/main/resources/application.properties index ff86c7e..710f97b 100644 --- a/services/src/main/resources/application.properties +++ b/services/src/main/resources/application.properties @@ -1,3 +1,3 @@ -persistence.api.connection.timeout.milliseconds = 5000 -persistence.api.socket.timeout.milliseconds = 10000 -persistence.api.base.url = http://localhost:8090/ \ No newline at end of file +persistence.api.connection.timeout.milliseconds=5000 +persistence.api.socket.timeout.milliseconds=10000 +persistence.api.base.url=http://localhost:8090/ \ No newline at end of file From 10b0a09690020b5bdd47ebb998a50c3a50166e3f Mon Sep 17 00:00:00 2001 From: dantanxiaotian Date: Sun, 10 Mar 2019 16:18:19 -0600 Subject: [PATCH 02/15] PAN-15 Updated the gradle dependencies --- services/build.gradle | 7 +++++- .../msudenver/tsp/services/dto/Account.java | 2 ++ .../services/parser/ParserServiceTest.java | 23 +++++++------------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/services/build.gradle b/services/build.gradle index 05efbeb..00a0119 100644 --- a/services/build.gradle +++ b/services/build.gradle @@ -18,11 +18,16 @@ repositories { } dependencies { - compile project(':persistence') compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.11' compile group: 'org.apache.httpcomponents', name: 'fluent-hc', version: '4.5.7' + compile group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2' + compile group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final' + compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure', version: '2.1.3.RELEASE' + compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '2.1.3.RELEASE' + compile group: 'org.springframework', name: 'spring-web', version: '5.1.5.RELEASE' compile group: 'com.google.code.gson', name: 'gson', version: '2.7' compile fileTree(dir: 'lib', include: '**/*.jar') testCompile group: 'junit', name: 'junit', version: '4.12' + testCompile group: 'org.springframework', name: 'spring-test', version: '5.1.5.RELEASE' } diff --git a/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java b/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java index b7135ec..1190dd8 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java +++ b/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java @@ -2,6 +2,7 @@ package edu.msudenver.tsp.services.dto; import com.google.gson.annotations.SerializedName; import lombok.Data; +import lombok.EqualsAndHashCode; import javax.persistence.Temporal; import javax.persistence.TemporalType; @@ -11,6 +12,7 @@ import java.io.Serializable; import java.util.Date; @Data +@EqualsAndHashCode(callSuper = true) public class Account extends BaseDto implements Serializable { @Size(max = 50) private String username; @Size(max = 256) private String password; diff --git a/services/src/test/java/edu/msudenver/tsp/services/parser/ParserServiceTest.java b/services/src/test/java/edu/msudenver/tsp/services/parser/ParserServiceTest.java index 829a02a..1b23d95 100644 --- a/services/src/test/java/edu/msudenver/tsp/services/parser/ParserServiceTest.java +++ b/services/src/test/java/edu/msudenver/tsp/services/parser/ParserServiceTest.java @@ -1,9 +1,8 @@ package edu.msudenver.tsp.services.parser; -import edu.msudenver.tsp.persistence.controller.DefinitionController; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.InjectMocks; +import org.mockito.Spy; import org.mockito.runners.MockitoJUnitRunner; import java.util.ArrayList; @@ -12,18 +11,12 @@ import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class ParserServiceTest { - private final DefinitionController definitionControllerMock = mock(DefinitionController.class); - private final ParserService mockParserService = mock(ParserService.class); - - @InjectMocks - private final ParserService parserService = new ParserService(definitionControllerMock, null, - null, null); + @Spy private ParserService parserService; @Test public void testEmptyStringEqualsEmptyString() { @@ -106,8 +99,8 @@ public class ParserServiceTest { final List expectedList = new ArrayList<>(); expectedList.add(""); - when(mockParserService.parseRawInput(anyString())).thenReturn(new Node("", null)); - final List actualList = parserService.retrieveStatements(mockParserService.parseRawInput("")); + when(parserService.parseRawInput(anyString())).thenReturn(new Node("", null)); + final List actualList = parserService.retrieveStatements(parserService.parseRawInput("")); assertEquals(expectedList, actualList); } @@ -124,8 +117,8 @@ public class ParserServiceTest { testNode.setRight(new Node("then", testNode)); testNode.getRight().setCenter(new Node(" x^2 is even", testNode.getRight())); - when(mockParserService.parseRawInput(anyString())).thenReturn(testNode); - final List actualList = parserService.retrieveStatements(mockParserService.parseRawInput("baseCase")); + when(parserService.parseRawInput(anyString())).thenReturn(testNode); + final List actualList = parserService.retrieveStatements(parserService.parseRawInput("baseCase")); assertEquals(expectedList, actualList); } @@ -133,8 +126,8 @@ public class ParserServiceTest { @Test public void testDriveParseUserInput() { final Node testNode = new Node("", null); - when(mockParserService.parseRawInput(anyString())).thenReturn(testNode); - when(mockParserService.retrieveStatements(testNode)).thenReturn(new ArrayList<>()); + when(parserService.parseRawInput(anyString())).thenReturn(testNode); + when(parserService.retrieveStatements(testNode)).thenReturn(new ArrayList<>()); final boolean successfulTestDrive = parserService.parseUserInput(""); From 238908f4facf34e91949a9a61283e90dfe1f1a98 Mon Sep 17 00:00:00 2001 From: dantanxiaotian Date: Sun, 10 Mar 2019 17:03:50 -0600 Subject: [PATCH 03/15] PAN-15 Updated tests --- .../services/UserServiceIntegrationTest.java | 9 ++++---- .../msudenver/tsp/services/dto/Account.java | 2 ++ .../services/parser/ParserServiceTest.java | 23 +++++++------------ 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java index e434d64..c3a71d0 100644 --- a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java +++ b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java @@ -1,7 +1,6 @@ package edu.msudenver.tsp.services; import edu.msudenver.tsp.services.dto.Account; -import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -14,7 +13,7 @@ import java.text.ParseException; import java.util.Date; import java.util.Optional; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = ServicesTestConfig.class) @@ -36,8 +35,8 @@ public class UserServiceIntegrationTest { assertTrue(testCreatedAccount.isPresent()); final Account returnedAccount = testCreatedAccount.get(); - Assert.assertEquals("test user", returnedAccount.getUsername()); - Assert.assertEquals("test password", returnedAccount.getPassword()); - Assert.assertEquals(false, returnedAccount.isAdministratorStatus()); + assertEquals("test user", returnedAccount.getUsername()); + assertEquals("test password", returnedAccount.getPassword()); + assertFalse(returnedAccount.isAdministratorStatus()); } } diff --git a/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java b/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java index b7135ec..1190dd8 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java +++ b/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java @@ -2,6 +2,7 @@ package edu.msudenver.tsp.services.dto; import com.google.gson.annotations.SerializedName; import lombok.Data; +import lombok.EqualsAndHashCode; import javax.persistence.Temporal; import javax.persistence.TemporalType; @@ -11,6 +12,7 @@ import java.io.Serializable; import java.util.Date; @Data +@EqualsAndHashCode(callSuper = true) public class Account extends BaseDto implements Serializable { @Size(max = 50) private String username; @Size(max = 256) private String password; diff --git a/services/src/test/java/edu/msudenver/tsp/services/parser/ParserServiceTest.java b/services/src/test/java/edu/msudenver/tsp/services/parser/ParserServiceTest.java index 829a02a..1b23d95 100644 --- a/services/src/test/java/edu/msudenver/tsp/services/parser/ParserServiceTest.java +++ b/services/src/test/java/edu/msudenver/tsp/services/parser/ParserServiceTest.java @@ -1,9 +1,8 @@ package edu.msudenver.tsp.services.parser; -import edu.msudenver.tsp.persistence.controller.DefinitionController; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.InjectMocks; +import org.mockito.Spy; import org.mockito.runners.MockitoJUnitRunner; import java.util.ArrayList; @@ -12,18 +11,12 @@ import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class ParserServiceTest { - private final DefinitionController definitionControllerMock = mock(DefinitionController.class); - private final ParserService mockParserService = mock(ParserService.class); - - @InjectMocks - private final ParserService parserService = new ParserService(definitionControllerMock, null, - null, null); + @Spy private ParserService parserService; @Test public void testEmptyStringEqualsEmptyString() { @@ -106,8 +99,8 @@ public class ParserServiceTest { final List expectedList = new ArrayList<>(); expectedList.add(""); - when(mockParserService.parseRawInput(anyString())).thenReturn(new Node("", null)); - final List actualList = parserService.retrieveStatements(mockParserService.parseRawInput("")); + when(parserService.parseRawInput(anyString())).thenReturn(new Node("", null)); + final List actualList = parserService.retrieveStatements(parserService.parseRawInput("")); assertEquals(expectedList, actualList); } @@ -124,8 +117,8 @@ public class ParserServiceTest { testNode.setRight(new Node("then", testNode)); testNode.getRight().setCenter(new Node(" x^2 is even", testNode.getRight())); - when(mockParserService.parseRawInput(anyString())).thenReturn(testNode); - final List actualList = parserService.retrieveStatements(mockParserService.parseRawInput("baseCase")); + when(parserService.parseRawInput(anyString())).thenReturn(testNode); + final List actualList = parserService.retrieveStatements(parserService.parseRawInput("baseCase")); assertEquals(expectedList, actualList); } @@ -133,8 +126,8 @@ public class ParserServiceTest { @Test public void testDriveParseUserInput() { final Node testNode = new Node("", null); - when(mockParserService.parseRawInput(anyString())).thenReturn(testNode); - when(mockParserService.retrieveStatements(testNode)).thenReturn(new ArrayList<>()); + when(parserService.parseRawInput(anyString())).thenReturn(testNode); + when(parserService.retrieveStatements(testNode)).thenReturn(new ArrayList<>()); final boolean successfulTestDrive = parserService.parseUserInput(""); From 5d429b2c8ea9f4bde67c1f7ee33db09dc5799a14 Mon Sep 17 00:00:00 2001 From: dantanxiaotian Date: Sun, 10 Mar 2019 17:06:47 -0600 Subject: [PATCH 04/15] PAN-15 Updated tests --- services/build.gradle | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/services/build.gradle b/services/build.gradle index 00a0119..05efbeb 100644 --- a/services/build.gradle +++ b/services/build.gradle @@ -18,16 +18,11 @@ repositories { } dependencies { + compile project(':persistence') compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.11' compile group: 'org.apache.httpcomponents', name: 'fluent-hc', version: '4.5.7' - compile group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2' - compile group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final' - compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure', version: '2.1.3.RELEASE' - compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '2.1.3.RELEASE' - compile group: 'org.springframework', name: 'spring-web', version: '5.1.5.RELEASE' compile group: 'com.google.code.gson', name: 'gson', version: '2.7' compile fileTree(dir: 'lib', include: '**/*.jar') testCompile group: 'junit', name: 'junit', version: '4.12' - testCompile group: 'org.springframework', name: 'spring-test', version: '5.1.5.RELEASE' } From ffaf020b8005651688ca877ad35a7a6fb3b5dc1a Mon Sep 17 00:00:00 2001 From: dantanxiaotian Date: Sun, 10 Mar 2019 17:21:32 -0600 Subject: [PATCH 05/15] PAN-15 WORKING AND WITH TRAVIS!!! --- services/build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/services/build.gradle b/services/build.gradle index 05efbeb..fd28275 100644 --- a/services/build.gradle +++ b/services/build.gradle @@ -25,4 +25,5 @@ dependencies { compile fileTree(dir: 'lib', include: '**/*.jar') testCompile group: 'junit', name: 'junit', version: '4.12' + testCompile group: 'org.springframework', name: 'spring-test', version: '5.1.5.RELEASE' } From 46122cbafd1c7211635b07723d0324c2a50790ee Mon Sep 17 00:00:00 2001 From: dantanxiaotian Date: Sun, 10 Mar 2019 19:58:04 -0600 Subject: [PATCH 06/15] PAN-15 Created gradle tasks to start the persistence API and to start the API asynchronously --- build.gradle | 26 +++++++++++++++++++++----- persistence/build.gradle | 11 +++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index 6690226..6c4a792 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,20 @@ +import java.util.concurrent.Callable +import java.util.concurrent.ExecutorService +import java.util.concurrent.Executors + +class RunAsyncTask extends DefaultTask { + String taskToExecute = ':persistence:startPersistenceApi' + @TaskAction + def startAsync() { + ExecutorService es = Executors.newSingleThreadExecutor() + es.submit({taskToExecute.execute()} as Callable) + } +} + buildscript { repositories { mavenCentral() + maven { url 'http://dl.bintray.com/vermeulen-mp/gradle-plugins' } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE") @@ -15,6 +29,7 @@ plugins { id "org.sonarqube" version "2.6" id 'org.unbroken-dome.test-sets' version '1.4.5' id 'war' + id "com.wiredforcode.spawn" version "0.8.2" } apply plugin: 'org.springframework.boot' @@ -87,11 +102,6 @@ subprojects { } } -bootJar { - baseName = 'gs-spring-boot' - version = '0.1.0' -} - sourceCompatibility = 1.8 targetCompatibility = 1.8 @@ -136,6 +146,12 @@ testSets { integrationTest } +task startApi(type: RunAsyncTask) { + dependsOn ':persistence:loadDb' + dependsOn 'build' + taskToExecute = ':persistence:startPersistenceApi' +} + compileKotlin { kotlinOptions.jvmTarget = "1.8" } diff --git a/persistence/build.gradle b/persistence/build.gradle index b832e7e..4672194 100644 --- a/persistence/build.gradle +++ b/persistence/build.gradle @@ -3,6 +3,9 @@ plugins { id 'java' } +apply plugin: 'io.spring.dependency-management' +apply plugin: 'org.springframework.boot' + description = 'Provides database access and connectivity' group 'edu.msudenver.tsp' version '1.0' @@ -50,3 +53,11 @@ task loadDb(type: Exec, group: 'Verification', description: 'Reloads the local d commandLine=['cmd','/c','loaddb.bat'] } } + +task startPersistenceApi(type: JavaExec, description: 'Starts the Persistence API') { + dependsOn 'loadDb' + dependsOn 'build' + classpath = files('build/libs/persistence-1.0.jar') + classpath += sourceSets.main.runtimeClasspath + main = 'edu.msudenver.tsp.persistence.PersistenceApi' +} From 860e5379e6f70ad11be3fc83597cce1c8b78f54e Mon Sep 17 00:00:00 2001 From: dantanxiaotian Date: Wed, 20 Mar 2019 16:06:24 -0600 Subject: [PATCH 07/15] PAN-15 UserService.java and completed UserSservice integration test --- ...damonium-theorem-prover.main.kotlin_module | Bin 16 -> 0 bytes .../scripts/mysql/local_development.sql | 1 + .../repository/AccountsRepository.java | 3 + .../controller/AccountControllerTest.java | 1 + .../services/UserServiceIntegrationTest.java | 24 +++- .../msudenver/tsp/services/RestService.java | 9 +- .../msudenver/tsp/services/UserService.java | 115 +++++++++++++++++- .../msudenver/tsp/services/dto/Account.java | 1 + .../tsp/services/factory/RequestFactory.java | 4 + 9 files changed, 148 insertions(+), 10 deletions(-) delete mode 100644 out/production/classes/META-INF/edu.msudenver.tsp.pandamonium-theorem-prover.main.kotlin_module diff --git a/out/production/classes/META-INF/edu.msudenver.tsp.pandamonium-theorem-prover.main.kotlin_module b/out/production/classes/META-INF/edu.msudenver.tsp.pandamonium-theorem-prover.main.kotlin_module deleted file mode 100644 index 8fb60192d378759239a3ecbf60eac8c8de446e9c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16 RcmZQzU|?ooU|@t|UH|}6022TJ diff --git a/persistence/scripts/mysql/local_development.sql b/persistence/scripts/mysql/local_development.sql index 1f885cc..eb1549c 100644 --- a/persistence/scripts/mysql/local_development.sql +++ b/persistence/scripts/mysql/local_development.sql @@ -16,6 +16,7 @@ values ('admin', 'secret', true), ('BrittanyBi', 'secret', true), ('lanlanzeliu', 'secret', true), ('tramanh305', 'secret', true); + create table definitions ( id int not null auto_increment primary key unique, name varchar(200) not null, diff --git a/persistence/src/main/java/edu/msudenver/tsp/persistence/repository/AccountsRepository.java b/persistence/src/main/java/edu/msudenver/tsp/persistence/repository/AccountsRepository.java index 454cd77..542ffe8 100644 --- a/persistence/src/main/java/edu/msudenver/tsp/persistence/repository/AccountsRepository.java +++ b/persistence/src/main/java/edu/msudenver/tsp/persistence/repository/AccountsRepository.java @@ -6,4 +6,7 @@ import org.springframework.stereotype.Repository; @Repository public interface AccountsRepository extends CrudRepository { + + + } diff --git a/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/AccountControllerTest.java b/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/AccountControllerTest.java index 0b88630..8dd69ca 100644 --- a/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/AccountControllerTest.java +++ b/persistence/src/test/java/edu/msudenver/tsp/persistence/controller/AccountControllerTest.java @@ -33,6 +33,7 @@ public class AccountControllerTest { @Test public void testGetAllAccounts() { + final AccountDto accountDto = createAccount(); final List accountDtoList = new ArrayList<>(); accountDtoList.add(accountDto); diff --git a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java index c3a71d0..c006236 100644 --- a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java +++ b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java @@ -9,7 +9,6 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import java.text.ParseException; import java.util.Date; import java.util.Optional; @@ -24,7 +23,7 @@ public class UserServiceIntegrationTest { private UserService userService; @Test - public void testCreateNewUser() throws ParseException { + public void testUserService(){ final Account testAccount = new Account(); testAccount.setUsername("test user"); testAccount.setPassword("test password"); @@ -38,5 +37,26 @@ public class UserServiceIntegrationTest { assertEquals("test user", returnedAccount.getUsername()); assertEquals("test password", returnedAccount.getPassword()); assertFalse(returnedAccount.isAdministratorStatus()); + + final Optional updatePasswordTestCreatedAccount = userService.updatePassword(returnedAccount, "password"); + + assertTrue(updatePasswordTestCreatedAccount.isPresent()); + final Account returnedUpdatedPasswordAccount = updatePasswordTestCreatedAccount.get(); + assertEquals("test user", returnedUpdatedPasswordAccount.getUsername()); + assertEquals("password", returnedUpdatedPasswordAccount.getPassword()); + assertFalse(returnedAccount.isAdministratorStatus()); + + final Optional updateUsernameTestCreatedAccount = userService.updateUsername(returnedUpdatedPasswordAccount, "user"); + + assertTrue(updateUsernameTestCreatedAccount.isPresent()); + final Account returnedUpdatedUsernameAccount = updateUsernameTestCreatedAccount.get(); + assertEquals("user", returnedUpdatedUsernameAccount.getUsername()); + assertEquals("password", returnedUpdatedUsernameAccount.getPassword()); + assertFalse(returnedAccount.isAdministratorStatus()); + + final boolean result = userService.deleteAccount(returnedUpdatedUsernameAccount); + + assertTrue(result); } + } diff --git a/services/src/main/java/edu/msudenver/tsp/services/RestService.java b/services/src/main/java/edu/msudenver/tsp/services/RestService.java index 78f0cb9..a8d043a 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/RestService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/RestService.java @@ -43,16 +43,21 @@ public class RestService { return send(requestFactory.post(uri, requestJson), null, connectionTimeout, socketTimeout, type); } + Optional patch(final String uri, final String requestJson, final TypeToken type, final Integer connectionTimeout, final Integer socketTimeout) { + LOG.info("Sending Patch {} with body: {}", uri, requestJson); + return send(requestFactory.patch(uri, requestJson), null, connectionTimeout, socketTimeout, type); + } Optional post(final String uri, final String requestJson, final Integer connectionTimeout, final Integer socketTimeout) { LOG.info("Sending POST {} with body: {}", uri, requestJson); return send(requestFactory.post(uri, requestJson), null, connectionTimeout, socketTimeout); } - Optional put(final String uri, final String requestJson, final TypeToken type, final Integer connectionTimeout, final Integer socketTimeout, final String auth) { + Optional put(final String uri, final String requestJson, final TypeToken type, final Integer connectionTimeout, final Integer socketTimeout) { LOG.info("Sending PUT {} with body: {}", uri, requestJson); - return send(requestFactory.put(uri, requestJson), auth, connectionTimeout, socketTimeout, type); + return send(requestFactory.put(uri, requestJson), null, connectionTimeout, socketTimeout, type); } + private Optional send(final Request request, final String auth, final Integer connectionTimeout, final Integer socketTimeout, final TypeToken type) { try { final Optional optionalHttpResponse = send(request, auth, connectionTimeout, socketTimeout); diff --git a/services/src/main/java/edu/msudenver/tsp/services/UserService.java b/services/src/main/java/edu/msudenver/tsp/services/UserService.java index 0862ae5..9cb0b46 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/UserService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/UserService.java @@ -6,6 +6,7 @@ import edu.msudenver.tsp.services.dto.Account; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import java.time.Duration; @@ -16,16 +17,19 @@ import java.util.Optional; @Service public class UserService { private final RestService restService; - @Value("${persistence.api.connection.timeout.milliseconds}") private int connectionTimeoutMilliseconds; - @Value("${persistence.api.socket.timeout.milliseconds}") private int socketTimeoutMilliseconds; - @Value("${persistence.api.base.url}") private String persistenceApiBaseUrl; + @Value("${persistence.api.connection.timeout.milliseconds}") + private int connectionTimeoutMilliseconds; + @Value("${persistence.api.socket.timeout.milliseconds}") + private int socketTimeoutMilliseconds; + @Value("${persistence.api.base.url}") + private String persistenceApiBaseUrl; @Autowired public UserService(final RestService restService) { this.restService = restService; } - public Optional createNewAccount(final Account account) { + public Optional createNewAccount(final Account account) { if (account == null) { LOG.error("Given null account, returning {}"); return Optional.empty(); @@ -33,7 +37,8 @@ public class UserService { final Instant start = Instant.now(); try { - final TypeToken typeToken = new TypeToken() {}; + final TypeToken typeToken = new TypeToken() { + }; final Optional persistenceApiResponse = restService.post(persistenceApiBaseUrl + "accounts/", new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account), typeToken, @@ -41,7 +46,7 @@ public class UserService { socketTimeoutMilliseconds); if (persistenceApiResponse.isPresent()) { - LOG.info("Returning {}", persistenceApiResponse.get()); + LOG.info("Returning {}", persistenceApiResponse); } else { LOG.info("Unable to create new account {}", account.toString()); } @@ -54,4 +59,102 @@ public class UserService { LOG.info("Create new account request took {} ms", Duration.between(start, Instant.now()).toMillis()); } } + + public Optional updatePassword(final Account account , final String password){ + + if(account ==null){ + LOG.error("user not exist, returning{}"); + return Optional.empty(); + } + + final Integer id = account.getId(); + account.setPassword(password); + final Instant start = Instant.now(); + + try{ + final String auth = ""; + final TypeToken typeToken = new TypeToken(){}; + final Optional persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "accounts/"+id, + new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account), + typeToken, + connectionTimeoutMilliseconds, + socketTimeoutMilliseconds); + + if (persistenceApiResponse.isPresent()) { + LOG.info("Returning {}", persistenceApiResponse.get()); + } else { + LOG.info("Unable to update password for account {}",account.toString()); + } + + return persistenceApiResponse; + } catch (final Exception e) { + LOG.error("Error updating account {}", e); + return Optional.empty(); + } finally { + LOG.info("Update account request took {} ms", Duration.between(start, Instant.now()).toMillis()); + } + } + + public Optional updateUsername(final Account account , final String username){ + + if(account ==null){ + LOG.error("user not exist, returning{}"); + return Optional.empty(); + } + + final Integer id = account.getId(); + account.setUsername(username); + final Instant start = Instant.now(); + + try{ + final String auth = ""; + final TypeToken typeToken = new TypeToken(){}; + final Optional persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "accounts/"+id, + new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account), + typeToken, + connectionTimeoutMilliseconds, + socketTimeoutMilliseconds); + + if (persistenceApiResponse.isPresent()) { + LOG.info("Returning {}", persistenceApiResponse.get()); + } else { + LOG.info("Unable to update username for account {}",account.toString()); + } + + return persistenceApiResponse; + } catch (final Exception e) { + LOG.error("Error updating account {}", e); + return Optional.empty(); + } finally { + LOG.info("Update account request took {} ms", Duration.between(start, Instant.now()).toMillis()); + } + } + public boolean deleteAccount(final Account account){ + if(account ==null){ + LOG.error("Username not exist, returning{}"); + return false; + } + final Integer id = account.getId(); + final Instant start = Instant.now(); + + try{ + + final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl +"/accounts/"+id, + connectionTimeoutMilliseconds, + socketTimeoutMilliseconds, HttpStatus.NO_CONTENT ); + if(persistenceApiResponse){ + LOG.info("return {}", persistenceApiResponse); + } + else { + LOG.info("Unable to delete user {}",account.toString()); + } + + return persistenceApiResponse; + }catch (final Exception e) { + LOG.error("Error deleting user {}", e); + return false; + } finally { + LOG.info("delete user request took {} ms", Duration.between(start, Instant.now()).toMillis()); + } + } } diff --git a/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java b/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java index 1190dd8..b1a04d9 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java +++ b/services/src/main/java/edu/msudenver/tsp/services/dto/Account.java @@ -21,4 +21,5 @@ public class Account extends BaseDto implements Serializable { private static final long serialVersionUID = 7095627971593953734L; + } diff --git a/services/src/main/java/edu/msudenver/tsp/services/factory/RequestFactory.java b/services/src/main/java/edu/msudenver/tsp/services/factory/RequestFactory.java index 726b9a8..e108cd1 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/factory/RequestFactory.java +++ b/services/src/main/java/edu/msudenver/tsp/services/factory/RequestFactory.java @@ -22,4 +22,8 @@ public class RequestFactory { public Request put(final String uri, final String requestJson) { return StringUtils.isNotBlank(requestJson) ? Request.Put(uri).bodyString(requestJson, ContentType.APPLICATION_JSON) : Request.Put(uri); } + + public Request patch(final String uri, final String requestJson) { + return StringUtils.isNotBlank(requestJson) ? Request.Patch(uri).bodyString(requestJson, ContentType.APPLICATION_JSON) : Request.Patch(uri); + } } \ No newline at end of file From f5f959b4530cf351436d02d94586d7e556fe9852 Mon Sep 17 00:00:00 2001 From: atusa17 Date: Wed, 20 Mar 2019 22:28:51 -0600 Subject: [PATCH 08/15] Merge branches 'PAN-15' and 'master' of https://github.com/atusa17/ptp into PAN-15 # Conflicts: # services/build.gradle --- persistence/build.gradle | 3 -- .../scripts/mysql/local_development.sql | 1 - .../msudenver/tsp/services/UserService.java | 33 +++++++++---------- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/persistence/build.gradle b/persistence/build.gradle index 4672194..811725d 100644 --- a/persistence/build.gradle +++ b/persistence/build.gradle @@ -3,9 +3,6 @@ plugins { id 'java' } -apply plugin: 'io.spring.dependency-management' -apply plugin: 'org.springframework.boot' - description = 'Provides database access and connectivity' group 'edu.msudenver.tsp' version '1.0' diff --git a/persistence/scripts/mysql/local_development.sql b/persistence/scripts/mysql/local_development.sql index e8acdaf..52480e5 100644 --- a/persistence/scripts/mysql/local_development.sql +++ b/persistence/scripts/mysql/local_development.sql @@ -16,7 +16,6 @@ values ('admin', 'secret', true), ('BrittanyBi', 'secret', true), ('lanlanzeliu', 'secret', true), ('tramanh305', 'secret', true); - create table definitions ( id int not null auto_increment primary key unique, name varchar(200) not null, diff --git a/services/src/main/java/edu/msudenver/tsp/services/UserService.java b/services/src/main/java/edu/msudenver/tsp/services/UserService.java index 9cb0b46..7382653 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/UserService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/UserService.java @@ -17,19 +17,16 @@ import java.util.Optional; @Service public class UserService { private final RestService restService; - @Value("${persistence.api.connection.timeout.milliseconds}") - private int connectionTimeoutMilliseconds; - @Value("${persistence.api.socket.timeout.milliseconds}") - private int socketTimeoutMilliseconds; - @Value("${persistence.api.base.url}") - private String persistenceApiBaseUrl; + @Value("${persistence.api.connection.timeout.milliseconds}") private int connectionTimeoutMilliseconds; + @Value("${persistence.api.socket.timeout.milliseconds}") private int socketTimeoutMilliseconds; + @Value("${persistence.api.base.url}") private String persistenceApiBaseUrl; @Autowired public UserService(final RestService restService) { this.restService = restService; } - public Optional createNewAccount(final Account account) { + public Optional createNewAccount(final Account account) { if (account == null) { LOG.error("Given null account, returning {}"); return Optional.empty(); @@ -37,8 +34,7 @@ public class UserService { final Instant start = Instant.now(); try { - final TypeToken typeToken = new TypeToken() { - }; + final TypeToken typeToken = new TypeToken() {}; final Optional persistenceApiResponse = restService.post(persistenceApiBaseUrl + "accounts/", new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account), typeToken, @@ -46,7 +42,7 @@ public class UserService { socketTimeoutMilliseconds); if (persistenceApiResponse.isPresent()) { - LOG.info("Returning {}", persistenceApiResponse); + LOG.info("Returning {}", persistenceApiResponse.get()); } else { LOG.info("Unable to create new account {}", account.toString()); } @@ -74,7 +70,7 @@ public class UserService { try{ final String auth = ""; final TypeToken typeToken = new TypeToken(){}; - final Optional persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "accounts/"+id, + final Optional persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "accounts/id?id=" + id, new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account), typeToken, connectionTimeoutMilliseconds, @@ -93,7 +89,7 @@ public class UserService { } finally { LOG.info("Update account request took {} ms", Duration.between(start, Instant.now()).toMillis()); } - } + } public Optional updateUsername(final Account account , final String username){ @@ -109,7 +105,7 @@ public class UserService { try{ final String auth = ""; final TypeToken typeToken = new TypeToken(){}; - final Optional persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "accounts/"+id, + final Optional persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "accounts/id?id="+id, new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account), typeToken, connectionTimeoutMilliseconds, @@ -129,9 +125,10 @@ public class UserService { LOG.info("Update account request took {} ms", Duration.between(start, Instant.now()).toMillis()); } } - public boolean deleteAccount(final Account account){ - if(account ==null){ - LOG.error("Username not exist, returning{}"); + + public boolean deleteAccount(final Account account) { + if(account == null){ + LOG.error("Username does not exist, returning {}"); return false; } final Integer id = account.getId(); @@ -139,14 +136,14 @@ public class UserService { try{ - final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl +"/accounts/"+id, + final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl + "/accounts/id?id=" + id, connectionTimeoutMilliseconds, socketTimeoutMilliseconds, HttpStatus.NO_CONTENT ); if(persistenceApiResponse){ LOG.info("return {}", persistenceApiResponse); } else { - LOG.info("Unable to delete user {}",account.toString()); + LOG.info("Unable to delete user {}", account.toString()); } return persistenceApiResponse; From 9695f5082ad34a47f3551e778adaeff20c1704f5 Mon Sep 17 00:00:00 2001 From: atusa17 Date: Wed, 20 Mar 2019 22:30:11 -0600 Subject: [PATCH 09/15] Merge branches 'PAN-15' and 'master' of https://github.com/atusa17/ptp into PAN-15 # Conflicts: # services/build.gradle --- .../main/java/edu/msudenver/tsp/services/UserService.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/src/main/java/edu/msudenver/tsp/services/UserService.java b/services/src/main/java/edu/msudenver/tsp/services/UserService.java index 7382653..ffb78a5 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/UserService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/UserService.java @@ -70,7 +70,7 @@ public class UserService { try{ final String auth = ""; final TypeToken typeToken = new TypeToken(){}; - final Optional persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "accounts/id?id=" + id, + final Optional persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "accounts/" + id, new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account), typeToken, connectionTimeoutMilliseconds, @@ -105,7 +105,7 @@ public class UserService { try{ final String auth = ""; final TypeToken typeToken = new TypeToken(){}; - final Optional persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "accounts/id?id="+id, + final Optional persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "accounts/"+id, new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account), typeToken, connectionTimeoutMilliseconds, @@ -136,7 +136,7 @@ public class UserService { try{ - final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl + "/accounts/id?id=" + id, + final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl + "/accounts/" + id, connectionTimeoutMilliseconds, socketTimeoutMilliseconds, HttpStatus.NO_CONTENT ); if(persistenceApiResponse){ From 89f1c55f51a4f4d8c8c6bb32271115c4df29c00c Mon Sep 17 00:00:00 2001 From: dantanxiaotian Date: Sun, 24 Mar 2019 14:54:06 -0600 Subject: [PATCH 10/15] PAN-15 Fixed unit tests --- build.gradle | 6 ++- .../msudenver/tsp/services/UserService.java | 24 +++++----- .../tsp/services/UserServiceTest.java | 47 +++++++++++++++++++ .../msudenver/tsp/website/Application.java | 19 +------- src/main/webapp/index.jsp | 15 ++++-- 5 files changed, 79 insertions(+), 32 deletions(-) create mode 100644 services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java diff --git a/build.gradle b/build.gradle index cc21aa1..e00bb1b 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,4 @@ + buildscript { repositories { mavenCentral() @@ -70,7 +71,6 @@ subprojects { testCompile group: 'junit', name: 'junit', version: '4.11' testCompile group: 'junit', name: 'junit', version: '4.12' testCompile('org.mockito:mockito-core:1.10.19') {exclude(group: 'org.hamcrest')} - } test { @@ -107,7 +107,11 @@ dependencies { compile 'org.slf4j:slf4j-api:1.7.22' compile "joda-time:joda-time:2.2" compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.0.5.RELEASE' + compile('org.springframework.boot:spring-boot-starter-web','org.apache.tomcat.embed:tomcat-embed-jasper' + ,'javax.servlet:jstl') + testCompile 'javax.el:javax.el-api:3.0.0' + testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.1.3.RELEASE' testCompile group: 'junit', name: 'junit', version: '4.12' testCompile "org.springframework:spring-test:5.0.9.RELEASE" testCompile('org.mockito:mockito-core:1.10.19') {exclude(group: 'org.hamcrest')} diff --git a/services/src/main/java/edu/msudenver/tsp/services/UserService.java b/services/src/main/java/edu/msudenver/tsp/services/UserService.java index ffb78a5..d717e2b 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/UserService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/UserService.java @@ -56,19 +56,21 @@ public class UserService { } } - public Optional updatePassword(final Account account , final String password){ - - if(account ==null){ - LOG.error("user not exist, returning{}"); + public Optional updateAccount(final Account account) { + if (account == null) { + LOG.error("User does not exist, returning {}"); return Optional.empty(); } - final Integer id = account.getId(); - account.setPassword(password); + if (account.getId() == 0) { + LOG.error("No user ID specified! Returning {}"); + return Optional.empty(); + } + + final int id = account.getId(); final Instant start = Instant.now(); - try{ - final String auth = ""; + try { final TypeToken typeToken = new TypeToken(){}; final Optional persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "accounts/" + id, new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account), @@ -79,15 +81,15 @@ public class UserService { if (persistenceApiResponse.isPresent()) { LOG.info("Returning {}", persistenceApiResponse.get()); } else { - LOG.info("Unable to update password for account {}",account.toString()); + LOG.info("Unable to update user {} with id", account.getId()); } return persistenceApiResponse; } catch (final Exception e) { - LOG.error("Error updating account {}", e); + LOG.error("Error updating user {}", e); return Optional.empty(); } finally { - LOG.info("Update account request took {} ms", Duration.between(start, Instant.now()).toMillis()); + LOG.info("Update user request took {} ms", Duration.between(start, Instant.now()).toMillis()); } } diff --git a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java new file mode 100644 index 0000000..52379b0 --- /dev/null +++ b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java @@ -0,0 +1,47 @@ +package edu.msudenver.tsp.services; + +import com.google.gson.reflect.TypeToken; +import edu.msudenver.tsp.services.dto.Account; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import java.text.ParseException; +import java.util.Date; +import java.util.Optional; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.when; + + + + +@RunWith(MockitoJUnitRunner.class) +public class UserServiceTest { + @Mock + private RestService restService; + @InjectMocks + private UserService userService; + + @Test + public void testCreateNewAccount() throws ParseException { + + final Account account = new Account(); + account.setUsername("Test username"); + account.setPassword("test password"); + account.setAdministratorStatus(false); + account.setLastLogin(new Date()); + + when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) + .thenReturn(Optional.of(account)); + + final Optional response = userService.createNewAccount(account); + + assertTrue(response.isPresent()); + assertEquals(account, response.get()); + } +} \ No newline at end of file diff --git a/src/main/java/edu/msudenver/tsp/website/Application.java b/src/main/java/edu/msudenver/tsp/website/Application.java index c8f0854..3339429 100644 --- a/src/main/java/edu/msudenver/tsp/website/Application.java +++ b/src/main/java/edu/msudenver/tsp/website/Application.java @@ -1,15 +1,9 @@ package edu.msudenver.tsp.website; -import lombok.extern.slf4j.Slf4j; -import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; -import java.util.Arrays; -@Slf4j @SpringBootApplication public class Application { @@ -17,17 +11,8 @@ public class Application { SpringApplication.run(Application.class, args); } - @Bean - public CommandLineRunner commandLineRunner(final ApplicationContext ctx) { - return args -> { - LOG.info("Beans provided by Spring Boot:"); - final String[] beanNames = ctx.getBeanDefinitionNames(); - Arrays.sort(beanNames); - for (final String beanName : beanNames) { - LOG.info(beanName); - } - }; - } + + } diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index f8d618c..6417074 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -5,12 +5,21 @@ Time: 8:03 PM To change this template use File | Settings | File Templates. --%> -<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> + - $Title$ + + Theroem Prover - $END$ +
+
+

Theorem Prover

+

Hello! ${message}

+ + Click on this link to visit theorem entering page. +
+
From 6791f6c61fe64cfa1cc96d65669da3def5c31e61 Mon Sep 17 00:00:00 2001 From: dantanxiaotian Date: Sun, 24 Mar 2019 17:31:40 -0600 Subject: [PATCH 11/15] Merge branch 'master' of https://github.com/atusa17/ptp into PAN-15 # Conflicts: # src/main/java/edu/msudenver/tsp/website/Application.java --- .../services/UserServiceIntegrationTest.java | 38 +++++++-------- .../msudenver/tsp/services/UserService.java | 41 +++------------- .../tsp/services/UserServiceTest.java | 47 ++++++++++++++++--- 3 files changed, 66 insertions(+), 60 deletions(-) diff --git a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java index e8abd0b..be372fc 100644 --- a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java +++ b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java @@ -24,11 +24,7 @@ public class UserServiceIntegrationTest { @Test public void testUserService(){ - final Account testAccount = new Account(); - testAccount.setUsername("test user"); - testAccount.setPassword("test password"); - testAccount.setAdministratorStatus(false); - testAccount.setLastLogin(new Date()); + final Account testAccount = creatAccount(); final Optional testCreatedAccount = userService.createNewAccount(testAccount); @@ -38,25 +34,29 @@ public class UserServiceIntegrationTest { assertEquals("test password", returnedAccount.getPassword()); assertFalse(returnedAccount.isAdministratorStatus()); - final Optional updatePasswordTestCreatedAccount = userService.updateAccount(returnedAccount); + returnedAccount.setUsername("test updatedUser"); + returnedAccount.setPassword("test updatedPassword"); - assertTrue(updatePasswordTestCreatedAccount.isPresent()); - final Account returnedUpdatedPasswordAccount = updatePasswordTestCreatedAccount.get(); - assertEquals("test user", returnedUpdatedPasswordAccount.getUsername()); - assertEquals("password", returnedUpdatedPasswordAccount.getPassword()); - assertFalse(returnedAccount.isAdministratorStatus()); + final Optional updatedTestCreatedAccount = userService.updateAccount(returnedAccount); - final Optional updateUsernameTestCreatedAccount = userService.updateUsername(returnedUpdatedPasswordAccount, "user"); + assertTrue(updatedTestCreatedAccount .isPresent()); + final Account returnedUpdatedAccount = updatedTestCreatedAccount.get(); + assertEquals("test updatedUser", returnedUpdatedAccount.getUsername()); + assertEquals("test updatedPassword", returnedUpdatedAccount.getPassword()); + assertFalse(returnedUpdatedAccount.isAdministratorStatus()); - assertTrue(updateUsernameTestCreatedAccount.isPresent()); - final Account returnedUpdatedUsernameAccount = updateUsernameTestCreatedAccount.get(); - assertEquals("user", returnedUpdatedUsernameAccount.getUsername()); - assertEquals("password", returnedUpdatedUsernameAccount.getPassword()); - assertFalse(returnedAccount.isAdministratorStatus()); - - final boolean result = userService.deleteAccount(returnedUpdatedUsernameAccount); + final boolean result = userService.deleteAccount(returnedUpdatedAccount); assertTrue(result); } + private Account creatAccount(){ + final Account testAccount = new Account(); + testAccount.setUsername("test user"); + testAccount.setPassword("test password"); + testAccount.setAdministratorStatus(false); + testAccount.setLastLogin(new Date()); + + return testAccount; + } } diff --git a/services/src/main/java/edu/msudenver/tsp/services/UserService.java b/services/src/main/java/edu/msudenver/tsp/services/UserService.java index d717e2b..2e7a468 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/UserService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/UserService.java @@ -93,47 +93,18 @@ public class UserService { } } - public Optional updateUsername(final Account account , final String username){ - - if(account ==null){ - LOG.error("user not exist, returning{}"); - return Optional.empty(); - } - - final Integer id = account.getId(); - account.setUsername(username); - final Instant start = Instant.now(); - - try{ - final String auth = ""; - final TypeToken typeToken = new TypeToken(){}; - final Optional persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "accounts/"+id, - new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account), - typeToken, - connectionTimeoutMilliseconds, - socketTimeoutMilliseconds); - - if (persistenceApiResponse.isPresent()) { - LOG.info("Returning {}", persistenceApiResponse.get()); - } else { - LOG.info("Unable to update username for account {}",account.toString()); - } - - return persistenceApiResponse; - } catch (final Exception e) { - LOG.error("Error updating account {}", e); - return Optional.empty(); - } finally { - LOG.info("Update account request took {} ms", Duration.between(start, Instant.now()).toMillis()); - } - } public boolean deleteAccount(final Account account) { if(account == null){ LOG.error("Username does not exist, returning {}"); return false; } - final Integer id = account.getId(); + + if (account.getId() == 0) { + LOG.error("No user ID specified! Returning {}"); + return false; + } + final int id = account.getId(); final Instant start = Instant.now(); try{ diff --git a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java index ab1f321..1fb9dda 100644 --- a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java +++ b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java @@ -27,11 +27,7 @@ public class UserServiceTest { @Test public void testCreateNewAccount() throws ParseException { - final Account account = new Account(); - account.setUsername("Test username"); - account.setPassword("test password"); - account.setAdministratorStatus(false); - account.setLastLogin(new Date()); + final Account account = createAccount(); when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) .thenReturn(Optional.of(account)); @@ -41,4 +37,43 @@ public class UserServiceTest { assertTrue(response.isPresent()); assertEquals(account, response.get()); } -} \ No newline at end of file + + @Test + public void testUpdateAccount() throws ParseException { + final Account account = createAccount(); + account.setId(1); + + when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) + .thenReturn(Optional.of(account)); + + final Optional response = userService.updateAccount(account); + + assertTrue(response.isPresent()); + assertEquals(account, response.get()); + } + + @Test + public void testDeleteAccount() throws ParseException { + final boolean response= true; + final Account account = createAccount(); + account.setId(1); + + when(restService.delete(anyString(), anyInt(), anyInt(), any())) + .thenReturn(response); + + final boolean persistenceApiResponse = userService.deleteAccount(account); + + assertTrue(persistenceApiResponse ); + assertEquals(response, persistenceApiResponse ); + } + + + private Account createAccount() { + final Account account = new Account(); + account.setUsername("Test username"); + account.setPassword("test password"); + account.setAdministratorStatus(true); + account.setLastLogin(new Date()); + return account; + } +} From 0bc56f57e6978cd5634e06a4c3f7c724303102e7 Mon Sep 17 00:00:00 2001 From: atusa17 Date: Sun, 24 Mar 2019 17:49:07 -0600 Subject: [PATCH 12/15] PAN-15 Merged with master --- build.gradle | 1 - .../msudenver/tsp/services/UserServiceIntegrationTest.java | 1 + .../java/edu/msudenver/tsp/services/UserServiceTest.java | 6 ++---- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index 2dfd518..d2d9c24 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,3 @@ - buildscript { repositories { mavenCentral() diff --git a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java index be372fc..88da28f 100644 --- a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java +++ b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java @@ -49,6 +49,7 @@ public class UserServiceIntegrationTest { assertTrue(result); } + private Account creatAccount(){ final Account testAccount = new Account(); testAccount.setUsername("test user"); diff --git a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java index 1fb9dda..7f19401 100644 --- a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java +++ b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java @@ -19,10 +19,8 @@ import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class UserServiceTest { - @Mock - private RestService restService; - @InjectMocks - private UserService userService; + @Mock private RestService restService; + @InjectMocks private UserService userService; @Test public void testCreateNewAccount() throws ParseException { From b1d4ed9719c74b14d7ca0769be51ac9a4157e043 Mon Sep 17 00:00:00 2001 From: dantanxiaotian Date: Wed, 27 Mar 2019 12:18:12 -0600 Subject: [PATCH 13/15] PAN-15 fix the request changes --- .../services/UserServiceIntegrationTest.java | 43 ++++--- .../msudenver/tsp/services/UserService.java | 114 ++++++++++++++++-- .../tsp/services/UserServiceTest.java | 56 +++++++-- .../website/controller/LogInController.java | 35 ++++++ 4 files changed, 212 insertions(+), 36 deletions(-) create mode 100644 src/main/java/edu/msudenver/tsp/website/controller/LogInController.java diff --git a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java index 88da28f..b3a547d 100644 --- a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java +++ b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java @@ -24,36 +24,47 @@ public class UserServiceIntegrationTest { @Test public void testUserService(){ - final Account testAccount = creatAccount(); - - final Optional testCreatedAccount = userService.createNewAccount(testAccount); + final Account testAccount = createAccount(); + final Optional testCreatedAccount = userService.createAccount(testAccount); assertTrue(testCreatedAccount.isPresent()); final Account returnedAccount = testCreatedAccount.get(); - assertEquals("test user", returnedAccount.getUsername()); - assertEquals("test password", returnedAccount.getPassword()); + assertEquals("test_user", returnedAccount.getUsername()); + assertEquals("test_password", returnedAccount.getPassword()); assertFalse(returnedAccount.isAdministratorStatus()); - returnedAccount.setUsername("test updatedUser"); - returnedAccount.setPassword("test updatedPassword"); + final Optional getAccountById = userService.getAccountById(returnedAccount.getId()); + assertTrue(getAccountById.isPresent()); + final Account returnedAccountById = getAccountById.get(); + assertEquals("test_user", returnedAccountById.getUsername()); + assertEquals("test_password", returnedAccountById.getPassword()); + assertFalse(returnedAccountById.isAdministratorStatus()); - final Optional updatedTestCreatedAccount = userService.updateAccount(returnedAccount); + final Optional getAccountByUsername = userService.getAccountByUsername(returnedAccount.getUsername()); + assertTrue(getAccountByUsername.isPresent()); + final Account returnedAccountByUsername = getAccountByUsername.get(); + assertEquals("test_user", returnedAccountByUsername.getUsername()); + assertEquals("test_password", returnedAccountByUsername.getPassword()); + assertFalse(returnedAccountById.isAdministratorStatus()); - assertTrue(updatedTestCreatedAccount .isPresent()); - final Account returnedUpdatedAccount = updatedTestCreatedAccount.get(); - assertEquals("test updatedUser", returnedUpdatedAccount.getUsername()); - assertEquals("test updatedPassword", returnedUpdatedAccount.getPassword()); + returnedAccount.setUsername("test_updatedUser"); + returnedAccount.setPassword("test_updatedPassword"); + + final Optional updatedAccount = userService.updateAccount(returnedAccount); + assertTrue(updatedAccount .isPresent()); + final Account returnedUpdatedAccount = updatedAccount.get(); + assertEquals("test_updatedUser", returnedUpdatedAccount.getUsername()); + assertEquals("test_updatedPassword", returnedUpdatedAccount.getPassword()); assertFalse(returnedUpdatedAccount.isAdministratorStatus()); final boolean result = userService.deleteAccount(returnedUpdatedAccount); - assertTrue(result); } - private Account creatAccount(){ + private Account createAccount(){ final Account testAccount = new Account(); - testAccount.setUsername("test user"); - testAccount.setPassword("test password"); + testAccount.setUsername("test_user"); + testAccount.setPassword("test_password"); testAccount.setAdministratorStatus(false); testAccount.setLastLogin(new Date()); diff --git a/services/src/main/java/edu/msudenver/tsp/services/UserService.java b/services/src/main/java/edu/msudenver/tsp/services/UserService.java index 2e7a468..593b0d9 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/UserService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/UserService.java @@ -26,7 +26,99 @@ public class UserService { this.restService = restService; } - public Optional createNewAccount(final Account account) { + public Optional getListOfAccount(){ + final String auth = ""; + final Instant start = Instant.now(); + + try { + final TypeToken typeToken = new TypeToken() {}; + final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/", + typeToken, + connectionTimeoutMilliseconds, + socketTimeoutMilliseconds, + auth); + + if (persistenceApiResponse.isPresent()) { + LOG.info("Returning {}", persistenceApiResponse.get()); + } else { + LOG.info("Unable to get the list of accounts"); + } + + return persistenceApiResponse; + } catch (final Exception e) { + LOG.error("Error getting the list of accounts {}", e); + return Optional.empty(); + } finally { + LOG.info("Getting the list of accounts request took {} ms", Duration.between(start, Instant.now()).toMillis()); + } + } + + public Optional getAccountById(final int id) { + + if (id == 0) { + LOG.error("No user ID specified! Returning {}"); + return Optional.empty(); + } + + final String auth = ""; + final Instant start = Instant.now(); + + try { + final TypeToken typeToken = new TypeToken() {}; + final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/id?id="+id, + typeToken, + connectionTimeoutMilliseconds, + socketTimeoutMilliseconds, + auth); + + if (persistenceApiResponse.isPresent()) { + LOG.info("Returning {}", persistenceApiResponse.get()); + } else { + LOG.info("Unable to find account with id {}", id); + } + + return persistenceApiResponse; + } catch (final Exception e) { + LOG.error("Error getting account by id {}", e); + return Optional.empty(); + } finally { + LOG.info("Getting account by ID request took {} ms", Duration.between(start, Instant.now()).toMillis()); + } + } + + public Optional getAccountByUsername(final String username) { + if (username == null) { + LOG.error("No username specified! Returning {}"); + return Optional.empty(); + } + + final String auth = ""; + final Instant start = Instant.now(); + + try { + final TypeToken typeToken = new TypeToken() {}; + final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/username?username="+username, + typeToken, + connectionTimeoutMilliseconds, + socketTimeoutMilliseconds, + auth); + + if (persistenceApiResponse.isPresent()) { + LOG.info("Returning {}", persistenceApiResponse.get()); + } else { + LOG.info("Unable to GET account with username{}", username); + } + + return persistenceApiResponse; + } catch (final Exception e) { + LOG.error("Error getting account by username {}", e); + return Optional.empty(); + } finally { + LOG.info("Getting account by username request took {} ms", Duration.between(start, Instant.now()).toMillis()); + } + } + + public Optional createAccount(final Account account) { if (account == null) { LOG.error("Given null account, returning {}"); return Optional.empty(); @@ -58,7 +150,7 @@ public class UserService { public Optional updateAccount(final Account account) { if (account == null) { - LOG.error("User does not exist, returning {}"); + LOG.error("Specified account is null; returning {}"); return Optional.empty(); } @@ -81,7 +173,7 @@ public class UserService { if (persistenceApiResponse.isPresent()) { LOG.info("Returning {}", persistenceApiResponse.get()); } else { - LOG.info("Unable to update user {} with id", account.getId()); + LOG.info("Unable to update user with id {}", account.getId()); } return persistenceApiResponse; @@ -95,8 +187,8 @@ public class UserService { public boolean deleteAccount(final Account account) { - if(account == null){ - LOG.error("Username does not exist, returning {}"); + if (account == null){ + LOG.error("Specified account is null; returning {}"); return false; } @@ -107,13 +199,13 @@ public class UserService { final int id = account.getId(); final Instant start = Instant.now(); - try{ + try { - final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl + "/accounts/" + id, + final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl + "accounts/" + id, connectionTimeoutMilliseconds, - socketTimeoutMilliseconds, HttpStatus.NO_CONTENT ); - if(persistenceApiResponse){ - LOG.info("return {}", persistenceApiResponse); + socketTimeoutMilliseconds, HttpStatus.NO_CONTENT); + if (persistenceApiResponse){ + LOG.info("Returning {}", persistenceApiResponse); } else { LOG.info("Unable to delete user {}", account.toString()); @@ -124,7 +216,7 @@ public class UserService { LOG.error("Error deleting user {}", e); return false; } finally { - LOG.info("delete user request took {} ms", Duration.between(start, Instant.now()).toMillis()); + LOG.info("Delete user request took {} ms", Duration.between(start, Instant.now()).toMillis()); } } } diff --git a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java index 7f19401..4a478d9 100644 --- a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java +++ b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java @@ -8,8 +8,9 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import java.text.ParseException; +import java.util.ArrayList; import java.util.Date; +import java.util.List; import java.util.Optional; import static org.junit.Assert.assertEquals; @@ -23,27 +24,65 @@ public class UserServiceTest { @InjectMocks private UserService userService; @Test - public void testCreateNewAccount() throws ParseException { + public void testGetListOfAccounts(){ + final Account account1 = createAccount(); + final Account account2 = createAccount(); + final List accountList = new ArrayList<>(); + accountList.add(account1); + accountList.add(account2); + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) + .thenReturn(Optional.of(accountList)); + final Optional response = userService.getListOfAccount(); + + assertTrue(response.isPresent()); + assertEquals(accountList, response.get()); + } + + @Test + public void testGetAccountById(){ final Account account = createAccount(); + account.setId(1); - when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) .thenReturn(Optional.of(account)); - - final Optional response = userService.createNewAccount(account); + final Optional response = userService.getAccountById(1); assertTrue(response.isPresent()); assertEquals(account, response.get()); } @Test - public void testUpdateAccount() throws ParseException { + public void testGetAccountByUsername(){ + final Account account = createAccount(); + final String username = account.getUsername(); + + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) + .thenReturn(Optional.of(account)); + final Optional response = userService.getAccountByUsername(username); + + assertTrue(response.isPresent()); + assertEquals(account, response.get()); + } + @Test + public void testCreateAccount(){ + final Account account = createAccount(); + + when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) + .thenReturn(Optional.of(account)); + final Optional response = userService.createAccount(account); + + assertTrue(response.isPresent()); + assertEquals(account, response.get()); + } + + @Test + public void testUpdateAccount(){ final Account account = createAccount(); account.setId(1); when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) .thenReturn(Optional.of(account)); - final Optional response = userService.updateAccount(account); assertTrue(response.isPresent()); @@ -51,14 +90,13 @@ public class UserServiceTest { } @Test - public void testDeleteAccount() throws ParseException { + public void testDeleteAccount(){ final boolean response= true; final Account account = createAccount(); account.setId(1); when(restService.delete(anyString(), anyInt(), anyInt(), any())) .thenReturn(response); - final boolean persistenceApiResponse = userService.deleteAccount(account); assertTrue(persistenceApiResponse ); diff --git a/src/main/java/edu/msudenver/tsp/website/controller/LogInController.java b/src/main/java/edu/msudenver/tsp/website/controller/LogInController.java new file mode 100644 index 0000000..70247ff --- /dev/null +++ b/src/main/java/edu/msudenver/tsp/website/controller/LogInController.java @@ -0,0 +1,35 @@ +package edu.msudenver.tsp.website.controller; + +import edu.msudenver.tsp.website.forms.TheoremForm; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.servlet.ModelAndView; + + + @Slf4j + @Controller + @AllArgsConstructor + @RequestMapping("/login") + public class LogInController { + @GetMapping({"/",""}) + public ModelAndView enterTheoremPage() { + LOG.info("Received request to display the theorem entry page: returning model with name 'Theorem'"); + return new ModelAndView("Theorem"); + } + + @PostMapping({"/",""}) + public String saveTheorem(@Validated final TheoremForm theoremForm, final Model model) { + model.addAttribute("theoremName", theoremForm.getTheoremName()); + model.addAttribute("theorem", theoremForm.getTheorem()); + LOG.info("Saving theorem {}...", theoremForm); + + return "success"; + } + } +} From adbcd786f0cd20caca30f4621150678f769730c6 Mon Sep 17 00:00:00 2001 From: dantanxiaotian Date: Tue, 2 Apr 2019 16:38:54 -0600 Subject: [PATCH 14/15] PAN-15 fix the request changes --- .../msudenver/tsp/services/UserService.java | 8 +- .../tsp/services/UserServiceTest.java | 221 +++++++++++++++++- .../website/controller/LogInController.java | 35 --- 3 files changed, 216 insertions(+), 48 deletions(-) delete mode 100644 src/main/java/edu/msudenver/tsp/website/controller/LogInController.java diff --git a/services/src/main/java/edu/msudenver/tsp/services/UserService.java b/services/src/main/java/edu/msudenver/tsp/services/UserService.java index 593b0d9..d5333f0 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/UserService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/UserService.java @@ -27,7 +27,7 @@ public class UserService { } public Optional getListOfAccount(){ - final String auth = ""; + final String auth = null; final Instant start = Instant.now(); try { @@ -60,7 +60,7 @@ public class UserService { return Optional.empty(); } - final String auth = ""; + final String auth = null; final Instant start = Instant.now(); try { @@ -92,7 +92,7 @@ public class UserService { return Optional.empty(); } - final String auth = ""; + final String auth = null; final Instant start = Instant.now(); try { @@ -208,7 +208,7 @@ public class UserService { LOG.info("Returning {}", persistenceApiResponse); } else { - LOG.info("Unable to delete user {}", account.toString()); + LOG.info("Unable to delete user {}"); } return persistenceApiResponse; diff --git a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java index 4a478d9..6debc4a 100644 --- a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java +++ b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java @@ -13,10 +13,8 @@ import java.util.Date; import java.util.List; import java.util.Optional; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.*; -import static org.mockito.Mockito.when; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; @RunWith(MockitoJUnitRunner.class) public class UserServiceTest { @@ -39,10 +37,41 @@ public class UserServiceTest { assertEquals(accountList, response.get()); } + @Test + public void testGetListOfAccounts_persistenceApiResponseIsNotPresent(){ + final Account account1 = createAccount(); + final Account account2 = createAccount(); + final List accountList = new ArrayList<>(); + accountList.add(account1); + accountList.add(account2); + + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) + .thenReturn(Optional.empty()); + final Optional response = userService.getListOfAccount(); + + assertFalse(response.isPresent()); + } + + @Test + public void testGetListOfAccounts_catchException(){ + final Account account1 = createAccount(); + final Account account2 = createAccount(); + final List accountList = new ArrayList<>(); + accountList.add(account1); + accountList.add(account2); + final Exception e = new Exception(); + + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) + .thenThrow(Exception.class); + final Optional response = userService.getListOfAccount(); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); + } + @Test public void testGetAccountById(){ final Account account = createAccount(); - account.setId(1); when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) .thenReturn(Optional.of(account)); @@ -50,20 +79,74 @@ public class UserServiceTest { assertTrue(response.isPresent()); assertEquals(account, response.get()); + verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString()); + } + + @Test + public void testGetAccountById_nullId() { + final Optional response = userService.getAccountById(0); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); + verifyZeroInteractions(restService); + } + + @Test + public void testGetAccountById_persistenceApiResponseIsNotPresent(){ + + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) + .thenReturn(Optional.empty()); + final Optional response = userService.getAccountById(1); + + assertFalse(response.isPresent()); + verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString()); + } + + @Test + public void testGetAccountById_catchException(){ + + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) + .thenThrow(Exception.class); + final Optional response = userService.getAccountById(1); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); } @Test public void testGetAccountByUsername(){ final Account account = createAccount(); - final String username = account.getUsername(); when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) .thenReturn(Optional.of(account)); - final Optional response = userService.getAccountByUsername(username); + final Optional response = userService.getAccountByUsername(account.getUsername()); assertTrue(response.isPresent()); assertEquals(account, response.get()); + verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString()); } + + @Test + public void testGetAccountByUsername_nullUsername(){ + final Optional response = userService.getAccountByUsername(null); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); + verifyZeroInteractions(restService); + } + + @Test + public void testGetAccountByUsername_persistenceApiResponseIsNotPresent(){ + final Account account = createAccount(); + + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) + .thenReturn(Optional.empty()); + final Optional response = userService.getAccountByUsername(account.getUsername()); + + assertFalse(response.isPresent()); + verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString()); + } + @Test public void testCreateAccount(){ final Account account = createAccount(); @@ -74,6 +157,27 @@ public class UserServiceTest { assertTrue(response.isPresent()); assertEquals(account, response.get()); + verify(restService).post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + } + + @Test + public void testCreateAccount_NoAccountFound() { + final Optional response = userService.createAccount(null); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); + verify(restService, times(0)).post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + } + + @Test + public void testCreateAccount_catchException(){ + final Account account = createAccount(); + + when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenThrow(Exception.class); + final Optional response = userService.createAccount(account); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); } @Test @@ -87,6 +191,54 @@ public class UserServiceTest { assertTrue(response.isPresent()); assertEquals(account, response.get()); + verify(restService).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + } + + @Test + public void testUpdateAccount_nullAccount(){ + final Optional response = userService.updateAccount(null); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); + verify(restService, times(0)).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + } + + @Test + public void testUpdateAccount_nullId(){ + final Account account = createAccount(); + account.setId(0); + + final Optional response = userService.updateAccount(account); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); + verify(restService, times(0)).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + } + + @Test + public void testUpdateAccount_PersistenceApiResponseIsNotPresent(){ + final Account account = createAccount(); + account.setId(1); + + when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) + .thenReturn(Optional.empty()); + final Optional response = userService.updateAccount(account); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); + } + + @Test + public void testUpdateAccount_catchException(){ + final Account account = createAccount(); + account.setId(1); + + when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) + .thenThrow(Exception.class); + final Optional response = userService.updateAccount(account); + + assertFalse(response.isPresent()); + assertEquals(Optional.empty(), response); } @Test @@ -99,8 +251,59 @@ public class UserServiceTest { .thenReturn(response); final boolean persistenceApiResponse = userService.deleteAccount(account); - assertTrue(persistenceApiResponse ); - assertEquals(response, persistenceApiResponse ); + assertNotNull(persistenceApiResponse); + assertTrue(persistenceApiResponse); + assertEquals(response, persistenceApiResponse); + verify(restService).delete(anyString(), anyInt(), anyInt(), any()); + } + + @Test + public void testDeleteAccount_nullAccount(){ + final boolean persistenceApiResponse = userService.deleteAccount(null); + + assertNotNull(persistenceApiResponse); + assertFalse(persistenceApiResponse); + verify(restService, times(0)).delete(anyString(), anyInt(), anyInt(), any()); + } + + @Test + public void testDeleteAccount_nullId(){ + final Account account = createAccount(); + account.setId(0); + + final boolean persistenceApiResponse = userService.deleteAccount(account); + + assertNotNull(persistenceApiResponse); + assertFalse(persistenceApiResponse); + verify(restService, times(0)).delete(anyString(), anyInt(), anyInt(), any()); + } + + @Test + public void testDeleteAccount_persistenceApiResponseIsNotPresent(){ + final boolean response= false; + final Account account = createAccount(); + account.setId(1); + + when(restService.delete(anyString(), anyInt(), anyInt(), any())) + .thenReturn(response); + final boolean persistenceApiResponse = userService.deleteAccount(account); + + assertNotNull(persistenceApiResponse); + assertFalse(persistenceApiResponse); + assertEquals(response, persistenceApiResponse); + verify(restService).delete(anyString(), anyInt(), anyInt(), any()); + } + + @Test + public void testDeleteAccount_catchException(){ + final Account account = createAccount(); + account.setId(1); + + when(restService.delete(anyString(), anyInt(), anyInt(), any())).thenThrow(Exception.class); + final boolean persistenceApiResponse = userService.deleteAccount(account); + + assertNotNull(persistenceApiResponse); + assertFalse(persistenceApiResponse); } diff --git a/src/main/java/edu/msudenver/tsp/website/controller/LogInController.java b/src/main/java/edu/msudenver/tsp/website/controller/LogInController.java deleted file mode 100644 index 70247ff..0000000 --- a/src/main/java/edu/msudenver/tsp/website/controller/LogInController.java +++ /dev/null @@ -1,35 +0,0 @@ -package edu.msudenver.tsp.website.controller; - -import edu.msudenver.tsp.website.forms.TheoremForm; -import lombok.AllArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.servlet.ModelAndView; - - - @Slf4j - @Controller - @AllArgsConstructor - @RequestMapping("/login") - public class LogInController { - @GetMapping({"/",""}) - public ModelAndView enterTheoremPage() { - LOG.info("Received request to display the theorem entry page: returning model with name 'Theorem'"); - return new ModelAndView("Theorem"); - } - - @PostMapping({"/",""}) - public String saveTheorem(@Validated final TheoremForm theoremForm, final Model model) { - model.addAttribute("theoremName", theoremForm.getTheoremName()); - model.addAttribute("theorem", theoremForm.getTheorem()); - LOG.info("Saving theorem {}...", theoremForm); - - return "success"; - } - } -} From 36036b710de0ccc24e3977a11e1f677ce167231a Mon Sep 17 00:00:00 2001 From: atusa17 Date: Sun, 7 Apr 2019 15:18:19 -0600 Subject: [PATCH 15/15] PAN-15 Finished Unit Tests --- .../services/UserServiceIntegrationTest.java | 6 +- .../msudenver/tsp/services/UserService.java | 63 +++--- .../tsp/services/UserServiceTest.java | 209 +++++++++--------- 3 files changed, 136 insertions(+), 142 deletions(-) diff --git a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java index b3a547d..be36c25 100644 --- a/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java +++ b/services/src/integrationTest/java/edu/msudenver/tsp/services/UserServiceIntegrationTest.java @@ -33,14 +33,14 @@ public class UserServiceIntegrationTest { assertEquals("test_password", returnedAccount.getPassword()); assertFalse(returnedAccount.isAdministratorStatus()); - final Optional getAccountById = userService.getAccountById(returnedAccount.getId()); + final Optional getAccountById = userService.findAccountById(returnedAccount.getId()); assertTrue(getAccountById.isPresent()); final Account returnedAccountById = getAccountById.get(); assertEquals("test_user", returnedAccountById.getUsername()); assertEquals("test_password", returnedAccountById.getPassword()); assertFalse(returnedAccountById.isAdministratorStatus()); - final Optional getAccountByUsername = userService.getAccountByUsername(returnedAccount.getUsername()); + final Optional getAccountByUsername = userService.findAccountByUsername(returnedAccount.getUsername()); assertTrue(getAccountByUsername.isPresent()); final Account returnedAccountByUsername = getAccountByUsername.get(); assertEquals("test_user", returnedAccountByUsername.getUsername()); @@ -61,7 +61,7 @@ public class UserServiceIntegrationTest { assertTrue(result); } - private Account createAccount(){ + private Account createAccount() { final Account testAccount = new Account(); testAccount.setUsername("test_user"); testAccount.setPassword("test_password"); diff --git a/services/src/main/java/edu/msudenver/tsp/services/UserService.java b/services/src/main/java/edu/msudenver/tsp/services/UserService.java index d5333f0..3125824 100644 --- a/services/src/main/java/edu/msudenver/tsp/services/UserService.java +++ b/services/src/main/java/edu/msudenver/tsp/services/UserService.java @@ -11,6 +11,7 @@ import org.springframework.stereotype.Service; import java.time.Duration; import java.time.Instant; +import java.util.List; import java.util.Optional; @Slf4j @@ -26,95 +27,92 @@ public class UserService { this.restService = restService; } - public Optional getListOfAccount(){ - final String auth = null; + public Optional> getListOfAccounts() { final Instant start = Instant.now(); try { - final TypeToken typeToken = new TypeToken() {}; - final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/", + final TypeToken> typeToken = new TypeToken>() {}; + final Optional> persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/", typeToken, connectionTimeoutMilliseconds, socketTimeoutMilliseconds, - auth); + null); if (persistenceApiResponse.isPresent()) { LOG.info("Returning {}", persistenceApiResponse.get()); } else { - LOG.info("Unable to get the list of accounts"); + LOG.warn("Unable to get the list of accounts"); } return persistenceApiResponse; } catch (final Exception e) { - LOG.error("Error getting the list of accounts {}", e); + LOG.error("Error getting the list of accounts", e); return Optional.empty(); } finally { - LOG.info("Getting the list of accounts request took {} ms", Duration.between(start, Instant.now()).toMillis()); + LOG.info("Get the list of accounts request took {}ms", Duration.between(start, Instant.now()).toMillis()); } } - public Optional getAccountById(final int id) { + public Optional findAccountById(final int id) { if (id == 0) { LOG.error("No user ID specified! Returning {}"); return Optional.empty(); } - final String auth = null; final Instant start = Instant.now(); try { final TypeToken typeToken = new TypeToken() {}; - final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/id?id="+id, + final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/id?id=" + id, typeToken, connectionTimeoutMilliseconds, socketTimeoutMilliseconds, - auth); + null); if (persistenceApiResponse.isPresent()) { LOG.info("Returning {}", persistenceApiResponse.get()); } else { - LOG.info("Unable to find account with id {}", id); + LOG.warn("Unable to find account with id {}", id); } return persistenceApiResponse; } catch (final Exception e) { - LOG.error("Error getting account by id {}", e); + LOG.error("Error finding account by id", e); return Optional.empty(); } finally { - LOG.info("Getting account by ID request took {} ms", Duration.between(start, Instant.now()).toMillis()); + LOG.info("Find account by ID request took {}ms", Duration.between(start, Instant.now()).toMillis()); } } - public Optional getAccountByUsername(final String username) { + public Optional findAccountByUsername(final String username) { if (username == null) { LOG.error("No username specified! Returning {}"); return Optional.empty(); } - final String auth = null; final Instant start = Instant.now(); try { final TypeToken typeToken = new TypeToken() {}; - final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/username?username="+username, + final Optional persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/username?username=" + username, typeToken, connectionTimeoutMilliseconds, socketTimeoutMilliseconds, - auth); + null); if (persistenceApiResponse.isPresent()) { LOG.info("Returning {}", persistenceApiResponse.get()); } else { - LOG.info("Unable to GET account with username{}", username); + LOG.warn("Unable to GET account with username {}", username); } return persistenceApiResponse; } catch (final Exception e) { - LOG.error("Error getting account by username {}", e); + LOG.error("Error finding account by username", e); return Optional.empty(); } finally { - LOG.info("Getting account by username request took {} ms", Duration.between(start, Instant.now()).toMillis()); + LOG.info("Find account by username request took {}ms", Duration.between(start, Instant.now()).toMillis()); } } @@ -136,15 +134,15 @@ public class UserService { if (persistenceApiResponse.isPresent()) { LOG.info("Returning {}", persistenceApiResponse.get()); } else { - LOG.info("Unable to create new account {}", account.toString()); + LOG.warn("Unable to create new account {}", account.toString()); } return persistenceApiResponse; } catch (final Exception e) { - LOG.error("Error creating new account {}", e); + LOG.error("Error creating new account", e); return Optional.empty(); } finally { - LOG.info("Create new account request took {} ms", Duration.between(start, Instant.now()).toMillis()); + LOG.info("Create new account request took {}ms", Duration.between(start, Instant.now()).toMillis()); } } @@ -173,15 +171,15 @@ public class UserService { if (persistenceApiResponse.isPresent()) { LOG.info("Returning {}", persistenceApiResponse.get()); } else { - LOG.info("Unable to update user with id {}", account.getId()); + LOG.warn("Unable to update user with id {}", account.getId()); } return persistenceApiResponse; } catch (final Exception e) { - LOG.error("Error updating user {}", e); + LOG.error("Error updating user", e); return Optional.empty(); } finally { - LOG.info("Update user request took {} ms", Duration.between(start, Instant.now()).toMillis()); + LOG.info("Update user request took {}ms", Duration.between(start, Instant.now()).toMillis()); } } @@ -196,6 +194,7 @@ public class UserService { LOG.error("No user ID specified! Returning {}"); return false; } + final int id = account.getId(); final Instant start = Instant.now(); @@ -204,19 +203,19 @@ public class UserService { final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl + "accounts/" + id, connectionTimeoutMilliseconds, socketTimeoutMilliseconds, HttpStatus.NO_CONTENT); - if (persistenceApiResponse){ + if (persistenceApiResponse) { LOG.info("Returning {}", persistenceApiResponse); } else { - LOG.info("Unable to delete user {}"); + LOG.error("Unable to delete user {}", account); } return persistenceApiResponse; }catch (final Exception e) { - LOG.error("Error deleting user {}", e); + LOG.error("Error deleting user", e); return false; } finally { - LOG.info("Delete user request took {} ms", Duration.between(start, Instant.now()).toMillis()); + LOG.info("Delete user request took {}ms", Duration.between(start, Instant.now()).toMillis()); } } } diff --git a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java index 6debc4a..1d42311 100644 --- a/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java +++ b/services/src/test/java/edu/msudenver/tsp/services/UserServiceTest.java @@ -22,60 +22,47 @@ public class UserServiceTest { @InjectMocks private UserService userService; @Test - public void testGetListOfAccounts(){ - final Account account1 = createAccount(); - final Account account2 = createAccount(); + public void testGetListOfAccounts() { final List accountList = new ArrayList<>(); - accountList.add(account1); - accountList.add(account2); + accountList.add(createAccount()); + accountList.add(createAccount()); - when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) - .thenReturn(Optional.of(accountList)); - final Optional response = userService.getListOfAccount(); + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(), anyString())).thenReturn(Optional.of(accountList)); + + final Optional> response = userService.getListOfAccounts(); assertTrue(response.isPresent()); assertEquals(accountList, response.get()); + verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString()); } @Test - public void testGetListOfAccounts_persistenceApiResponseIsNotPresent(){ - final Account account1 = createAccount(); - final Account account2 = createAccount(); - final List accountList = new ArrayList<>(); - accountList.add(account1); - accountList.add(account2); + public void testGetListOfAccounts_PersistenceApiResponseIsEmpty() { + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.empty()); - when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) - .thenReturn(Optional.empty()); - final Optional response = userService.getListOfAccount(); + final Optional> response = userService.getListOfAccounts(); assertFalse(response.isPresent()); + verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString()); } @Test - public void testGetListOfAccounts_catchException(){ - final Account account1 = createAccount(); - final Account account2 = createAccount(); - final List accountList = new ArrayList<>(); - accountList.add(account1); - accountList.add(account2); - final Exception e = new Exception(); + public void testGetListOfAccounts_RestServiceThrowsException() { + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenThrow(Exception.class); - when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) - .thenThrow(Exception.class); - final Optional response = userService.getListOfAccount(); + final Optional> response = userService.getListOfAccounts(); assertFalse(response.isPresent()); - assertEquals(Optional.empty(), response); + verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString()); } @Test - public void testGetAccountById(){ + public void testFindAccountById() { final Account account = createAccount(); - when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) - .thenReturn(Optional.of(account)); - final Optional response = userService.getAccountById(1); + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.of(account)); + + final Optional response = userService.findAccountById(1); assertTrue(response.isPresent()); assertEquals(account, response.get()); @@ -83,8 +70,8 @@ public class UserServiceTest { } @Test - public void testGetAccountById_nullId() { - final Optional response = userService.getAccountById(0); + public void testFindAccountById_IdEqualsZero() { + final Optional response = userService.findAccountById(0); assertFalse(response.isPresent()); assertEquals(Optional.empty(), response); @@ -92,34 +79,32 @@ public class UserServiceTest { } @Test - public void testGetAccountById_persistenceApiResponseIsNotPresent(){ + public void testFindAccountById_PersistenceApiResponseIsEmpty() { + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.empty()); - when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) - .thenReturn(Optional.empty()); - final Optional response = userService.getAccountById(1); + final Optional response = userService.findAccountById(1); assertFalse(response.isPresent()); verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString()); } @Test - public void testGetAccountById_catchException(){ + public void testFindAccountById_RestServiceThrowsException() { + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenThrow(Exception.class); - when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) - .thenThrow(Exception.class); - final Optional response = userService.getAccountById(1); + final Optional response = userService.findAccountById(1); assertFalse(response.isPresent()); - assertEquals(Optional.empty(), response); + verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString()); } @Test - public void testGetAccountByUsername(){ + public void testFindAccountByUsername() { final Account account = createAccount(); - when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) - .thenReturn(Optional.of(account)); - final Optional response = userService.getAccountByUsername(account.getUsername()); + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.of(account)); + + final Optional response = userService.findAccountByUsername(account.getUsername()); assertTrue(response.isPresent()); assertEquals(account, response.get()); @@ -127,8 +112,8 @@ public class UserServiceTest { } @Test - public void testGetAccountByUsername_nullUsername(){ - final Optional response = userService.getAccountByUsername(null); + public void testFindAccountByUsername_NullUsername() { + final Optional response = userService.findAccountByUsername(null); assertFalse(response.isPresent()); assertEquals(Optional.empty(), response); @@ -136,23 +121,33 @@ public class UserServiceTest { } @Test - public void testGetAccountByUsername_persistenceApiResponseIsNotPresent(){ + public void testFindAccountByUsername_PersistenceApiResponseIsEmpty() { final Account account = createAccount(); - when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())) - .thenReturn(Optional.empty()); - final Optional response = userService.getAccountByUsername(account.getUsername()); + when(restService.get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString())).thenReturn(Optional.empty()); + + final Optional response = userService.findAccountByUsername(account.getUsername()); assertFalse(response.isPresent()); verify(restService).get(anyString(),any(TypeToken.class), anyInt(), anyInt(),anyString()); } @Test - public void testCreateAccount(){ + public void testFindAccountByUsername_RestServiceThrowsException() { + when(restService.get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString())).thenThrow(Exception.class); + + final Optional response = userService.findAccountByUsername("test"); + + assertFalse(response.isPresent()); + verify(restService).get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString()); + } + + @Test + public void testCreateAccount() { final Account account = createAccount(); - when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) - .thenReturn(Optional.of(account)); + when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenReturn(Optional.of(account)); + final Optional response = userService.createAccount(account); assertTrue(response.isPresent()); @@ -161,7 +156,7 @@ public class UserServiceTest { } @Test - public void testCreateAccount_NoAccountFound() { + public void testCreateAccount_NullAccount() { final Optional response = userService.createAccount(null); assertFalse(response.isPresent()); @@ -170,23 +165,32 @@ public class UserServiceTest { } @Test - public void testCreateAccount_catchException(){ - final Account account = createAccount(); + public void testCreateAccount_AccountCouldNotBeCreated() { + when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenReturn(Optional.empty()); - when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenThrow(Exception.class); - final Optional response = userService.createAccount(account); + final Optional response = userService.createAccount(createAccount()); assertFalse(response.isPresent()); - assertEquals(Optional.empty(), response); + verify(restService).post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); } @Test - public void testUpdateAccount(){ + public void testCreateAccount_RestServiceThrowsException() { + when(restService.post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenThrow(Exception.class); + + final Optional response = userService.createAccount(createAccount()); + + assertFalse(response.isPresent()); + verify(restService).post(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + } + + @Test + public void testUpdateAccount() { final Account account = createAccount(); account.setId(1); - when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) - .thenReturn(Optional.of(account)); + when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenReturn(Optional.of(account)); + final Optional response = userService.updateAccount(account); assertTrue(response.isPresent()); @@ -195,115 +199,106 @@ public class UserServiceTest { } @Test - public void testUpdateAccount_nullAccount(){ + public void testUpdateAccount_NullAccount() { final Optional response = userService.updateAccount(null); assertFalse(response.isPresent()); - assertEquals(Optional.empty(), response); - verify(restService, times(0)).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + verifyZeroInteractions(restService); } @Test - public void testUpdateAccount_nullId(){ + public void testUpdateAccount_IdEqualsZero() { final Account account = createAccount(); account.setId(0); final Optional response = userService.updateAccount(account); assertFalse(response.isPresent()); - assertEquals(Optional.empty(), response); - verify(restService, times(0)).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); + verifyZeroInteractions(restService); } @Test - public void testUpdateAccount_PersistenceApiResponseIsNotPresent(){ + public void testUpdateAccount_PersistenceApiResponseIsEmpty() { final Account account = createAccount(); account.setId(1); - when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) - .thenReturn(Optional.empty()); + when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenReturn(Optional.empty()); + final Optional response = userService.updateAccount(account); assertFalse(response.isPresent()); - assertEquals(Optional.empty(), response); + verify(restService).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); } @Test - public void testUpdateAccount_catchException(){ + public void testUpdateAccount_RestServiceThrowsException() { final Account account = createAccount(); account.setId(1); - when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())) - .thenThrow(Exception.class); + when(restService.patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt())).thenThrow(Exception.class); + final Optional response = userService.updateAccount(account); assertFalse(response.isPresent()); - assertEquals(Optional.empty(), response); + verify(restService).patch(anyString(), anyString(), any(TypeToken.class), anyInt(), anyInt()); } @Test - public void testDeleteAccount(){ - final boolean response= true; + public void testDeleteAccount() { final Account account = createAccount(); account.setId(1); - when(restService.delete(anyString(), anyInt(), anyInt(), any())) - .thenReturn(response); - final boolean persistenceApiResponse = userService.deleteAccount(account); + when(restService.delete(anyString(), anyInt(), anyInt(), any())).thenReturn(true); - assertNotNull(persistenceApiResponse); - assertTrue(persistenceApiResponse); - assertEquals(response, persistenceApiResponse); + final boolean isDeleteSuccessful = userService.deleteAccount(account); + + assertTrue(isDeleteSuccessful); verify(restService).delete(anyString(), anyInt(), anyInt(), any()); } @Test - public void testDeleteAccount_nullAccount(){ - final boolean persistenceApiResponse = userService.deleteAccount(null); + public void testDeleteAccount_NullAccount() { + final boolean isDeleteSuccessful = userService.deleteAccount(null); - assertNotNull(persistenceApiResponse); - assertFalse(persistenceApiResponse); - verify(restService, times(0)).delete(anyString(), anyInt(), anyInt(), any()); + assertFalse(isDeleteSuccessful); + verifyZeroInteractions(restService); } @Test - public void testDeleteAccount_nullId(){ + public void testDeleteAccount_IdEqualsZero() { final Account account = createAccount(); account.setId(0); - final boolean persistenceApiResponse = userService.deleteAccount(account); + final boolean isDeleteSuccessful = userService.deleteAccount(account); - assertNotNull(persistenceApiResponse); - assertFalse(persistenceApiResponse); - verify(restService, times(0)).delete(anyString(), anyInt(), anyInt(), any()); + assertFalse(isDeleteSuccessful); + verifyZeroInteractions(restService); } @Test - public void testDeleteAccount_persistenceApiResponseIsNotPresent(){ - final boolean response= false; + public void testDeleteAccount_PersistenceApiResponseIsEmpty() { final Account account = createAccount(); account.setId(1); - when(restService.delete(anyString(), anyInt(), anyInt(), any())) - .thenReturn(response); - final boolean persistenceApiResponse = userService.deleteAccount(account); + when(restService.delete(anyString(), anyInt(), anyInt(), any())).thenReturn(false); - assertNotNull(persistenceApiResponse); - assertFalse(persistenceApiResponse); - assertEquals(response, persistenceApiResponse); + final boolean isDeleteSuccessful = userService.deleteAccount(account); + + assertFalse(isDeleteSuccessful); verify(restService).delete(anyString(), anyInt(), anyInt(), any()); } @Test - public void testDeleteAccount_catchException(){ + public void testDeleteAccount_RestServiceThrowsException() { final Account account = createAccount(); account.setId(1); when(restService.delete(anyString(), anyInt(), anyInt(), any())).thenThrow(Exception.class); + final boolean persistenceApiResponse = userService.deleteAccount(account); - assertNotNull(persistenceApiResponse); assertFalse(persistenceApiResponse); + verify(restService).delete(anyString(), anyInt(), anyInt(), any()); }