Skip to content

Commit 959ae1b

Browse files
Merge pull request #94 from wttech/mocks
Mocks
2 parents bdb0439 + b333c0f commit 959ae1b

File tree

67 files changed

+1413
-280
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1413
-280
lines changed

Taskfile.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,12 @@ tasks:
253253
- sh npmw install
254254
- sh npmw run dev
255255

256-
develop:frontend:format:
256+
develop:frontend:fix:
257257
desc: format frontend code
258258
dir: ui.frontend
259-
cmd: sh npmw run format
259+
cmds:
260+
- sh npmw run format
261+
- sh npmw run lint
260262

261263
release:
262264
desc: Release a new version

core/src/main/java/com/vml/es/aem/acm/core/assist/Assistancer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private synchronized void maybeUpdateVariablesCache(ResourceResolver resolver) {
152152
LOG.info("Variables cache - updating");
153153
try (ExecutionContext context = executor.createContext(
154154
ExecutionId.generate(), ExecutionMode.PARSE, Code.consoleMinimal(), resolver)) {
155-
variablesCache = context.getBindingVariables();
155+
variablesCache = context.getCodeContext().getBindingVariables();
156156
variablesCacheTimestamp = currentTime;
157157
}
158158
LOG.info("Variables cache - updated");
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.vml.es.aem.acm.core.code;
2+
3+
import com.vml.es.aem.acm.core.acl.Acl;
4+
import com.vml.es.aem.acm.core.code.script.ExtensionScript;
5+
import com.vml.es.aem.acm.core.format.Formatter;
6+
import com.vml.es.aem.acm.core.mock.MockContext;
7+
import com.vml.es.aem.acm.core.osgi.OsgiContext;
8+
import com.vml.es.aem.acm.core.replication.Activator;
9+
import com.vml.es.aem.acm.core.repo.Repo;
10+
import com.vml.es.aem.acm.core.script.ScriptRepository;
11+
import com.vml.es.aem.acm.core.script.ScriptType;
12+
import groovy.lang.Binding;
13+
import java.util.List;
14+
import java.util.Map;
15+
import java.util.stream.Collectors;
16+
import org.apache.sling.api.resource.ResourceResolver;
17+
import org.slf4j.LoggerFactory;
18+
19+
public class CodeContext {
20+
21+
private final OsgiContext osgiContext;
22+
23+
private final ResourceResolver resourceResolver;
24+
25+
private final Binding binding;
26+
27+
private final List<ExtensionScript> extensionScripts;
28+
29+
public CodeContext(OsgiContext osgiContext, ResourceResolver resourceResolver) {
30+
this.osgiContext = osgiContext;
31+
this.resourceResolver = resourceResolver;
32+
this.binding = createBinding(osgiContext, resourceResolver);
33+
this.extensionScripts = findExtensionScripts(resourceResolver);
34+
}
35+
36+
private List<ExtensionScript> findExtensionScripts(ResourceResolver resourceResolver) {
37+
return new ScriptRepository(resourceResolver)
38+
.findAll(ScriptType.EXTENSION)
39+
.map(s -> new ExtensionScript(this, s))
40+
.collect(Collectors.toList());
41+
}
42+
43+
public Binding createBinding(OsgiContext osgiContext, ResourceResolver resourceResolver) {
44+
Binding result = new Binding();
45+
46+
result.setVariable("log", LoggerFactory.getLogger(getClass()));
47+
result.setVariable("resourceResolver", resourceResolver);
48+
result.setVariable("osgi", osgiContext);
49+
result.setVariable("repo", new Repo(resourceResolver));
50+
result.setVariable("acl", new Acl(resourceResolver));
51+
result.setVariable("formatter", new Formatter());
52+
result.setVariable("activator", new Activator(resourceResolver, osgiContext.getReplicator()));
53+
54+
return result;
55+
}
56+
57+
public void prepareRun(ExecutionContext executionContext) {
58+
for (ExtensionScript script : extensionScripts) {
59+
script.prepareRun(executionContext);
60+
}
61+
}
62+
63+
public void completeRun(Execution execution) {
64+
for (ExtensionScript script : extensionScripts) {
65+
script.completeRun(execution);
66+
}
67+
}
68+
69+
public void prepareMock(MockContext mockContext) {
70+
for (ExtensionScript script : extensionScripts) {
71+
script.prepareMock(mockContext);
72+
}
73+
}
74+
75+
public Binding getBinding() {
76+
return binding;
77+
}
78+
79+
@SuppressWarnings("unchecked")
80+
public List<Variable> getBindingVariables() {
81+
Map<String, Object> variables = binding.getVariables();
82+
return variables.entrySet().stream()
83+
.map(entry -> new Variable(entry.getKey(), entry.getValue().getClass()))
84+
.collect(Collectors.toList());
85+
}
86+
87+
public ResourceResolver getResourceResolver() {
88+
return resourceResolver;
89+
}
90+
91+
public OsgiContext getOsgiContext() {
92+
return osgiContext;
93+
}
94+
}

core/src/main/java/com/vml/es/aem/acm/core/code/Condition.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.vml.es.aem.acm.core.code;
22

3+
import com.vml.es.aem.acm.core.osgi.InstanceInfo;
34
import com.vml.es.aem.acm.core.osgi.InstanceType;
45
import com.vml.es.aem.acm.core.script.ScriptScheduler;
56
import com.vml.es.aem.acm.core.util.DateUtils;
@@ -17,7 +18,8 @@ public class Condition {
1718

1819
public Condition(ExecutionContext executionContext) {
1920
this.executionContext = executionContext;
20-
this.executionHistory = new ExecutionHistory(executionContext.getResourceResolver());
21+
this.executionHistory =
22+
new ExecutionHistory(executionContext.getCodeContext().getResourceResolver());
2123
}
2224

2325
public boolean always() {
@@ -59,15 +61,11 @@ public boolean idleSelf() {
5961
}
6062

6163
public Stream<Execution> queuedExecutions() {
62-
return executionContext.getOsgiContext().getExecutionQueue().findAll().filter(e -> !isSelfExecution(e));
64+
return getExecutionQueue().findAll().filter(e -> !isSelfExecution(e));
6365
}
6466

6567
public Stream<Execution> queuedSelfExecutions() {
66-
return executionContext
67-
.getOsgiContext()
68-
.getExecutionQueue()
69-
.findAll()
70-
.filter(e -> !isSelfExecution(e) && isSameExecutable(e));
68+
return getExecutionQueue().findAll().filter(e -> !isSelfExecution(e) && isSameExecutable(e));
7169
}
7270

7371
public boolean isSelfExecution(Execution e) {
@@ -79,6 +77,10 @@ public boolean isSameExecutable(Execution e) {
7977
e.getExecutable().getId(), executionContext.getExecutable().getId());
8078
}
8179

80+
private ExecutionQueue getExecutionQueue() {
81+
return executionContext.getCodeContext().getOsgiContext().getExecutionQueue();
82+
}
83+
8284
// Time period-based
8385

8486
public boolean everyMinute() {
@@ -319,13 +321,14 @@ public boolean isDate(ZonedDateTime zonedDateTime) {
319321
}
320322

321323
public boolean isDate(LocalDateTime localDateTime) {
322-
long intervalMillis = executionContext
323-
.getOsgiContext()
324-
.getService(ScriptScheduler.class)
325-
.getIntervalMillis();
324+
long intervalMillis = getScriptScheduler().getIntervalMillis();
326325
return DateUtils.isInRange(localDateTime, LocalDateTime.now(), intervalMillis);
327326
}
328327

328+
private ScriptScheduler getScriptScheduler() {
329+
return executionContext.getCodeContext().getOsgiContext().getService(ScriptScheduler.class);
330+
}
331+
329332
// Duration-based since the last execution
330333

331334
public Duration passedDuration() {
@@ -359,30 +362,34 @@ public boolean passedDays(long days) {
359362
// Instance-based
360363

361364
public boolean isInstanceRunMode(String runMode) {
362-
return executionContext.getOsgiContext().getInstanceInfo().isRunMode(runMode);
365+
return getInstanceInfo().isRunMode(runMode);
366+
}
367+
368+
private InstanceInfo getInstanceInfo() {
369+
return executionContext.getCodeContext().getOsgiContext().getInstanceInfo();
363370
}
364371

365372
public boolean isInstanceAuthor() {
366-
return executionContext.getOsgiContext().getInstanceInfo().isAuthor();
373+
return getInstanceInfo().isAuthor();
367374
}
368375

369376
public boolean isInstancePublish() {
370-
return executionContext.getOsgiContext().getInstanceInfo().isPublish();
377+
return getInstanceInfo().isPublish();
371378
}
372379

373380
public boolean isInstanceOnPrem() {
374-
return executionContext.getOsgiContext().getInstanceInfo().getType() == InstanceType.ON_PREM;
381+
return getInstanceInfo().getType() == InstanceType.ON_PREM;
375382
}
376383

377384
public boolean isInstanceCloud() {
378-
return executionContext.getOsgiContext().getInstanceInfo().getType().isCloud();
385+
return getInstanceInfo().getType().isCloud();
379386
}
380387

381388
public boolean isInstanceCloudContainer() {
382-
return executionContext.getOsgiContext().getInstanceInfo().getType() == InstanceType.CLOUD_CONTAINER;
389+
return getInstanceInfo().getType() == InstanceType.CLOUD_CONTAINER;
383390
}
384391

385392
public boolean isInstanceCloudSdk() {
386-
return executionContext.getOsgiContext().getInstanceInfo().getType() == InstanceType.CLOUD_SDK;
393+
return getInstanceInfo().getType() == InstanceType.CLOUD_SDK;
387394
}
388395
}
Lines changed: 16 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
package com.vml.es.aem.acm.core.code;
22

3-
import com.vml.es.aem.acm.core.acl.Acl;
4-
import com.vml.es.aem.acm.core.format.Formatter;
5-
import com.vml.es.aem.acm.core.osgi.OsgiContext;
6-
import com.vml.es.aem.acm.core.replication.Activator;
7-
import com.vml.es.aem.acm.core.repo.Repo;
83
import groovy.lang.Binding;
9-
import java.util.List;
10-
import java.util.Map;
11-
import java.util.stream.Collectors;
12-
import org.apache.sling.api.resource.ResourceResolver;
13-
import org.slf4j.Logger;
144
import org.slf4j.LoggerFactory;
155

166
public class ExecutionContext implements AutoCloseable {
@@ -25,36 +15,24 @@ public class ExecutionContext implements AutoCloseable {
2515

2616
private final Executable executable;
2717

28-
private final OsgiContext osgiContext;
29-
30-
private final ResourceResolver resourceResolver;
31-
32-
private final Extender extender;
18+
private final CodeContext codeContext;
3319

3420
private boolean history = true;
3521

3622
private boolean debug = false;
3723

3824
private final Arguments arguments = new Arguments();
3925

40-
private Binding binding = new Binding();
41-
4226
public ExecutionContext(
43-
String id,
44-
ExecutionMode mode,
45-
Executor executor,
46-
Executable executable,
47-
OsgiContext osgiContext,
48-
ResourceResolver resourceResolver) {
27+
String id, ExecutionMode mode, Executor executor, Executable executable, CodeContext codeContext) {
4928
this.id = id;
5029
this.mode = mode;
5130
this.output = mode == ExecutionMode.RUN ? new OutputFile(id) : new OutputString();
5231
this.executor = executor;
5332
this.executable = executable;
54-
this.osgiContext = osgiContext;
55-
this.resourceResolver = resourceResolver;
56-
this.binding = createBinding(resourceResolver);
57-
this.extender = new Extender(this);
33+
this.codeContext = codeContext;
34+
35+
customizeBinding();
5836
}
5937

6038
public String getId() {
@@ -73,16 +51,8 @@ public Executable getExecutable() {
7351
return executable;
7452
}
7553

76-
public Extender getExtender() {
77-
return extender;
78-
}
79-
80-
public ResourceResolver getResourceResolver() {
81-
return resourceResolver;
82-
}
83-
84-
public OsgiContext getOsgiContext() {
85-
return osgiContext;
54+
public CodeContext getCodeContext() {
55+
return codeContext;
8656
}
8757

8858
public ExecutionMode getMode() {
@@ -109,37 +79,15 @@ public Arguments getArguments() {
10979
return arguments;
11080
}
11181

112-
public Binding getBinding() {
113-
return binding;
114-
}
115-
116-
@SuppressWarnings("unchecked")
117-
public List<Variable> getBindingVariables() {
118-
Map<String, Object> variables = binding.getVariables();
119-
return variables.entrySet().stream()
120-
.map(entry -> new Variable(entry.getKey(), entry.getValue().getClass()))
121-
.collect(Collectors.toList());
122-
}
123-
124-
private Binding createBinding(ResourceResolver resourceResolver) {
125-
Binding result = new Binding();
126-
127-
result.setVariable("args", arguments);
128-
result.setVariable("condition", new Condition(this));
129-
result.setVariable("log", createLogger(executable));
130-
result.setVariable("out", new CodePrintStream(this));
131-
result.setVariable("resourceResolver", resourceResolver);
132-
result.setVariable("osgi", osgiContext);
133-
result.setVariable("repo", new Repo(resourceResolver));
134-
result.setVariable("acl", new Acl(resourceResolver));
135-
result.setVariable("formatter", new Formatter());
136-
result.setVariable("activator", new Activator(resourceResolver, osgiContext.getReplicator()));
137-
138-
return result;
139-
}
82+
private void customizeBinding() {
83+
Binding binding = getCodeContext().getBinding();
14084

141-
private Logger createLogger(Executable executable) {
142-
return LoggerFactory.getLogger(String.format("%s(%s)", getClass().getName(), executable.getId()));
85+
binding.setVariable("args", arguments);
86+
binding.setVariable("condition", new Condition(this));
87+
binding.setVariable(
88+
"log",
89+
LoggerFactory.getLogger(String.format("%s(%s)", getClass().getName(), executable.getId())));
90+
binding.setVariable("out", new CodePrintStream(this));
14391
}
14492

14593
@Override
@@ -148,6 +96,6 @@ public void close() {
14896
}
14997

15098
public void variable(String name, Object value) {
151-
binding.setVariable(name, value);
99+
codeContext.getBinding().setVariable(name, value);
152100
}
153101
}

core/src/main/java/com/vml/es/aem/acm/core/code/Executor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ protected void activate(Config config) {
5050

5151
public ExecutionContext createContext(
5252
String id, ExecutionMode mode, Executable executable, ResourceResolver resourceResolver) {
53-
ExecutionContext result = new ExecutionContext(id, mode, this, executable, osgiContext, resourceResolver);
53+
CodeContext codeContext = new CodeContext(osgiContext, resourceResolver);
54+
ExecutionContext result = new ExecutionContext(id, mode, this, executable, codeContext);
5455
result.setDebug(config.debug());
5556
result.setHistory(config.history());
5657
return result;
@@ -69,13 +70,15 @@ public Execution execute(Executable executable, ExecutionContextOptions contextO
6970
}
7071

7172
public Execution execute(ExecutionContext context) throws AcmException {
73+
context.getCodeContext().prepareRun(context);
7274
ImmediateExecution execution = executeImmediately(context);
7375
if (context.getMode() == ExecutionMode.RUN) {
7476
if (context.isHistory() && (context.isDebug() || (execution.getStatus() != ExecutionStatus.SKIPPED))) {
75-
ExecutionHistory history = new ExecutionHistory(context.getResourceResolver());
77+
ExecutionHistory history =
78+
new ExecutionHistory(context.getCodeContext().getResourceResolver());
7679
history.save(context, execution);
7780
}
78-
context.getExtender().complete(execution);
81+
context.getCodeContext().completeRun(execution);
7982
}
8083
return execution;
8184
}

0 commit comments

Comments
 (0)