Skip to content

Commit

Permalink
chore: self-review
Browse files Browse the repository at this point in the history
  • Loading branch information
richardtreier committed Oct 1, 2024
1 parent ef1d864 commit 5f4d97f
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 149 deletions.
11 changes: 9 additions & 2 deletions config/src/main/java/de/sovity/edc/utils/config/ConfigProps.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,18 @@ public class ConfigProps {
.defaultValue("*")
.build().also(ALL_CE_PROPS::add);

private static final ConfigProp EDC_BUILD_DATE = ConfigProp.builder()
public static final ConfigProp EDC_BUILD_DATE = ConfigProp.builder()
.category(Category.ADVANCED)
.property("edc.build.date")
.description("Build Date, usually set via CI into a build arg into the built image")
.defaultValue("Unknown version")
.defaultValue("Unknown Version")
.build().also(ALL_CE_PROPS::add);

public static final ConfigProp EDC_LAST_COMMIT_INFO = ConfigProp.builder()
.category(Category.ADVANCED)
.property("edc.build.date")
.description("Last Commit Info / Build Version, usually set via CI into a build arg into the built image")
.defaultValue("Unknown Version")
.build().also(ALL_CE_PROPS::add);

public static final ConfigProp MY_EDC_DOCKER_COMPOSE_SERVICE_NAME = ConfigProp.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,4 @@
@FunctionalInterface
public interface ConfigPropDefaultValueFn {
String apply(Map<String, String> props);

static ConfigPropDefaultValueFn constant(String value) {
return p -> value;
}
}
1 change: 1 addition & 0 deletions extensions/edc-ui-config/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
implementation(libs.jakarta.rsApi)
implementation(libs.jakarta.validationApi)

testImplementation(project(":utils:test-utils"))
testImplementation(libs.edc.controlPlaneCore)
testImplementation(libs.edc.junit)
testImplementation(libs.edc.http) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

package de.sovity.edc.extension.version.controller;

import de.sovity.edc.extension.e2e.connector.config.ConnectorConfig;
import de.sovity.edc.extension.e2e.connector.config.ConnectorConfigFactory;
import de.sovity.edc.extension.e2e.db.JdbcCredentials;
import de.sovity.edc.extension.e2e.db.TestDatabase;
import io.restassured.http.ContentType;
import org.eclipse.edc.connector.dataplane.selector.spi.store.DataPlaneInstanceStore;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.junit.annotations.ApiTest;
Expand All @@ -23,33 +28,48 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.Map;

import static de.sovity.edc.extension.version.controller.TestUtils.createConfiguration;
import static de.sovity.edc.extension.version.controller.TestUtils.mockRequest;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ApiTest
@ExtendWith(EdcExtension.class)
class EdcUiConfigTest {
private static final String SOME_EXAMPLE_PROP = "this should also be passed through";

private ConnectorConfig config;

@BeforeEach
void setUp(EdcExtension extension) {
extension.registerServiceMock(ProtocolWebhook.class, mock(ProtocolWebhook.class));
extension.registerServiceMock(JsonLd.class, mock(JsonLd.class));
extension.registerServiceMock(
DataPlaneInstanceStore.class,
mock(DataPlaneInstanceStore.class));
extension.setConfiguration(createConfiguration(Map.of(
"edc.ui.some.example.prop", SOME_EXAMPLE_PROP
)));
extension.registerServiceMock(DataPlaneInstanceStore.class, mock(DataPlaneInstanceStore.class));

var testDatabase = mock(TestDatabase.class);
when(testDatabase.getJdbcCredentials()).thenReturn(new JdbcCredentials("unused", "unused", "unused"));

config = ConnectorConfigFactory.forTestDatabase("provider", testDatabase);
config.setProperty("edc.ui.some.example.prop", SOME_EXAMPLE_PROP);

extension.setConfiguration(config.getProperties());
}

@Test
void testEdcUiConfigWithEverythingSet() {
mockRequest().assertThat()
.body("EDC_UI_SOME_EXAMPLE_PROP", equalTo(SOME_EXAMPLE_PROP));
var request = given()
.baseUri(config.getManagementApiUrl())
.header("X-Api-Key", config.getManagementApiKey())
.when()
.contentType(ContentType.JSON)
.get("/edc-ui-config")
.then()
.statusCode(200)
.contentType(ContentType.JSON);

request.assertThat()
.body("EDC_UI_SOME_EXAMPLE_PROP", equalTo(SOME_EXAMPLE_PROP));
;

}
}

This file was deleted.

3 changes: 3 additions & 0 deletions extensions/last-commit-info/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ dependencies {
annotationProcessor(libs.lombok)
compileOnly(libs.lombok)

implementation(project(":config"))

api(libs.edc.coreSpi)
api(libs.edc.controlPlaneSpi)
implementation(libs.edc.apiCore)
Expand All @@ -19,6 +21,7 @@ dependencies {
testAnnotationProcessor(libs.lombok)
testCompileOnly(libs.lombok)

testImplementation(project(":utils:test-utils"))
testImplementation(libs.edc.controlPlaneCore)
testImplementation(libs.edc.junit)
testImplementation(libs.edc.http) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public String name() {

@Override
public void initialize(ServiceExtensionContext context) {
var lastCommitInfoService = new LastCommitInfoService(context);
var lastCommitInfoService = new LastCommitInfoService(context.getConfig());
var controller = new LastCommitInfoController(lastCommitInfoService);
webService.registerResource(config.getContextAlias(), controller);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,50 @@

package de.sovity.edc.extension;

import org.eclipse.edc.spi.system.ServiceExtensionContext;
import de.sovity.edc.utils.config.ConfigProps;
import lombok.RequiredArgsConstructor;
import org.eclipse.edc.spi.system.configuration.Config;

import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Scanner;

@RequiredArgsConstructor
public class LastCommitInfoService {

private final ServiceExtensionContext context;
private final Config config;

public LastCommitInfoService(ServiceExtensionContext context) {
this.context = context;
}
public LastCommitInfo getLastCommitInfo() {
var lastCommitInfo = new LastCommitInfo();
lastCommitInfo.setEnvLastCommitInfo(getEnvLastCommitInfo());
lastCommitInfo.setJarLastCommitInfo(getJarLastCommitInfo());

private String readFileInCurrentClassClasspath(String path) {
var classLoader = LastCommitInfoService.class.getClassLoader();
var is = classLoader.getResourceAsStream(path);
var scanner = new Scanner(Objects.requireNonNull(is), StandardCharsets.UTF_8).useDelimiter("\\A");
return scanner.hasNext() ? scanner.next() : "";
lastCommitInfo.setJarBuildDate(getJarBuildDate());
lastCommitInfo.setEnvBuildDate(getEnvBuildDate());
return lastCommitInfo;
}


public String getJarLastCommitInfo() {
private String getJarLastCommitInfo() {
return this.readFileInCurrentClassClasspath("jar-last-commit-info.txt").trim();
}

public String getEnvLastCommitInfo() {
return context.getSetting("edc.last.commit.info", "");
private String getEnvLastCommitInfo() {
return ConfigProps.EDC_LAST_COMMIT_INFO.getStringOrThrow(config);
}

public String getJarBuildDate() {
private String getJarBuildDate() {
return readFileInCurrentClassClasspath("jar-build-date.txt").trim();
}

public String getEnvBuildDate() {
return context.getSetting("edc.build.date", "");
private String getEnvBuildDate() {
return ConfigProps.EDC_BUILD_DATE.getStringOrThrow(config);
}

public LastCommitInfo getLastCommitInfo() {
var lastCommitInfo = new LastCommitInfo();
lastCommitInfo.setEnvLastCommitInfo(getEnvLastCommitInfo());
lastCommitInfo.setJarLastCommitInfo(getJarLastCommitInfo());

lastCommitInfo.setJarBuildDate(getJarBuildDate());
lastCommitInfo.setEnvBuildDate(getEnvBuildDate());
return lastCommitInfo;
private String readFileInCurrentClassClasspath(String path) {
var classLoader = LastCommitInfoService.class.getClassLoader();
var is = classLoader.getResourceAsStream(path);
var scanner = new Scanner(Objects.requireNonNull(is), StandardCharsets.UTF_8).useDelimiter("\\A");
return scanner.hasNext() ? scanner.next() : "";
}
}

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

package de.sovity.edc.extension.version.controller;

import de.sovity.edc.extension.e2e.connector.config.ConnectorConfig;
import de.sovity.edc.extension.e2e.connector.config.ConnectorConfigFactory;
import de.sovity.edc.extension.e2e.db.JdbcCredentials;
import de.sovity.edc.extension.e2e.db.TestDatabase;
import de.sovity.edc.utils.config.ConfigProps;
import io.restassured.http.ContentType;
import org.eclipse.edc.connector.dataplane.selector.spi.store.DataPlaneInstanceStore;
import org.eclipse.edc.jsonld.spi.JsonLd;
Expand All @@ -30,34 +35,36 @@
import static org.eclipse.edc.junit.testfixtures.TestUtils.getFreePort;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ApiTest
@ExtendWith(EdcExtension.class)
class LastCommitInfoTest {

private ConnectorConfig config;

@BeforeEach
void setUp(EdcExtension extension) {

extension.registerServiceMock(ProtocolWebhook.class, mock(ProtocolWebhook.class));
extension.registerServiceMock(JsonLd.class, mock(JsonLd.class));
extension.registerServiceMock(
DataPlaneInstanceStore.class,
mock(DataPlaneInstanceStore.class));
extension.setConfiguration(Map.of(
"web.http.port", String.valueOf(getFreePort()),
"web.http.path", "/api",
"web.http.management.port", String.valueOf(TestUtils.DATA_PORT),
"web.http.management.path", "/api/v1/data",
"edc.api.auth.key", TestUtils.AUTH_KEY,
"edc.last.commit.info", "test env commit message",
"edc.build.date", "2023-05-08T15:30:00Z"));
extension.registerServiceMock(DataPlaneInstanceStore.class, mock(DataPlaneInstanceStore.class));

var testDatabase = mock(TestDatabase.class);
when(testDatabase.getJdbcCredentials()).thenReturn(new JdbcCredentials("unused", "unused", "unused"));

config = ConnectorConfigFactory.forTestDatabase("provider", testDatabase);
config.setProperty(ConfigProps.EDC_LAST_COMMIT_INFO, "test env commit message");
config.setProperty(ConfigProps.EDC_BUILD_DATE, "2023-05-08T15:15:00Z");

extension.setConfiguration(config.getProperties());
}

@Test
void testEnvAndJar() {
var request = given()
.baseUri("http://localhost:" + TestUtils.DATA_PORT)
.basePath("/api/v1/data")
.header("X-Api-Key", TestUtils.AUTH_KEY)
.baseUri(config.getManagementApiUrl())
.header("X-Api-Key", config.getManagementApiKey())
.when()
.contentType(ContentType.JSON)
.get("/last-commit-info")
Expand All @@ -66,7 +73,7 @@ void testEnvAndJar() {
.contentType(ContentType.JSON);

request.assertThat().body("envLastCommitInfo", equalTo("test env commit message"))
.body("envBuildDate", equalTo("2023-05-08T15:30:00Z"))
.body("envBuildDate", equalTo("2023-05-08T15:15:00Z"))
.body("jarLastCommitInfo", equalTo("test jar commit message"))
.body("jarBuildDate", equalTo("2023-05-09T15:30:00Z"));

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public RequestSpecification prepareManagementApiCall() {
var apiUrl = config.getManagementApiUrl();
var request = given().baseUri(apiUrl);

var header = config.getManagementApiAuthHeader().get();
var header = config.getManagementApiAuthHeader();
if (header != null) {
request = request.header(new Header(header.getLeft(), header.getRight()));
}
Expand Down
Loading

0 comments on commit 5f4d97f

Please sign in to comment.