Skip to content

Commit

Permalink
Merge pull request #3195 from cloudfoundry/testing-updates
Browse files Browse the repository at this point in the history
Testing Updates
  • Loading branch information
duanemay authored Jan 9, 2025
2 parents dc25133 + 5e1c30c commit 0c7b6cd
Show file tree
Hide file tree
Showing 627 changed files with 21,908 additions and 24,240 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ subprojects {
testImplementation(libraries.junit5JupiterApi)
testImplementation(libraries.junit5JupiterParams)
testRuntimeOnly(libraries.junit5JupiterEngine)
testRuntimeOnly(libraries.junitVintageEngine)
testImplementation(libraries.unboundIdLdapSdk)
testRuntimeOnly(libraries.jacocoAgent)

Expand Down
5 changes: 1 addition & 4 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ versions.seleniumVersion = "4.27.0"
versions.braveVersion = "6.0.3"
versions.jacksonVersion = "2.18.2"
versions.jsonPathVersion = "2.9.0"
versions.awaitilityVersion = "4.2.2"
versions.opensaml = "4.0.1" // Spring Security 5.8.x allows OpenSAML 3 or 4. OpenSAML 3 has reached its end-of-life. Spring Security 6 drops support for 3, using 4.
versions.awaitilityVersion = "4.2.2"

// Versions we're overriding from the Spring Boot Bom (Dependabot does not issue PRs to bump these versions, so we need to manually bump them)
ext["mariadb.version"] = "2.7.12" // Bumping to v3 breaks some pipeline jobs (and compatibility with Amazon Aurora MySQL), so pinning to v2 for now. v2 (current version) is stable and will be supported until about September 2025 (https://mariadb.com/kb/en/about-mariadb-connector-j/).
Expand Down Expand Up @@ -66,19 +66,16 @@ libraries.hsqldb = "org.hsqldb:hsqldb"
libraries.jacksonAnnotations = "com.fasterxml.jackson.core:jackson-annotations:${versions.jacksonVersion}"
libraries.jacksonDatabind = "com.fasterxml.jackson.core:jackson-databind:${versions.jacksonVersion}"
libraries.jacksonDataformatYaml = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${versions.jacksonVersion}"
libraries.jakartaAnnotationApi = "jakarta.annotation:jakarta.annotation-api:1.3.5"
libraries.javaxServlet = "javax.servlet:jstl"
libraries.javaxValidationApi = "javax.validation:validation-api"
libraries.javaxXmlBindApi = "javax.xml.bind:jaxb-api"
libraries.glassfishJaxb = "org.glassfish.jaxb:jaxb-runtime"
libraries.jsonAssert = "org.skyscreamer:jsonassert"
libraries.jsonPath = "com.jayway.jsonpath:json-path:${versions.jsonPathVersion}"
libraries.jsonPathAssert = "com.jayway.jsonpath:json-path-assert:${versions.jsonPathVersion}"
libraries.junit = "junit:junit"
libraries.junit5JupiterApi = "org.junit.jupiter:junit-jupiter-api"
libraries.junit5JupiterEngine = "org.junit.jupiter:junit-jupiter-engine"
libraries.junit5JupiterParams = "org.junit.jupiter:junit-jupiter-params"
libraries.junitVintageEngine = "org.junit.vintage:junit-vintage-engine"
libraries.log4jCore = "org.apache.logging.log4j:log4j-core"
libraries.lombok = "org.projectlombok:lombok"
libraries.mariaJdbcDriver = "org.mariadb.jdbc:mariadb-java-client"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
public class JsonUtils {
private static final ObjectMapper objectMapper = new ObjectMapper();

private JsonUtils() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

public static String writeValueAsString(Object object) throws JsonUtilException {
try {
return objectMapper.writeValueAsString(object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import com.fasterxml.jackson.core.type.TypeReference;
import org.cloudfoundry.identity.uaa.util.JsonUtils;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;

class MetricsQueueTest {

Expand Down Expand Up @@ -50,26 +48,25 @@ void summary() {
@Test
void totals() {
RequestMetricSummary summary = queue.getTotals();
assertNotNull(summary);
assertEquals(3, summary.getCount());
assertEquals(1, summary.getIntolerableCount());
assertEquals(((double) (MAX_TIME + 3 + 5)) / 3.0, summary.getAverageTime(), DELTA);
assertEquals((double) MAX_TIME + 1, summary.getAverageIntolerableTime(), DELTA);
assertEquals(3, summary.getDatabaseQueryCount());
assertEquals(3, summary.getAverageDatabaseQueryTime(), DELTA);
assertEquals(2, summary.getDatabaseIntolerableQueryCount());
assertEquals(3.5, summary.getAverageDatabaseIntolerableQueryTime(), DELTA);
assertThat(summary).isNotNull();
assertThat(summary.getCount()).isEqualTo(3);
assertThat(summary.getIntolerableCount()).isOne();
assertThat(summary.getAverageTime()).isCloseTo(((double) (MAX_TIME + 3 + 5)) / 3.0, within(DELTA));
assertThat(summary.getAverageIntolerableTime()).isCloseTo((double) MAX_TIME + 1, within(DELTA));
assertThat(summary.getDatabaseQueryCount()).isEqualTo(3);
assertThat(summary.getAverageDatabaseQueryTime()).isCloseTo(3, within(DELTA));
assertThat(summary.getDatabaseIntolerableQueryCount()).isEqualTo(2);
assertThat(summary.getAverageDatabaseIntolerableQueryTime()).isCloseTo(3.5, within(DELTA));
}

@Test
void json_serialize() {
String json = JsonUtils.writeValueAsString(queue);
Map<String, Object> object = JsonUtils.readValue(json, new TypeReference<Map<String, Object>>() {
});
assertNotNull(object);
assertEquals(3, object.size());
assertThat(object).hasSize(3);
MetricsQueue deserialized = JsonUtils.readValue(json, MetricsQueue.class);
assertNotNull(deserialized);
assertThat(deserialized).isNotNull();
validateMetricsQueue(deserialized);
}

Expand All @@ -93,7 +90,7 @@ void overflowLimitRespected() throws Exception {
for (int i = 0; i < threads.length; i++) {
threads[i].join();
}
assertThat(queue.getLastRequests().size(), Matchers.lessThanOrEqualTo(MetricsQueue.MAX_ENTRIES));
assertThat(queue.getLastRequests()).hasSizeLessThanOrEqualTo(MetricsQueue.MAX_ENTRIES);
}

@Test
Expand All @@ -106,35 +103,33 @@ void offer() {
metric.stop(200, 2);
queue.offer(metric);
RequestMetricSummary totals = queue.getTotals();
assertEquals(3, totals.getDatabaseQueryCount());
assertEquals(2, totals.getDatabaseIntolerableQueryCount());
assertThat(totals.getDatabaseQueryCount()).isEqualTo(3);
assertThat(totals.getDatabaseIntolerableQueryCount()).isEqualTo(2);
}

private static void validateMetricsQueue(MetricsQueue queue) {
Map<StatusCodeGroup, RequestMetricSummary> summary = queue.getDetailed();
assertNotNull(summary);
assertEquals(2, summary.size());
assertThat(summary).hasSize(2);
RequestMetricSummary twoHundredResponses = summary.get(StatusCodeGroup.SUCCESS);
assertNotNull(twoHundredResponses);
assertEquals(2, twoHundredResponses.getCount());
assertEquals(1, twoHundredResponses.getIntolerableCount());
assertEquals((double) (MAX_TIME + 3) / 2.0, twoHundredResponses.getAverageTime(), DELTA);
assertEquals(MAX_TIME + 1, twoHundredResponses.getAverageIntolerableTime(), DELTA);
assertEquals(2, twoHundredResponses.getDatabaseQueryCount());
assertEquals(3.5, twoHundredResponses.getAverageDatabaseQueryTime(), DELTA);
assertThat(twoHundredResponses).isNotNull();
assertThat(twoHundredResponses.getCount()).isEqualTo(2);
assertThat(twoHundredResponses.getIntolerableCount()).isOne();
assertThat(twoHundredResponses.getAverageTime()).isCloseTo((double) (MAX_TIME + 3) / 2.0, within(DELTA));
assertThat(twoHundredResponses.getAverageIntolerableTime()).isCloseTo(MAX_TIME + 1, within(DELTA));
assertThat(twoHundredResponses.getDatabaseQueryCount()).isEqualTo(2);
assertThat(twoHundredResponses.getAverageDatabaseQueryTime()).isCloseTo(3.5, within(DELTA));

RequestMetricSummary fiveHundredResponses = summary.get(StatusCodeGroup.SERVER_ERROR);
assertNotNull(fiveHundredResponses);
assertEquals(1, fiveHundredResponses.getCount());
assertEquals(0, fiveHundredResponses.getIntolerableCount());
assertEquals(5, fiveHundredResponses.getAverageTime(), DELTA);
assertEquals(0, fiveHundredResponses.getAverageIntolerableTime(), DELTA);
assertEquals(1, fiveHundredResponses.getDatabaseQueryCount());
assertEquals(2, fiveHundredResponses.getAverageDatabaseQueryTime(), DELTA);
assertEquals(0, fiveHundredResponses.getDatabaseIntolerableQueryCount());
assertEquals(0, fiveHundredResponses.getAverageDatabaseIntolerableQueryTime(), DELTA);

assertEquals(3, queue.getLastRequests().size());
assertThat(fiveHundredResponses).isNotNull();
assertThat(fiveHundredResponses.getCount()).isOne();
assertThat(fiveHundredResponses.getIntolerableCount()).isZero();
assertThat(fiveHundredResponses.getAverageTime()).isCloseTo(5, within(DELTA));
assertThat(fiveHundredResponses.getAverageIntolerableTime()).isCloseTo(0, within(DELTA));
assertThat(fiveHundredResponses.getDatabaseQueryCount()).isOne();
assertThat(fiveHundredResponses.getAverageDatabaseQueryTime()).isCloseTo(2, within(DELTA));
assertThat(fiveHundredResponses.getDatabaseIntolerableQueryCount()).isZero();
assertThat(fiveHundredResponses.getAverageDatabaseIntolerableQueryTime()).isCloseTo(0, within(DELTA));

assertThat(queue.getLastRequests()).hasSize(3);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;

class MetricsUtilTest {

Expand All @@ -14,13 +15,13 @@ void addToAverage() {
double avergeCount = 1.0;

double newAverage = MetricsUtil.addToAverage(avergeCount, average, 1.0, 1.0);
assertEquals(1.0, newAverage, DELTA);
assertThat(newAverage).isCloseTo(1.0, within(DELTA));

newAverage = MetricsUtil.addToAverage(avergeCount, average, 20.0, 20.0);
assertEquals(1.0, newAverage, DELTA);
assertThat(newAverage).isCloseTo(1.0, within(DELTA));

newAverage = MetricsUtil.addToAverage(avergeCount, average, 0, 0);
assertEquals(1.0, newAverage, DELTA);
assertThat(newAverage).isCloseTo(1.0, within(DELTA));
}

@Test
Expand All @@ -29,12 +30,12 @@ void addAverages() {
double avergeCount = 1.0;

double newAverage = MetricsUtil.addAverages(avergeCount, average, 5.0, 1.0);
assertEquals(1.0, newAverage, DELTA);
assertThat(newAverage).isCloseTo(1.0, within(DELTA));

newAverage = MetricsUtil.addAverages(avergeCount, average, 20.0, 1.0);
assertEquals(1.0, newAverage, DELTA);
assertThat(newAverage).isCloseTo(1.0, within(DELTA));

newAverage = MetricsUtil.addAverages(avergeCount, average, 0, 0);
assertEquals(1.0, newAverage, DELTA);
assertThat(newAverage).isCloseTo(1.0, within(DELTA));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
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.assertj.core.api.Assertions.assertThat;

class QueryMetricTest {

Expand All @@ -17,21 +16,21 @@ void setup() {

@Test
void getQuery() {
assertEquals("query", metric.getQuery());
assertThat(metric.getQuery()).isEqualTo("query");
}

@Test
void isSuccess() {
assertTrue(metric.isIntolerable());
assertThat(metric.isIntolerable()).isTrue();
}

@Test
void getRequestStartTime() {
assertEquals(1, metric.getRequestStartTime());
assertThat(metric.getRequestStartTime()).isOne();
}

@Test
void getRequestCompleteTime() {
assertEquals(6, metric.getRequestCompleteTime());
assertThat(metric.getRequestCompleteTime()).isEqualTo(6);
}
}
}
Loading

0 comments on commit 0c7b6cd

Please sign in to comment.