Skip to content

Commit

Permalink
UTS-192 - Modified test case for SparkInstanceTest addressing PR comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Jose Sztul committed Dec 31, 2015
1 parent 00f075a commit 9ea3b80
Showing 1 changed file with 94 additions and 69 deletions.
163 changes: 94 additions & 69 deletions src/test/java/spark/SparkInstanceTest.java
@@ -1,168 +1,193 @@
package spark;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.powermock.reflect.Whitebox;
import spark.ssl.SslStores;

import javax.servlet.http.HttpServletResponse;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class SparkInstanceTest {

private static final String IP_ADDRESS = "127.0.0.1";
private static final int NOT_FOUND_STATUS_CODE = HttpServletResponse.SC_NOT_FOUND;

private SparkInstance sparkInstance;

@Rule
public ExpectedException thrown = ExpectedException.none();

@Before
public void test(){
sparkInstance = new SparkInstance();
}

@Test(expected = HaltException.class)
public void testHalt_withOutParameters_thenThrowHaltException(){
SparkInstance sparkInstance = new SparkInstance();
public void testHalt_whenOutParameters_thenThrowHaltException(){
sparkInstance.halt();
}

@Test(expected = HaltException.class)
public void testHalt_withStatusCode_thenThrowHaltException(){
SparkInstance sparkInstance = new SparkInstance();
sparkInstance.halt(100);
public void testHalt_whenStatusCode_thenThrowHaltException(){
sparkInstance.halt(NOT_FOUND_STATUS_CODE);
}

@Test(expected = HaltException.class)
public void testHalt_withBodyContent_thenThrowHaltException(){
SparkInstance sparkInstance = new SparkInstance();
public void testHalt_whenBodyContent_thenThrowHaltException(){
sparkInstance.halt("error");
}

@Test(expected = HaltException.class)
public void testHalt_withStatusCodeAndBodyContent_thenThrowHaltException(){
SparkInstance sparkInstance = new SparkInstance();
sparkInstance.halt(100, "error");
public void testHalt_whenStatusCodeAndBodyContent_thenThrowHaltException(){
sparkInstance.halt(NOT_FOUND_STATUS_CODE, "error");
}

@Test
public void testIpAddress_withInitializedFalse(){
SparkInstance sparkInstance = new SparkInstance();
sparkInstance.ipAddress("127.0.0.1");
public void testIpAddress_whenInitializedFalse(){
sparkInstance.ipAddress(IP_ADDRESS);

String ipAddress = Whitebox.getInternalState(sparkInstance, "ipAddress");
assertEquals("Value is not equal", "127.0.0.1", ipAddress);
assertEquals("IP address should be set to the IP address that was specified", IP_ADDRESS, ipAddress);
}

@Test(expected = IllegalStateException.class)
public void testIpAddress_withInitializedTrue_thenThrowIllegalStateException(){
SparkInstance sparkInstance = new SparkInstance();
@Test
public void testIpAddress_whenInitializedTrue_thenThrowIllegalStateException(){
thrown.expect(IllegalStateException.class);
thrown.expectMessage("This must be done before route mapping has begun");

Whitebox.setInternalState(sparkInstance, "initialized", true);
sparkInstance.ipAddress("127.0.0.1");
sparkInstance.ipAddress(IP_ADDRESS);
}

@Test
public void testSetIpAddress_withInitializedFalse(){
SparkInstance sparkInstance = new SparkInstance();
sparkInstance.setIpAddress("127.0.0.1");
public void testSetIpAddress_whenInitializedFalse(){
sparkInstance.setIpAddress(IP_ADDRESS);

String ipAddress = Whitebox.getInternalState(sparkInstance, "ipAddress");
assertEquals("Value is not equal", "127.0.0.1", ipAddress);
assertEquals("IP address should be set to the IP address that was specified", IP_ADDRESS, ipAddress);
}

@Test(expected = IllegalStateException.class)
public void testSetIpAddress_withInitializedTrue_thenThrowIllegalStateException(){
SparkInstance sparkInstance = new SparkInstance();
@Test
public void testSetIpAddress_whenInitializedTrue_thenThrowIllegalStateException(){
thrown.expect(IllegalStateException.class);
thrown.expectMessage("This must be done before route mapping has begun");

Whitebox.setInternalState(sparkInstance, "initialized", true);
sparkInstance.setIpAddress("127.0.0.1");
sparkInstance.setIpAddress(IP_ADDRESS);
}

@Test
public void testPort_withInitializedFalse(){
SparkInstance sparkInstance = new SparkInstance();
public void testPort_whenInitializedFalse(){
sparkInstance.port(8080);

int port = Whitebox.getInternalState(sparkInstance, "port");
assertEquals("Value is not equal", 8080, port);
assertEquals("Port should be set to the Port that was specified", 8080, port);
}

@Test(expected = IllegalStateException.class)
public void testPort_withInitializedTrue_thenThrowIllegalStateException(){
SparkInstance sparkInstance = new SparkInstance();
@Test
public void testPort_whenInitializedTrue_thenThrowIllegalStateException(){
thrown.expect(IllegalStateException.class);
thrown.expectMessage("This must be done before route mapping has begun");

Whitebox.setInternalState(sparkInstance, "initialized", true);
sparkInstance.port(8080);
}

@Test
public void testSetPort_withInitializedFalse(){
SparkInstance sparkInstance = new SparkInstance();
public void testSetPort_whenInitializedFalse(){
sparkInstance.setPort(8080);

int port = Whitebox.getInternalState(sparkInstance, "port");
assertEquals("Value is not equal", 8080, port);
assertEquals("Port should be set to the Port that was specified", 8080, port);
}

@Test(expected = IllegalStateException.class)
public void testSetPort_withInitializedTrue_thenThrowIllegalStateException(){
SparkInstance sparkInstance = new SparkInstance();
@Test
public void testSetPort_whenInitializedTrue_thenThrowIllegalStateException(){
thrown.expect(IllegalStateException.class);
thrown.expectMessage("This must be done before route mapping has begun");

Whitebox.setInternalState(sparkInstance, "initialized", true);
sparkInstance.setPort(8080);
}

@Test
public void testThreadPool_withOnlyMaxThreads(){
SparkInstance sparkInstance = new SparkInstance();
public void testThreadPool_whenOnlyMaxThreads(){
sparkInstance.threadPool(100);
int maxThreads = Whitebox.getInternalState(sparkInstance, "maxThreads");
int minThreads = Whitebox.getInternalState(sparkInstance, "minThreads");
int threadIdleTimeoutMillis = Whitebox.getInternalState(sparkInstance, "threadIdleTimeoutMillis");
assertEquals("Value is not equal", 100, maxThreads);
assertEquals("Value is not equal", -1, minThreads);
assertEquals("Value is not equal", -1, threadIdleTimeoutMillis);
assertEquals("Should return maxThreads specified", 100, maxThreads);
assertEquals("Should return minThreads specified", -1, minThreads);
assertEquals("Should return threadIdleTimeoutMillis specified", -1, threadIdleTimeoutMillis);
}

@Test
public void testThreadPool_withMaxMinAndTimeoutParameters(){
SparkInstance sparkInstance = new SparkInstance();
public void testThreadPool_whenMaxMinAndTimeoutParameters(){
sparkInstance.threadPool(100, 50, 75);
int maxThreads = Whitebox.getInternalState(sparkInstance, "maxThreads");
int minThreads = Whitebox.getInternalState(sparkInstance, "minThreads");
int threadIdleTimeoutMillis = Whitebox.getInternalState(sparkInstance, "threadIdleTimeoutMillis");
assertEquals("Value is not equal", 100, maxThreads);
assertEquals("Value is not equal", 50, minThreads);
assertEquals("Value is not equal", 75, threadIdleTimeoutMillis);
assertEquals("Should return maxThreads specified", 100, maxThreads);
assertEquals("Should return minThreads specified", 50, minThreads);
assertEquals("Should return threadIdleTimeoutMillis specified", 75, threadIdleTimeoutMillis);
}

@Test(expected = IllegalStateException.class)
public void testThreadPool_withMaxMinAndTimeoutParameters_thenThrowIllegalStateException(){
SparkInstance sparkInstance = new SparkInstance();
@Test
public void testThreadPool_whenMaxMinAndTimeoutParameters_thenThrowIllegalStateException(){
thrown.expect(IllegalStateException.class);
thrown.expectMessage("This must be done before route mapping has begun");

Whitebox.setInternalState(sparkInstance, "initialized", true);
sparkInstance.threadPool(100, 50, 75);
}

@Test
public void testSecure_thenReturnNewSslStores(){
SparkInstance sparkInstance = new SparkInstance();
sparkInstance.secure("keyfile", "keypassword", "truststorefile", "truststorepassword");
SslStores sslStores = Whitebox.getInternalState(sparkInstance, "sslStores");
assertNotNull("SslStores is null", sslStores);
assertEquals("keystoreFile is not equal", "keyfile", sslStores.keystoreFile());
assertEquals("keystorePassword is not equal", "keypassword", sslStores.keystorePassword());
assertEquals("trustStoreFile is not equal", "truststorefile", sslStores.trustStoreFile());
assertEquals("trustStorePassword is not equal", "truststorepassword", sslStores.trustStorePassword());
assertNotNull("Should return a SslStores because we configured it to have one", sslStores);
assertEquals("Should return keystoreFile from SslStores", "keyfile", sslStores.keystoreFile());
assertEquals("Should return keystorePassword from SslStores", "keypassword", sslStores.keystorePassword());
assertEquals("Should return trustStoreFile from SslStores", "truststorefile", sslStores.trustStoreFile());
assertEquals("Should return trustStorePassword from SslStores", "truststorepassword", sslStores.trustStorePassword());
}

@Test(expected = IllegalStateException.class)
public void testSecure_withInitializedTrue_thenThrowIllegalStateException(){
SparkInstance sparkInstance = new SparkInstance();
@Test
public void testSecure_whenInitializedTrue_thenThrowIllegalStateException(){
thrown.expect(IllegalStateException.class);
thrown.expectMessage("This must be done before route mapping has begun");

Whitebox.setInternalState(sparkInstance, "initialized", true);
sparkInstance.secure(null, null, null, null);
}

@Test(expected = IllegalArgumentException.class)
public void testSecure_withInitializedFalse_thenThrowIllegalArgumentException(){
SparkInstance sparkInstance = new SparkInstance();
@Test
public void testSecure_whenInitializedFalse_thenThrowIllegalArgumentException(){
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Must provide a keystore file to run secured");

sparkInstance.secure(null, null, null, null);
}

@Test(expected = IllegalStateException.class)
public void testWebSocketIdleTimeoutMillis_withInitializedTrue_thenThrowIllegalStateException(){
SparkInstance sparkInstance = new SparkInstance();
@Test
public void testWebSocketIdleTimeoutMillis_whenInitializedTrue_thenThrowIllegalStateException(){
thrown.expect(IllegalStateException.class);
thrown.expectMessage("This must be done before route mapping has begun");

Whitebox.setInternalState(sparkInstance, "initialized", true);
sparkInstance.webSocketIdleTimeoutMillis(100);
}

@Test(expected = IllegalStateException.class)
public void testWebSocket_withInitializedTrue_thenThrowIllegalStateException(){
SparkInstance sparkInstance = new SparkInstance();
@Test
public void testWebSocket_whenInitializedTrue_thenThrowIllegalStateException(){
thrown.expect(IllegalStateException.class);
thrown.expectMessage("This must be done before route mapping has begun");

Whitebox.setInternalState(sparkInstance, "initialized", true);
sparkInstance.webSocket("/", Object.class);
}
Expand Down

0 comments on commit 9ea3b80

Please sign in to comment.