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

Upgrade to Jdk17 and junit5, migrate from hamcrest to assertj #8

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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: 2 additions & 2 deletions .github/workflows/build-test-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Set up JDK 8
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
java-version: '8'
java-version: '17'
distribution: 'temurin'
cache: 'maven'
- name: Setting up Github Package Repository as Maven Repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-and-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2
with:
java-version: '8'
java-version: '17'
distribution: 'temurin'
cache: 'maven'
- name: Setting up Github Package Repository as Maven Repository
Expand Down
2 changes: 1 addition & 1 deletion castor-common/3RD-PARTY-LICENSES/sbom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
<name>Project Lombok</name>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<version>1.18.20</version>
<projectUrl>https://projectlombok.org</projectUrl>
<licenses>
<license>
Expand Down
27 changes: 21 additions & 6 deletions castor-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
</dependency>

<!-- external dependencies -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down Expand Up @@ -61,18 +66,28 @@

<!-- Test dependencies -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
*/
package io.carbynestack.castor.common;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import lombok.Builder;
import lombok.Singular;
import lombok.Value;

/** Functional interface for providing a bearer token based on an {@link CastorServiceUri} value. */
@Value
@Builder
public class BearerTokenProvider implements Function<CastorServiceUri, String> {
@Singular Map<CastorServiceUri, String> bearerTokens;
Map<CastorServiceUri, String> bearerTokens;

private BearerTokenProvider(Map<CastorServiceUri, String> bearerTokens) {
this.bearerTokens = bearerTokens;
}

/**
* Returns the bearer token for the given {@link CastorServiceUri} or <i>null</i> of not token is
Expand All @@ -29,4 +31,28 @@ public class BearerTokenProvider implements Function<CastorServiceUri, String> {
public String apply(CastorServiceUri serviceUri) {
return bearerTokens.get(serviceUri);
}

public static BearerTokenProviderBuilder builder() {
return new BearerTokenProviderBuilder();
}

public static class BearerTokenProviderBuilder {
private final Map<CastorServiceUri, String> bearerTokens = new HashMap<>();

private BearerTokenProviderBuilder() {}

public BearerTokenProviderBuilder bearerToken(CastorServiceUri castorServiceUri, String token) {
this.bearerTokens.put(castorServiceUri, token);
return this;
}

@Override
public String toString() {
return "BearerTokenProviderBuilder{" + "bearerTokens=" + bearerTokens + '}';
}

public BearerTokenProvider build() {
return new BearerTokenProvider(bearerTokens);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,41 @@
import static io.carbynestack.castor.common.CastorServiceUri.INVALID_SERVICE_ADDRESS_EXCEPTION_MSG;
import static io.carbynestack.castor.common.CastorServiceUri.MUST_NOT_BE_EMPTY_EXCEPTION_MSG;
import static io.carbynestack.castor.common.rest.CastorRestApiEndpoints.*;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import io.carbynestack.castor.common.entities.TupleType;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.UUID;
import lombok.SneakyThrows;
import org.hamcrest.junit.MatcherAssert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class CastorServiceUriTest {
class CastorServiceUriTest {

@Test
public void
givenNullAsServiceAddress_whenCreatingCastorServiceUri_thenThrowIllegalArgumentException() {
void givenNullAsServiceAddress_whenCreatingCastorServiceUri_thenThrowIllegalArgumentException() {
IllegalArgumentException expectedIae =
assertThrows(IllegalArgumentException.class, () -> new CastorServiceUri(null));
assertEquals(MUST_NOT_BE_EMPTY_EXCEPTION_MSG, expectedIae.getMessage());
}

@Test
public void
givenNoSchemeDefined_whenCreatingCastorServiceUri_thenThrowIllegalArgumentException() {
void givenNoSchemeDefined_whenCreatingCastorServiceUri_thenThrowIllegalArgumentException() {
IllegalArgumentException iae =
assertThrows(IllegalArgumentException.class, () -> new CastorServiceUri("localhost:8080"));
assertEquals(INVALID_SERVICE_ADDRESS_EXCEPTION_MSG, iae.getMessage());
}

@Test
public void
givenInvalidUriString_whenCreatingCastorServiceUri_thenThrowIllegalArgumentException() {
void givenInvalidUriString_whenCreatingCastorServiceUri_thenThrowIllegalArgumentException() {
IllegalArgumentException iae =
assertThrows(IllegalArgumentException.class, () -> new CastorServiceUri("invalidUri"));
assertEquals(INVALID_SERVICE_ADDRESS_EXCEPTION_MSG, iae.getMessage());
}

@Test
public void
void
givenHttpsUriStringWithDomain_whenCreatingCastorServiceUri_thenCreateExpectedCastorServiceUri() {
CastorServiceUri serviceUri = new CastorServiceUri("https://castor.carbynestack.io:8080");
assertEquals("https://castor.carbynestack.io:8080", serviceUri.getRestServiceUri().toString());
Expand All @@ -64,7 +59,7 @@ public class CastorServiceUriTest {
}

@Test
public void
void
givenWsUriStringWithDomain_whenCreatingCastorServiceUri_thenCreateExpectedCastorServiceUri() {
CastorServiceUri serviceUri = new CastorServiceUri("ws://castor.carbynestack.io:8080");
assertEquals("http://castor.carbynestack.io:8080", serviceUri.getRestServiceUri().toString());
Expand All @@ -79,7 +74,7 @@ public class CastorServiceUriTest {
}

@Test
public void
void
givenUriStringWithTrailingSlash_whenCreateCastorServiceUri_thenReturnExpectedCastorServiceUri() {
CastorServiceUri aUri = new CastorServiceUri("https://castor.carbynestack.io:8081/");
URI inputMaskUri = aUri.getIntraVcpTelemetryUri();
Expand All @@ -94,7 +89,7 @@ public class CastorServiceUriTest {
}

@Test
public void givenCastorServiceUri_whenGetActivateTupleChunkUri_thenReturnExpectedUri() {
void givenCastorServiceUri_whenGetActivateTupleChunkUri_thenReturnExpectedUri() {
CastorServiceUri serviceUri = new CastorServiceUri("https://castor.carbynestack.io:8081");
UUID chunkId = UUID.fromString("80fbba1b-3da8-4b1e-8a2c-cebd65229fad");
String expectedPath =
Expand All @@ -107,37 +102,34 @@ public void givenCastorServiceUri_whenGetActivateTupleChunkUri_thenReturnExpecte
}

@Test
public void givenCastorServiceUri_whenGetRequestTupleUri_thenReturnExpectedUri() {
void givenCastorServiceUri_whenGetRequestTupleUri_thenReturnExpectedUri() {
CastorServiceUri serviceUri = new CastorServiceUri("https://castor.carbynestack.io:8081");
UUID requestId = UUID.fromString("80fbba1b-3da8-4b1e-8a2c-cebd65229fad");
TupleType tupleType = TupleType.INPUT_MASK_GFP;
int count = 3;
URI actualRequestTuplesUri =
serviceUri.getIntraVcpRequestTuplesUri(requestId, tupleType, count);
assertEquals(INTRA_VCP_OPERATIONS_SEGMENT + TUPLES_ENDPOINT, actualRequestTuplesUri.getPath());
MatcherAssert.assertThat(
actualRequestTuplesUri.getQuery(),
allOf(
containsString(DOWNLOAD_COUNT_PARAMETER + "=" + count),
containsString(DOWNLOAD_REQUEST_ID_PARAMETER + "=" + requestId),
containsString(DOWNLOAD_TUPLE_TYPE_PARAMETER + "=" + tupleType.name())));
assertThat(actualRequestTuplesUri.getQuery())
.contains(
DOWNLOAD_COUNT_PARAMETER + "=" + count,
DOWNLOAD_REQUEST_ID_PARAMETER + "=" + requestId,
DOWNLOAD_TUPLE_TYPE_PARAMETER + "=" + tupleType.name());
}

@Test
public void givenCastorServiceUri_whenGetRequestTelemetryUri_thenReturnExpectedUri() {
void givenCastorServiceUri_whenGetRequestTelemetryUri_thenReturnExpectedUri() {
CastorServiceUri serviceUri = new CastorServiceUri("https://castor.carbynestack.io:8081");
long interval = 5000L;
URI actualRequestTelemetryUri = serviceUri.getRequestTelemetryUri(interval);
assertEquals(
INTRA_VCP_OPERATIONS_SEGMENT + TELEMETRY_ENDPOINT, actualRequestTelemetryUri.getPath());
MatcherAssert.assertThat(
actualRequestTelemetryUri.getQuery(),
allOf(containsString(TELEMETRY_INTERVAL + "=" + interval)));
assertThat(actualRequestTelemetryUri.getQuery()).contains(TELEMETRY_INTERVAL + "=" + interval);
}

@SneakyThrows
@Test
public void givenValidPathSegments_whenBuildingResourceUri_thenReturnExpectedUri() {
void givenValidPathSegments_whenBuildingResourceUri_thenReturnExpectedUri()
throws URISyntaxException {
String baseUri = "https://castor.carbynestack.io/Castor";
String pathVariable = "1234";
CastorServiceUri CastorServiceUri = new CastorServiceUri(baseUri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@

import static io.carbynestack.castor.common.entities.ArrayBackedTuple.*;
import static io.carbynestack.castor.common.entities.Field.GFP;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;

import io.carbynestack.castor.common.exceptions.CastorClientException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import lombok.SneakyThrows;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.RandomUtils;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class ArrayBackedTupleTest {
class ArrayBackedTupleTest {

@SneakyThrows
@Test
public void givenValidStream_whenCreateFromStream_thenReturnExpectedTuple() {
void givenValidStream_whenCreateFromStream_thenReturnExpectedTuple() throws IOException {
byte[] expectedTupleValueData = RandomUtils.nextBytes(GFP.getElementSize());
byte[] expectedTupleMacData = RandomUtils.nextBytes(GFP.getElementSize());
Share expectedShare = Share.of(expectedTupleValueData, expectedTupleMacData);
Expand All @@ -36,9 +34,8 @@ public void givenValidStream_whenCreateFromStream_thenReturnExpectedTuple() {
assertEquals(expectedShare, actualBitTuple.getShare(0));
}

@SneakyThrows
@Test
public void givenStreamOfInvalidLength_whenCreateFromStream_thenReturnExpectedTuple() {
void givenStreamOfInvalidLength_whenCreateFromStream_thenReturnExpectedTuple() {
byte[] tupleValueData = RandomUtils.nextBytes(GFP.getElementSize());
byte[] invalidTupleMacData = RandomUtils.nextBytes(GFP.getElementSize() - 1);
IOException actualIoe =
Expand All @@ -52,19 +49,17 @@ public void givenStreamOfInvalidLength_whenCreateFromStream_thenReturnExpectedTu
assertEquals(NO_MORE_TUPLE_DATA_AVAILABLE_EXCEPTION_MSG, actualIoe.getMessage());
}

@SneakyThrows
@Test
public void givenValidNumberOfShares_whenCreateNewTuple_thenReturnExpectedTuple() {
void givenValidNumberOfShares_whenCreateNewTuple_thenReturnExpectedTuple() {
byte[] expectedTupleValueData = RandomUtils.nextBytes(GFP.getElementSize());
byte[] expectedTupleMacData = RandomUtils.nextBytes(GFP.getElementSize());
Share expectedShare = Share.of(expectedTupleValueData, expectedTupleMacData);
Bit<Field.Gfp> actualBitTuple = new Bit<>(GFP, expectedShare);
assertArrayEquals(new Share[] {expectedShare}, actualBitTuple.getShares());
}

@SneakyThrows
@Test
public void givenInvalidNumberOfShares_whenCreateNewTuple_thenThrowIllegalArgumentException() {
void givenInvalidNumberOfShares_whenCreateNewTuple_thenThrowIllegalArgumentException() {
IllegalArgumentException actualIae =
assertThrows(IllegalArgumentException.class, () -> new Bit<>(GFP, new Share[0]));
assertEquals(
Expand All @@ -75,10 +70,10 @@ public void givenInvalidNumberOfShares_whenCreateNewTuple_thenThrowIllegalArgume
actualIae.getMessage());
}

@SneakyThrows
@Test
public void
givenRequestedShareIndexIsOutOfBounds_whenRetrievingIndividualShare_thenThrowCastorClientException() {
void
givenRequestedShareIndexIsOutOfBounds_whenRetrievingIndividualShare_thenThrowCastorClientException()
throws IOException {
int invalidIndex = TupleType.BIT_GFP.getArity() + 1;
byte[] expectedTupleValueData = RandomUtils.nextBytes(GFP.getElementSize());
byte[] expectedTupleMacData = RandomUtils.nextBytes(GFP.getElementSize());
Expand Down
Loading