Skip to content

Commit

Permalink
qa.commons 3.7.6
Browse files Browse the repository at this point in the history
remove old cache test
add new CacheTest as new class in the test scope
update pom to provide a test-jar
  • Loading branch information
Calvin committed Jul 25, 2023
1 parent ccd286b commit c739646
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 150 deletions.
18 changes: 17 additions & 1 deletion qanary_commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
<modelVersion>4.0.0</modelVersion>
<groupId>eu.wdaqua.qanary</groupId>
<artifactId>qa.commons</artifactId>
<version>3.7.5</version>
<version>3.7.6</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<java.version>11</java.version>
<spring-boot-admin.version>2.7.10</spring-boot-admin.version>
Expand Down Expand Up @@ -297,6 +300,19 @@
</execution>
</executions>
</plugin>

<!-- For Component Cache Tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
99 changes: 99 additions & 0 deletions qanary_commons/src/test/java/qa/commons/QanaryCacheTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package qa.commons;

import eu.wdaqua.qanary.communications.CacheOfRestTemplateResponse;
import eu.wdaqua.qanary.communications.RestTemplateWithCaching;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.*;

public class QanaryCacheTest {
private static final Logger LOGGER = LoggerFactory.getLogger(QanaryCacheTest.class);

private final int testPort;
// time span for caching, tests wait this time span during the test runs
private final int maxTimeSpanSeconds;
private final RestTemplateWithCaching myRestTemplate;
private final CacheOfRestTemplateResponse myCacheOfResponse;

public QanaryCacheTest(
int testPort, //
int maxTimeSpanSeconds, //
@Autowired RestTemplateWithCaching myRestTemplate, //
@Autowired CacheOfRestTemplateResponse myCacheOfResponse //
) {
this.testPort = testPort;
this.maxTimeSpanSeconds = maxTimeSpanSeconds;
this.myRestTemplate = myRestTemplate;
this.myCacheOfResponse = myCacheOfResponse;
}

/**
* @throws InterruptedException
* @throws URISyntaxException
*/
public void givenRestTemplate_whenRequested_thenLogAndModifyResponse() throws InterruptedException, URISyntaxException {

assertNotNull(this.myRestTemplate);
assertNotNull(this.myCacheOfResponse);

URI testServiceURL0 = new URI("http://localhost:" + this.testPort + "/");
URI testServiceURL1 = new URI("http://localhost:" + this.testPort + "/component-description");

long initialNumberOfRequests = this.myCacheOfResponse.getNumberOfExecutedRequests();

callRestTemplateWithCaching(testServiceURL0, Cache.NOT_CACHED); // cache miss
callRestTemplateWithCaching(testServiceURL0, Cache.CACHED); // cache hit
callRestTemplateWithCaching(testServiceURL0, Cache.CACHED); // cache hit

TimeUnit.SECONDS.sleep(this.maxTimeSpanSeconds + 5); // wait until it is too late for caching

callRestTemplateWithCaching(testServiceURL0, Cache.NOT_CACHED); // cache miss: too long ago
callRestTemplateWithCaching(testServiceURL0, Cache.CACHED); // cache hit
callRestTemplateWithCaching(testServiceURL1, Cache.NOT_CACHED); // cache miss: different URI
callRestTemplateWithCaching(testServiceURL0, Cache.CACHED); // cache hit
callRestTemplateWithCaching(testServiceURL1, Cache.CACHED); // cache hit

assertEquals(initialNumberOfRequests + 3, this.myCacheOfResponse.getNumberOfExecutedRequests());

}

/**
* @param uri
* @param cacheStatus
* @throws URISyntaxException
*/
private void callRestTemplateWithCaching(URI uri, Cache cacheStatus) throws URISyntaxException {
long numberOfNewlyExecutedRequests = this.myCacheOfResponse.getNumberOfExecutedRequests();
ResponseEntity<String> responseEntity = this.myRestTemplate.getForEntity(uri, String.class);
numberOfNewlyExecutedRequests = this.myCacheOfResponse.getNumberOfExecutedRequests() - numberOfNewlyExecutedRequests;
this.LOGGER.info("numberOfExecutedRequest since last request: new={}, count={}, teststatus={}", //
numberOfNewlyExecutedRequests, this.myCacheOfResponse.getNumberOfExecutedRequests(), cacheStatus);

assertEquals(HttpStatus.OK, responseEntity.getStatusCode());

switch (cacheStatus) {
case NOT_CACHED:
assertEquals(1, numberOfNewlyExecutedRequests);
break;
case CACHED:
assertEquals(0, numberOfNewlyExecutedRequests);
break;
default:
fail("Test case misconfigured");
break;
}
}

private enum Cache {
CACHED, NOT_CACHED
}
}

144 changes: 0 additions & 144 deletions qanary_commons/src/test/java/qa/commons/RestTemplateCacheLiveTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

@TestConfiguration
public class RestTemplateCacheLiveTestConfiguration {
// define here the current CaffeineCacheManager configuration
static {
System.setProperty("qanary.webservicecalls.cache.specs",
"maximumSize=1000,expireAfterAccess=" + RestTemplateCacheLiveTest.MAX_TIME_SPAN_SECONDS + "s");
}

@Bean
@Primary
Expand Down

0 comments on commit c739646

Please sign in to comment.