Skip to content
Snippets Groups Projects
Commit 29da3f77 authored by Thiemo Belmega's avatar Thiemo Belmega
Browse files

Implement REST Interface to get all the existing Objects of a resource type

parent 76195c50
No related branches found
No related tags found
No related merge requests found
Showing
with 148 additions and 60 deletions
......@@ -3,11 +3,13 @@ package de.unipotsdam.cs.toolup.ws.beans;
import de.unipotsdam.cs.toolup.database.DatabaseController;
import de.unipotsdam.cs.toolup.exceptions.InvalidIdException;
import de.unipotsdam.cs.toolup.model.Application;
import de.unipotsdam.cs.toolup.model.BusinessObject;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
......@@ -23,23 +25,6 @@ public class ApplicationBean extends BusinessObjectBean {
this.features = app.getRelatedFeatures();
}
public Collection<String> getCategories() {
return categories;
}
public void setCategories(Collection<String> categories) {
this.categories = categories;
}
public Collection<String> getFeatures() {
return features;
}
public void setFeatures(Collection<String> features) {
this.features = features;
}
public static ApplicationBean getBean(String id) throws InvalidIdException, SQLException, IOException {
Application app = (Application)DatabaseController.getInstance().load(id);
return new ApplicationBean(app);
......@@ -56,4 +41,30 @@ public class ApplicationBean extends BusinessObjectBean {
return appBeans;
}
public static Collection<ApplicationBean> getAllApplications() throws IOException, SQLException, InvalidIdException {
Collection<ApplicationBean> result = new HashSet<>();
Map<String, BusinessObject> allApps = DatabaseController.getInstance().loadAllFrom(DatabaseController.TABLE_NAME_APPLICATION);
for (BusinessObject app : allApps.values()) {
result.add(new ApplicationBean((Application) app));
}
return result;
}
public Collection<String> getCategories() {
return categories;
}
public void setCategories(Collection<String> categories) {
this.categories = categories;
}
public Collection<String> getFeatures() {
return features;
}
public void setFeatures(Collection<String> features) {
this.features = features;
}
}
......@@ -4,7 +4,7 @@ import de.unipotsdam.cs.toolup.model.BusinessObject;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
public class BusinessObjectBean {
public class BusinessObjectBean implements Comparable<BusinessObjectBean> {
private String id;
private String title;
......@@ -60,4 +60,10 @@ public class BusinessObjectBean {
.append(this.id)
.toHashCode();
}
@Override
public int compareTo(BusinessObjectBean o) {
return this.getId().compareTo(o.getId());
}
}
......@@ -2,11 +2,14 @@ package de.unipotsdam.cs.toolup.ws.beans;
import de.unipotsdam.cs.toolup.database.DatabaseController;
import de.unipotsdam.cs.toolup.exceptions.InvalidIdException;
import de.unipotsdam.cs.toolup.model.BusinessObject;
import de.unipotsdam.cs.toolup.model.Category;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
public class CategoryBean extends BusinessObjectBean{
......@@ -24,6 +27,15 @@ public class CategoryBean extends BusinessObjectBean{
}
public static Collection<CategoryBean> getAllCategories() throws IOException, SQLException {
Collection<CategoryBean> result = new HashSet<>();
Map<String, BusinessObject> allCats = DatabaseController.getInstance().loadAllFrom(DatabaseController.TABLE_NAME_CATEGORY);
for (BusinessObject cat : allCats.values()) {
result.add(new CategoryBean((Category) cat));
}
return result;
}
}
......
......@@ -2,11 +2,14 @@ package de.unipotsdam.cs.toolup.ws.beans;
import de.unipotsdam.cs.toolup.database.DatabaseController;
import de.unipotsdam.cs.toolup.exceptions.InvalidIdException;
import de.unipotsdam.cs.toolup.model.BusinessObject;
import de.unipotsdam.cs.toolup.model.Feature;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
public class FeatureBean extends BusinessObjectBean{
......@@ -22,6 +25,15 @@ public class FeatureBean extends BusinessObjectBean{
return new FeatureBean(feat);
}
public static Collection<FeatureBean> getAllFeatures() throws IOException, SQLException {
Collection<FeatureBean> result = new HashSet<>();
Map<String, BusinessObject> allFeats = DatabaseController.getInstance().loadAllFrom(DatabaseController.TABLE_NAME_FEATURE);
for (BusinessObject feat : allFeats.values()) {
result.add(new FeatureBean((Feature) feat));
}
return result;
}
}
......
......@@ -7,11 +7,19 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.Collection;
@Path("application")
public class ApplicationResource {
@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Collection<ApplicationBean> getAll() throws Exception {
return ApplicationBean.getAllApplications();
}
@GET
@Path("/{id}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
......
......@@ -7,15 +7,25 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.Collection;
@Path("category")
public class CategoryResource {
@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Collection<CategoryBean> getAll() throws Exception {
return CategoryBean.getAllCategories();
}
@GET
@Path("/{id}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public CategoryBean get(@PathParam("id") String id) throws Exception{
return CategoryBean.getBean(id);
}
}
......@@ -7,15 +7,26 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
@Path("feature")
public class FeatureResource {
@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Collection<FeatureBean> getAll() throws IOException, SQLException {
return FeatureBean.getAllFeatures();
}
@GET
@Path("/{id}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public FeatureBean get(@PathParam("id") String id) throws Exception {
return FeatureBean.getBean(id);
}
}
......@@ -3,17 +3,26 @@ package de.unipotsdam.cs.toolup.ws.integration;
import de.unipotsdam.cs.toolup.database.DatabaseController;
import de.unipotsdam.cs.toolup.model.BusinessObject;
import de.unipotsdam.cs.toolup.model.BusinessObjectTest;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.ContentType;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import static de.unipotsdam.cs.toolup.model.BusinessObject.*;
import static de.unipotsdam.cs.toolup.model.BusinessObjectTest.*;
import static junit.framework.Assert.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
public class IntegrationTestWebService extends AbstractTestWebService {
......@@ -25,7 +34,7 @@ public class IntegrationTestWebService extends AbstractTestWebService {
@Test
public void testThatGetRequestReturnsStatusCodeOK() throws Exception {
//arrange
HttpGet request = new HttpGet(TOOLUP_URL + APPLICATION_RESOURCE_URL + BusinessObjectTest.APPLICATION_TEST_ID_1);
HttpGet request = new HttpGet(TOOLUP_URL + APPLICATION_RESOURCE_URL + APPLICATION_TEST_ID_1);
request.addHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType());
//act
......@@ -38,7 +47,7 @@ public class IntegrationTestWebService extends AbstractTestWebService {
@Test
public void testThatGetRequestReturnsMimeTypeJson() throws Exception {
//arrange
HttpGet request = new HttpGet(TOOLUP_URL + APPLICATION_RESOURCE_URL + BusinessObjectTest.APPLICATION_TEST_ID_1);
HttpGet request = new HttpGet(TOOLUP_URL + APPLICATION_RESOURCE_URL + APPLICATION_TEST_ID_1);
request.addHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType());
//act
......@@ -48,12 +57,15 @@ public class IntegrationTestWebService extends AbstractTestWebService {
assertEquals(ContentType.APPLICATION_JSON.getMimeType(), response.getHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue());
}
@Test
public void testThatGetRequestReturnsCategoryAsJson() throws Exception {
/**
* @param id the id of a BusinessObject of the type related to the resourceUrl
*/
@Test(dataProvider = "provideIdAndResourceUrl")
public void testThatGetRequestReturnsBusinessObjectAsJson(String id, String resourceUrl) throws Exception {
//arrange
BusinessObject expectedBO = DatabaseController.getInstance().load(BusinessObjectTest.CATEGORY_TEST_ID_11);
BusinessObject expectedBO = DatabaseController.getInstance().load(id);
HttpGet request = new HttpGet(TOOLUP_URL + CATEGORY_RESOURCE_URL + BusinessObjectTest.CATEGORY_TEST_ID_11);
HttpGet request = new HttpGet(TOOLUP_URL + resourceUrl + id);
request.addHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType());
//act
......@@ -62,36 +74,29 @@ public class IntegrationTestWebService extends AbstractTestWebService {
//assert
String responsePayload = EntityUtils.toString(response.getEntity());
JSONObject actualBO = new JSONObject(responsePayload);
assertEquals(expectedBO.getUuid(), actualBO.getString(BusinessObject.JSON_KEY_ID));
assertEquals(expectedBO.getTitle(), actualBO.getString(BusinessObject.JSON_KEY_TITLE));
assertEquals(expectedBO.getDescription(), actualBO.getString(BusinessObject.JSON_KEY_DESCRIPTION));
assertEquals(expectedBO.getUuid(), actualBO.getString(JSON_KEY_ID));
assertEquals(expectedBO.getTitle(), actualBO.getString(JSON_KEY_TITLE));
assertEquals(expectedBO.getDescription(), actualBO.getString(JSON_KEY_DESCRIPTION));
}
@Test
public void testThatGetRequestReturnsFeatureAsJson() throws Exception {
//arrange
BusinessObject expectedBO = DatabaseController.getInstance().load( BusinessObjectTest.FEATURE_TEST_ID_21);
HttpGet request = new HttpGet(TOOLUP_URL + FEATURE_RESOURCE_URL + BusinessObjectTest.FEATURE_TEST_ID_21);
request.addHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType());
//act
HttpResponse response = client.execute(request);
//assert
String responsePayload = EntityUtils.toString(response.getEntity());
JSONObject actualBO = new JSONObject(responsePayload);
assertEquals(expectedBO.getUuid(), actualBO.getString(BusinessObject.JSON_KEY_ID));
assertEquals(expectedBO.getTitle(), actualBO.getString(BusinessObject.JSON_KEY_TITLE));
assertEquals(expectedBO.getDescription(), actualBO.getString(BusinessObject.JSON_KEY_DESCRIPTION));
@DataProvider
public Iterator<Object[]> provideIdAndResourceUrl() {
List<Object[]> data = Arrays.asList(
new Object[]{CATEGORY_TEST_ID_11, CATEGORY_RESOURCE_URL},
new Object[]{CATEGORY_TEST_ID_12, CATEGORY_RESOURCE_URL},
new Object[]{APPLICATION_TEST_ID_1, APPLICATION_RESOURCE_URL},
new Object[]{FEATURE_TEST_ID_22, FEATURE_RESOURCE_URL}
);
return data.iterator();
}
@Test
public void testThatGetRequestReturnsApplicationAsJson() throws Exception {
/**
* @param expectedIds a Collection of all the IDs, that the test database has in the table related to the resource url
*/
@Test(dataProvider = "provideAllIdsAndResourceUrl")
public void testThatGetRequestReturnsAllApplicationsAsJson(Collection<String> expectedIds, String resourceUrl) throws Exception {
//arrange
BusinessObject expectedBO = DatabaseController.getInstance().load(BusinessObjectTest.APPLICATION_TEST_ID_1);
HttpGet request = new HttpGet(TOOLUP_URL + APPLICATION_RESOURCE_URL + BusinessObjectTest.APPLICATION_TEST_ID_1);
HttpGet request = new HttpGet(TOOLUP_URL + resourceUrl);
request.addHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType());
//act
......@@ -99,9 +104,21 @@ public class IntegrationTestWebService extends AbstractTestWebService {
//assert
String responsePayload = EntityUtils.toString(response.getEntity());
JSONObject actualBO = new JSONObject(responsePayload);
assertEquals(expectedBO.getUuid(), actualBO.getString(BusinessObject.JSON_KEY_ID));
assertEquals(expectedBO.getTitle(), actualBO.getString(BusinessObject.JSON_KEY_TITLE));
assertEquals(expectedBO.getDescription(), actualBO.getString(BusinessObject.JSON_KEY_DESCRIPTION));
JSONArray allBOsOfThisTable = new JSONArray(responsePayload);
JSONObject obj1 = allBOsOfThisTable.getJSONObject(0);
JSONObject obj2 = allBOsOfThisTable.getJSONObject(1);
assertTrue(expectedIds.contains(obj1.getString(JSON_KEY_ID)));
assertTrue(expectedIds.contains(obj2.getString(JSON_KEY_ID)));
}
@DataProvider
public Iterator<Object[]> provideAllIdsAndResourceUrl() {
List<Object[]> data = Arrays.asList(
new Object[]{Arrays.asList(CATEGORY_TEST_ID_11, CATEGORY_TEST_ID_12), CATEGORY_RESOURCE_URL},
new Object[]{Arrays.asList(APPLICATION_TEST_ID_1, APPLICATION_TEST_ID_2), APPLICATION_RESOURCE_URL},
new Object[]{Arrays.asList(FEATURE_TEST_ID_21, FEATURE_TEST_ID_22), FEATURE_RESOURCE_URL}
);
return data.iterator();
}
}
package de.unipotsdam.cs.toolup.ws.resource;
import de.unipotsdam.cs.toolup.model.BusinessObjectTest;
import de.unipotsdam.cs.toolup.ws.beans.ApplicationBean;
import org.testng.annotations.Test;
......@@ -8,6 +8,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static de.unipotsdam.cs.toolup.model.BusinessObjectTest.*;
import static org.testng.AssertJUnit.assertTrue;
public class LookupResourceTest {
......@@ -15,11 +16,11 @@ public class LookupResourceTest {
@Test
public void testThatPostRequestReturnsMapOfApplicationBeans() throws Exception {
//arrange
ApplicationBean app = ApplicationBean.getBean(BusinessObjectTest.APPLICATION_TEST_ID_1);
ApplicationBean app = ApplicationBean.getBean(APPLICATION_TEST_ID_1);
LookupResource lookupResource = new LookupResource();
//act
Collection<ApplicationBean> apps = lookupResource.post("feature-test_id_21, feature-test_id_22");
Collection<ApplicationBean> apps = lookupResource.post(FEATURE_TEST_ID_21 + ", " + FEATURE_TEST_ID_22);
//assert
assertTrue(apps.contains(app));
......@@ -28,13 +29,13 @@ public class LookupResourceTest {
@Test
public void testThatPostRequestReturnsMapOfApplicationBeans2() throws Exception {
//arrange
ApplicationBean app1 = ApplicationBean.getBean(BusinessObjectTest.APPLICATION_TEST_ID_1);
ApplicationBean app2 = ApplicationBean.getBean(BusinessObjectTest.APPLICATION_TEST_ID_1);
ApplicationBean app1 = ApplicationBean.getBean(APPLICATION_TEST_ID_1);
ApplicationBean app2 = ApplicationBean.getBean(APPLICATION_TEST_ID_1);
List<ApplicationBean> expectedApps = Arrays.asList(app1, app2);
LookupResource lookupResource = new LookupResource();
//act
Collection<ApplicationBean> apps = lookupResource.post("feature-test_id_21");
Collection<ApplicationBean> apps = lookupResource.post(FEATURE_TEST_ID_21);
//assert
assertTrue(apps.containsAll(expectedApps));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment