diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml new file mode 100644 index 0000000..1eac603 --- /dev/null +++ b/.github/workflows/github-actions.yml @@ -0,0 +1,17 @@ +name: Deploy + +on: + push: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: akhileshns/heroku-deploy@v3.12.12 # This is the action + with: + heroku_api_key: ${{secrets.HEROKU_API_KEY}} + heroku_app_name: "javawebapp-with-gareth" #Must be unique in Heroku + heroku_email: "ennoucas@gmail.com" diff --git a/src/main/java/com/develogical/QueryProcessor.java b/src/main/java/com/develogical/QueryProcessor.java index 832670e..fca25c1 100644 --- a/src/main/java/com/develogical/QueryProcessor.java +++ b/src/main/java/com/develogical/QueryProcessor.java @@ -1,5 +1,11 @@ package com.develogical; +import java.util.Arrays; +import java.util.Comparator; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + public class QueryProcessor { public String process(String query) { @@ -7,6 +13,44 @@ public String process(String query) { return "William Shakespeare (26 April 1564 - 23 April 1616) was an " + "English poet, playwright, and actor, widely regarded as the greatest " + "writer in the English language and the world's pre-eminent dramatist."; + } else if (query.toLowerCase().contains("what is your name")){ + return "Gareth & Lucas"; + } else if (query.toLowerCase().contains("which of the following numbers is the largest")){ + return Arrays.stream( + query + .replaceAll("\\s+", "") + .split(":")[2] + .split(",")) + .max(Comparator.comparingInt(Integer::parseInt)).get(); + } else if (query.toLowerCase().contains("multiplied")){ + Pattern p = Pattern.compile("\\d+"); + Matcher m = p.matcher(query.split(":")[1]); + int total = 1; + while(m.find()) { + total*= Integer.parseInt(m.group()); + } + return Integer.toString(total); + } else if (query.toLowerCase().contains("minus")){ + Pattern p = Pattern.compile("\\d+"); + Matcher m = p.matcher(query.split(":")[1]); + int total = 0; + m.find(); + total += Integer.parseInt(m.group()); + m.find(); + total -= Integer.parseInt(m.group()); + return Integer.toString(total); + } else if (query.toLowerCase().contains("minister")) { + return "2017"; + } else if (query.toLowerCase().contains("square")) { + return Arrays.stream( + query + .replaceAll("\\s+", "") + .split(":")[2] + .split(",")).filter(value -> { + double sq = Math.sqrt(Integer.parseInt(value)); + double cb = Math.cbrt(Integer.parseInt(value)); + return (sq - Math.floor(sq)) == 0 && (cb - Math.floor(cb)) == 0; + }).collect(Collectors.joining(",")); } return ""; } diff --git a/src/main/java/com/develogical/WebServer.java b/src/main/java/com/develogical/WebServer.java index c2a9e2d..f356d0e 100644 --- a/src/main/java/com/develogical/WebServer.java +++ b/src/main/java/com/develogical/WebServer.java @@ -26,6 +26,7 @@ public WebServer() throws Exception { server.start(); } + //test github action change static class Website extends HttpServlet { @Override diff --git a/src/test/java/com/develogical/QueryProcessorTest.java b/src/test/java/com/develogical/QueryProcessorTest.java index 89467ca..ef4fefb 100644 --- a/src/test/java/com/develogical/QueryProcessorTest.java +++ b/src/test/java/com/develogical/QueryProcessorTest.java @@ -2,6 +2,9 @@ import org.junit.Test; +import java.util.Arrays; +import java.util.Comparator; + import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -11,17 +14,62 @@ public class QueryProcessorTest { QueryProcessor queryProcessor = new QueryProcessor(); @Test - public void returnsEmptyStringIfCannotProcessQuery() throws Exception { + public void returnsEmptyStringIfCannotProcessQuery() { assertThat(queryProcessor.process("test"), is("")); } @Test - public void knowsAboutShakespeare() throws Exception { + public void knowsAboutShakespeare() { assertThat(queryProcessor.process("Shakespeare"), containsString("playwright")); } @Test - public void isNotCaseSensitive() throws Exception { + public void isNotCaseSensitive() { assertThat(queryProcessor.process("shakespeare"), containsString("playwright")); } + + // /api?q=xxxxx: what is your name + @Test + public void returnsName() { + assertThat(queryProcessor.process("what is your name"), is("Gareth & Lucas")); + } + + @Test + public void returnsLargest() { + assertThat(queryProcessor.process("awefawef:which of the following numbers is the largest: 2016, 20345, 132123"), is("132123")); + } + + ///api?q=416be190: what is 14 multiplied by 19 + @Test + public void returnsMultiplication() { + assertThat(queryProcessor.process("416be190:what is 14 multiplied by 19"), is("266")); + } + + // 0which of the following numbers are primes: %20584,%2047 +// @Test +// public void returnsPrimeNumbers() { +// assertThat(queryProcessor.process("416be190:which of the following numbers are primes: 584, 47"), is("47")); +// } + + // which of the following numbers is both a square and a cube: 713, 2116 + @Test + public void returnsSquareAndCube() { + assertThat(queryProcessor.process("416be190:which of the following numbers is both a square and a cube: 713, 64"), is("64")); + } + + // what is 7 minus 8 + @Test + public void returnsMinus() { + assertThat(queryProcessor.process("416be190:what is 7 minus 8"), is("-1")); + } + + // what is the 7th number in the Fibonacci% + + // which year was Theresa May first elected as the Prime Minister of Great Britain + + @Test + public void returnsWhichYearWasTheresaMayFirstElectedAsThePrimeMinisterOfGreatBritain() { + assertThat(queryProcessor.process("416be190:which year was Theresa May first elected as the Prime Minister of Great Britain"), is("2017")); + } + }