Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tests): convert junit4 based testcases to junit5 and clean up in kayenta #975

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ subprojects { project ->
}

test {
useJUnitPlatform()
testLogging {
exceptionFormat = "full"
}
Expand Down Expand Up @@ -76,6 +77,7 @@ subprojects { project ->
api 'org.scala-lang:scala-reflect:2.12.12'
api 'com.typesafe.scala-logging:scala-logging_2.12:3.9.2'
testImplementation 'org.scalatest:scalatest_2.12:3.0.9'
testRuntimeOnly 'org.scalatestplus:junit-5-9_2.12:3.2.16.0'
}
implementation enforcedPlatform("io.spinnaker.orca:orca-bom:$orcaVersion")
compileOnly "org.projectlombok:lombok"
Expand Down Expand Up @@ -105,6 +107,8 @@ subprojects { project ->
testImplementation "org.hamcrest:hamcrest-core"
testRuntimeOnly "cglib:cglib-nodep"
testRuntimeOnly "org.objenesis:objenesis"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine"
testRuntimeOnly "org.junit.vintage:junit-vintage-engine" //Required for spock tests to execute along with junit5 tests
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.netflix.kayenta.atlas;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import com.netflix.kayenta.atlas.model.Backend;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class BackendTest {
@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.netflix.kayenta.atlas;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -12,20 +14,20 @@
import java.util.List;
import java.util.Optional;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@Configuration
@ComponentScan({"com.netflix.kayenta.retrofit.config"})
class BackendsConfig {}

@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {BackendsConfig.class})
public class BackendsTest {
@Autowired private ResourceLoader resourceLoader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import javax.validation.constraints.NotNull;
import lombok.*;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@Configuration
@ComponentScan({"com.netflix.kayenta.retrofit.config"})
Expand All @@ -43,7 +43,7 @@ class CanaryMetricConfigWithResults {
@NotNull @Getter private List<AtlasResults> atlasResults;
}

@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {TestConfig.class})
public class IntegrationTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.netflix.kayenta.atlas.config;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.kayenta.atlas.model.AtlasResults;
Expand All @@ -9,7 +10,7 @@
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.List;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class AtlasSSEConverterTest {

Expand Down Expand Up @@ -39,18 +40,19 @@ public void dataPlusClose() {
assertEquals(2, results.size());
}

@Test(expected = RetryableQueryException.class)
@Test
public void missingCloseThrows() {
atlasResultsFromSSE(timeseriesMessage);
assertThrows(RetryableQueryException.class, () -> atlasResultsFromSSE(timeseriesMessage));
}

@Test(expected = FatalQueryException.class)
@Test
public void fatalErrorWithoutClose() {
atlasResultsFromSSE(errorMessageIllegalStateMessage);
assertThrows(
FatalQueryException.class, () -> atlasResultsFromSSE(errorMessageIllegalStateMessage));
}

@Test(expected = RetryableQueryException.class)
@Test
public void retryableErrorWithoutClose() {
atlasResultsFromSSE(retryableErrorMessage);
assertThrows(RetryableQueryException.class, () -> atlasResultsFromSSE(retryableErrorMessage));
}
}
1 change: 0 additions & 1 deletion kayenta-blobs/kayenta-blobs.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ dependencies {

api 'com.microsoft.azure:azure-storage:8.3.0'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '1.9.10'
testImplementation group: 'com.tngtech.java', name: 'junit-dataprovider', version: '1.13.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.netflix.kayenta.blobs.storage;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -28,17 +29,14 @@
import com.netflix.kayenta.security.MapBackedAccountCredentialsRepository;
import com.netflix.kayenta.storage.ObjectType;
import com.netflix.spinnaker.kork.web.exceptions.NotFoundException;
import com.tngtech.java.junit.dataprovider.*;
import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mockito;

@RunWith(DataProviderRunner.class)
@Slf4j
public class TestableBlobsStorageServiceTest {

Expand All @@ -48,7 +46,7 @@ public class TestableBlobsStorageServiceTest {
private AccountCredentialsRepository credentialsRepository;
private CanaryConfigIndex mockedCanaryConfigIndex;

@Before
@BeforeEach
public void setUp() {
List<String> testAccountNames =
Arrays.asList("AzDev_Testing_Account_1", "AzDev_Testing_Account_2");
Expand Down Expand Up @@ -76,18 +74,18 @@ public void setUp() {
testAccountNames, kayentaObjectMapper, credentialsRepository, mockedCanaryConfigIndex);
}

@After
@AfterEach
public void tearDown() {}

@Test
@UseDataProvider("servicesAccountDataset")
@ParameterizedTest
@MethodSource("servicesAccountDataset")
public void servicesAccount(String accountName, boolean expected) {
log.info(String.format("Running servicesAccountTest(%s)", accountName));
Assert.assertEquals(expected, testBlobsStorageService.servicesAccount(accountName));
assertEquals(expected, testBlobsStorageService.servicesAccount(accountName));
}

@Test
@UseDataProvider("loadObjectDataset")
@ParameterizedTest
@MethodSource("loadObjectDataset")
public void loadObject(
String accountName,
ObjectType objectType,
Expand All @@ -104,24 +102,23 @@ public void loadObject(

CanaryConfig result =
testBlobsStorageService.loadObject(accountName, objectType, testItemKey);
Assert.assertEquals(applications.get(0), result.getApplications().get(0));
assertEquals(applications.get(0), result.getApplications().get(0));
} catch (IllegalArgumentException e) {
Assert.assertEquals("Unable to resolve account " + accountName + ".", e.getMessage());
assertEquals("Unable to resolve account " + accountName + ".", e.getMessage());
} catch (NotFoundException e) {
Assert.assertEquals(
assertEquals(
"Could not fetch items from Azure Cloud Storage: Item not found at "
+ rootFolder
+ "/canary_config/"
+ testItemKey,
e.getMessage());
} catch (IllegalStateException e) {
Assert.assertEquals(
"Unable to deserialize object (key: " + testItemKey + ")", e.getMessage());
assertEquals("Unable to deserialize object (key: " + testItemKey + ")", e.getMessage());
}
}

@Test
@UseDataProvider("storeObjectDataset")
@ParameterizedTest
@MethodSource("storeObjectDataset")
public void storeObject(
String accountName,
ObjectType objectType,
Expand All @@ -147,14 +144,14 @@ public void storeObject(
testBlobsStorageService.storeObject(
accountName, objectType, testItemKey, canaryConfig, fakeFileName, isAnUpdate);
HashMap<String, String> result = testBlobsStorageService.blobStored;
Assert.assertEquals(fakeBlobName, result.get("blob"));
assertEquals(fakeBlobName, result.get("blob"));
} catch (IllegalArgumentException e) {
Assert.assertEquals("Unable to resolve account " + accountName + ".", e.getMessage());
assertEquals("Unable to resolve account " + accountName + ".", e.getMessage());
}
}

@Test
@UseDataProvider("deleteObjectDataset")
@ParameterizedTest
@MethodSource("deleteObjectDataset")
public void deleteObject(String accountName, ObjectType objectType, String testItemKey) {

String fakeBlobName =
Expand All @@ -177,14 +174,14 @@ public void deleteObject(String accountName, ObjectType objectType, String testI
"Running deleteObjectTest for rootFolder/" + objectType.getGroup() + "/" + testItemKey);
testBlobsStorageService.deleteObject(accountName, objectType, testItemKey);
HashMap<String, String> result = testBlobsStorageService.blobStored;
Assert.assertEquals("invoked", result.get(String.format("deleteIfexists(%s)", fakeBlobName)));
assertEquals("invoked", result.get(String.format("deleteIfexists(%s)", fakeBlobName)));
} catch (IllegalArgumentException e) {
Assert.assertEquals("Unable to resolve account " + accountName + ".", e.getMessage());
assertEquals("Unable to resolve account " + accountName + ".", e.getMessage());
}
}

@Test
@UseDataProvider("listObjectKeysDataset")
@ParameterizedTest
@MethodSource("listObjectKeysDataset")
public void listObjectKeys(
String accountName, ObjectType objectType, List<String> applications, boolean skipIndex) {

Expand All @@ -193,16 +190,15 @@ public void listObjectKeys(
List<Map<String, Object>> result =
testBlobsStorageService.listObjectKeys(accountName, objectType, applications, skipIndex);
if (objectType == ObjectType.CANARY_CONFIG) {
Assert.assertEquals("canary_test", result.get(0).get("name"));
assertEquals("canary_test", result.get(0).get("name"));
} else {
Assert.assertEquals(6, result.size());
assertEquals(6, result.size());
}
} catch (IllegalArgumentException e) {
Assert.assertEquals("Unable to resolve account " + accountName + ".", e.getMessage());
assertEquals("Unable to resolve account " + accountName + ".", e.getMessage());
}
}

@DataProvider
public static Object[][] servicesAccountDataset() {
return new Object[][] {
{"AzDev_Testing_Account_1", true},
Expand All @@ -212,7 +208,6 @@ public static Object[][] servicesAccountDataset() {
};
}

@DataProvider
public static Object[][] loadObjectDataset() {
return new Object[][] {
{
Expand Down Expand Up @@ -260,7 +255,6 @@ public static Object[][] loadObjectDataset() {
};
}

@DataProvider
public static Object[][] storeObjectDataset() {
return new Object[][] {
{"Kayenta_Account_1", ObjectType.CANARY_CONFIG, "Test_Canary", "Test_App", false},
Expand All @@ -271,7 +265,6 @@ public static Object[][] storeObjectDataset() {
};
}

@DataProvider
public static Object[][] deleteObjectDataset() {
return new Object[][] {
{"Kayenta_Account_1", ObjectType.CANARY_CONFIG, "some(GUID)"},
Expand All @@ -282,7 +275,6 @@ public static Object[][] deleteObjectDataset() {
};
}

@DataProvider
public static Object[][] listObjectKeysDataset() {
return new Object[][] {
{"Kayenta_Account_1", ObjectType.CANARY_CONFIG, Collections.singletonList("Test_App"), true},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.netflix.kayenta.canary.CanaryConfig;
import com.netflix.kayenta.canary.CanaryMetricSetQueryConfig;
import com.netflix.kayenta.canary.CanaryScope;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class QueryConfigUtilsTest {

Expand Down
Loading