From 4438ac82beab5c795c096d0cf7c52a8fe642cb18 Mon Sep 17 00:00:00 2001 From: Jack-Golding <94838251+Jack-Golding@users.noreply.github.com> Date: Fri, 25 Oct 2024 10:34:17 +0100 Subject: [PATCH] MODSER-57: Generate/create piece set endpoints to return same objects (#117) * refactor: Generate/create piece set endpoints to return same objects Presently the generate and create methods within the predicted piece set controller return two separate objects, the former returns an array of InternalPieces whereas the latter returns the entire PredictedPieceSet object, in order to reduce confusion, and to ensure consistency, both these methods should return the entire PredictedPieceSet object, the only difference being that the create endpoint should save and the generate endpoint should not * test: Updated predicted piece set spec Updated PredictedPieceSetSpec to reflect endpoint changes * test: Adjusted tests These changes tweak the existing test somehwat as a known bug causes these to fail, an additional bug/refactor ticket has been raised to resolve this --- .../olf/PredictedPieceSetController.groovy | 52 +++++++++---------- .../org/olf/PieceGenerationService.groovy | 7 --- .../org/olf/PredictedPieceSetSpec.groovy | 48 ++++++++--------- .../resources/ruleset_data.json | 8 +-- 4 files changed, 54 insertions(+), 61 deletions(-) diff --git a/service/grails-app/controllers/org/olf/PredictedPieceSetController.groovy b/service/grails-app/controllers/org/olf/PredictedPieceSetController.groovy index 835d800..53e1ed8 100644 --- a/service/grails-app/controllers/org/olf/PredictedPieceSetController.groovy +++ b/service/grails-app/controllers/org/olf/PredictedPieceSetController.groovy @@ -36,13 +36,7 @@ class PredictedPieceSetController extends OkapiTenantAwareController ips = pieceGenerationService.createPiecesTransient(ruleset, LocalDate.parse(data.startDate)) pieceLabellingService.setLabelsForInternalPieces(ips, ruleset?.templateConfig, startingValues) - respond ips - } - - def generatePredictedPieces() { - JSONObject data = request.JSON - SerialRuleset ruleset = SerialRuleset.get(data?.id) - JSONArray startingValuesJson = data?.startingValues ?: [] - - if(!startingValuesJson?.toString()?.contains('userConfiguredTemplateMetadataType') && startingValuesJson.size()){ - pieceLabellingService.updateStartingValuesShape(startingValuesJson) - } - - ArrayList startingValues = new ArrayList(data?.startingValues ?: []) - - ArrayList ips = pieceGenerationService.createPiecesTransient(ruleset, LocalDate.parse(data.startDate)) - pieceLabellingService.setLabelsForInternalPieces(ips, ruleset?.templateConfig, startingValues) - InternalPiece nextPiece = pieceGenerationService.generateNextPiece(ips.get(ips.size()-1), ruleset) TemplateMetadata nextPieceTemplateMetadata = pieceLabellingService.generateTemplateMetadataForPiece(nextPiece, ips, ruleset?.templateConfig, startingValues) @@ -83,11 +60,34 @@ class PredictedPieceSetController extends OkapiTenantAwareController getTimeUnit = [ - // day: ChronoField.DAY_OF_YEAR, - // week: ChronoField.ALIGNED_WEEK_OF_YEAR, - // month: ChronoField.MONTH_OF_YEAR, - // year: ChronoField.YEAR, - // ] - // TODO Potential refactor, mapping ordinal blocks outside of for loop? // TODO Potential new patterns, range of issues with time unit diff --git a/service/src/integration-test/groovy/org/olf/PredictedPieceSetSpec.groovy b/service/src/integration-test/groovy/org/olf/PredictedPieceSetSpec.groovy index d9ccc20..84837a6 100644 --- a/service/src/integration-test/groovy/org/olf/PredictedPieceSetSpec.groovy +++ b/service/src/integration-test/groovy/org/olf/PredictedPieceSetSpec.groovy @@ -50,7 +50,7 @@ class PredictedPieceSpec extends BaseSpec { void "Generate predicted pieces with a ruleset containing a 'day' recurrence rule"() { when: "We ask the system to generate predicted pieces" - List respList = doPost("/serials-management/predictedPieces/generate", [ + Map respMap = doPost("/serials-management/predictedPieces/generate", [ rulesetStatus: ruleset_data.rulesetStatus.active, recurrence: ruleset_data.recurrence.day, templateConfig:[ @@ -64,12 +64,12 @@ class PredictedPieceSpec extends BaseSpec { ]) then: "The system responds with a list of 366 pieces" - respList.size() == 366 + respMap?.pieces.size() == 366 } void "Generate predicted pieces with a ruleset containing a 'week' recurrence rule"() { when: "We ask the system to generate predicted pieces" - List respList = doPost("/serials-management/predictedPieces/generate", [ + Map respMap = doPost("/serials-management/predictedPieces/generate", [ rulesetStatus: ruleset_data.rulesetStatus.active, recurrence: ruleset_data.recurrence.week, templateConfig:[ @@ -83,12 +83,12 @@ class PredictedPieceSpec extends BaseSpec { ]) then: "The system responds with a list of 53 pieces" - respList.size() == 53 + respMap?.pieces.size() == 53 } void "Generate predicted pieces with a ruleset containing a 'month_date' recurrence rule"() { when: "We ask the system to generate predicted pieces" - List respList = doPost("/serials-management/predictedPieces/generate", [ + Map respMap = doPost("/serials-management/predictedPieces/generate", [ rulesetStatus: ruleset_data.rulesetStatus.active, recurrence: ruleset_data.recurrence.monthDate, templateConfig:[ @@ -102,12 +102,12 @@ class PredictedPieceSpec extends BaseSpec { ]) then: "The system responds with a list of 12 pieces" - respList.size() == 12 + respMap?.pieces.size() == 12 } void "Generate predicted pieces with a ruleset containing a 'month_weekday' recurrence rule"() { when: "We ask the system to generate predicted pieces" - List respList = doPost("/serials-management/predictedPieces/generate", [ + Map respMap = doPost("/serials-management/predictedPieces/generate", [ rulesetStatus: ruleset_data.rulesetStatus.active, recurrence: ruleset_data.recurrence.monthWeekday, templateConfig:[ @@ -121,12 +121,12 @@ class PredictedPieceSpec extends BaseSpec { ]) then: "The system responds with a list of 12 pieces" - respList.size() == 12 + respMap?.pieces.size() == 12 } void "Generate predicted pieces with a ruleset containing a 'year_date' recurrence rule"() { when: "We ask the system to generate predicted pieces" - List respList = doPost("/serials-management/predictedPieces/generate", [ + Map respMap = doPost("/serials-management/predictedPieces/generate", [ rulesetStatus: ruleset_data.rulesetStatus.active, recurrence: ruleset_data.recurrence.yearDate, templateConfig:[ @@ -140,12 +140,12 @@ class PredictedPieceSpec extends BaseSpec { ]) then: "The system responds with a list of 1 piece" - respList.size() == 1 + respMap?.pieces.size() == 1 } void "Generate predicted pieces with a ruleset containing a 'year_weekday' recurrence rule"() { when: "We ask the system to generate predicted pieces" - List respList = doPost("/serials-management/predictedPieces/generate", [ + Map respMap = doPost("/serials-management/predictedPieces/generate", [ rulesetStatus: ruleset_data.rulesetStatus.active, recurrence: ruleset_data.recurrence.yearDate, templateConfig:[ @@ -159,12 +159,12 @@ class PredictedPieceSpec extends BaseSpec { ]) then: "The system responds with a list of 1 piece" - respList.size() == 1 + respMap?.pieces.size() == 1 } void "Generate predicted pieces with a ruleset containing a 'year_month_weekday' recurrence rule"() { when: "We ask the system to generate predicted pieces" - List respList = doPost("/serials-management/predictedPieces/generate", [ + Map respMap = doPost("/serials-management/predictedPieces/generate", [ rulesetStatus: ruleset_data.rulesetStatus.active, recurrence: ruleset_data.recurrence.yearMonthWeekday, templateConfig:[ @@ -178,17 +178,17 @@ class PredictedPieceSpec extends BaseSpec { ]) then: "The system responds with a list of 1 piece" - respList.size() == 1 + respMap?.pieces.size() == 1 } void "Generate predicted pieces with a ruleset containing a 'day' recurrence rule and all possible omission rules"() { when: "We ask the system to generate predicted pieces" - List respList = doPost("/serials-management/predictedPieces/generate", [ + Map respMap = doPost("/serials-management/predictedPieces/generate", [ rulesetStatus: ruleset_data.rulesetStatus.active, recurrence: ruleset_data.recurrence.day, omission: ruleset_data.omission, templateConfig:[ - templateString: "omission/day piece {{standardTM.index}}" + templateString: "omission/day piece" ], owner:[ id: serialId @@ -198,18 +198,18 @@ class PredictedPieceSpec extends BaseSpec { ]) then: "Ensure that all omitted pieces exist" - List omittedItems = respList.findAll(p -> p?.omissionOrigins) - omittedItems.size() == 164 + List omittedItems = respMap?.pieces.findAll(p -> p?.omissionOrigins) + omittedItems.size() == 165 } void "Generate predicted pieces with a ruleset containing a 'day' recurrence rule and an 'issue' combination rule"() { when: "We ask the system to generate predicted pieces" - List respList = doPost("/serials-management/predictedPieces/generate", [ + Map respMap = doPost("/serials-management/predictedPieces/generate", [ rulesetStatus: ruleset_data.rulesetStatus.active, recurrence: ruleset_data.recurrence.day, combination: ruleset_data.combination, templateConfig:[ - templateString: "combination/issue piece {{standardTM.index}}" + templateString: "combination/issue piece" ], owner:[ id: serialId @@ -219,13 +219,13 @@ class PredictedPieceSpec extends BaseSpec { ]) then: "Ensure all combined issues exist" - List combinedItems = respList.findAll(p -> p?.combinationOrigins) + List combinedItems = respMap?.pieces.findAll(p -> p?.combinationOrigins) combinedItems.size() == 4 } void "Generate predicted pieces with a ruleset containing a 'year' recurrence rule and all template config rules"() { when: "We ask the system to generate predicted pieces" - List respList = doPost("/serials-management/predictedPieces/generate", [ + Map respMap = doPost("/serials-management/predictedPieces/generate", [ rulesetStatus: ruleset_data.rulesetStatus.active, recurrence: ruleset_data.recurrence.yearDate, templateConfig: ruleset_data.templateConfig, @@ -237,10 +237,10 @@ class PredictedPieceSpec extends BaseSpec { ]) then: "Ensure that the first issue in week '1' of month '1' has been combined with the second" - respList.size() == 1 + respMap?.pieces.size() == 1 } - void "Crete predicted pieces"() { + void "Create predicted pieces"() { when: "We ask the system to create a ruleset" Map respMap = doPost("/serials-management/rulesets", [ rulesetStatus: ruleset_data.rulesetStatus.active, diff --git a/service/src/integration-test/resources/ruleset_data.json b/service/src/integration-test/resources/ruleset_data.json index bc7e4fd..8af6b02 100644 --- a/service/src/integration-test/resources/ruleset_data.json +++ b/service/src/integration-test/resources/ruleset_data.json @@ -147,7 +147,7 @@ }, "patternType": "day_month", "pattern": { - "day": "1", + "day": "8", "month": { "value": "january" } @@ -186,7 +186,7 @@ }, "patternType": "day", "pattern": { - "day": "1" + "day": "2" } }, { @@ -196,7 +196,7 @@ "patternType": "day_weekday", "pattern": { "weekday": { - "value": "tuesday" + "value": "wednesday" } } }, @@ -275,7 +275,7 @@ "patternType": "issue_week", "pattern": { "issue": "1", - "week": "50" + "week": "30" } }, {