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

feat: add end-to-end test #4

Merged
merged 1 commit into from
Mar 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public String name() {

@Override
public void initialize(ServiceExtensionContext context) {
webServer.addPortMapping(CONTEXT_NAME, PORT, PATH);
webService.registerResource(CONTEXT_NAME, new ManagementApiController(store));
webService.registerResource(CONTEXT_NAME, new AuthenticationRequestFilter(authService));
}
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ format.version = "1.1"
edc = "0.5.0"
nimbus = "9.37.3"
restAssured = "5.4.0"
jupiter = "5.4.0"

[libraries]
edc-core-jetty = { module = "org.eclipse.edc:jetty-core", version.ref = "edc" }
Expand All @@ -24,6 +25,7 @@ edc-junit = { module = "org.eclipse.edc:junit", version.ref = "edc" }
# Third party libs
nimbus-jwt = { module = "com.nimbusds:nimbus-jose-jwt", version.ref = "nimbus" }
restAssured = { module = "io.rest-assured:rest-assured", version.ref = "restAssured" }
junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "jupiter" }

[plugins]
shadow = { id = "com.github.johnrengelman.shadow", version = "8.1.1" }
Expand Down
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ include(":core:core-services")
include(":api:directory-api")
include(":api:management-api")
include(":runtimes:server")
include(":system-tests:test-server")
include(":system-tests:test-directory")
26 changes: 26 additions & 0 deletions system-tests/test-directory/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*/

plugins {
java
}

dependencies {

testImplementation(libs.edc.junit)
testImplementation(libs.junit.jupiter.api)
testImplementation(libs.restAssured)

testCompileOnly(project(":system-tests:test-server"))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.tractusx.bdrs.test.directory;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.restassured.specification.RequestSpecification;
import org.eclipse.edc.junit.extensions.EdcRuntimeExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import java.util.zip.GZIPInputStream;

import static io.restassured.RestAssured.config;
import static io.restassured.RestAssured.given;
import static io.restassured.config.DecoderConfig.ContentDecoder.DEFLATE;
import static io.restassured.config.DecoderConfig.decoderConfig;
import static io.restassured.http.ContentType.JSON;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.junit.testfixtures.TestUtils.getFreePort;

/**
* Performs end-to-end testing of the BPN Directory.
*/
public class E2EDirectoryTest {
private static final URI apiEndpoint = URI.create("http://localhost:" + getFreePort() + "/api");
private static final URI managementEndpoint = URI.create("http://localhost:" + getFreePort() + "/management/v1");
private static final String BPN_DIRECTORY = "bpn-directory";

private static final String AUTH_KEY = "1234";

private static final String BPN1 = "BPN12345";
private static final String DID1 = "did:web:localhost/foo";

private static final String BPN2 = "BPN67890";
private static final String DID2 = "did:web:localhost/bar";
private static final String UPDATED_DID2 = "did:web:localhost/baz";

@RegisterExtension
protected static EdcRuntimeExtension runtime =
new EdcRuntimeExtension(
":system-tests:test-server",
"bdrs",
Map.of("web.http.port", String.valueOf(apiEndpoint.getPort()),
"web.http.management.port", String.valueOf(managementEndpoint.getPort()),
"web.http.management.path", String.valueOf(managementEndpoint.getPath()),
"edc.api.auth.key", AUTH_KEY)
);

private ObjectMapper mapper;

@Test
void verifyPublicBpnDirectoryRequest() throws IOException {
seedServer(BPN1, DID1);
var result = getBpnDirectory(apiRequest());
assertThat(result.get(BPN1)).isEqualTo(DID1);
}

@Test
void verifyManagementApi() throws IOException {
seedServer(BPN1, DID1);
seedServer(BPN2, DID2);

// verify BPNs are returned
var result = getBpnDirectory(managementRequest());

assertThat(result.get(BPN1)).isEqualTo(DID1);
assertThat(result.get(BPN2)).isEqualTo(DID2);

// verify delete
managementRequest()
.when()
.delete(BPN_DIRECTORY + "/" + BPN1)
.then()
.statusCode(204);

result = getBpnDirectory(managementRequest());

assertThat(result.get(BPN1)).isNull();
assertThat(result.get(BPN2)).isEqualTo(DID2);

// verify update
var content = Map.of("bpn", BPN2, "did", UPDATED_DID2);
managementRequest()
.body(content)
.when()
.put(BPN_DIRECTORY)
.then()
.statusCode(204);

result = getBpnDirectory(managementRequest());

assertThat(result.get(BPN2)).isEqualTo(UPDATED_DID2);
}

@BeforeEach
void setUp() {
mapper = new ObjectMapper();
}

private void seedServer(String bpn, String did) {
var content = Map.of("bpn", bpn, "did", did);
managementRequest()
.body(content)
.when()
.post(BPN_DIRECTORY)
.then()
.statusCode(204);
}

private Map<String, String> getBpnDirectory(RequestSpecification spec) throws IOException {
return deserialize(spec
.config(config().decoderConfig(decoderConfig().contentDecoders(DEFLATE)))
.when()
.get(BPN_DIRECTORY)
.then()
.statusCode(200).
extract()
.response()
.asByteArray());
}


private RequestSpecification apiRequest() {
return given().baseUri(apiEndpoint.toString())
.headers(Map.of());
}

private RequestSpecification managementRequest() {
return given().baseUri(managementEndpoint.toString())
.headers(Map.of("x-api-key", AUTH_KEY))
.contentType(JSON);
}

private Map<String, String> deserialize(byte[] response) throws IOException {
var stream = new GZIPInputStream(new ByteArrayInputStream(response));
var decompressed = stream.readAllBytes();
//noinspection unchecked
return mapper.readValue(decompressed, Map.class);
}

}
46 changes: 46 additions & 0 deletions system-tests/test-server/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
*/

plugins {
id("application")
alias(libs.plugins.shadow)
}

dependencies {
api(libs.bundles.bdrs.boot)
api(project(":core:core-services"))
api(project(":api:directory-api"))
api(project(":api:management-api"))
}

tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar> {
exclude("**/pom.properties", "**/pom.xm")
mergeServiceFiles()
archiveFileName.set("${project.name}.jar")
}

application {
mainClass.set("org.eclipse.edc.boot.system.runtime.BaseRuntime")
}

edcBuild {
publish.set(false)
}