Skip to content

Commit

Permalink
Issue #181: Set next jobInstanceID to last jobInstanceId + 1
Browse files Browse the repository at this point in the history
  • Loading branch information
bsaylor authored and jbornemann committed May 24, 2017
1 parent c087244 commit 4027c75
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,21 @@ class JcrGrabbitJobInstanceDao extends AbstractJcrDao implements GrabbitJobInsta
if (!resolver) throw new IllegalArgumentException("resolver == null")

final rootResource = resolver.getResource(JOB_INSTANCE_ROOT)
final nextId = (rootResource.children.asList().size() + 1) as Long

final lastInstance = rootResource?.children?.asList()?.max { Resource resource ->
final properties = resource.adaptTo(ValueMap)
properties[INSTANCE_ID] as Long
}
final lastInstanceProperties = lastInstance?.adaptTo(ValueMap)
Long nextId = lastInstanceProperties?.get(INSTANCE_ID) as Long
if (!nextId) {
nextId = 1
} else {
nextId += 1
}

log.debug "Next JobInstance Id : $nextId"
nextId

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,55 @@ class JcrGrabbitJobInstanceDaoSpec extends Specification {
result.size() == 2
}

def "CreateJobInstance for empty repository"() {
setup:
final builder =
node("var",
node("grabbit",
node("job",
node("repository",
node("jobInstances"),
node("jobExecutions")
)
)
)
)
final emptyMockFactory = new SimpleResourceResolverFactory(repository(builder).build())

when:
final jobInstanceDao = new JcrGrabbitJobInstanceDao(emptyMockFactory)
final result = jobInstanceDao.createJobInstance("FirstJob", new JobParameters())

then:
result.id == 1
}

def "CreateJobInstance for partial repository"() {
setup:
final builder =
node("var",
node("grabbit",
node("job",
node("repository",
node("jobInstances",
node("2",
property(INSTANCE_ID, 2),
property(NAME, "someOtherJob"),
)
),
node("jobExecutions")
)
)
)
)
final partialMockFactory = new SimpleResourceResolverFactory(repository(builder).build())

when:
final jobInstanceDao = new JcrGrabbitJobInstanceDao(partialMockFactory)
final result = jobInstanceDao.createJobInstance("FirstJob", new JobParameters())

then:
result.id == 3
}

}

0 comments on commit 4027c75

Please sign in to comment.