Skip to content

Commit

Permalink
Unit tests without starting embedded webserver
Browse files Browse the repository at this point in the history
  • Loading branch information
quinarygio committed May 19, 2017
1 parent 4c10495 commit 6e2c3b0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 195 deletions.
35 changes: 0 additions & 35 deletions online-rest-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,46 +81,11 @@
<version>${resteasy.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-undertow</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>tjws</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,20 @@
package eu.itesla_project.online.rest.api.test;

import static org.mockito.Mockito.when;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.plugins.server.undertow.UndertowJaxrsServer;
import org.jboss.resteasy.test.TestPortProvider;
import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import eu.itesla_project.cases.CaseType;
import eu.itesla_project.iidm.network.Country;
import eu.itesla_project.iidm.network.Network;
import eu.itesla_project.iidm.network.NetworkFactory;
import eu.itesla_project.modules.contingencies.ActionParameters;
import eu.itesla_project.modules.online.OnlineDb;
import eu.itesla_project.modules.online.OnlineDbFactory;
import eu.itesla_project.modules.online.OnlineProcess;
import eu.itesla_project.modules.online.OnlineStep;
import eu.itesla_project.modules.online.OnlineWorkflowDetails;
import eu.itesla_project.modules.online.OnlineWorkflowParameters;
import eu.itesla_project.modules.online.OnlineWorkflowResults;
import eu.itesla_project.modules.online.OnlineWorkflowRulesResults;
import eu.itesla_project.modules.online.OnlineWorkflowWcaResults;
import eu.itesla_project.modules.online.StateProcessingStatus;
import eu.itesla_project.modules.online.TimeHorizon;
import eu.itesla_project.modules.optimizer.CCOFinalStatus;
import eu.itesla_project.online.rest.api.RestApplication;
import eu.itesla_project.online.rest.api.factories.ProcessApiServiceFactory;
import eu.itesla_project.online.rest.api.DateTimeParameter;
import eu.itesla_project.online.rest.api.ProcessApiService;
import eu.itesla_project.online.rest.api.impl.ProcessApiServiceImpl;
import eu.itesla_project.online.rest.api.test.mock.OnlineDbFactoryMock;
import eu.itesla_project.online.rest.api.util.OnlineDBUtils;
Expand All @@ -65,198 +25,125 @@
import eu.itesla_project.online.rest.model.Violation;
import eu.itesla_project.online.rest.model.WorkflowInfo;
import eu.itesla_project.online.rest.model.WorkflowResult;
import eu.itesla_project.security.LimitViolation;
import eu.itesla_project.security.LimitViolationType;

@RunWith(PowerMockRunner.class)
@PrepareForTest(ProcessApiServiceFactory.class)
@PowerMockIgnore({ "javax.management.*", "javax.net.ssl.*" })

public class ProcessApiTest {

private static UndertowJaxrsServer server;
private final ProcessDBUtils dbMock = new OnlineDBUtils(new OnlineDbFactoryMock());

@BeforeClass
public static void setUpBeforeClass() throws Exception {
server = new UndertowJaxrsServer().start();

server.deploy(RestApplication.class, "online-service");
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
server.stop();
}
private final ProcessApiService service = new ProcessApiServiceImpl(dbMock);

@Test
public void testGetProcesses() throws UnsupportedEncodingException {
PowerMockito.mockStatic(ProcessApiServiceFactory.class);
when(ProcessApiServiceFactory.getProcessApi()).thenReturn(new ProcessApiServiceImpl(dbMock));

// get all processes
Client client = ClientBuilder.newClient();
Response res = client.target(TestPortProvider.generateURL("/online-service/process")).request().get();
Response res = service.getProcessList(null, null, null, null, null, null);
Assert.assertEquals(200, res.getStatus());
JSONArray arr = new JSONArray(res.readEntity(String.class));
JSONArray arr = new JSONArray((String) res.getEntity());
Assert.assertEquals("Test get all processes", 2, arr.length());
client.close();
}

@Test
public void testGetByBasecase() throws UnsupportedEncodingException {
PowerMockito.mockStatic(ProcessApiServiceFactory.class);
when(ProcessApiServiceFactory.getProcessApi()).thenReturn(new ProcessApiServiceImpl(dbMock));
Client client = ClientBuilder.newClient();
Response res = client.target(TestPortProvider.generateURL("/online-service/process"))
.queryParam("basecase", "2016-01-11T02:00:00.000+00:00").request().get();
Response res = service.getProcessList(null, "2016-01-11T02:00:00.000+00:00", null, null, null, null);
Assert.assertEquals(200, res.getStatus());
JSONArray arr = new JSONArray(res.readEntity(String.class));
JSONArray arr = new JSONArray((String) res.getEntity());
Assert.assertEquals(1, arr.length());
JSONObject p = arr.getJSONObject(0);
JSONArray arr2 = p.getJSONArray("workflows");
JSONObject wf = arr2.getJSONObject(0);
Assert.assertEquals("Test filter by basecase", "2016-01-11T02:00:00.000+00:00", wf.get("baseCase"));
client.close();
}

@Test
public void testGetByName() throws UnsupportedEncodingException {
PowerMockito.mockStatic(ProcessApiServiceFactory.class);
when(ProcessApiServiceFactory.getProcessApi()).thenReturn(new ProcessApiServiceImpl(dbMock));
Client client = ClientBuilder.newClient();
Response res = client.target(TestPortProvider.generateURL("/online-service/process"))
.queryParam("name", "name2").request().get();
Response res = service.getProcessList(null, null, "name2", null, null, null);
Assert.assertEquals(200, res.getStatus());
JSONArray arr = new JSONArray(res.readEntity(String.class));
JSONArray arr = new JSONArray((String) res.getEntity());
Assert.assertEquals(1, arr.length());
JSONObject pn = arr.getJSONObject(0);
Assert.assertEquals("Test filter by name", "name2", pn.get("name"));
client.close();
}

@Test
public void testGetByOwner() throws UnsupportedEncodingException {
PowerMockito.mockStatic(ProcessApiServiceFactory.class);
when(ProcessApiServiceFactory.getProcessApi()).thenReturn(new ProcessApiServiceImpl(dbMock));
Client client = ClientBuilder.newClient();
Response res = client.target(TestPortProvider.generateURL("/online-service/process"))
.queryParam("owner", "owner1").request().get();
Response res = service.getProcessList("owner1", null, null, null, null, null);
Assert.assertEquals(200, res.getStatus());
JSONArray arr = new JSONArray(res.readEntity(String.class));
JSONArray arr = new JSONArray((String) res.getEntity());
Assert.assertEquals(1, arr.length());
JSONObject obj = arr.getJSONObject(0);
Assert.assertEquals("Test filter by owner", "owner1", obj.get("owner"));
client.close();
}

@Test
public void testGetByDate() throws UnsupportedEncodingException {
PowerMockito.mockStatic(ProcessApiServiceFactory.class);
when(ProcessApiServiceFactory.getProcessApi()).thenReturn(new ProcessApiServiceImpl(dbMock));
Client client = ClientBuilder.newClient();
public void testGetByDate() throws Exception {
DateTime dt = new DateTime(2016, 1, 16, 01, 0, 0, 0);
String date = dt.toDateTimeISO().toString();
long dt_millis = dt.getMillis();
Response res = client.target(TestPortProvider.generateURL("/online-service/process"))
.queryParam("date", URLEncoder.encode(date, "UTF-8")).request().get();

Response res = service.getProcessList(null, null, null, new DateTimeParameter(date), null, null);
Assert.assertEquals(200, res.getStatus());
JSONArray arr = new JSONArray(res.readEntity(String.class));
JSONArray arr = new JSONArray((String) res.getEntity());
Assert.assertEquals(1, arr.length());
JSONObject pn = arr.getJSONObject(0);
long millis = DateTime.parse((String) pn.get("date")).getMillis();
Assert.assertEquals("Test filter by date", dt_millis, millis);
client.close();
}

@Test
public void testWrongParamFormat() throws UnsupportedEncodingException {
PowerMockito.mockStatic(ProcessApiServiceFactory.class);
when(ProcessApiServiceFactory.getProcessApi()).thenReturn(new ProcessApiServiceImpl(dbMock));
Client client = ClientBuilder.newClient();
// wrong date format returns 400 error code
Response res = client.target(TestPortProvider.generateURL("/online-service/process"))
.queryParam("date", "YYYY-WWW").request().get();
Assert.assertEquals("Test wrong date parameter format", 400, res.getStatus());
res.readEntity(String.class);
client.close();
try{
DateTimeParameter dt = new DateTimeParameter("YYYY-WWW");
Assert.fail("Wrong date format accepted");
}
catch(Exception ex)
{
Response res = ((WebApplicationException) ex).getResponse();
Assert.assertEquals("Test wrong date parameter format", 400, res.getStatus());
return;
}

}

@Test
public void testGetByCreationDate() throws UnsupportedEncodingException {
PowerMockito.mockStatic(ProcessApiServiceFactory.class);
when(ProcessApiServiceFactory.getProcessApi()).thenReturn(new ProcessApiServiceImpl(dbMock));
Client client = ClientBuilder.newClient();
public void testGetByCreationDate() throws Exception {
DateTime dt = new DateTime(2016, 1, 15, 01, 10, 0, 0);
String date = dt.toDateTimeISO().toString();
long dt_millis = dt.getMillis();
Response res = client.target(TestPortProvider.generateURL("/online-service/process"))
.queryParam("creationDate", URLEncoder.encode(date, "UTF-8")).request().get();
Response res = service.getProcessList(null, null, null, null, new DateTimeParameter(date), null);
Assert.assertEquals(200, res.getStatus());

JSONArray arr = new JSONArray(res.readEntity(String.class));
JSONArray arr = new JSONArray((String) res.getEntity());
Assert.assertEquals(1, arr.length());
JSONObject pn = arr.getJSONObject(0);
long millis = DateTime.parse((String) pn.get("creationDate")).getMillis();
Assert.assertEquals("Test filter by creation date", dt_millis, millis);
client.close();
}

@Test
public void testGetByProcessId() throws UnsupportedEncodingException {
PowerMockito.mockStatic(ProcessApiServiceFactory.class);
when(ProcessApiServiceFactory.getProcessApi()).thenReturn(new ProcessApiServiceImpl(dbMock));
Client client = ClientBuilder.newClient();
Response res = client.target(TestPortProvider.generateURL("/online-service/process/2222")).request().get();
res.readEntity(String.class);
Response res = service.getProcessById("2222", null);
Assert.assertEquals("Test get workflow by processId", 200, res.getStatus());
client.close();
}

@Test
public void testProcessNotFound() throws UnsupportedEncodingException {
PowerMockito.mockStatic(ProcessApiServiceFactory.class);
when(ProcessApiServiceFactory.getProcessApi()).thenReturn(new ProcessApiServiceImpl(dbMock));
Client client = ClientBuilder.newClient();
Response res = client.target(TestPortProvider.generateURL("/online-service/process/0000")).request().get();
res.readEntity(String.class);
Response res = service.getProcessById("0000", null);
Assert.assertEquals("Test processId not found", 404, res.getStatus());
client.close();
}

@Test
public void testError() throws UnsupportedEncodingException {
PowerMockito.mockStatic(ProcessApiServiceFactory.class);
when(ProcessApiServiceFactory.getProcessApi()).thenReturn(new ProcessApiServiceImpl(dbMock));
Client client = ClientBuilder.newClient();
Response res = client.target(TestPortProvider.generateURL("/online-service/process/error")).request().get();
res.readEntity(String.class);
Response res = service.getProcessById("error", null);
Assert.assertEquals("Test get process error", 500, res.getStatus());
client.close();
}

@Test
public void testGetByProcessAndWorkflowId() throws UnsupportedEncodingException {
PowerMockito.mockStatic(ProcessApiServiceFactory.class);
when(ProcessApiServiceFactory.getProcessApi()).thenReturn(new ProcessApiServiceImpl(dbMock));
Client client = ClientBuilder.newClient();
// get workflow by processId and WorkflowId
Response res = client.target(TestPortProvider.generateURL("/online-service/process/2222/2233")).request().get();
res.readEntity(String.class);
Response res = service.getWorkflowResult("2222", "2233", null);
Assert.assertEquals("Test get workflow by processId and workflowId", 200, res.getStatus());
client.close();
}

@Test
public void testWorkflowNotFound() throws UnsupportedEncodingException {
PowerMockito.mockStatic(ProcessApiServiceFactory.class);
when(ProcessApiServiceFactory.getProcessApi()).thenReturn(new ProcessApiServiceImpl(dbMock));
Client client = ClientBuilder.newClient();
// get workflow by processId and WorkflowId
Response res = client.target(TestPortProvider.generateURL("/online-service/process/2222/8888")).request().get();
res.readEntity(String.class);
Response res = service.getWorkflowResult("2222", "8888", null);
Assert.assertEquals("Test workflowId not found", 404, res.getStatus());
client.close();
}

@Test
Expand Down Expand Up @@ -304,24 +191,14 @@ public void testModelEquality() throws UnsupportedEncodingException {

@Test
public void testGetProcessSynthesis() throws UnsupportedEncodingException {
PowerMockito.mockStatic(ProcessApiServiceFactory.class);
when(ProcessApiServiceFactory.getProcessApi()).thenReturn(new ProcessApiServiceImpl(dbMock));
Client client = ClientBuilder.newClient();
Response res = client.target(TestPortProvider.generateURL("/online-service/synthesis/1111")).request().get();
res.readEntity(String.class);
Response res = service.getProcessSynthesis("1111", null);
Assert.assertEquals("Test get process synthesis by processId", 200, res.getStatus());
client.close();
}

@Test
public void testProcessSynthesisNotfound() throws UnsupportedEncodingException {
PowerMockito.mockStatic(ProcessApiServiceFactory.class);
when(ProcessApiServiceFactory.getProcessApi()).thenReturn(new ProcessApiServiceImpl(dbMock));
Client client = ClientBuilder.newClient();
Response res = client.target(TestPortProvider.generateURL("/online-service/synthesis/1234")).request().get();
res.readEntity(String.class);
Response res = service.getProcessSynthesis("1234", null);
Assert.assertEquals("Test process synthesis not found", 404, res.getStatus());
client.close();
}


Expand Down

0 comments on commit 6e2c3b0

Please sign in to comment.