From 5ae113576daa60079e7046875eceea3769299833 Mon Sep 17 00:00:00 2001 From: Philipp Zehnder Date: Tue, 30 Apr 2024 09:47:36 +0200 Subject: [PATCH] feat: Add unit tests for CustomServiceTagResolver --- streampipes-service-extensions/pom.xml | 12 ++++ .../CustomServiceTagResolverTest.java | 68 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 streampipes-service-extensions/src/test/java/org/apache/streampipes/service/extensions/CustomServiceTagResolverTest.java diff --git a/streampipes-service-extensions/pom.xml b/streampipes-service-extensions/pom.xml index 8370ad77c0..26dcda6b5b 100644 --- a/streampipes-service-extensions/pom.xml +++ b/streampipes-service-extensions/pom.xml @@ -57,6 +57,18 @@ + + + + org.junit.jupiter + junit-jupiter-api + test + + + org.mockito + mockito-core + test + diff --git a/streampipes-service-extensions/src/test/java/org/apache/streampipes/service/extensions/CustomServiceTagResolverTest.java b/streampipes-service-extensions/src/test/java/org/apache/streampipes/service/extensions/CustomServiceTagResolverTest.java new file mode 100644 index 0000000000..72e95db8b8 --- /dev/null +++ b/streampipes-service-extensions/src/test/java/org/apache/streampipes/service/extensions/CustomServiceTagResolverTest.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.streampipes.service.extensions; + + +import org.apache.streampipes.commons.environment.Environment; +import org.apache.streampipes.commons.environment.variable.StringEnvironmentVariable; +import org.apache.streampipes.model.extensions.svcdiscovery.SpServiceTag; +import org.apache.streampipes.model.extensions.svcdiscovery.SpServiceTagPrefix; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class CustomServiceTagResolverTest { + + private StringEnvironmentVariable customServiceTags; + private CustomServiceTagResolver resolver; + + @BeforeEach + void setUp() { + var env = mock(Environment.class); + customServiceTags = mock(StringEnvironmentVariable.class); + when(env.getCustomServiceTags()).thenReturn(customServiceTags); + resolver = new CustomServiceTagResolver(env); + } + + @Test + void returnsCustomServiceTagsWhenTheyExist() { + when(customServiceTags.exists()).thenReturn(true); + when(customServiceTags.getValue()).thenReturn("tag1,tag2"); + + var result = resolver.getCustomServiceTags(); + + assertEquals(2, result.size()); + assertTrue(result.contains(SpServiceTag.create(SpServiceTagPrefix.CUSTOM, "tag1"))); + assertTrue(result.contains(SpServiceTag.create(SpServiceTagPrefix.CUSTOM, "tag2"))); + } + + @Test + void returnsEmptySetWhenNoCustomServiceTagsExist() { + when(customServiceTags.exists()).thenReturn(false); + + var result = resolver.getCustomServiceTags(); + + assertTrue(result.isEmpty()); + } +} \ No newline at end of file