Skip to content

Commit 9bad3b0

Browse files
authored
Adds Tests to check error-management of Endpoints of the AAS Repository (#759)
* Adds Tests to check error-management of Endpoints * Adds license header & changes comments
1 parent 194c57e commit 9bad3b0

2 files changed

Lines changed: 189 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*******************************************************************************
2+
* Copyright (C) 2025 the Eclipse BaSyx Authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*
23+
* SPDX-License-Identifier: MIT
24+
******************************************************************************/
25+
26+
27+
package org.eclipse.digitaltwin.basyx.aasrepository.http;
28+
29+
import static org.junit.Assert.assertEquals;
30+
31+
import java.io.IOException;
32+
33+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
34+
import org.eclipse.digitaltwin.basyx.http.Base64UrlEncodedIdentifier;
35+
import org.eclipse.digitaltwin.basyx.http.serialization.BaSyxHttpTestUtils;
36+
import org.junit.After;
37+
import org.junit.Before;
38+
import org.junit.Test;
39+
import org.springframework.http.HttpStatus;
40+
41+
/**
42+
* Tests false usage of the AAS Repository HTTP API
43+
*
44+
* @author fried
45+
*/
46+
public abstract class AasRepositoryHTTPErrorSuite {
47+
48+
private static final String NON_EXISTENT_ID = "nonExisting";
49+
50+
protected abstract String getURL();
51+
52+
@Before
53+
@After
54+
public abstract void resetRepository();
55+
56+
@Test
57+
public void getNonExistentAas_returns404() throws IOException {
58+
String url = getSpecificAasAccessURL(NON_EXISTENT_ID);
59+
60+
CloseableHttpResponse response = BaSyxHttpTestUtils.executeGetOnURL(url);
61+
62+
assertEquals(HttpStatus.NOT_FOUND.value(), response.getCode());
63+
}
64+
65+
@Test
66+
public void deleteNonExistentAas_returns404() throws IOException {
67+
String url = getSpecificAasAccessURL(NON_EXISTENT_ID);
68+
69+
CloseableHttpResponse response = BaSyxHttpTestUtils.executeDeleteOnURL(url);
70+
71+
assertEquals(HttpStatus.NOT_FOUND.value(), response.getCode());
72+
}
73+
74+
@Test
75+
public void postInvalidJson_returns400() throws IOException {
76+
String malformedJson = "{ \"idShort\": \"invalid\" "; // kein Abschluss
77+
78+
CloseableHttpResponse response = BaSyxHttpTestUtils.executePostOnURL(getURL(), malformedJson);
79+
80+
assertEquals(HttpStatus.BAD_REQUEST.value(), response.getCode());
81+
}
82+
83+
@Test
84+
public void putAssetInformationOnNonExistentAas_returns404() throws IOException {
85+
String json = BaSyxHttpTestUtils.readJSONStringFromClasspath("assetInfoSimple.json");
86+
87+
String url = getSpecificAasAccessURL(NON_EXISTENT_ID) + "/asset-information";
88+
89+
CloseableHttpResponse response = BaSyxHttpTestUtils.executePutOnURL(url, json);
90+
91+
assertEquals(HttpStatus.NOT_FOUND.value(), response.getCode());
92+
}
93+
94+
@Test
95+
public void addDuplicateSubmodelReference_returns409() throws IOException {
96+
// Vorbereitung
97+
BaSyxHttpTestUtils.executePostOnURL(getURL(), BaSyxHttpTestUtils.readJSONStringFromClasspath("AasSimple_1.json"));
98+
String ref = BaSyxHttpTestUtils.readJSONStringFromClasspath("SingleSubmodelReference_1.json");
99+
100+
String submodelRefUrl = getSpecificAasAccessURL("customIdentifier") + "/submodel-refs";
101+
102+
BaSyxHttpTestUtils.executePostOnURL(submodelRefUrl, ref); // erster Versuch erfolgreich
103+
CloseableHttpResponse second = BaSyxHttpTestUtils.executePostOnURL(submodelRefUrl, ref); // zweiter Versuch
104+
105+
assertEquals(HttpStatus.CONFLICT.value(), second.getCode());
106+
}
107+
108+
@Test
109+
public void getWithInvalidCursor_returns400() throws IOException {
110+
// Vorbereitung
111+
BaSyxHttpTestUtils.executePostOnURL(getURL(), BaSyxHttpTestUtils.readJSONStringFromClasspath("AasSimple_1.json"));
112+
113+
String url = getURL() + "?cursor=x";
114+
CloseableHttpResponse response = BaSyxHttpTestUtils.executeGetOnURL(url);
115+
assertEquals(HttpStatus.BAD_REQUEST.value(), response.getCode());
116+
}
117+
118+
private String getSpecificAasAccessURL(String aasId) {
119+
return getURL() + "/" + Base64UrlEncodedIdentifier.encodeIdentifier(aasId);
120+
}
121+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*******************************************************************************
2+
* Copyright (C) 2025 the Eclipse BaSyx Authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*
23+
* SPDX-License-Identifier: MIT
24+
******************************************************************************/
25+
26+
package org.eclipse.digitaltwin.basyx.aasrepository.http;
27+
28+
import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository;
29+
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
30+
import org.junit.AfterClass;
31+
import org.junit.BeforeClass;
32+
import org.springframework.boot.builder.SpringApplicationBuilder;
33+
import org.springframework.context.ConfigurableApplicationContext;
34+
35+
/**
36+
* Integration-test for false AAS API usage
37+
*
38+
* @author fried
39+
*/
40+
public class TestAasRepositoryHTTPError extends AasRepositoryHTTPErrorSuite {
41+
42+
private static ConfigurableApplicationContext appContext;
43+
44+
@BeforeClass
45+
public static void startAasRepo() {
46+
appContext = new SpringApplicationBuilder(DummyAasRepositoryComponent.class).profiles("httptests").run();
47+
}
48+
49+
@AfterClass
50+
public static void stopAasRepo() {
51+
appContext.close();
52+
}
53+
54+
@Override
55+
protected String getURL() {
56+
return "http://localhost:8080/shells";
57+
}
58+
59+
@Override
60+
public void resetRepository() {
61+
AasRepository repo = appContext.getBean(AasRepository.class);
62+
repo.getAllAas(PaginationInfo.NO_LIMIT)
63+
.getResult()
64+
.stream()
65+
.map(aas -> aas.getId())
66+
.forEach(repo::deleteAas);
67+
}
68+
}

0 commit comments

Comments
 (0)