From 2d2c38bd79ebc005d606d83d93e0da89c821398c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20M=C3=A9sz=C3=A1ros?= Date: Sun, 31 Mar 2024 20:15:29 +0200 Subject: [PATCH] fix js test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Attila Mészáros --- .../csviri/operator/resourceglue/Utils.java | 2 +- .../resourceglue/JavaScripConditionTest.java | 38 ++++++++++--------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/csviri/operator/resourceglue/Utils.java b/src/main/java/io/csviri/operator/resourceglue/Utils.java index 2910048..ecd6144 100644 --- a/src/main/java/io/csviri/operator/resourceglue/Utils.java +++ b/src/main/java/io/csviri/operator/resourceglue/Utils.java @@ -33,7 +33,7 @@ public static Map getActualResourcesByNameInW var dependentSpec = glue.getSpec().getResources().stream() .filter(r -> Utils.getApiVersion(r).equals(sr.getApiVersion()) && Utils.getKind(r).equals(sr.getKind()) - // checking the name from annotation since it might be templated name + // comparing the name from annotation since the resource name might be templated in spec // therefore "Utils.getName(relatedResourceSpec).equals(sr.getMetadata().getName())" would not // work && r.getName() diff --git a/src/test/java/io/csviri/operator/resourceglue/JavaScripConditionTest.java b/src/test/java/io/csviri/operator/resourceglue/JavaScripConditionTest.java index cbb9a48..406b7ff 100644 --- a/src/test/java/io/csviri/operator/resourceglue/JavaScripConditionTest.java +++ b/src/test/java/io/csviri/operator/resourceglue/JavaScripConditionTest.java @@ -18,6 +18,7 @@ import io.javaoperatorsdk.operator.api.reconciler.Context; import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource; +import static io.csviri.operator.resourceglue.reconciler.glue.GlueReconciler.DEPENDENT_NAME_ANNOTATION_KEY; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; @@ -25,6 +26,7 @@ class JavaScripConditionTest { + public static final String DR_NAME = "secondary"; Context mockContext = mock(Context.class); DependentResource dr = mock(DependentResource.class); Glue dummyGlue = new Glue(); @@ -63,25 +65,27 @@ void injectsTargetResourceResource() { } @Test - void injectsSecondaryResourcesResource() { - when(mockContext.getSecondaryResources(any())).thenReturn(Set.of(configMap())); - when(dr.getSecondaryResource(any(), any())).thenReturn(Optional.of(configMap())); - - Glue glue = new Glue(); - glue.setSpec(new ResourceGlueSpec()); - glue.getSpec().setResources(new ArrayList<>()); - var drSpec = new DependentResourceSpec(); - drSpec.setName("secondary"); - drSpec.setResource(configMap()); - glue.getSpec().getResources().add(drSpec); + void injectsSecondaryResourcesResource() { + var cm = configMap(); + cm.getMetadata().getAnnotations().put(DEPENDENT_NAME_ANNOTATION_KEY, DR_NAME); + when(mockContext.getSecondaryResources(any())).thenReturn(Set.of(cm)); + when(dr.getSecondaryResource(any(), any())).thenReturn(Optional.of(cm)); + + Glue glue = new Glue(); + glue.setSpec(new ResourceGlueSpec()); + glue.getSpec().setResources(new ArrayList<>()); + var drSpec = new DependentResourceSpec(); + drSpec.setName(DR_NAME); + drSpec.setResource(configMap()); + glue.getSpec().getResources().add(drSpec); - var condition = new JavaScripCondition(""" - secondary.data.key1 == "val1"; - """); + var condition = new JavaScripCondition(""" + secondary.data.key1 == "val1"; + """); - var res = condition.isMet(dr, glue, mockContext); - assertThat(res).isTrue(); - } + var res = condition.isMet(dr, glue, mockContext); + assertThat(res).isTrue(); + } private GenericKubernetesResource configMap() { try (InputStream is = JavaScripConditionTest.class.getResourceAsStream("/ConfigMap.yaml")) {