Skip to content

Commit f1811d2

Browse files
committed
Refactoring
Extracted AbstractJcrSpec Added ClientServiceSpec integration test
1 parent f631e92 commit f1811d2

File tree

10 files changed

+238
-134
lines changed

10 files changed

+238
-134
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ For the Grabbit developers, [instructions for releasing a new version of Grabbit
241241
* [Groovy v2.3.6](http://groovy.codehaus.org/Download)
242242
* [Google Protocol Buffers v2.4.1](https://code.google.com/p/protobuf/downloads/list) - The compiler and runtime library is used for Serialization and De-serialization of Data
243243
* [Spring Batch v2.2.7.RELEASE](http://docs.spring.io/spring-batch/2.2.x/downloads.html) - It is used on the server and client to read/write, marshal/unmarshall and send/receive the data to client in a controlled manner.
244-
* [Jackalope v2.0.0](https://bintray.com/twcable/aem/jackalope/2.0.0/view) - Jackalope is used for testing
245-
* [CQ Gradle Plugins v2.0.1](https://bintray.com/twcable/aem/cq-gradle-plugins/2.0.1/view) : They provide Gradle build support.
244+
* [Jackalope](https://bintray.com/twcable/aem/jackalope/) - Jackalope is used for testing
245+
* [CQ Gradle Plugins](https://bintray.com/twcable/aem/cq-gradle-plugins/) : They provide Gradle build support.
246246
* [Gradle Protocol Buffers Plugin v0.9.1](http://search.maven.org/#artifactdetails%7Cws.antonov.gradle.plugins%7Cgradle-plugin-protobuf%7C0.9.1%7Cjar) - It provides easy integration of the ProtoBuf compiler with Gradle.
247247

248248
# LICENSE

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ allprojects {
4141
// hack to allow getting to a working version of AEM 6.1 workflow-console jar
4242
// without running afoul of licensing restrictions
4343
flatDir { dirs rootProject.projectDir }
44+
45+
mavenLocal()
4446
}
4547
}
4648

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2015 Time Warner Cable, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.twcable.grabbit.server.services
18+
19+
import com.twcable.grabbit.GrabbitConfiguration
20+
import com.twcable.grabbit.client.services.ClientService
21+
import com.twcable.grabbit.client.services.impl.DefaultClientService
22+
import com.twcable.grabbit.jcr.AbstractJcrSpec
23+
import com.twcable.jackalope.impl.sling.SimpleResourceResolverFactory
24+
import com.twcable.jackalope.impl.sling.SlingRepositoryImpl
25+
import org.apache.sling.jcr.api.SlingRepository
26+
import org.springframework.context.ApplicationContext
27+
import org.springframework.context.ConfigurableApplicationContext
28+
import org.springframework.context.support.ClassPathXmlApplicationContext
29+
import org.springframework.context.support.StaticApplicationContext
30+
import spock.lang.Subject
31+
32+
@Subject(ServerService)
33+
class ClientServiceSpec extends AbstractJcrSpec {
34+
35+
SlingRepository slingRepository
36+
37+
ConfigurableApplicationContext appCtx
38+
39+
ClientService syncClientService
40+
41+
42+
def setup() {
43+
slingRepository = new SlingRepositoryImpl()
44+
45+
ApplicationContext parentAppCtx = new StaticApplicationContext()
46+
parentAppCtx.beanFactory.registerSingleton("slingRepository", slingRepository)
47+
parentAppCtx.beanFactory.registerSingleton("resourceResolverFactory", new SimpleResourceResolverFactory(slingRepository))
48+
parentAppCtx.refresh()
49+
appCtx = new ClassPathXmlApplicationContext(["META-INF/spring/client-batch-job.xml", "META-INF/spring/client-workflow-on-step.xml", "META-INF/spring/client-workflow-off-step.xml"] as String[], parentAppCtx)
50+
51+
syncClientService = new DefaultClientService(slingRepository: slingRepository,
52+
applicationContext: appCtx)
53+
}
54+
55+
56+
def "Service initiate a grab and return jobs"() {
57+
when:
58+
def jobIds = syncClientService.initiateGrab(new GrabbitConfiguration("admin", "adminPass", "testbox", "4502", false, [new GrabbitConfiguration.PathConfiguration("/content/test", [], [], false)]), "admin")
59+
60+
then:
61+
jobIds.size() == 1
62+
}
63+
64+
}

grabbit/src/main/groovy/com/twcable/grabbit/client/batch/ClientBatchJob.groovy

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import groovy.util.logging.Slf4j
2222
import org.springframework.batch.core.BatchStatus
2323
import org.springframework.batch.core.JobExecution
2424
import org.springframework.batch.core.launch.JobOperator
25-
import org.springframework.context.ConfigurableApplicationContext
25+
import org.springframework.context.ApplicationContext
2626

2727
import javax.annotation.Nonnull
2828

@@ -48,7 +48,9 @@ class ClientBatchJob {
4848
public static final String CONTENT_AFTER_DATE = "contentAfterDate"
4949
public static final String DELETE_BEFORE_WRITE = "deleteBeforeWrite"
5050

51+
@SuppressWarnings("GrFinalVariableAccess")
5152
private final Map<String, String> jobParameters
53+
@SuppressWarnings("GrFinalVariableAccess")
5254
private final JobOperator jobOperator
5355

5456

@@ -80,12 +82,12 @@ class ClientBatchJob {
8082

8183
@CompileStatic
8284
static class ServerBuilder {
83-
final ConfigurableApplicationContext configAppContext
85+
final ApplicationContext configAppContext
8486
String host
8587
String port
8688

8789

88-
ServerBuilder(ConfigurableApplicationContext configurableApplicationContext) {
90+
ServerBuilder(ApplicationContext configurableApplicationContext) {
8991
this.configAppContext = configurableApplicationContext
9092
}
9193

@@ -193,16 +195,16 @@ class ClientBatchJob {
193195

194196
ClientBatchJob build() {
195197
final jobParameters = [
196-
"timestamp" : System.currentTimeMillis() as String,
197-
"${PATH}" : pathConfiguration.path,
198-
"${HOST}" : serverBuilder.host,
199-
"${PORT}" : serverBuilder.port,
200-
"${CLIENT_USERNAME}" : credentialsBuilder.clientUsername,
201-
"${SERVER_USERNAME}" : credentialsBuilder.serverUsername,
202-
"${SERVER_PASSWORD}" : credentialsBuilder.serverPassword,
203-
"${EXCLUDE_PATHS}" : pathConfiguration.excludePaths.join("*"),
204-
"${WORKFLOW_CONFIGS}" : pathConfiguration.workflowConfigIds.join("|"),
205-
"${DELETE_BEFORE_WRITE}" : "${pathConfiguration.deleteBeforeWrite}"
198+
timestamp : System.currentTimeMillis() as String,
199+
(PATH) : pathConfiguration.path,
200+
(HOST) : serverBuilder.host,
201+
(PORT) : serverBuilder.port,
202+
(CLIENT_USERNAME) : credentialsBuilder.clientUsername,
203+
(SERVER_USERNAME) : credentialsBuilder.serverUsername,
204+
(SERVER_PASSWORD) : credentialsBuilder.serverPassword,
205+
(EXCLUDE_PATHS) : pathConfiguration.excludePaths.join("*"),
206+
(WORKFLOW_CONFIGS) : pathConfiguration.workflowConfigIds.join("|"),
207+
(DELETE_BEFORE_WRITE): "${pathConfiguration.deleteBeforeWrite}"
206208
] as Map<String, String>
207209

208210
if (deltaContentBuilder.doDeltaContent) {
@@ -212,18 +214,15 @@ class ClientBatchJob {
212214
if (lastSuccessFulJobExecution) {
213215
final contentAfterDate = DateUtil.getISOStringFromDate(lastSuccessFulJobExecution.endTime)
214216
log.info "Last Successful run for ${pathConfiguration.path} was on $contentAfterDate"
215-
return new ClientBatchJob(
216-
jobParameters + (["${CONTENT_AFTER_DATE}": contentAfterDate] as Map<String, String>),
217-
serverBuilder.configAppContext.getBean("clientJobOperator", JobOperator)
218-
)
217+
jobParameters.put(CONTENT_AFTER_DATE, contentAfterDate)
219218
}
220219
else {
221220
log.warn "There was no successful job run for $pathConfiguration.path. Defaulting to normal content grab"
222221
}
223222
}
224223
return new ClientBatchJob(
225-
jobParameters,
226-
serverBuilder.configAppContext.getBean("clientJobOperator", JobOperator)
224+
jobParameters,
225+
serverBuilder.configAppContext.getBean("clientJobOperator", JobOperator)
227226
)
228227
}
229228
}

grabbit/src/main/groovy/com/twcable/grabbit/client/services/impl/DefaultClientService.groovy

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ import com.twcable.grabbit.client.batch.ClientBatchJob
2222
import com.twcable.grabbit.client.services.ClientService
2323
import groovy.transform.CompileStatic
2424
import groovy.util.logging.Slf4j
25-
import org.apache.felix.scr.annotations.Activate
2625
import org.apache.felix.scr.annotations.Component
2726
import org.apache.felix.scr.annotations.Reference
2827
import org.apache.felix.scr.annotations.Service
2928
import org.apache.sling.jcr.api.SlingRepository
3029
import org.springframework.batch.core.JobExecution
3130
import org.springframework.batch.core.JobInstance
3231
import org.springframework.batch.core.explore.JobExplorer
33-
import org.springframework.context.ConfigurableApplicationContext
32+
import org.springframework.context.ApplicationContext
3433

3534
@Slf4j
3635
@CompileStatic
@@ -44,14 +43,8 @@ class DefaultClientService implements ClientService {
4443
@Reference(bind = 'setSlingRepository')
4544
SlingRepository slingRepository
4645

47-
@Reference(bind = 'setConfigurableApplicationContext')
48-
ConfigurableApplicationContext configurableApplicationContext
49-
50-
51-
@Activate
52-
void activate() {
53-
log.info "Activate\n\n"
54-
}
46+
@Reference(bind = 'setApplicationContext')
47+
ApplicationContext applicationContext
5548

5649

5750
@Override
@@ -67,13 +60,13 @@ class DefaultClientService implements ClientService {
6760

6861
for (PathConfiguration pathConfig : configuration.pathConfigurations) {
6962
try {
70-
final clientBatchJob = new ClientBatchJob.ServerBuilder(configurableApplicationContext)
71-
.andServer(configuration.serverHost, configuration.serverPort)
72-
.andCredentials(clientUsername, configuration.serverUsername, configuration.serverPassword)
73-
.andDoDeltaContent(doDeltaContent)
74-
.andClientJobExecutions(clientJobExecutions)
75-
.andConfiguration(pathConfig)
76-
.build()
63+
final clientBatchJob = new ClientBatchJob.ServerBuilder(applicationContext)
64+
.andServer(configuration.serverHost, configuration.serverPort)
65+
.andCredentials(clientUsername, configuration.serverUsername, configuration.serverPassword)
66+
.andDoDeltaContent(doDeltaContent)
67+
.andClientJobExecutions(clientJobExecutions)
68+
.andConfiguration(pathConfig)
69+
.build()
7770
final Long currentJobExecutionId = clientBatchJob.start()
7871
jobExecutionIds << currentJobExecutionId
7972
}
@@ -88,7 +81,7 @@ class DefaultClientService implements ClientService {
8881

8982

9083
private List<JobExecution> fetchAllClientJobExecutions() {
91-
final explorer = configurableApplicationContext.getBean("clientJobExplorer", JobExplorer)
84+
final explorer = applicationContext.getBean("clientJobExplorer", JobExplorer)
9285
final instances = explorer.getJobInstances("clientJob", 0, Integer.MAX_VALUE - 1) ?: [] as List<JobInstance>
9386
final executions = instances.collect { explorer.getJobExecutions(it) }.flatten() as List<JobExecution>
9487
executions

grabbit/src/main/groovy/com/twcable/grabbit/servlets/GrabbitServlet.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import org.apache.sling.api.SlingHttpServletRequest
3131
import org.apache.sling.api.SlingHttpServletResponse
3232
import org.apache.sling.api.servlets.SlingAllMethodsServlet
3333
import org.springframework.batch.core.explore.JobExplorer
34-
import org.springframework.context.ConfigurableApplicationContext
34+
import org.springframework.context.ApplicationContext
3535

3636
import javax.servlet.http.HttpServletResponse
3737

@@ -40,8 +40,8 @@ import javax.servlet.http.HttpServletResponse
4040
@SlingServlet(methods = ['GET', 'PUT'], resourceTypes = ['twcable:grabbit/job'])
4141
class GrabbitServlet extends SlingAllMethodsServlet {
4242

43-
@Reference(bind = 'setConfigurableApplicationContext')
44-
ConfigurableApplicationContext configurableApplicationContext
43+
@Reference(bind = 'setApplicationContext')
44+
ApplicationContext applicationContext
4545

4646
@Reference(bind = 'setClientService')
4747
ClientService clientService
@@ -114,7 +114,7 @@ class GrabbitServlet extends SlingAllMethodsServlet {
114114

115115

116116
private String getJsonString(String jobId) {
117-
final JobExplorer jobExplorer = configurableApplicationContext.getBean("clientJobExplorer", JobExplorer)
117+
final JobExplorer jobExplorer = applicationContext.getBean("clientJobExplorer", JobExplorer)
118118
if (jobId.isNumber()) {
119119
//Returns Status for A Job
120120
final ClientJobStatus status = ClientJobStatus.get(jobExplorer, Long.valueOf(jobId))

grabbit/src/test/groovy/com/twcable/grabbit/client/batch/ClientBatchJobSpec.groovy

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,62 +22,59 @@ import org.springframework.batch.core.BatchStatus
2222
import org.springframework.batch.core.JobExecution
2323
import org.springframework.batch.core.JobParametersBuilder
2424
import org.springframework.batch.core.launch.JobOperator
25-
import org.springframework.context.ConfigurableApplicationContext
25+
import org.springframework.context.ApplicationContext
2626
import spock.lang.Shared
2727
import spock.lang.Specification
2828
import spock.lang.Subject
2929
import spock.lang.Unroll
3030

3131
@Subject(ClientBatchJob)
32+
@SuppressWarnings("GroovyAccessibility")
3233
class ClientBatchJobSpec extends Specification {
3334

3435
@Shared
35-
def dateNow
36-
37-
38-
def setupSpec() {
39-
dateNow = new Date()
40-
}
36+
Date dateNow = new Date()
4137

4238

4339
@Unroll
44-
def "Make sure ClientBatch job gets configured correctly"() {
40+
def "Job Params: #path #doDeltaContent #contentAfterDate #deleteBeforeWrite"() {
4541
when:
46-
final appContext = Mock(ConfigurableApplicationContext)
42+
final appContext = Mock(ApplicationContext)
4743
appContext.getBean(_ as String, JobOperator) >> Mock(JobOperator)
4844
final job = new ClientBatchJob.ServerBuilder(appContext)
49-
.andServer("host", "port")
50-
.andCredentials("clientUser", "serverUser", "serverPass")
51-
.andDoDeltaContent(doDeltaContent)
52-
.andClientJobExecutions(jobExecutions)
53-
.andConfiguration(new GrabbitConfiguration.PathConfiguration(path, [], [], deleteBeforeWrite))
54-
.build()
45+
.andServer("host", "port")
46+
.andCredentials("clientUser", "serverUser", "serverPass")
47+
.andDoDeltaContent(doDeltaContent)
48+
.andClientJobExecutions(jobExecutions)
49+
.andConfiguration(new GrabbitConfiguration.PathConfiguration(path, [], [], deleteBeforeWrite))
50+
.build()
5551

5652
then:
5753
job != null
5854
job.jobParameters != null
59-
job.jobParameters.get("${ClientBatchJob.PATH}") == path
60-
job.jobParameters.get("${ClientBatchJob.CONTENT_AFTER_DATE}") == contentAfterDate
61-
job.jobParameters.get("${ClientBatchJob.DELETE_BEFORE_WRITE}").toBoolean() == deleteBeforeWrite
55+
job.jobParameters.get(ClientBatchJob.PATH) == path
56+
job.jobParameters.get(ClientBatchJob.CONTENT_AFTER_DATE) == contentAfterDate
57+
job.jobParameters.get(ClientBatchJob.DELETE_BEFORE_WRITE).toBoolean() == deleteBeforeWrite
6258

6359
where:
64-
doDeltaContent | path | contentAfterDate | deleteBeforeWrite
65-
true | "/path1" | DateUtil.getISOStringFromDate(dateNow) | true
66-
false | "/path1" | null | false
67-
true | "/path2" | null | true
68-
false | "/path2" | null | false
69-
60+
path | doDeltaContent | contentAfterDate | deleteBeforeWrite
61+
"/path1" | true | DateUtil.getISOStringFromDate(dateNow) | true
62+
"/path1" | false | null | false
63+
"/path2" | true | null | true
64+
"/path2" | false | null | false
7065
}
7166

7267

7368
def getJobExecutions() {
7469
def ex1 = new JobExecution(1, new JobParametersBuilder().addString("path", "/path1").toJobParameters())
7570
ex1.endTime = dateNow
7671
ex1.status = BatchStatus.COMPLETED
72+
7773
def ex2 = new JobExecution(2, new JobParametersBuilder().addString("path", "/path2").toJobParameters())
7874
ex2.endTime = dateNow
7975
ex2.status = BatchStatus.FAILED
8076

8177
[ex1, ex2]
8278
}
79+
8380
}

0 commit comments

Comments
 (0)