diff --git a/challenger/src/main/java/uk/co/compendiumdev/challenge/apimodel/ChallengeThingifier.java b/challenger/src/main/java/uk/co/compendiumdev/challenge/apimodel/ChallengeThingifier.java index 040f1b98..a6feb517 100644 --- a/challenger/src/main/java/uk/co/compendiumdev/challenge/apimodel/ChallengeThingifier.java +++ b/challenger/src/main/java/uk/co/compendiumdev/challenge/apimodel/ChallengeThingifier.java @@ -9,6 +9,7 @@ import uk.co.compendiumdev.thingifier.core.domain.definitions.field.definition.Field; import uk.co.compendiumdev.thingifier.core.domain.definitions.field.definition.FieldType; import uk.co.compendiumdev.thingifier.core.domain.instances.EntityInstance; +import uk.co.compendiumdev.thingifier.core.domain.instances.EntityInstanceCollection; public class ChallengeThingifier { @@ -46,14 +47,20 @@ public void populateThingifierFrom(ChallengeDefinitions challengeDefinitions) { // create all instances from the definitions, then when we want to // set all the status codes to the specific challenger status for (ChallengeDefinitionData challenge : challengeDefinitions.getChallenges()) { - challengeThingifier.getThingInstancesNamed(challengeDefn.getName(), EntityRelModel.DEFAULT_DATABASE_NAME) - .createManagedInstance(). + EntityInstanceCollection instances = challengeThingifier.getThingInstancesNamed(challengeDefn.getName(), EntityRelModel.DEFAULT_DATABASE_NAME); + createManagedInstance(instances). overrideValue("id", challenge.id). setValue("name", challenge.name). setValue("description", challenge.description); } } + private EntityInstance createManagedInstance(EntityInstanceCollection entityStorage) { + EntityInstance instance = new EntityInstance(entityStorage.definition()); + entityStorage.addInstance(instance); + return instance; + } + public void populateThingifierFromStatus(ChallengerAuthData challenger){ ChallengerAuthData challengerToUse = challenger; diff --git a/challenger/src/main/java/uk/co/compendiumdev/challenge/apimodel/TodoAPIDataPopulator.java b/challenger/src/main/java/uk/co/compendiumdev/challenge/apimodel/TodoAPIDataPopulator.java index f6046907..40ffbb3e 100644 --- a/challenger/src/main/java/uk/co/compendiumdev/challenge/apimodel/TodoAPIDataPopulator.java +++ b/challenger/src/main/java/uk/co/compendiumdev/challenge/apimodel/TodoAPIDataPopulator.java @@ -3,6 +3,7 @@ import uk.co.compendiumdev.thingifier.core.EntityRelModel; import uk.co.compendiumdev.thingifier.core.domain.definitions.ERSchema; import uk.co.compendiumdev.thingifier.core.domain.instances.ERInstanceData; +import uk.co.compendiumdev.thingifier.core.domain.instances.EntityInstance; import uk.co.compendiumdev.thingifier.core.domain.instances.EntityInstanceCollection; import uk.co.compendiumdev.thingifier.core.domain.datapopulator.DataPopulator; @@ -26,8 +27,9 @@ public void populate(final ERSchema schema, final ERInstanceData database) { EntityInstanceCollection todo = database.getInstanceCollectionForEntityNamed("todo"); for(String todoItem : todos){ - todo.createManagedInstance(). - setValue("title", todoItem); + EntityInstance instance = new EntityInstance(todo.definition()); + todo.addInstance(instance); + instance.setValue("title", todoItem); } } } diff --git a/challenger/src/main/java/uk/co/compendiumdev/challenge/practicemodes/simpleapi/SimpleAPITestDataPopulator.java b/challenger/src/main/java/uk/co/compendiumdev/challenge/practicemodes/simpleapi/SimpleAPITestDataPopulator.java index 39b34349..e855ddb9 100644 --- a/challenger/src/main/java/uk/co/compendiumdev/challenge/practicemodes/simpleapi/SimpleAPITestDataPopulator.java +++ b/challenger/src/main/java/uk/co/compendiumdev/challenge/practicemodes/simpleapi/SimpleAPITestDataPopulator.java @@ -3,6 +3,7 @@ import uk.co.compendiumdev.thingifier.core.domain.datapopulator.DataPopulator; import uk.co.compendiumdev.thingifier.core.domain.definitions.ERSchema; import uk.co.compendiumdev.thingifier.core.domain.instances.ERInstanceData; +import uk.co.compendiumdev.thingifier.core.domain.instances.EntityInstance; import uk.co.compendiumdev.thingifier.core.domain.instances.EntityInstanceCollection; import java.util.Random; @@ -26,7 +27,7 @@ public void populate(final ERSchema schema, final ERInstanceData database) { Random random = new Random(); for(String type : types){ - items.createManagedInstance(). + createManagedInstance(items). setValue("type", type). setValue("numberinstock", String.valueOf(random.nextInt(20))). setValue("isbn13", randomIsbn(random)). @@ -35,6 +36,13 @@ public void populate(final ERSchema schema, final ERInstanceData database) { } } + + private EntityInstance createManagedInstance(EntityInstanceCollection entityStorage) { + EntityInstance instance = new EntityInstance(entityStorage.definition()); + entityStorage.addInstance(instance); + return instance; + } + private String randomIsbn(Random random){ String isbn13 = "xxx-x-xx-xxxxxx-x"; diff --git a/challenger/src/main/java/uk/co/compendiumdev/challenge/practicemodes/simulation/SimulationRoutes.java b/challenger/src/main/java/uk/co/compendiumdev/challenge/practicemodes/simulation/SimulationRoutes.java index 4753ee96..9c8e9a81 100644 --- a/challenger/src/main/java/uk/co/compendiumdev/challenge/practicemodes/simulation/SimulationRoutes.java +++ b/challenger/src/main/java/uk/co/compendiumdev/challenge/practicemodes/simulation/SimulationRoutes.java @@ -45,13 +45,13 @@ public SimulationRoutes(DefaultGUIHTML guiTemplates){ public void setUpData(){ // fake the data storage - this.simulation = new Thingifier(); + simulation = new Thingifier(); simulation.setDocumentation("Simulation Mode", "A simulated API, each request generates a new set of data but responses are processed by an API handler."); - this.entityDefn = this.simulation.defineThing("entity", "entities"); + entityDefn = simulation.defineThing("entity", "entities"); - this.entityDefn.addAsPrimaryKeyField(Field.is("id", FieldType.AUTO_INCREMENT)); - this.entityDefn.addFields( + entityDefn.addAsPrimaryKeyField(Field.is("id", FieldType.AUTO_INCREMENT)); + entityDefn.addFields( Field.is("name", FieldType.STRING). makeMandatory(). withValidation(new MaximumLengthValidationRule(50)). @@ -61,22 +61,22 @@ public void setUpData(){ withValidation(new MaximumLengthValidationRule(200)) ); - this.entityStorage = this.simulation.getThingInstancesNamed("entity", EntityRelModel.DEFAULT_DATABASE_NAME); + entityStorage = simulation.getThingInstancesNamed("entity", EntityRelModel.DEFAULT_DATABASE_NAME); for(int id=1; id<=10; id++){ - this.entityStorage.createManagedInstance(). - //setValue("id", String.valueOf(id)). - setValue("name", "entity number " +id); + createManagedInstance(entityStorage). + //setValue("id", String.valueOf(id)). + setValue("name", "entity number " +id); } - this.entityStorage.createManagedInstance(). - //setValue("id", String.valueOf(id)). - setValue("name", "bob"); + createManagedInstance(entityStorage). + //setValue("id", String.valueOf(id)). + setValue("name", "bob"); // this gives us access to the common http processing functions - this.httpApi = new ThingifierHttpApi(this.simulation); - this.jsonThing = new JsonThing(this.simulation.apiConfig().jsonOutput()); + httpApi = new ThingifierHttpApi(simulation); + jsonThing = new JsonThing(simulation.apiConfig().jsonOutput()); apiDocDefn = new ThingifierApiDocumentationDefn(); apiDocDefn.setThingifier(simulation); @@ -108,6 +108,12 @@ public void setUpData(){ } + private EntityInstance createManagedInstance(EntityInstanceCollection entityStorage) { + EntityInstance instance = new EntityInstance(entityStorage.definition()); + entityStorage.addInstance(instance); + return instance; + } + public void configure() { setUpData(); @@ -143,14 +149,14 @@ public void configure() { // process it because the request validated List instances = new ArrayList(); - for (EntityInstance possible : this.entityStorage.getInstances()) { + for (EntityInstance possible : entityStorage.getInstances()) { if (!idsToRemove.contains( possible.getFieldValue("id").asInteger())) { instances.add(possible); } } - Thingifier cloned = this.simulation.cloneWithDifferentData(instances); + Thingifier cloned = simulation.cloneWithDifferentData(instances); return new RestApiGetHandler(cloned).handle("entities", anHttpApiRequest.getFilterableQueryParams(), anHttpApiRequest.getHeaders()); }; @@ -173,7 +179,7 @@ public void configure() { // process it because the request validated String id = anHttpApiRequest.getUrlParam(":id"); - EntityInstance instance = this.entityStorage.findInstanceByPrimaryKey(id); + EntityInstance instance = entityStorage.findInstanceByPrimaryKey(id); if (instance == null) { response = ApiResponse.error404("Could not find Entity with ID " + id); } else { @@ -218,9 +224,9 @@ public void configure() { return new SparkApiRequestResponseHandler(request, result, simulation). usingHandler((anHttpApiRequest) -> { - return ApiResponse.created(this.entityStorage. + return ApiResponse.created(entityStorage. findInstanceByPrimaryKey("11"), - this.simulation.apiConfig()); + simulation.apiConfig()); }).handle(); }); @@ -231,8 +237,8 @@ public void configure() { if (id.equals("11")) { // we can create id 11 response = ApiResponse.created( - this.entityStorage.findInstanceByPrimaryKey("11"), - this.simulation.apiConfig()); + entityStorage.findInstanceByPrimaryKey("11"), + simulation.apiConfig()); } else { if (id.equals("10")) { // 10 is the entity we amend to name:eris @@ -240,7 +246,7 @@ public void configure() { overrideValue("id", "10").setValue("name", "eris"); response = ApiResponse.success().returnSingleInstance(fake); } else { - final EntityInstance instance = this.entityStorage.findInstanceByPrimaryKey(id); + final EntityInstance instance = entityStorage.findInstanceByPrimaryKey(id); if (instance == null) { if (anHttpApiRequest.getVerb() == HttpApiRequest.VERB.POST) { response = ApiResponse.error404("Could not find Entity with ID " + id); @@ -279,7 +285,7 @@ public void configure() { // we can delete id 9 response = new ApiResponse(204); } else { - final EntityInstance instance = this.entityStorage.findInstanceByPrimaryKey(id); + final EntityInstance instance = entityStorage.findInstanceByPrimaryKey(id); if (instance == null) { response = ApiResponse.error404("Could not find Entity with ID " + id); } else { diff --git a/ercoremodel/src/main/java/uk/co/compendiumdev/thingifier/core/domain/instances/EntityInstanceCollection.java b/ercoremodel/src/main/java/uk/co/compendiumdev/thingifier/core/domain/instances/EntityInstanceCollection.java index 4cf6ce78..ba2efd05 100644 --- a/ercoremodel/src/main/java/uk/co/compendiumdev/thingifier/core/domain/instances/EntityInstanceCollection.java +++ b/ercoremodel/src/main/java/uk/co/compendiumdev/thingifier/core/domain/instances/EntityInstanceCollection.java @@ -64,7 +64,7 @@ public EntityInstanceCollection addInstances(List addInstances) return this; } - public EntityInstanceCollection addInstance(EntityInstance instance) { + public EntityInstance addInstance(EntityInstance instance) { if(instance.getEntity()!=definition){ throw new RuntimeException(String.format( @@ -134,15 +134,13 @@ public EntityInstanceCollection addInstance(EntityInstance instance) { } } - return this; + return instance; } /* create and add */ // TODO: this looks like it was added to support testing, consider removing and adding to a test helper public EntityInstance createManagedInstance() { - EntityInstance instance = new EntityInstance(definition); - addInstance(instance); - return instance; + return addInstance( new EntityInstance(definition)); } public int countInstances() {