From 8132872858599e441e3c118b2885dcd9c4c211a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20St=C3=A4bler?= Date: Tue, 19 Mar 2024 13:06:39 +0100 Subject: [PATCH] Add unit test for FeaturesConfig class (#3771) * Add unit test for FeaturesConfig class * Update data-plane/core/src/test/java/dev/knative/eventing/kafka/broker/core/features/FeaturesConfigTest.java Co-authored-by: Calum Murray --------- Co-authored-by: Calum Murray --- .../core/features/FeaturesConfigTest.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 data-plane/core/src/test/java/dev/knative/eventing/kafka/broker/core/features/FeaturesConfigTest.java diff --git a/data-plane/core/src/test/java/dev/knative/eventing/kafka/broker/core/features/FeaturesConfigTest.java b/data-plane/core/src/test/java/dev/knative/eventing/kafka/broker/core/features/FeaturesConfigTest.java new file mode 100644 index 0000000000..0a008d483c --- /dev/null +++ b/data-plane/core/src/test/java/dev/knative/eventing/kafka/broker/core/features/FeaturesConfigTest.java @@ -0,0 +1,66 @@ +/* + * Copyright © 2018 Knative Authors (knative-dev@googlegroups.com) + * + * Licensed 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 dev.knative.eventing.kafka.broker.core.features; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class FeaturesConfigTest { + + private File featuresDir; + + @BeforeEach + public void setup() throws IOException { + featuresDir = Files.createTempDirectory("features").toFile(); + + Files.createFile(Path.of(featuresDir.getAbsolutePath(), FeaturesConfig.KEY_AUTHENTICATION_OIDC)) + .toFile(); + } + + @AfterEach + public void cleanup() throws IOException { + Files.walk(featuresDir.toPath()) + .filter(Files::isRegularFile) + .map(Path::toFile) + .forEach(File::delete); + + featuresDir.delete(); + } + + @Test + public void testFeaturesConfigAuthenticationOIDC() throws IOException { + FeaturesConfig fc = new FeaturesConfig(featuresDir.getAbsolutePath()); + Assertions.assertFalse(fc.isAuthenticationOIDC(), "should be false, if feature file is empty"); + + try (FileWriter writer = + new FileWriter(Paths.get(featuresDir.getAbsolutePath(), FeaturesConfig.KEY_AUTHENTICATION_OIDC) + .toString())) { + writer.write("enabled"); + } + + fc = new FeaturesConfig(featuresDir.getAbsolutePath()); + Assertions.assertTrue(fc.isAuthenticationOIDC(), "should be true, if feature is enabled"); + } +}