From 86bcfa9d60ac452fd18682f2c9bd61b05e3899fc Mon Sep 17 00:00:00 2001 From: gjoranv Date: Thu, 28 Jul 2022 19:33:58 +0200 Subject: Convert 'flags' to junit5 --- flags/pom.xml | 27 ++++++++++-------- .../test/java/com/yahoo/vespa/flags/FlagsTest.java | 32 +++++++++++----------- .../yahoo/vespa/flags/OrderedFlagSourceTest.java | 8 +++--- .../vespa/flags/custom/ClusterCapacityTest.java | 8 +++--- .../yahoo/vespa/flags/custom/SharedHostTest.java | 10 +++---- .../com/yahoo/vespa/flags/file/FlagDbFileTest.java | 13 +++++---- .../com/yahoo/vespa/flags/json/ConditionTest.java | 14 +++++----- .../com/yahoo/vespa/flags/json/FlagDataTest.java | 10 +++---- .../yahoo/vespa/flags/json/SerializationTest.java | 12 ++++---- 9 files changed, 69 insertions(+), 65 deletions(-) diff --git a/flags/pom.xml b/flags/pom.xml index 21206b160a6..0c268fbd644 100644 --- a/flags/pom.xml +++ b/flags/pom.xml @@ -55,11 +55,6 @@ provided - - junit - junit - test - org.mockito mockito-core @@ -67,8 +62,7 @@ org.hamcrest - hamcrest-junit - 2.0.0.0 + hamcrest-library test @@ -76,17 +70,26 @@ testutil ${project.version} test + + + junit + junit + + + org.hamcrest + hamcrest-core + + + org.hamcrest + hamcrest-library + + org.junit.jupiter junit-jupiter test - - org.junit.vintage - junit-vintage-engine - test - diff --git a/flags/src/test/java/com/yahoo/vespa/flags/FlagsTest.java b/flags/src/test/java/com/yahoo/vespa/flags/FlagsTest.java index 3c022c1ddd2..3edde140de8 100644 --- a/flags/src/test/java/com/yahoo/vespa/flags/FlagsTest.java +++ b/flags/src/test/java/com/yahoo/vespa/flags/FlagsTest.java @@ -4,7 +4,7 @@ package com.yahoo.vespa.flags; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.node.BooleanNode; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import java.util.List; @@ -12,11 +12,10 @@ import java.util.Objects; import java.util.Optional; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.emptyOrNullString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; @@ -29,7 +28,7 @@ import static org.mockito.Mockito.when; */ public class FlagsTest { @Test - public void testBoolean() { + void testBoolean() { final boolean defaultValue = false; FlagSource source = mock(FlagSource.class); BooleanFlag booleanFlag = Flags.defineFeatureFlag("id", defaultValue, List.of("owner"), "1970-01-01", "2100-01-01", "description", @@ -45,8 +44,9 @@ public class FlagsTest { ArgumentCaptor vector = ArgumentCaptor.forClass(FetchVector.class); verify(source).fetch(any(), vector.capture()); // hostname is set by default - assertThat(vector.getValue().getValue(FetchVector.Dimension.HOSTNAME).isPresent(), is(true)); - assertThat(vector.getValue().getValue(FetchVector.Dimension.HOSTNAME).get(), is(not(emptyOrNullString()))); + Optional hostname = vector.getValue().getValue(FetchVector.Dimension.HOSTNAME); + assertTrue(hostname.isPresent()); + assertFalse(hostname.get().isEmpty()); // zone is set because it was set on the unbound flag above assertThat(vector.getValue().getValue(FetchVector.Dimension.ZONE_ID), is(Optional.of("a-zone"))); // application and node type are not set @@ -66,41 +66,41 @@ public class FlagsTest { } @Test - public void testString() { + void testString() { testGeneric(Flags.defineStringFlag("string-id", "default value", List.of("owner"), "1970-01-01", "2100-01-01", "description", - "modification effect", FetchVector.Dimension.ZONE_ID, FetchVector.Dimension.HOSTNAME), - "other value"); + "modification effect", FetchVector.Dimension.ZONE_ID, FetchVector.Dimension.HOSTNAME), + "other value"); } @Test - public void testInt() { + void testInt() { testGeneric(Flags.defineIntFlag("int-id", 2, List.of("owner"), "1970-01-01", "2100-01-01", "desc", "mod"), 3); } @Test - public void testLong() { + void testLong() { testGeneric(Flags.defineLongFlag("long-id", 1L, List.of("owner"), "1970-01-01", "2100-01-01", "desc", "mod"), 2L); } @Test - public void testDouble() { + void testDouble() { testGeneric(Flags.defineDoubleFlag("double-id", 3.142, List.of("owner"), "1970-01-01", "2100-01-01", "desc", "mod"), 2.718); } @Test - public void testList() { + void testList() { testGeneric(Flags.defineListFlag("list-id", List.of("a"), String.class, List.of("owner"), "1970-01-01", "2100-01-01", "desc", "mod"), List.of("a", "b", "c")); } @Test - public void testJacksonClass() { + void testJacksonClass() { ExampleJacksonClass defaultInstance = new ExampleJacksonClass(); ExampleJacksonClass instance = new ExampleJacksonClass(); instance.integer = -2; instance.string = "foo"; testGeneric(Flags.defineJacksonFlag("jackson-id", defaultInstance, ExampleJacksonClass.class, - List.of("owner"), "1970-01-01", "2100-01-01", "description", "modification effect", FetchVector.Dimension.HOSTNAME), + List.of("owner"), "1970-01-01", "2100-01-01", "description", "modification effect", FetchVector.Dimension.HOSTNAME), instance); testGeneric(Flags.defineListFlag("jackson-list-id", List.of(defaultInstance), ExampleJacksonClass.class, List.of("owner"), "1970-01-01", "2100-01-01", "desc", "mod"), diff --git a/flags/src/test/java/com/yahoo/vespa/flags/OrderedFlagSourceTest.java b/flags/src/test/java/com/yahoo/vespa/flags/OrderedFlagSourceTest.java index dfd676b6472..6da994386a3 100644 --- a/flags/src/test/java/com/yahoo/vespa/flags/OrderedFlagSourceTest.java +++ b/flags/src/test/java/com/yahoo/vespa/flags/OrderedFlagSourceTest.java @@ -1,12 +1,12 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.flags; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Optional; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -18,7 +18,7 @@ import static org.mockito.Mockito.when; */ public class OrderedFlagSourceTest { @Test - public void test() { + void test() { FlagSource source1 = mock(FlagSource.class); FlagSource source2 = mock(FlagSource.class); OrderedFlagSource orderedSource = new OrderedFlagSource(source1, source2); diff --git a/flags/src/test/java/com/yahoo/vespa/flags/custom/ClusterCapacityTest.java b/flags/src/test/java/com/yahoo/vespa/flags/custom/ClusterCapacityTest.java index 066955ee369..189e3fd79c9 100644 --- a/flags/src/test/java/com/yahoo/vespa/flags/custom/ClusterCapacityTest.java +++ b/flags/src/test/java/com/yahoo/vespa/flags/custom/ClusterCapacityTest.java @@ -2,15 +2,15 @@ package com.yahoo.vespa.flags.custom; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.IOException; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; public class ClusterCapacityTest { @Test - public void serialization() throws IOException { + void serialization() throws IOException { ClusterCapacity clusterCapacity = new ClusterCapacity(7, 1.2, 3.4, 5.6, null); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(clusterCapacity); @@ -25,7 +25,7 @@ public class ClusterCapacityTest { } @Test - public void serialization2() throws IOException { + void serialization2() throws IOException { ClusterCapacity clusterCapacity = new ClusterCapacity(7, 1.2, 3.4, 5.6, 2.3); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(clusterCapacity); diff --git a/flags/src/test/java/com/yahoo/vespa/flags/custom/SharedHostTest.java b/flags/src/test/java/com/yahoo/vespa/flags/custom/SharedHostTest.java index 852e1f9b7b1..1c8fbc41d35 100644 --- a/flags/src/test/java/com/yahoo/vespa/flags/custom/SharedHostTest.java +++ b/flags/src/test/java/com/yahoo/vespa/flags/custom/SharedHostTest.java @@ -2,22 +2,22 @@ package com.yahoo.vespa.flags.custom; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.IOException; import java.util.List; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; public class SharedHostTest { @Test - public void serialization() throws IOException { + void serialization() throws IOException { verifySerialization(new SharedHost(List.of( new HostResources(1.0, 2.0, 3.0, 4.0, "fast", "remote", - "container", 5, "x86_64")), 6)); + "container", 5, "x86_64")), 6)); verifySerialization(new SharedHost(List.of( new HostResources(1.0, 2.0, 3.0, 4.0, "fast", "remote", - "admin", 5, "arm64")), null)); + "admin", 5, "arm64")), null)); } private void verifySerialization(SharedHost sharedHost) throws IOException { diff --git a/flags/src/test/java/com/yahoo/vespa/flags/file/FlagDbFileTest.java b/flags/src/test/java/com/yahoo/vespa/flags/file/FlagDbFileTest.java index f2d389188f9..884d44e12b6 100644 --- a/flags/src/test/java/com/yahoo/vespa/flags/file/FlagDbFileTest.java +++ b/flags/src/test/java/com/yahoo/vespa/flags/file/FlagDbFileTest.java @@ -6,8 +6,7 @@ import com.yahoo.vespa.flags.FlagId; import com.yahoo.vespa.flags.json.FlagData; import com.yahoo.vespa.test.file.TestFileSystem; import org.hamcrest.collection.IsMapContaining; -import org.hamcrest.collection.IsMapWithSize; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; @@ -19,6 +18,8 @@ import java.util.Map; import static com.yahoo.yolean.Exceptions.uncheck; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author hakonhall @@ -28,7 +29,7 @@ public class FlagDbFileTest { private final FlagDbFile flagDb = new FlagDbFile(fileSystem); @Test - public void test() { + void test() { Map dataMap = new HashMap<>(); FlagId id1 = new FlagId("id1"); FlagData data1 = new FlagData(id1, new FetchVector()); @@ -38,12 +39,12 @@ public class FlagDbFileTest { dataMap.put(id2, data2); // Non-existing directory => empty map - assertThat(flagDb.read(), IsMapWithSize.anEmptyMap()); + assertTrue(flagDb.read().isEmpty()); // sync() will create directory with map content assertThat(flagDb.sync(dataMap), equalTo(true)); Map readDataMap = flagDb.read(); - assertThat(readDataMap, IsMapWithSize.aMapWithSize(2)); + assertEquals(2, readDataMap.size()); assertThat(readDataMap, IsMapContaining.hasKey(id1)); assertThat(readDataMap, IsMapContaining.hasKey(id2)); @@ -61,7 +62,7 @@ public class FlagDbFileTest { dataMap.put(id3, data3); assertThat(flagDb.sync(dataMap), equalTo(true)); Map anotherReadDataMap = flagDb.read(); - assertThat(anotherReadDataMap, IsMapWithSize.aMapWithSize(2)); + assertEquals(2, anotherReadDataMap.size()); assertThat(anotherReadDataMap, IsMapContaining.hasKey(id1)); assertThat(anotherReadDataMap, IsMapContaining.hasKey(id3)); assertThat(anotherReadDataMap.get(id1).serializeToJson(), equalTo("{\"id\":\"id1\",\"attributes\":{\"hostname\":\"h1\"}}")); diff --git a/flags/src/test/java/com/yahoo/vespa/flags/json/ConditionTest.java b/flags/src/test/java/com/yahoo/vespa/flags/json/ConditionTest.java index 677efd0fe1c..4da66bd5cc1 100644 --- a/flags/src/test/java/com/yahoo/vespa/flags/json/ConditionTest.java +++ b/flags/src/test/java/com/yahoo/vespa/flags/json/ConditionTest.java @@ -2,18 +2,18 @@ package com.yahoo.vespa.flags.json; import com.yahoo.vespa.flags.FetchVector; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author hakonhall */ public class ConditionTest { @Test - public void testWhitelist() { + void testWhitelist() { String hostname1 = "host1"; var params = new Condition.CreateParams(FetchVector.Dimension.HOSTNAME).withValues(hostname1); Condition condition = WhitelistCondition.create(params); @@ -24,7 +24,7 @@ public class ConditionTest { } @Test - public void testBlacklist() { + void testBlacklist() { String hostname1 = "host1"; var params = new Condition.CreateParams(FetchVector.Dimension.HOSTNAME).withValues(hostname1); Condition condition = BlacklistCondition.create(params); @@ -35,7 +35,7 @@ public class ConditionTest { } @Test - public void testRelational() { + void testRelational() { verifyVespaVersionFor("<", true, false, false); verifyVespaVersionFor("<=", true, true, false); verifyVespaVersionFor(">", false, false, true); diff --git a/flags/src/test/java/com/yahoo/vespa/flags/json/FlagDataTest.java b/flags/src/test/java/com/yahoo/vespa/flags/json/FlagDataTest.java index 5856cf99558..c89b5883fd1 100644 --- a/flags/src/test/java/com/yahoo/vespa/flags/json/FlagDataTest.java +++ b/flags/src/test/java/com/yahoo/vespa/flags/json/FlagDataTest.java @@ -3,13 +3,13 @@ package com.yahoo.vespa.flags.json; import com.yahoo.vespa.flags.FetchVector; import com.yahoo.vespa.flags.RawFlag; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Optional; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author hakonhall @@ -52,7 +52,7 @@ public class FlagDataTest { private final FetchVector vector = new FetchVector(); @Test - public void test() { + void test() { // Second rule matches with the default zone matching verify(Optional.of("false"), vector); diff --git a/flags/src/test/java/com/yahoo/vespa/flags/json/SerializationTest.java b/flags/src/test/java/com/yahoo/vespa/flags/json/SerializationTest.java index 35478768924..2cc19917793 100644 --- a/flags/src/test/java/com/yahoo/vespa/flags/json/SerializationTest.java +++ b/flags/src/test/java/com/yahoo/vespa/flags/json/SerializationTest.java @@ -5,23 +5,23 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.JsonNodeType; import com.yahoo.vespa.flags.json.wire.WireCondition; import com.yahoo.vespa.flags.json.wire.WireFlagData; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.IOException; import java.util.List; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.anEmptyMap; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author hakonhall */ public class SerializationTest { @Test - public void emptyJson() throws IOException { + void emptyJson() throws IOException { String json = "{\"id\":\"id1\"}"; WireFlagData wireData = WireFlagData.deserialize(json); assertThat(wireData.id, equalTo("id1")); @@ -33,7 +33,7 @@ public class SerializationTest { } @Test - public void deserialization() throws IOException { + void deserialization() throws IOException { String json = "{\n" + " \"id\": \"id2\",\n" + " \"rules\": [\n" + @@ -107,7 +107,7 @@ public class SerializationTest { } @Test - public void jsonWithStrayFields() { + void jsonWithStrayFields() { String json = "{\n" + " \"id\": \"id3\",\n" + " \"foo\": true,\n" + @@ -136,7 +136,7 @@ public class SerializationTest { assertThat(whitelistCondition.dimension, equalTo("zone")); assertThat(whitelistCondition.values, nullValue()); assertThat(wireData.rules.get(0).value, nullValue()); - assertThat(wireData.defaultFetchVector, anEmptyMap()); + assertTrue(wireData.defaultFetchVector.isEmpty()); assertThat(wireData.serializeToJson(), equalTo("{\"id\":\"id3\",\"rules\":[{\"conditions\":[{\"type\":\"whitelist\",\"dimension\":\"zone\"}]}],\"attributes\":{}}")); -- cgit v1.2.3