Skip to content

Commit

Permalink
feature: eclipse-tractusx/traceability-foss#1190 add getContractDefin…
Browse files Browse the repository at this point in the history
…ition and deleteContractDefinition functionality
  • Loading branch information
ds-lcapellino committed Jul 16, 2024
1 parent 2f8ac9b commit 79bdd32
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/********************************************************************************
* Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* 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
********************************************************************************/
package org.eclipse.tractusx.irs.edc.client.contract.model;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;


/**
* EdcContractDefinitionQuerySpec used for requesting contract definitions
*/
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder

Check notice

Code scanning / CodeQL

Use of default toString() Note

Default toString(): EdcContext inherits toString() from Object, and so is not suitable for printing.
public class EdcContractDefinitionQuerySpec {
@JsonProperty("@context")
private EdcContext edcContext;

@JsonProperty("filterExpression")
private List<FilterExpression> filterExpression;


/**
* EdcContext used for EdcContractDefinitionQuerySpec
*/
@Getter
@Setter
@NoArgsConstructor
public static class EdcContext {
@JsonProperty("@vocab")
private String vocab = "https://w3id.org/edc/v0.0.1/ns/";
}

/**
* FilterExpression used for EdcContractDefinitionQuerySpec
*/
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public static class FilterExpression {
@JsonProperty("operandLeft")
private String operandLeft;
@JsonProperty("operator")
private String operator;
@JsonProperty("operandRight")
private Object operandRight;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.edc.spi.query.QuerySpec;
import org.eclipse.tractusx.irs.edc.client.EdcConfiguration;
import org.eclipse.tractusx.irs.edc.client.asset.model.EdcContext;
import org.eclipse.tractusx.irs.edc.client.contract.model.EdcContractDefinition;
import org.eclipse.tractusx.irs.edc.client.contract.model.EdcContractDefinitionCriteria;
import org.eclipse.tractusx.irs.edc.client.contract.model.EdcContractDefinitionQuerySpec;
import org.eclipse.tractusx.irs.edc.client.contract.model.exception.CreateEdcContractDefinitionException;
import org.eclipse.tractusx.irs.edc.client.contract.model.exception.EdcContractDefinitionAlreadyExists;
import org.springframework.core.ParameterizedTypeReference;
Expand Down Expand Up @@ -113,9 +113,9 @@ public EdcContractDefinition createContractDefinitionRequest(final String assetI
.build();
}

public ResponseEntity<List<EdcContractDefinition>> getContractDefinitions(final QuerySpec querySpec) {
public ResponseEntity<List<EdcContractDefinition>> getContractDefinitions(final EdcContractDefinitionQuerySpec edcContractDefinitionQuerySpec) {
return restTemplate.exchange(config.getControlplane().getEndpoint().getContractDefinition() + "/request",
HttpMethod.POST, new HttpEntity<>(querySpec), new ParameterizedTypeReference<>() {
HttpMethod.POST, new HttpEntity<>(edcContractDefinitionQuerySpec), new ParameterizedTypeReference<>() {
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import java.util.List;
import java.util.Optional;

import org.eclipse.edc.spi.query.QuerySpec;
import org.eclipse.tractusx.irs.edc.client.EdcConfiguration;
import org.eclipse.tractusx.irs.edc.client.contract.model.EdcContractDefinition;
import org.eclipse.tractusx.irs.edc.client.contract.model.EdcContractDefinitionQuerySpec;
import org.eclipse.tractusx.irs.edc.client.contract.model.exception.CreateEdcContractDefinitionException;
import org.eclipse.tractusx.irs.edc.client.contract.model.exception.EdcContractDefinitionAlreadyExists;
import org.json.JSONException;
Expand Down Expand Up @@ -177,7 +177,7 @@ void givenGetContractDefinitions_thenReturnContractDefinitions() {

//when
final ResponseEntity<List<EdcContractDefinition>> contractDefinitions = service.getContractDefinitions(
new QuerySpec());
EdcContractDefinitionQuerySpec.builder().build());
//then
assertThat(contractDefinitions.getBody()).isNotEmpty();

Expand Down Expand Up @@ -226,4 +226,28 @@ void testCreateContractDefinitionResponse() throws IOException, JSONException {
//THEN
JSONAssert.assertEquals(jsonResponse, objectMapper.writeValueAsString(responseObj), false);
}

@Test
void testEdcContractDefinitionQuerySpec() throws IOException, JSONException {
//GIVEN
String jsonResponse = """
{
"@context": {
"@vocab": "https://w3id.org/edc/v0.0.1/ns/"
},
"filterExpression": [
{
"operandLeft": "assetsSelector.operandRight",
"operator": "in",
"operandRight":["3eeea531-3772-4f69-9ef6-c680ed9fd09d", "d333dc13-26f2-457a-bbe0-d3d421ebe91d", "07d35ae6-41b1-4de5-ae12-2da89b9dcade", "6a46922a-8f71-4c54-b5ac-e50c631841e9"]
}
]
}
""";
//WHEN
EdcContractDefinitionQuerySpec edcContractDefinitionQuerySpec = objectMapper.readValue(jsonResponse, EdcContractDefinitionQuerySpec.class);

//THEN
JSONAssert.assertEquals(jsonResponse, objectMapper.writeValueAsString(edcContractDefinitionQuerySpec), false);
}
}

0 comments on commit 79bdd32

Please sign in to comment.