-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: introduced message classes for errors (#4540)
* refactor: introduced message classes for errors * pr suggestions
- Loading branch information
Showing
38 changed files
with
1,336 additions
and
411 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...clipse/edc/protocol/dsp/catalog/transform/from/JsonObjectFromCatalogErrorTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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.edc.protocol.dsp.catalog.transform.from; | ||
|
||
import jakarta.json.JsonBuilderFactory; | ||
import jakarta.json.JsonObject; | ||
import org.eclipse.edc.connector.controlplane.catalog.spi.CatalogError; | ||
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer; | ||
import org.eclipse.edc.transform.spi.TransformerContext; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; | ||
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_ERROR; | ||
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CODE; | ||
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_REASON; | ||
|
||
/** | ||
* Transforms a {@link CatalogError} to a {@link JsonObject} in JSON-LD expanded form. | ||
*/ | ||
public class JsonObjectFromCatalogErrorTransformer extends AbstractJsonLdTransformer<CatalogError, JsonObject> { | ||
|
||
private final JsonBuilderFactory jsonFactory; | ||
|
||
public JsonObjectFromCatalogErrorTransformer(JsonBuilderFactory jsonFactory) { | ||
super(CatalogError.class, JsonObject.class); | ||
this.jsonFactory = jsonFactory; | ||
} | ||
|
||
@Override | ||
public @Nullable JsonObject transform(@NotNull CatalogError error, @NotNull TransformerContext context) { | ||
return jsonFactory.createObjectBuilder() | ||
.add(TYPE, DSPACE_TYPE_CATALOG_ERROR) | ||
.add(DSPACE_PROPERTY_CODE, error.getCode()) | ||
.add(DSPACE_PROPERTY_REASON, jsonFactory.createArrayBuilder(error.getMessages())) | ||
.build(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...se/edc/protocol/dsp/catalog/transform/from/JsonObjectFromCatalogErrorTransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.edc.protocol.dsp.catalog.transform.from; | ||
|
||
import jakarta.json.Json; | ||
import jakarta.json.JsonBuilderFactory; | ||
import org.eclipse.edc.connector.controlplane.catalog.spi.CatalogError; | ||
import org.eclipse.edc.transform.spi.TransformerContext; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; | ||
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_TYPE_CATALOG_ERROR; | ||
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_CODE; | ||
import static org.eclipse.edc.protocol.dsp.spi.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_REASON; | ||
import static org.mockito.Mockito.mock; | ||
|
||
class JsonObjectFromCatalogErrorTransformerTest { | ||
|
||
private final JsonBuilderFactory jsonFactory = Json.createBuilderFactory(Map.of()); | ||
private final TransformerContext context = mock(TransformerContext.class); | ||
|
||
private JsonObjectFromCatalogErrorTransformer transformer; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
transformer = new JsonObjectFromCatalogErrorTransformer(jsonFactory); | ||
} | ||
|
||
@Test | ||
void transform_returnJsonObject() { | ||
|
||
var error = CatalogError.Builder.newInstance() | ||
.code("code") | ||
.messages(List.of("message")) | ||
.build(); | ||
|
||
var result = transformer.transform(error, context); | ||
|
||
assertThat(result).isNotNull(); | ||
assertThat(result.getJsonString(TYPE).getString()).isEqualTo(DSPACE_TYPE_CATALOG_ERROR); | ||
assertThat(result.getString(DSPACE_PROPERTY_CODE)).isEqualTo("code"); | ||
assertThat(result.getJsonArray(DSPACE_PROPERTY_REASON)).contains(Json.createValue("message")); | ||
} | ||
} |
Oops, something went wrong.