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

Load related Applications/Categories when loading a BusinessObject

parent a3d2974a
No related branches found
No related tags found
No related merge requests found
package de.unipotsdam.cs.toolup.database;
import java.sql.SQLException;
import de.unipotsdam.cs.toolup.model.Application;
import de.unipotsdam.cs.toolup.model.BusinessObject;
public class BusinessObjectFactory {
public static BusinessObject createBusinessObject(String id, String title,
String description) {
String description) throws SQLException {
int indexOfFirstSlash = id.indexOf('/');
String className = id.substring(0, indexOfFirstSlash);
switch (className.toLowerCase()){
case "application": return new Application(id, title, description);
case "category": return new Category(id, title, description);
case "application": return new Application(id, title, description, DatabaseController.loadRelatedCategoriesForApp(id));
case "category": return new Category(id, title, description, DatabaseController.loadRelatedApplicationsForCat(id));
case "feature": return new Feature(id, title, description);
default: throw new UnsupportedOperationException("No class defined for this prefix:" + className);
}
......
......@@ -5,6 +5,8 @@ import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;
import de.unipotsdam.cs.toolup.model.BusinessObject;
......@@ -29,4 +31,24 @@ public class DatabaseController {
return uuid.substring(0, indexOfFirstSlash);
}
public static Set<String> loadRelation(String tableName, String keyColumnName, String resultColumnName, String id) throws SQLException {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&pw=");
Statement statement = connection.createStatement();
String query = "SELECT * FROM " + tableName + " WHERE " + keyColumnName + " = '" + id + "';";
ResultSet res = statement.executeQuery(query);
Set<String> relatedIds = new HashSet<String>();
while (res.next()) {
relatedIds.add(res.getString(resultColumnName));
}
return relatedIds;
}
public static Set<String> loadRelatedCategoriesForApp(String id) throws SQLException {
return loadRelation("application_belongs_to_category", "application_uuid", "category_uuid", id);
}
public static Set<String> loadRelatedApplicationsForCat(String id) throws SQLException {
return loadRelation("application_belongs_to_category", "category_uuid", "application_uuid", id);
}
}
package de.unipotsdam.cs.toolup.model;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
public class Application extends BusinessObject {
public Application(String uuid, String title, String description) {
private final Collection<String> relatedCategories;
public Application(String uuid, String title, String description, Set<String> relatedCategories) {
super(uuid, title, description);
this.relatedCategories = relatedCategories;
}
public Collection<String> getRelatedCategories() {
return new HashSet<String>(this.relatedCategories);
}
}
package de.unipotsdam.cs.toolup.database;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import de.unipotsdam.cs.toolup.model.BusinessObject;
public class Category extends BusinessObject {
public Category(String uuid, String title, String description) {
private final Collection<String> relatedApplications;
public Category(String uuid, String title, String description, Set<String> relatedApplications) {
super(uuid, title, description);
this.relatedApplications = relatedApplications;
}
public Collection<String> getRelatedApplications() {
return new HashSet<String>(this.relatedApplications);
}
}
package de.unipotsdam.cs.toolup.database;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
......@@ -15,6 +18,11 @@ import de.unipotsdam.cs.toolup.model.BusinessObject;
public class DatabaseControllerTest {
private static final String FEATURE_TEST_ID_21 = "feature/test_id_21";
private static final String APPLICATION_TEST_ID_2 = "application/test_id_2";
private static final String APPLICATION_TEST_ID_1 = "application/test_id_1";
private static final String CATEGORY_TEST_ID_11 = "category/test_id_11";
@Test(dataProvider = PROVIDE_BUSINESS_OBJECTS)
public void testThatLoadedBusinessObjectHasExpectedValues(String expectedTitle, String expectedDescription, Class<? extends BusinessObject> expectedClass, String id) throws SQLException {
//arrange
......@@ -41,10 +49,10 @@ public class DatabaseControllerTest {
public Object[][] provideBusinessObjects(){
return new Object[][]{
{"Dropbox","Dropbox Description",Application.class,"application/test_id_1"},
{"Box.UP","Box.UP Description",Application.class,"application/test_id_2"},
{"Cloud Speicher","Cloud Speicher Description",Category.class,"category/test_id_11"},
{"Kalender anlegen","Kalender anlegen Description",Feature.class,"feature/test_id_21"}
{"Dropbox","Dropbox Description",Application.class,APPLICATION_TEST_ID_1},
{"Box.UP","Box.UP Description",Application.class,APPLICATION_TEST_ID_2},
{"Cloud Speicher","Cloud Speicher Description",Category.class,CATEGORY_TEST_ID_11},
{"Kalender anlegen","Kalender anlegen Description",Feature.class,FEATURE_TEST_ID_21}
};
}
......@@ -69,12 +77,36 @@ public class DatabaseControllerTest {
public Object[][] provideSampleIds(){
return new Object[][]{
{"application","application/123456"},
{"feature","feature/123456"},
{"category","category/123456"}
{"application",APPLICATION_TEST_ID_1},
{"feature",FEATURE_TEST_ID_21},
{"category",CATEGORY_TEST_ID_11}
};
}
@Test
public void testThatLoadedCategoryHasRelatedApplications() throws SQLException {
//arrange
Collection<String> expectedAppIds = Arrays.asList(new String[] {APPLICATION_TEST_ID_1, APPLICATION_TEST_ID_2});
//act
Category cat = (Category) DatabaseController.load(CATEGORY_TEST_ID_11);
//assert
assertTrue(cat.getRelatedApplications().containsAll(expectedAppIds));
}
@Test
public void testThatLoadedApplicationHasRelatedCategories() throws SQLException {
//arrange
Collection<String> expectedCatIds = Arrays.asList(new String[] {CATEGORY_TEST_ID_11});
//act
Application app = (Application) DatabaseController.load(APPLICATION_TEST_ID_1);
//assert
assertTrue(app.getRelatedCategories().containsAll(expectedCatIds));
}
}
......@@ -12,10 +12,10 @@ public class ApplicationTest {
@Test
public void testThatAnApplicationDoesNotEqualAnOtherApplication() {
//arrange
BusinessObject anOtherApp = new Application(TEST_ID_1,null,null);
BusinessObject anOtherApp = new Application(TEST_ID_1,null,null, null);
//act
BusinessObject newApp = new Application(TEST_ID_2,null,null);
BusinessObject newApp = new Application(TEST_ID_2,null,null, null);
//assert
assertFalse(anOtherApp.equals(newApp));
......@@ -24,10 +24,10 @@ public class ApplicationTest {
@Test
public void testThatAnApplicationDoesEqualItself() {
//arrange
BusinessObject anOtherApp = new Application(TEST_ID_1,null,null);
BusinessObject anOtherApp = new Application(TEST_ID_1,null,null, null);
//act
BusinessObject newApp = new Application(TEST_ID_1,null,null);
BusinessObject newApp = new Application(TEST_ID_1,null,null, null);
//assert
assertTrue(anOtherApp.equals(newApp));
......
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