Skip to content

Commit

Permalink
MODSER-57: Generate/create piece set endpoints to return same objects (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
Jack-Golding authored Oct 25, 2024
1 parent 766cb9b commit 4438ac8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ class PredictedPieceSetController extends OkapiTenantAwareController<PredictedPi
PieceGenerationService pieceGenerationService
PieceLabellingService pieceLabellingService

// This takes in a JSON shape and outputs predicted pieces without saving domain objects
def generatePredictedPiecesTransient() {
JSONObject data = request.JSON
JSONArray startingValuesJson = data?.startingValues ?: []

SerialRuleset ruleset = new SerialRuleset(data)

private PredictedPieceSet setupPredictedPieces(JSONObject data, JSONArray startingValuesJson, SerialRuleset ruleset) {
//TODO Not super happy with the implementation of this conditional, however the JSONArray .get() freaks out over null array elements vs empty
// This conditional is to check if the starting array contains elements and if they are of the older/newer shape
if(!startingValuesJson?.toString()?.contains('userConfiguredTemplateMetadataType') && startingValuesJson.size()){
Expand All @@ -54,23 +48,6 @@ class PredictedPieceSetController extends OkapiTenantAwareController<PredictedPi
ArrayList<InternalPiece> 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<UserConfiguredTemplateMetadata> startingValues = new ArrayList<UserConfiguredTemplateMetadata>(data?.startingValues ?: [])

ArrayList<InternalPiece> 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)

Expand All @@ -83,11 +60,34 @@ class PredictedPieceSetController extends OkapiTenantAwareController<PredictedPi
startDate: data?.startDate,
firstPieceTemplateMetadata: firstPieceTemplateMetadata,
nextPieceTemplateMetadata: nextPieceTemplateMetadata
// TODO Check that this should be a flush
]).save(flush: true, failOnError: true)
])

return pps

}

// This takes in a JSON shape and outputs predicted pieces without saving domain objects
def generatePredictedPiecesTransient() {
JSONObject data = request.JSON
JSONArray startingValuesJson = data?.startingValues ?: []

SerialRuleset ruleset = new SerialRuleset(data)

PredictedPieceSet pps = setupPredictedPieces(data, startingValuesJson, ruleset)

respond pps
}

def generatePredictedPieces() {
JSONObject data = request.JSON
JSONArray startingValuesJson = data?.startingValues ?: []

SerialRuleset ruleset = SerialRuleset.get(data?.id)

PredictedPieceSet pps = setupPredictedPieces(data, startingValuesJson, ruleset)
// TODO Check that this should be a flush
pps.save(flush: true, failOnError: true)
respond pps
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ public class PieceGenerationService {
Integer currentTimeUnitPeriod = 1
LocalDate currentTimeUnit = startDate

// Map<String, ChronoField> 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:[
Expand All @@ -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:[
Expand All @@ -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:[
Expand All @@ -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:[
Expand All @@ -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:[
Expand All @@ -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:[
Expand All @@ -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:[
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions service/src/integration-test/resources/ruleset_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
},
"patternType": "day_month",
"pattern": {
"day": "1",
"day": "8",
"month": {
"value": "january"
}
Expand Down Expand Up @@ -186,7 +186,7 @@
},
"patternType": "day",
"pattern": {
"day": "1"
"day": "2"
}
},
{
Expand All @@ -196,7 +196,7 @@
"patternType": "day_weekday",
"pattern": {
"weekday": {
"value": "tuesday"
"value": "wednesday"
}
}
},
Expand Down Expand Up @@ -275,7 +275,7 @@
"patternType": "issue_week",
"pattern": {
"issue": "1",
"week": "50"
"week": "30"
}
},
{
Expand Down

0 comments on commit 4438ac8

Please sign in to comment.