Skip to content

Commit

Permalink
rename method and refactor URI creation
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcao13 committed Jul 28, 2023
1 parent 2c7b4a0 commit aa64c3e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public IntermediateResponse<MatchedMatchExpression> handle(RequestParameters par
if (StringUtils.isBlank(matchExpression)) {
throw new ApiException(400, "'matchExpression' is required.");
}
expressionEvaluator.evaluates(matchExpression);
expressionEvaluator.validate(matchExpression);
if (targets != null) {
Set<ServiceRef> matched =
expressionManager.resolveMatchingTargets(
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/io/cryostat/rules/MatchExpressionEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
package io.cryostat.rules;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -83,7 +82,7 @@ public class MatchExpressionEvaluator {
e -> {
switch (e.getEventType()) {
case REMOVED:
invalidate(e.getPayload());
invalidateCache(e.getPayload());
break;
default:
// ignore
Expand All @@ -94,7 +93,7 @@ public class MatchExpressionEvaluator {
e -> {
switch (e.getEventType()) {
case REMOVED:
invalidate(e.getPayload().getMatchExpression());
invalidateCache(e.getPayload().getMatchExpression());
break;
default:
// ignore
Expand All @@ -120,7 +119,7 @@ private boolean compute(String matchExpression, ServiceRef serviceRef) throws Sc
}
}

private void invalidate(String matchExpression) {
private void invalidateCache(String matchExpression) {
var it = cache.asMap().keySet().iterator();
while (it.hasNext()) {
Pair<String, ServiceRef> entry = it.next();
Expand All @@ -130,13 +129,9 @@ private void invalidate(String matchExpression) {
}
}

public void evaluates(String matchExpression) throws ScriptException {
try {
ServiceRef dummyRef = new ServiceRef("jvmId", new URI("file:///foo/bar"), "alias");
compute(matchExpression, dummyRef);
} catch (URISyntaxException e) {
logger.error(e);
}
public void validate(String matchExpression) throws ScriptException {
ServiceRef dummyRef = new ServiceRef("jvmId", URI.create("file:///foo/bar"), "alias");
compute(matchExpression, dummyRef);
}

public boolean applies(String matchExpression, ServiceRef serviceRef) throws ScriptException {
Expand Down
22 changes: 16 additions & 6 deletions src/test/java/io/cryostat/rules/MatchExpressionEvaluatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,16 @@ void setup() throws Exception {
this.platformAnnotations = Map.of("annotation1", "someAnnotation");
this.cryostatAnnotations = Map.of(AnnotationKey.JAVA_MAIN, "io.cryostat.Cryostat");

Mockito.when(serviceRef.getServiceUri()).thenReturn(this.serviceUri);
Mockito.when(serviceRef.getJvmId()).thenReturn(this.jvmId);
Mockito.when(serviceRef.getAlias()).thenReturn(Optional.of(this.alias));
Mockito.when(serviceRef.getLabels()).thenReturn(this.labels);
Mockito.when(serviceRef.getPlatformAnnotations()).thenReturn(this.platformAnnotations);
Mockito.when(serviceRef.getCryostatAnnotations()).thenReturn(this.cryostatAnnotations);
Mockito.lenient().when(serviceRef.getServiceUri()).thenReturn(this.serviceUri);
Mockito.lenient().when(serviceRef.getJvmId()).thenReturn(this.jvmId);
Mockito.lenient().when(serviceRef.getAlias()).thenReturn(Optional.of(this.alias));
Mockito.lenient().when(serviceRef.getLabels()).thenReturn(this.labels);
Mockito.lenient()
.when(serviceRef.getPlatformAnnotations())
.thenReturn(this.platformAnnotations);
Mockito.lenient()
.when(serviceRef.getCryostatAnnotations())
.thenReturn(this.cryostatAnnotations);
}

@Nested
Expand Down Expand Up @@ -277,5 +281,11 @@ void shouldThrowExceptionOnNonBooleanExpressionEval(String expr) throws Exceptio
Assertions.assertThrows(
ScriptException.class, () -> ruleMatcher.applies(expr, serviceRef));
}

@ParameterizedTest
@ValueSource(strings = {"1", "null", "target.alias", "\"a string\""})
void shouldThrowExceptionOnInvalidExpressionValidate(String expr) throws Exception {
Assertions.assertThrows(ScriptException.class, () -> ruleMatcher.validate(expr));
}
}
}

0 comments on commit aa64c3e

Please sign in to comment.