|
| 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 | +} |
0 commit comments