Skip to content

Commit

Permalink
Migrated junit Platform (#145)
Browse files Browse the repository at this point in the history
fixes #144 
- Migrated Captcha Module
- Migrated Server Module
- Migrated Server Module (fixed junit, hazelcast serialization problem)
  • Loading branch information
StefanSchubert authored Nov 25, 2023
1 parent 547261b commit 8c02b78
Show file tree
Hide file tree
Showing 30 changed files with 214 additions and 390 deletions.
19 changes: 6 additions & 13 deletions captcha/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<springdoc.openapiv2.version>2.2.0</springdoc.openapiv2.version>
<micrometer.prometheus.version>1.11.5</micrometer.prometheus.version>
<junit.version>4.13.2</junit.version>
<owasp.plugin.version>8.4.2</owasp.plugin.version>
<versions.maven.plugin.version>2.16.1</versions.maven.plugin.version>
<micrometer.prometheus.version>1.12.0</micrometer.prometheus.version>
<owasp.plugin.version>9.0.0</owasp.plugin.version>
<maven-surefire-plugin.version>3.2.2</maven-surefire-plugin.version>
<versions.maven.plugin.version>2.16.2</versions.maven.plugin.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -106,21 +106,13 @@
<version>${springdoc.openapiv2.version}</version>
</dependency>


<!-- Test dependencies -->
<!-- Test dependencies, since SB3.x junit5 is default -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

</dependencies>


Expand Down Expand Up @@ -151,6 +143,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<dependencies/>
</plugin>
<plugin>
Expand Down
49 changes: 25 additions & 24 deletions captcha/src/test/java/de/bluewhale/captcha/service/CheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,35 @@

import de.bluewhale.captcha.configs.AppConfig;
import jakarta.annotation.PostConstruct;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

import static org.springframework.test.util.AssertionErrors.*;

/**
* Functional and quality tests of the captcha validation
*
* @author Stefan Schubert
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@ContextConfiguration(classes = AppConfig.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class CheckerTest {

final String BASE_API_URL = "http://localhost:8081/captcha/api";
Expand All @@ -53,7 +54,7 @@ public void lazyInit() {
configuredTTL = Long.parseLong(ttl);
}

@Before
@BeforeEach
public void resetThresholdMeter(){
ChallengeRequestThrottle.resetAPICounter();
ChallengeRequestThrottle.setThrottleThreshold(MAX_CHALLENGE_REQUEST_THROUGHPUT_PER_MINUTE);
Expand All @@ -72,9 +73,9 @@ public void testTTLInit() throws Exception {
long ttl = ValidationCache.getTTL();

// Then
Assert.assertTrue("application.properties from testclass not accessed?", configuredTTL > 0);
Assert.assertTrue("application.properties from ValidationCache not accessed?", ttl > 0);
Assert.assertEquals("Different than configured TTL?", configuredTTL, ttl);
assertTrue("application.properties from testclass not accessed?", configuredTTL > 0);
assertTrue("application.properties from ValidationCache not accessed?", ttl > 0);
assertEquals("Different than configured TTL?", configuredTTL, ttl);
}


Expand All @@ -85,11 +86,11 @@ public void validateAnswer() throws Exception {
ValidationCache.registerToken(validToken);

// When
Assert.assertTrue("Register of token failed", ValidationCache.knowsCode(validToken));
assertTrue("Register of token failed", ValidationCache.knowsCode(validToken));
ValidationCache.invalidateCode(validToken);

// Then
Assert.assertFalse("Token was not consumed", ValidationCache.knowsCode(validToken));
assertFalse("Token was not consumed", ValidationCache.knowsCode(validToken));
}


Expand All @@ -109,7 +110,7 @@ public void validateAnswerViaRestCall() throws Exception {
final String checkresult = restTemplate.getForObject(checkURI, String.class, params);

// Then (be Happy)
Assert.assertEquals("Ouch - Token was not recognized", "Accepted", checkresult);
assertEquals("Ouch - Token was not recognized", "Accepted", checkresult);
}

@Test
Expand All @@ -129,12 +130,12 @@ public void testAPIThrottle() throws Exception {
String forObject = restTemplate.getForObject(checkURI, String.class);
} catch (RestClientException e) {
// 429 - TOO MANY Request Exception
Assert.assertTrue(e.getMessage().startsWith("429"));
assertTrue("Did not retrieved expected error code.",e.getMessage().startsWith("429"));
}
}

// Then (be Happy)
Assert.assertEquals("Throttle ignition too early.", MAX_CHALLENGE_REQUEST_THROUGHPUT_PER_MINUTE + 1, too_many_requests);
assertTrue("Throttle ignition too early.", (MAX_CHALLENGE_REQUEST_THROUGHPUT_PER_MINUTE + 1) == too_many_requests);
}


Expand All @@ -154,7 +155,7 @@ public void testResetAPIThrottle() throws Exception {
restTemplate.getForObject(checkURI, String.class);
} catch (RestClientException e) {
// 429 - TOO MANY Request Exception
Assert.assertTrue(e.getMessage().startsWith("429"));
assertTrue("Did not retrieved expected error code.",e.getMessage().startsWith("429"));
break;
}
}
Expand All @@ -165,9 +166,9 @@ public void testResetAPIThrottle() throws Exception {
// Then (should work without throwing excepting)
try {
String object = restTemplate.getForObject(checkURI, String.class);
Assert.assertTrue("Did not received a valid captcha.", object.length() > 10);
assertTrue("Did not received a valid captcha.", object.length() > 10);
} catch (Exception e) {
Assert.fail("Last call should have been accepted.");
fail("Last call should have been accepted.");
}
}

Expand All @@ -185,13 +186,13 @@ public void testThrottleMechanism() throws Exception {
restTemplate.getForObject(checkURI, String.class);
} catch (RestClientException e) {
// 429 - TOO MANY Request Exception
Assert.assertTrue(e.getMessage().startsWith("429"));
assertTrue("Did not retrieved expected error code.",e.getMessage().startsWith("429"));
break;
}
}

// Then
Assert.assertFalse("Throttel not activated.", ChallengeRequestThrottle.requestAllowed());
assertFalse("Throttel not activated.", ChallengeRequestThrottle.requestAllowed());

}

Expand All @@ -214,7 +215,7 @@ public void testCleanUpOfStaleTokens() throws Exception {

ValidationCache.setTTL(old_ttl); // restablish to avoid conflicts with other tests,
// Because of the expired TTL the stale one should be unknown by now
Assert.assertFalse("Cache cleanup failed", ValidationCache.knowsCode(staleToken));
assertFalse("Cache cleanup failed", ValidationCache.knowsCode(staleToken));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
package de.bluewhale.captcha.service;

import de.bluewhale.captcha.model.ChallengeTo;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.Locale;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Functional and quality tests of the captcha controller
Expand All @@ -26,12 +26,11 @@ public void provideChallengeForDefaultFallbackLanguage() throws Exception {
QAGenerator generator = new QAGenerator();
ChallengeTo challengeTo = generator.provideChallengeFor("unknown");

assertNotNull("Generator did not delivered a captcha", challengeTo);
assertNotNull("Generator did not delivered a captcha question", challengeTo.getQuestion());
assertNotNull("Generator did not delivered captcha answer options", challengeTo.getAnswers());
assertNotNull("Generator did not specified the used language", challengeTo.getLanguage());
assertEquals("Fallback language mechanism was not working", "en", challengeTo.getLanguage());
}
assertNotNull(challengeTo, "Generator did not delivered a captcha");
assertNotNull(challengeTo.getQuestion(), "Generator did not delivered a captcha question");
assertNotNull(challengeTo.getAnswers(), "Generator did not delivered captcha answer options");
assertNotNull(challengeTo.getLanguage(), "Generator did not specified the used language");
assertEquals("en", challengeTo.getLanguage(), "Fallback language mechanism was not working"); }


@Test
Expand All @@ -43,11 +42,10 @@ public void testChallengeForRequestedLanguage() throws Exception {
ChallengeTo challengeTo = generator.provideChallengeFor(Locale.GERMAN.getLanguage());

// Then
assertNotNull("Generator did not delivered a captcha", challengeTo);
assertNotNull("Generator did not delivered a captcha question", challengeTo.getQuestion());
assertNotNull("Generator did not delivered captcha answer options", challengeTo.getAnswers());
assertNotNull("Generator did not specified the used language", challengeTo.getLanguage());
assertEquals("Did not get my language", "de", challengeTo.getLanguage());
assertNotNull(challengeTo, "Generator did not delivered a captcha");
assertNotNull(challengeTo.getQuestion(), "Generator did not delivered a captcha question");
assertNotNull(challengeTo.getAnswers(), "Generator did not delivered captcha answer options");
assertNotNull(challengeTo.getLanguage(), "Generator did not specified the used language");
assertEquals("de", challengeTo.getLanguage(), "Did not get my language");
}

}
51 changes: 14 additions & 37 deletions sabi-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,22 @@
<springdoc.openapiv2.version>2.2.0</springdoc.openapiv2.version>
<eclipselink.version>4.0.2</eclipselink.version>
<mapstruct.version>1.5.5.Final</mapstruct.version>
<micrometer.prometheus.version>1.11.5</micrometer.prometheus.version>
<spring.instrument.version>6.0.13</spring.instrument.version>
<surefire.version>3.2.2</surefire.version>
<micrometer.prometheus.version>1.12.0</micrometer.prometheus.version>
<spring.instrument.version>6.1.1</spring.instrument.version>
<lombok.version>1.18.30</lombok.version>
<owasp.plugin.version>8.4.2</owasp.plugin.version>
<owasp.plugin.version>9.0.0</owasp.plugin.version>
<passay.version>1.6.4</passay.version>
<mariadb.java.client.version>3.2.0</mariadb.java.client.version>
<mariadb.java.client.version>3.3.0</mariadb.java.client.version>
<h2.version>2.1.214</h2.version>
<dumbster.version>1.6</dumbster.version>
<junit.version>4.13.2</junit.version>
<archunit.version>1.2.0</archunit.version>
<auth0.jwt.version>4.4.0</auth0.jwt.version>
<jakarta.xml.bind.version>4.0.0</jakarta.xml.bind.version>
<jetbrains.annotation.version>24.0.1</jetbrains.annotation.version>
<versions.maven.plugin.version>2.16.1</versions.maven.plugin.version>
<jetbrains.annotation.version>24.1.0</jetbrains.annotation.version>
<versions.maven.plugin.version>2.16.2</versions.maven.plugin.version>
<maven-surefire-plugin.version>3.2.2</maven-surefire-plugin.version>
</properties>

<dependencyManagement>
<dependencies>

<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>

</dependencies>
</dependencyManagement>

<dependencies>

<!-- SABI Modules -->
Expand Down Expand Up @@ -254,7 +241,7 @@
<artifactId>spring-instrument</artifactId>
</dependency>

<!-- Test dependencies -->
<!-- Test dependencies, Since SB3 we have junit5 as default -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down Expand Up @@ -291,13 +278,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<!-- In case you want to switch from junit4.x make sure to adopt the surefire provider as well. -->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<!-- Supports checking on architectural integrity -->
<dependency>
Expand Down Expand Up @@ -353,23 +333,20 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<version>${maven-surefire-plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit-platform</artifactId>
<version>${maven-surefire-plugin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${spring.instrument.version}</version>
</dependency>
<dependency>
<!-- Surefire seemed to have difficulties to determine the test provider junit4
in our case, so we configured the provider manually here.-->
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
<configuration>
<test>de.bluewhale.sabi.MasterTestSuite</test>
<!--suppress UnresolvedMavenProperty -->
<argLine>
-javaagent:${settings.localRepository}/org/springframework/spring-instrument/${spring.instrument.version}/spring-instrument-${spring.instrument.version}.jar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -91,6 +92,7 @@ public List<MeasurementTo> listMeasurementsFilteredBy(Long pTankID, Integer pUni


@Override
@Cacheable("unitsCache")
public @NotNull List<UnitTo> listAllMeasurementUnits() {
List<UnitTo> unitToList = Collections.emptyList();
List<UnitEntity> unitEntityList = unitRepository.findAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
import com.tngtech.archunit.library.Architectures;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;


/**
* Checking on architectural constraints
*
* @author Stefan Schubert
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
public class ArchitectureTest {

private static final String PACKAGE_PREFIX = "de.bluewhale.sabi.";
Expand Down
Loading

0 comments on commit 8c02b78

Please sign in to comment.