Skip to content

Commit

Permalink
Add unit test for FeaturesConfig class (knative-extensions#3771)
Browse files Browse the repository at this point in the history
* 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 <cmurray@redhat.com>

---------

Co-authored-by: Calum Murray <cmurray@redhat.com>
  • Loading branch information
creydr and Cali0707 committed Apr 2, 2024
1 parent b2ba6b4 commit 8132872
Showing 1 changed file with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 8132872

Please sign in to comment.