Skip to content
Open
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
@@ -0,0 +1,106 @@
/********************************************************************************
* Copyright (c) 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.traceability.integration.common.support;

import io.restassured.RestAssured;
import io.restassured.common.mapper.TypeRef;
import io.restassured.http.ContentType;
import lombok.RequiredArgsConstructor;
import org.eclipse.tractusx.traceability.common.security.JwtRole;
import org.jose4j.lang.JoseException;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class DigitalTwinPartApiSupport {

private final OAuth2Support oAuth2Support;
private static final JwtRole ROLE = JwtRole.ADMIN;

/**
* Executes POST request to /api/administration/digitalTwinPart endpoint
*/
public <T> T postDigitalTwinPart(Object requestBody, int expectedStatusCode, TypeRef<T> typeRef) {
try {
return RestAssured
.given()
.header(oAuth2Support.jwtAuthorization(ROLE))
.contentType(ContentType.JSON)
.log().all()
.body(requestBody)
.when()
.post("/api/administration/digitalTwinPart")
.then()
.log().all()
.statusCode(expectedStatusCode)
.extract()
.body()
.as(typeRef);
} catch (JoseException e) {
throw new RuntimeException("Failed to authorize JWT for postDigitalTwinPart", e);
}
}

/**
* Executes POST request to /api/administration/digitalTwinPart/detail endpoint
*/
public <T> T postDigitalTwinPartDetail(Object requestBody, int expectedStatusCode, Class<T> clazz) {
try {
return RestAssured
.given()
.header(oAuth2Support.jwtAuthorization(ROLE))
.contentType(ContentType.JSON)
.log().all()
.body(requestBody)
.when()
.post("/api/administration/digitalTwinPart/detail")
.then()
.log().all()
.statusCode(expectedStatusCode)
.extract()
.as(clazz);
} catch (JoseException e) {
throw new RuntimeException("Failed to authorize JWT for postDigitalTwinPartDetail", e);
}
}

/**
* Executes POST request to /api/administration/digitalTwinPart/searchable-values endpoint
*/
public <T> T postSearchableValues(Object requestBody, int expectedStatusCode, TypeRef<T> typeRef) {
try {
return RestAssured
.given()
.header(oAuth2Support.jwtAuthorization(ROLE))
.contentType(ContentType.JSON)
.log().all()
.body(requestBody)
.when()
.post("/api/administration/digitalTwinPart/searchable-values")
.then()
.log().all()
.statusCode(expectedStatusCode)
.extract()
.body()
.as(typeRef);
} catch (JoseException e) {
throw new RuntimeException("Failed to authorize JWT for postSearchableValues", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
* 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
* 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
Expand All @@ -29,14 +28,14 @@
import digitaltwinpart.DigitalTwinPartResponse;
import digitaltwinpart.SearchableDigitalTwinPartRequest;
import io.restassured.common.mapper.TypeRef;
import io.restassured.http.ContentType;
import org.eclipse.tractusx.traceability.common.model.PageResult;
import org.eclipse.tractusx.traceability.common.model.SearchCriteriaStrategy;
import org.eclipse.tractusx.traceability.configuration.infrastructure.model.TriggerConfigurationEntity;
import org.eclipse.tractusx.traceability.integration.IntegrationTestSpecification;
import org.eclipse.tractusx.traceability.integration.common.support.AASDatabaseSupport;
import org.eclipse.tractusx.traceability.integration.common.support.AssetsSupport;
import org.eclipse.tractusx.traceability.integration.common.support.ConfigurationSupport;
import org.eclipse.tractusx.traceability.integration.common.support.DigitalTwinPartApiSupport;
import org.eclipse.tractusx.traceability.integration.common.support.EdcSupport;
import org.eclipse.tractusx.traceability.integration.common.support.IrsApiSupport;
import org.jose4j.lang.JoseException;
Expand All @@ -46,9 +45,7 @@

import java.util.List;

import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.tractusx.traceability.common.security.JwtRole.ADMIN;

class DigitalTwinPartsControllerIT extends IntegrationTestSpecification {

Expand All @@ -60,9 +57,10 @@ class DigitalTwinPartsControllerIT extends IntegrationTestSpecification {
IrsApiSupport irsApiSupport;
@Autowired
AASDatabaseSupport aasDatabaseSupport;

@Autowired
ConfigurationSupport configurationSupport;
@Autowired
DigitalTwinPartApiSupport digitalTwinPartApiSupport;

@BeforeEach
void setUp() throws JsonProcessingException {
Expand All @@ -71,8 +69,8 @@ void setUp() throws JsonProcessingException {
}

@Test
void shouldReturnDigitalTwinPartSorted() throws JoseException {
//GIVEN
void shouldReturnDigitalTwinPartSorted() {
// GIVEN
assetsSupport.defaultAssetsStored();
String aasId = "ABC_1";
String aasId2 = "ABC_2";
Expand All @@ -86,27 +84,18 @@ void shouldReturnDigitalTwinPartSorted() throws JoseException {
.sort(List.of("aasId,desc"))
.build();

//WHEN
PageResult<DigitalTwinPartResponse> digitalTwinPartResponsePageResult = given()
.header(oAuth2Support.jwtAuthorization(ADMIN))
.contentType(ContentType.JSON)
.log().all()
.when()
.body(digitalTwinPartRequest)
.post("/api/administration/digitalTwinPart")
.then()
.log().all()
.statusCode(200)
.extract().body().as(new TypeRef<>() {
});
//THEN
// WHEN
PageResult<DigitalTwinPartResponse> digitalTwinPartResponsePageResult =
digitalTwinPartApiSupport.postDigitalTwinPart(digitalTwinPartRequest, 200, new TypeRef<>() {});

// THEN
assertThat(digitalTwinPartResponsePageResult.content()).hasSize(2);
assertThat(digitalTwinPartResponsePageResult.content().get(0).getAasId()).isEqualTo(aasId2);
}

@Test
void shouldReturnDigitalTwinPartFiltered() throws JoseException {
//GIVEN
void shouldReturnDigitalTwinPartFiltered() {
// GIVEN
assetsSupport.defaultAssetsStored();
String aasId = "ABC_1";
String aasId2 = "ABC_2";
Expand All @@ -124,39 +113,32 @@ void shouldReturnDigitalTwinPartFiltered() throws JoseException {
DigitalTwinPartFilter digitalTwinPartFilter = DigitalTwinPartFilter.builder()
.aasId(filterAttribute)
.build();

DigitalTwinPartRequest digitalTwinPartRequest = DigitalTwinPartRequest.builder()
.filters(List.of(digitalTwinPartFilter))
.page(0)
.size(10)
.sort(List.of("aasId,desc"))
.build();

//WHEN
PageResult<DigitalTwinPartResponse> digitalTwinPartResponsePageResult = given()
.header(oAuth2Support.jwtAuthorization(ADMIN))
.contentType(ContentType.JSON)
.log().all()
.when()
.body(digitalTwinPartRequest)
.post("/api/administration/digitalTwinPart")
.then()
.log().all()
.statusCode(200)
.extract().body().as(new TypeRef<>() {
});
//THEN
// WHEN
PageResult<DigitalTwinPartResponse> digitalTwinPartResponsePageResult =
digitalTwinPartApiSupport.postDigitalTwinPart(digitalTwinPartRequest, 200, new TypeRef<>() {});

// THEN
assertThat(digitalTwinPartResponsePageResult.content()).isNotEmpty();
assertThat(digitalTwinPartResponsePageResult.content().get(0).getAasId()).isEqualTo(aasId);
}

@Test
void shouldReturnDigitalTwinPartDetails() throws JoseException {
//GIVEN
void shouldReturnDigitalTwinPartDetails() {
// GIVEN
assetsSupport.defaultAssetsStored();
String aasId = "ABC_1";
String aasId2 = "ABC_2";
aasDatabaseSupport.createAASEntityByAASId(aasId);
aasDatabaseSupport.createAASEntityByAASId(aasId2);

TriggerConfigurationEntity triggerConfigurationEntity = TriggerConfigurationEntity.builder()
.cronExpressionRegisterOrderTTLReached("* * * * * *")
.cronExpressionMapCompletedOrders("* * * * * *")
Expand All @@ -169,53 +151,33 @@ void shouldReturnDigitalTwinPartDetails() throws JoseException {
.aasId(aasId)
.build();

//WHEN
DigitalTwinPartDetailResponse response = given()
.header(oAuth2Support.jwtAuthorization(ADMIN))
.contentType(ContentType.JSON)
.log().all()
.body(digitalTwinPartDetailRequest)
.when()
.post("/api/administration/digitalTwinPart/detail")
.then()
.log().all()
.statusCode(200)
.extract().as(DigitalTwinPartDetailResponse.class);
//THEN
// WHEN
DigitalTwinPartDetailResponse response =
digitalTwinPartApiSupport.postDigitalTwinPartDetail(digitalTwinPartDetailRequest, 200, DigitalTwinPartDetailResponse.class);

// THEN
assertThat(response.getAasId()).isEqualTo(aasId);
assertThat(response.getAasTTL()).isEqualTo(1000);
assertThat(response.getActor()).isEqualTo(ActorResponse.SYSTEM);
assertThat(response.getBpn()).isEqualTo("BPNL00000001TEST");
assertThat(response.getDigitalTwinType()).isEqualTo("PART_TYPE");

}

@Test
void shouldReturnSearchableValuesForFieldName() throws JoseException {
void shouldReturnSearchableValuesForFieldName() {
// GIVEN
assetsSupport.defaultAssetsStored();
String aasId = "ABC_FILTER_TEST";
aasDatabaseSupport.createAASEntityByAASId(aasId);

var request = new SearchableDigitalTwinPartRequest();
SearchableDigitalTwinPartRequest request = new SearchableDigitalTwinPartRequest();
request.setFieldName("aasId");
request.setStartWith("ABC");
request.setSize(10);

// WHEN
List<String> results = given()
.header(oAuth2Support.jwtAuthorization(ADMIN))
.contentType(ContentType.JSON)
.log().all()
.body(request)
.when()
.post("/api/administration/digitalTwinPart/searchable-values")
.then()
.log().all()
.statusCode(200)
.extract()
.body()
.as(new TypeRef<>() {});
List<String> results =
digitalTwinPartApiSupport.postSearchableValues(request, 200, new TypeRef<>() {});

// THEN
assertThat(results)
Expand Down