From 5de86dc4986efdf0fb379d2d219701424cee04bf Mon Sep 17 00:00:00 2001 From: BrittanyBi Date: Sun, 14 Apr 2019 20:28:11 -0600 Subject: [PATCH 1/4] PAN-64 Completed and refactored unit tests for ParserService. --- .../services/parser/ParserServiceTest.java | 216 ++++++++++++++++-- 1 file changed, 203 insertions(+), 13 deletions(-) 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 fdfecf0..f5f3436 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 @@ -9,20 +9,210 @@ import org.mockito.runners.MockitoJUnitRunner; import java.util.ArrayList; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; @RunWith(MockitoJUnitRunner.class) public class ParserServiceTest { @Mock private ParserService mockParserService; - @InjectMocks private ParserService parserService; @Test - public void testEmptyStringEqualsEmptyString() { + public void testParseUserInput_noErrors() { + final String userInput = "string"; + final Node root = new Node(userInput, null); + + final ParserService parse = mock(ParserService.class); + when(parse.parseRawInput(anyString())).thenReturn(root); + when(parse.retrieveStatements(root)).thenReturn(new ArrayList<>()); + when(parse.parseUserInput(userInput)).thenCallRealMethod(); + + assertTrue(parserService.parseUserInput(userInput)); + } + + @Test + public void testParseUserInput_thrownError() { + final String userInput = "string"; + final Node root = new Node(userInput, null); + final Exception error = new NullPointerException(); + + final ParserService parse = mock(ParserService.class); + when(parse.parseRawInput(anyString())).thenReturn(root); + when(parse.retrieveStatements(root)).thenThrow(error); + when(parse.parseUserInput(userInput)).thenCallRealMethod(); + + assertFalse(parse.parseUserInput(userInput)); + } + + @Test + public void testRecurse_NullNode() { + parserService.recurse(null); + } + + @Test + public void testRecurse_StatementContainsLet() { + final String statement = "let"; + final Node test = new Node(statement, null); + + final ParserService spy = spy(new ParserService()); + doNothing().when(spy).separateByLet(test, statement); + + spy.recurse(test); + } + + @Test + public void testRecurse_StatementContainsIf() { + final String statement = "if"; + final Node test = new Node(statement, null); + + final ParserService spy = spy(new ParserService()); + doNothing().when(spy).separateByIf(test, statement); + + spy.recurse(test); + } + + @Test + public void testRecurse_StatementContainsThen() { + final String statement = "then"; + final Node test = new Node(statement, null); + + final ParserService spy = spy(new ParserService()); + doNothing().when(spy).separateByThen(test, statement); + + spy.recurse(test); + } + + @Test + public void testSeparateByLet_StatementContainsOnlyLet() { + final String statement = "let x be even"; + final Node test = new Node(statement, null); + + final ParserService spy = spy(new ParserService()); + doNothing().when(spy).recurse(anyObject()); + spy.separateByLet(test, statement); + + final String keywordLet = test.getLeft().getStatement(); + assertEquals("let\n", keywordLet); + + final String restOfIt = test.getLeft().getCenter().getStatement(); + assertEquals(" x be even\n", restOfIt); + } + + @Test + public void testSeparateByLet_StatementContainsLetIf() { + final String statement = "let x be even. if x is prime then x is 2."; + final Node test = new Node(statement, null); + + final ParserService spy = spy(new ParserService()); + doNothing().when(spy).recurse(anyObject()); + spy.separateByLet(test, statement); + + final String keywordLet = test.getLeft().getStatement(); + assertEquals("let\n", keywordLet); + + final String restOfIt = test.getLeft().getCenter().getStatement(); + assertEquals(" x be even. \n", restOfIt); + } + + @Test + public void testSeparateByLet_StatementContainsLetThen() { + final String statement = "let x be an even prime. then x is 2."; + final Node test = new Node(statement, null); + + final ParserService spy = spy(new ParserService()); + doNothing().when(spy).recurse(anyObject()); + spy.separateByLet(test, statement); + + final String keywordLet = test.getLeft().getStatement(); + assertEquals("let\n", keywordLet); + + final String restOfIt = test.getLeft().getCenter().getStatement(); + assertEquals(" x be an even prime. \n", restOfIt); + } + + @Test + public void testSeparateByIf_StatementContainsOnlyIf() { + final String statement = "if x is even"; + final Node test = new Node(statement, null); + + final ParserService spy = spy(new ParserService()); + doNothing().when(spy).recurse(anyObject()); + spy.separateByIf(test, statement); + + final String keywordIf = test.getCenter().getStatement(); + assertEquals("if\n", keywordIf); + + final String restOfIt = test.getCenter().getCenter().getStatement(); + assertEquals(" x is even\n", restOfIt); + } + + @Test + public void testSeparateByIf_StatementContainsThen() { + final String statement = "if x is even then x^2 is even"; + final Node test = new Node(statement, null); + + final ParserService spy = spy(new ParserService()); + doNothing().when(spy).recurse(anyObject()); + spy.separateByIf(test, statement); + + final String keywordIf = test.getCenter().getStatement(); + assertEquals("if\n", keywordIf); + + final String restOfIt = test.getCenter().getCenter().getStatement(); + assertEquals(" x is even \n", restOfIt); + } + + @Test + public void testSeparateByThen_StatementContainsOnlyThen() { + final String statement = "then x^2 is even"; + final Node test = new Node(statement, null); + + final ParserService spy = spy(new ParserService()); + doNothing().when(spy).recurse(anyObject()); + spy.separateByThen(test, statement); + + final String keywordThen = test.getRight().getStatement(); + assertEquals("then\n", keywordThen); + + final String restOfIt = test.getRight().getCenter().getStatement(); + assertEquals(" x^2 is even\n", restOfIt); + } + + @Test + public void testPopulateStatementList_NodeIsNull(){ + final ArrayList list = new ArrayList<>(); + final ArrayList result = (ArrayList)parserService.populateStatementList(null, list); + + assertEquals(list, result); + } + + @Test + public void testPopulateStatementList_NodeExists(){ + final ArrayList list = new ArrayList<>(); + final String statement = "this is a statement "; + final Node input = new Node(statement, null); + + final ArrayList result = (ArrayList)parserService.populateStatementList(input, list); + + assertTrue(result.contains("this is a statement")); + } + + @Test + public void testParseRawInput_EmptyString() { + final String expected = "0: \n"; + final String actual; + + when(parserService.parseRawInput("")).thenReturn(new Node("", null)); + + actual = parserService.parseRawInput("").toString(); + + assertEquals(expected, actual); + } + + @Test + public void testParseRawInputAndRecurse_EmptyStringEqualsEmptyString() { final String expected = "0: \n"; final String actual = parserService.parseRawInput("").toString(); @@ -30,7 +220,7 @@ public class ParserServiceTest { } @Test - public void testUselessStringEqualsUselessString() { + public void testParseRawInput_UselessStringEqualsUselessString() { final String expected = "0: cat\n"; final String actual = parserService.parseRawInput("cat").toString(); @@ -38,7 +228,7 @@ public class ParserServiceTest { } @Test - public void testSingleIfReturnsIfPlusEmptyString() { + public void testParseRawInput_SingleIfReturnsIfPlusEmptyString() { final String expected = "0: if\n... 1: if\n... 2: \n\n"; final String actual = parserService.parseRawInput("if").toString(); @@ -46,7 +236,7 @@ public class ParserServiceTest { } @Test - public void testBaseCaseIfXIsEvenThenXSquaredIsEven() { + public void testParseRawInput_BaseCaseIfXIsEvenThenXSquaredIsEven() { final String expected = "0: if x is even then x^2 is even\n" + "... 1: if\n" + "... 2: x is even \n" + @@ -59,7 +249,7 @@ public class ParserServiceTest { } @Test - public void testLetXBeEvenThenXSquaredIsEven() { + public void testParseRawInput_LetXBeEvenThenXSquaredIsEven() { final String expected = "0: let x be even. then x^2 is even.\n" + "... 1: let\n" + "... 2: x be even. \n" + @@ -72,7 +262,7 @@ public class ParserServiceTest { } @Test - public void testLetIfThen() { + public void testParseRawInput_LetIfThen() { final String expected = "0: let a. if b, then c.\n" + "... 1: let\n" + "... 2: a. \n" + @@ -87,7 +277,7 @@ public class ParserServiceTest { } @Test - public void testLetStatementWithoutAnyIfOrThenStatements() { + public void testParseRawInput_LetStatementWithoutAnyIfOrThenStatements() { final String expected = "0: let a be equal to b.\n" + "... 1: let\n" + "... 2: a be equal to b.\n\n"; @@ -98,7 +288,7 @@ public class ParserServiceTest { } @Test - public void testEmptyStringReturnsEmptyList() { + public void testRetrieveStatements_EmptyStringReturnsEmptyList() { final List expectedList = new ArrayList<>(); expectedList.add(""); @@ -109,7 +299,7 @@ public class ParserServiceTest { } @Test - public void testBaseCaseReturnsXIsEven() { + public void testRetrieveStatements_BaseCaseReturnsXIsEven() { final List expectedList = new ArrayList<>(); expectedList.add("x is even"); expectedList.add("x^2 is even"); From ecd65844224fa2441d26fc5d9a7bd2f42d2ab5f7 Mon Sep 17 00:00:00 2001 From: BrittanyBi Date: Sun, 14 Apr 2019 20:29:11 -0600 Subject: [PATCH 2/4] PAN-64 Refactored ParserService. --- .../tsp/services/parser/ParserService.java | 94 +++++++++++-------- 1 file changed, 56 insertions(+), 38 deletions(-) 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 c3f37e1..e2d94a0 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 @@ -39,10 +39,8 @@ class ParserService { return root; } - private void recurse(final Node current) + public void recurse(final Node current) { - int startIndex; - int endIndex; final String statement; if (current != null) { @@ -51,60 +49,80 @@ class ParserService { return; } - String nextStatement; - if(statement.contains("let")) { - current.setLeft(new Node("let", current)); - - startIndex = statement.indexOf("let")+"let".length(); - - if(statement.contains("if")){ - endIndex = statement.indexOf("if"); - } else if(statement.contains("then")){ - endIndex = statement.indexOf("then"); - } else { - endIndex = statement.length(); - } - - nextStatement = statement.substring(startIndex, endIndex); - - current.getLeft().setCenter(new Node(nextStatement, current.getLeft())); - recurse(current.getLeft().getCenter()); + separateByLet(current, statement); } - if(statement.contains("if")) { - current.setCenter(new Node("if", current)); - - startIndex = statement.indexOf("if")+"if".length(); - endIndex = (statement.contains("then") ? statement.indexOf("then") : statement.length()); - nextStatement = statement.substring(startIndex, endIndex); - - current.getCenter().setCenter(new Node(nextStatement, current.getCenter())); - recurse(current.getCenter().getCenter()); + separateByIf(current, statement); } if(current.getStatement().contains("then")) { - current.setRight(new Node("then", current)); - - startIndex = statement.indexOf("then")+"then".length(); - nextStatement = statement.substring(startIndex); - - current.getRight().setCenter(new Node(nextStatement, current.getRight())); - recurse(current.getRight().getCenter()); + separateByThen(current, statement); } } + public void separateByLet(final Node current, final String statement){ + final int startIndex; + final int endIndex; + final String nextStatement; + + current.setLeft(new Node("let", current)); + + startIndex = statement.indexOf("let")+"let".length(); + + if(statement.contains("if")){ + endIndex = statement.indexOf("if"); + } else if(statement.contains("then")){ + endIndex = statement.indexOf("then"); + } else { + endIndex = statement.length(); + } + + nextStatement = statement.substring(startIndex, endIndex); + + current.getLeft().setCenter(new Node(nextStatement, current.getLeft())); + recurse(current.getLeft().getCenter()); + } + + public void separateByIf(final Node current, final String statement){ + final int startIndex; + final int endIndex; + final String nextStatement; + + current.setCenter(new Node("if", current)); + + startIndex = statement.indexOf("if")+"if".length(); + endIndex = (statement.contains("then") ? statement.indexOf("then") : statement.length()); + nextStatement = statement.substring(startIndex, endIndex); + + current.getCenter().setCenter(new Node(nextStatement, current.getCenter())); + recurse(current.getCenter().getCenter()); + } + + public void separateByThen(final Node current, final String statement){ + final int startIndex; + final String nextStatement; + + current.setRight(new Node("then", current)); + + startIndex = statement.indexOf("then")+"then".length(); + nextStatement = statement.substring(startIndex); + + current.getRight().setCenter(new Node(nextStatement, current.getRight())); + recurse(current.getRight().getCenter()); + } + public List retrieveStatements(final Node parsedTree) { return populateStatementList(parsedTree, new ArrayList<>()); } - private ArrayList populateStatementList(final Node node, final ArrayList statementList) + public ArrayList populateStatementList(final Node node, final ArrayList statementList) { if(node == null) { From 6ab0470fcada4d648fe8bfb4c5b93ccd22b6e74f Mon Sep 17 00:00:00 2001 From: BrittanyBi Date: Mon, 15 Apr 2019 20:49:56 -0600 Subject: [PATCH 3/4] PAN-64 Fixed bug in tests that was failing the 1st Travis build. --- .../msudenver/tsp/services/parser/ParserServiceTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 f5f3436..a7a3bf3 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 @@ -204,9 +204,9 @@ public class ParserServiceTest { final String expected = "0: \n"; final String actual; - when(parserService.parseRawInput("")).thenReturn(new Node("", null)); - - actual = parserService.parseRawInput("").toString(); + final ParserService spy = spy(new ParserService()); + when(spy.parseRawInput("")).thenReturn(new Node("", null)); + actual = spy.parseRawInput("").toString(); assertEquals(expected, actual); } From 7465568c97586a9c28e450b9639b4d154d2d3f35 Mon Sep 17 00:00:00 2001 From: dantanxiaotian Date: Wed, 1 May 2019 14:55:38 -0600 Subject: [PATCH 4/4] Add loginpage controller, login jsp and logincontroller unit test. --- build.gradle | 1 + .../msudenver/tsp/website/Application.java | 8 ++- .../website/controller/LogInController.java | 52 +++++++++++++++++++ src/main/resources/application.properties | 6 ++- src/main/webapp/WEB-INF/jsp/LogIn.jsp | 35 +++++++++++++ src/main/webapp/WEB-INF/jsp/index.jsp | 1 + .../controller/LogInControllerTest.java | 24 +++++++++ 7 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 src/main/java/edu/msudenver/tsp/website/controller/LogInController.java create mode 100644 src/main/webapp/WEB-INF/jsp/LogIn.jsp create mode 100644 src/test/java/edu/msudenver/tsp/website/controller/LogInControllerTest.java diff --git a/build.gradle b/build.gradle index d2d9c24..6e26871 100644 --- a/build.gradle +++ b/build.gradle @@ -99,6 +99,7 @@ repositories { } dependencies { + compile project(':services') compile group: 'org.jacoco', name: 'org.jacoco.core', version: '0.8.3' compile 'org.codehaus.groovy:groovy-all:2.3.11' compile 'org.apache.commons:commons-lang3:3.5' diff --git a/src/main/java/edu/msudenver/tsp/website/Application.java b/src/main/java/edu/msudenver/tsp/website/Application.java index 54da024..1b826ed 100644 --- a/src/main/java/edu/msudenver/tsp/website/Application.java +++ b/src/main/java/edu/msudenver/tsp/website/Application.java @@ -2,11 +2,17 @@ package edu.msudenver.tsp.website; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; @SpringBootApplication +@ComponentScan("edu.msudenver.tsp") + public class Application { - public static void main(final String[] args) { + + public static void main(String[] args) { SpringApplication.run(Application.class, args); } + + } 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..556c8d5 --- /dev/null +++ b/src/main/java/edu/msudenver/tsp/website/controller/LogInController.java @@ -0,0 +1,52 @@ +package edu.msudenver.tsp.website.controller; + + +import edu.msudenver.tsp.services.UserService; +import edu.msudenver.tsp.services.dto.Account; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +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.bind.annotation.RequestParam; +import org.springframework.web.servlet.ModelAndView; + +import java.util.Optional; + +@Slf4j +@Controller +@AllArgsConstructor +@RequestMapping("/login") +public class LogInController { + @Autowired + private final UserService userService; + + @GetMapping({"/", ""}) + public ModelAndView enterLogInPage() { + LOG.info("Received request to display the log in page: returning model with name 'LogIn'"); + return new ModelAndView("LogIn"); + } + + @PostMapping({"/", ""}) + public String singIn(Model model, @RequestParam("username") String username, @RequestParam("password") String password) { + Optional findUserByUsername = userService.findAccountByUsername(username); + if(findUserByUsername.isPresent()){ + if (password.equals(findUserByUsername.get().getPassword())) { + model.addAttribute("username", username); + model.addAttribute("password", password); + return "index"; + } + else { + model.addAttribute("error", "your username and password is invalid"); + return "LogIn"; + } + } + else { + model.addAttribute("error", "username and password can not be empty"); + return "LogIn"; + } + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f3ce324..09b74b5 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,2 +1,6 @@ spring.mvc.view.prefix:/WEB-INF/jsp/ -spring.mvc.view.suffix:.jsp \ No newline at end of file +spring.mvc.view.suffix:.jsp + +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 diff --git a/src/main/webapp/WEB-INF/jsp/LogIn.jsp b/src/main/webapp/WEB-INF/jsp/LogIn.jsp new file mode 100644 index 0000000..2670147 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/LogIn.jsp @@ -0,0 +1,35 @@ +<%-- + Created by IntelliJ IDEA. + User: tianliu + Date: 2019-04-24 + Time: 16:25 + To change this template use File | Settings | File Templates. +--%> +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> + + + + + + + + Log in with your account + + +
+
+

Log in

+
+
+
+ ${error}
+ + +

Create an account

+
+
+
+ + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/jsp/index.jsp b/src/main/webapp/WEB-INF/jsp/index.jsp index a31785b..d54c2ac 100644 --- a/src/main/webapp/WEB-INF/jsp/index.jsp +++ b/src/main/webapp/WEB-INF/jsp/index.jsp @@ -21,6 +21,7 @@

Hello! Welcome to Pandamoniumâ„¢ Theorem Prover!!

Click on this link to visit theorem entering page. +

Sign In

diff --git a/src/test/java/edu/msudenver/tsp/website/controller/LogInControllerTest.java b/src/test/java/edu/msudenver/tsp/website/controller/LogInControllerTest.java new file mode 100644 index 0000000..6012386 --- /dev/null +++ b/src/test/java/edu/msudenver/tsp/website/controller/LogInControllerTest.java @@ -0,0 +1,24 @@ +package edu.msudenver.tsp.website.controller; + +import edu.msudenver.tsp.services.UserService; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.servlet.ModelAndView; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + + + +public class LogInControllerTest { + @Autowired UserService userService; + private final LogInController logInController = new LogInController(userService); + + @Test + public void testEnterTheoremPage() { + ModelAndView modelAndView = logInController.enterLogInPage(); + assertNotNull(modelAndView); + assertEquals("LogIn", modelAndView.getViewName()); + + } +}