Skip to content

Commit

Permalink
split into web and core packages, fleshed out functionality and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Läufer <[email protected]>
  • Loading branch information
klaeufer committed Dec 25, 2023
1 parent 555e136 commit e9886e3
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 27 deletions.
28 changes: 28 additions & 0 deletions src/main/java/edu/luc/cs/cs371/primechecker/core/PrimeChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package edu.luc.cs.cs371.primechecker.core;

import java.math.BigInteger;

public enum PrimeChecker {;

public static boolean isPrimeSlow(final BigInteger i) {
final var compareToTwo = i.compareTo(BigInteger.TWO);
if (compareToTwo < 0) {
return false;
}
if (compareToTwo == 0) {
return true;
}
if (!i.testBit(0)) {
return false;
}
final var sqroot = i.sqrt();
var k = BigInteger.valueOf(3);
while (k.compareTo(sqroot) <= 0) {
if (i.mod(k).equals(BigInteger.ZERO)) {
return false;
}
k = k.add(BigInteger.TWO);
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.luc.cs.cs371.primechecker;
package edu.luc.cs.cs371.primechecker.web;

import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
Expand Down Expand Up @@ -31,8 +31,8 @@ public HttpResponseMessage run(
context.getLogger().info("Java HTTP trigger processed a request.");

// Parse query parameter
final String query = request.getQueryParameters().get("name");
final String name = request.getBody().orElse(query);
final var query = request.getQueryParameters().get("name");
final var name = request.getBody().orElse(query);

if (name == null) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.luc.cs.cs371.primechecker;
package edu.luc.cs.cs371.primechecker.web;

import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
Expand All @@ -9,6 +9,9 @@
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;

import edu.luc.cs.cs371.primechecker.core.PrimeChecker;

import java.math.BigInteger;
import java.util.Optional;

/**
Expand All @@ -31,13 +34,19 @@ public HttpResponseMessage run(
context.getLogger().info("Java HTTP trigger processed a request.");

// Parse query parameter
final String query = request.getQueryParameters().get("number");
final String number = request.getBody().orElse(query);
final var query = request.getQueryParameters().get("number");
final var numberString = request.getBody().orElse(query);

if (number == null) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build();
if (numberString == null) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a number on the query string or in the request body").build();
} else {
return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + number).build();
final var number = new BigInteger(numberString);
final var result = PrimeChecker.isPrimeSlow(number);
if (result) {
return request.createResponseBuilder(HttpStatus.OK).body("Yay, " + number + " is a prime!").build();
} else {
return request.createResponseBuilder(HttpStatus.NOT_FOUND).body("Bummer, " + number + " is not a prime!").build();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package edu.luc.cs.cs371.primechecker.core;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.math.BigInteger;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class PrimeCheckerTest {

@ParameterizedTest
@ValueSource(ints = {-7, -1, 0, 1, 4, 6, 8, 9, 6008, 6033})
void isPrime_ShouldReturnFalse(int number) {
assertFalse(PrimeChecker.isPrimeSlow(BigInteger.valueOf(number)));
}

@ParameterizedTest
@ValueSource(ints = {2, 3, 5, 7, 17, 6007})
void isPrime_ShouldReturnTrue(int number) {
assertTrue(PrimeChecker.isPrimeSlow(BigInteger.valueOf(number)));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.luc.cs.cs371.primechecker;
package edu.luc.cs.cs371.primechecker.web;

import com.microsoft.azure.functions.*;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

Expand All @@ -26,26 +27,26 @@ public void testHttpTriggerJava() throws Exception {
@SuppressWarnings("unchecked")
final HttpRequestMessage<Optional<String>> req = mock(HttpRequestMessage.class);

final Map<String, String> queryParams = new HashMap<>();
final var queryParams = new HashMap<String, String>();
queryParams.put("name", "Azure");
doReturn(queryParams).when(req).getQueryParameters();

final Optional<String> queryBody = Optional.empty();
final var queryBody = Optional.<String>empty();
doReturn(queryBody).when(req).getBody();

doAnswer(new Answer<HttpResponseMessage.Builder>() {
@Override
public HttpResponseMessage.Builder answer(InvocationOnMock invocation) {
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
final var status = (HttpStatus) invocation.getArguments()[0];
return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status);
}
}).when(req).createResponseBuilder(any(HttpStatus.class));

final ExecutionContext context = mock(ExecutionContext.class);
final var context = mock(ExecutionContext.class);
doReturn(Logger.getGlobal()).when(context).getLogger();

// Invoke
final HttpResponseMessage ret = new HelloFunction().run(req, context);
final var ret = new HelloFunction().run(req, context);

// Verify
assertEquals(HttpStatus.OK, ret.getStatus());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.luc.cs.cs371.primechecker;
package edu.luc.cs.cs371.primechecker.web;

import com.microsoft.azure.functions.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.luc.cs.cs371.primechecker;
package edu.luc.cs.cs371.primechecker.web;

import com.microsoft.azure.functions.*;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

Expand All @@ -14,40 +15,49 @@


/**
* Unit test for HelloFunction class.
* Unit test for PrimeCheckerunction class.
*/
public class PrimeCheckerFunctionTest {
/**
* Unit test for HttpTriggerJava method.
*/
@Test
public void testHttpTriggerJava() throws Exception {
public void test6007() throws Exception {
assertEquals(HttpStatus.OK, runPrimeCheckerFunction("6007"));
}

@Test
public void test6008() throws Exception {
assertEquals(HttpStatus.NOT_FOUND, runPrimeCheckerFunction("6008"));
}

private HttpStatusType runPrimeCheckerFunction(final String numberString) throws Exception {
// Setup
@SuppressWarnings("unchecked")
final HttpRequestMessage<Optional<String>> req = mock(HttpRequestMessage.class);

final Map<String, String> queryParams = new HashMap<>();
queryParams.put("number", "6007");
final var queryParams = new HashMap<String, String>();
queryParams.put("number", numberString);
doReturn(queryParams).when(req).getQueryParameters();

final Optional<String> queryBody = Optional.empty();
final var queryBody = Optional.<String>empty();
doReturn(queryBody).when(req).getBody();

doAnswer(new Answer<HttpResponseMessage.Builder>() {
@Override
public HttpResponseMessage.Builder answer(InvocationOnMock invocation) {
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
final var status = (HttpStatus) invocation.getArguments()[0];
return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status);
}
}).when(req).createResponseBuilder(any(HttpStatus.class));

final ExecutionContext context = mock(ExecutionContext.class);
final var context = mock(ExecutionContext.class);
doReturn(Logger.getGlobal()).when(context).getLogger();

// Invoke
final HttpResponseMessage ret = new PrimeCheckerFunction().run(req, context);
final var ret = new PrimeCheckerFunction().run(req, context);

// Verify
assertEquals(HttpStatus.OK, ret.getStatus());
// Return status
return ret.getStatus();
}
}

0 comments on commit e9886e3

Please sign in to comment.