Skip to content

Commit

Permalink
fixed getList implementation to handle page parsing (#20)
Browse files Browse the repository at this point in the history
* fixed getList implementation to handle page parsing
* Deleted TestServer.java because there is no need for integration tests right now.
* Removed fast service starter dependency and group id because it is not needed
* Added note to changelog
* Change BaseInterface#getList to BaseInterface#getList
* remove groupId from checks module
* fix description interface

---------

Co-authored-by: Michael Jacoby <[email protected]>
  • Loading branch information
GW1708 and mjacoby authored Jan 16, 2025
1 parent 806a7eb commit 84986e2
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 92 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,9 @@ to instantiate an interface for connection with that server.
<!--start:changelog-header-->
### 1.1.0-SNAPSHOT (current development version)<!--end:changelog-header-->

- Serialization
- De-/Serialization
- Fixed serialization of submodel ids in deleteSubmodel and deleteSubmodelReference in AASInterface
- Fixed an error that occurred when trying to retrieve lists from a server.

### 1.0.0

Expand Down Expand Up @@ -282,4 +283,4 @@ Distributed under the Apache 2.0 License. See `LICENSE` for more information.

Copyright (C) 2024 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131 Karlsruhe, Germany.

You should have received a copy of the Apache 2.0 License along with this program. If not, see https://www.apache.org/licenses/LICENSE-2.0.html.
You should have received a copy of the Apache 2.0 License along with this program. If not, see https://www.apache.org/licenses/LICENSE-2.0.html.
1 change: 0 additions & 1 deletion checks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<version>1.1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>de.fraunhofer.iosb.ilt.faaast.client</groupId>
<artifactId>checks</artifactId>
<name>checks</name>
<description>Checkstyle Checks for the FA³ST Client.</description>
Expand Down
1 change: 0 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<version>1.1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>de.fraunhofer.iosb.ilt.faaast.client</groupId>
<artifactId>core</artifactId>
<name>core</name>
<description>Main functionality of the FA³ST Client.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void deleteThumbnail() throws StatusCodeException, ConnectivityException
* @throws ConnectivityException if the connection to the server cannot be established
*/
public List<Reference> getAllSubmodelReferences() throws StatusCodeException, ConnectivityException {
return getList(submodelRefPath(), Reference.class);
return getAll(submodelRefPath(), Reference.class);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public AASRegistryInterface(URI endpoint, HttpClient httpClient) {
* @throws ConnectivityException if the connection to the server cannot be established
*/
public List<DefaultAssetAdministrationShellDescriptor> getAll() throws StatusCodeException, ConnectivityException {
return getList(DefaultAssetAdministrationShellDescriptor.class);
return getAll(DefaultAssetAdministrationShellDescriptor.class);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public List<AssetAdministrationShell> getAll() throws StatusCodeException, Conne
* @throws ConnectivityException if the connection to the server cannot be established
*/
public List<AssetAdministrationShell> getAll(AASSearchCriteria aasSearchCriteria) throws StatusCodeException, ConnectivityException {
return getList(null, aasSearchCriteria, Content.DEFAULT, QueryModifier.DEFAULT, AssetAdministrationShell.class);
return getAll(null, aasSearchCriteria, Content.DEFAULT, QueryModifier.DEFAULT, AssetAdministrationShell.class);
}


Expand Down Expand Up @@ -225,7 +225,7 @@ public List<Reference> getAllAsReference() throws StatusCodeException, Connectiv
* @throws ConnectivityException if the connection to the server cannot be established
*/
public List<Reference> getAllAsReference(AASSearchCriteria aasSearchCriteria) throws StatusCodeException, ConnectivityException {
return getList(null, aasSearchCriteria, Content.REFERENCE, QueryModifier.DEFAULT, Reference.class);
return getAll(null, aasSearchCriteria, Content.REFERENCE, QueryModifier.DEFAULT, Reference.class);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public abstract class BaseInterface {
HttpStatus.INTERNAL_SERVER_ERROR);

protected final HttpClient httpClient;
private final URI endpoint;
protected final URI endpoint;

/**
* Creates a new instance.
Expand Down Expand Up @@ -288,8 +288,8 @@ protected <T extends ElementValue> T getValue(String path, QueryModifier modifie
* @throws StatusCodeException if HTTP request returns invalid statsu code
* @throws InvalidPayloadException if deserializing the payload fails
*/
protected <T> List<T> getList(Class<T> responseType) throws ConnectivityException, StatusCodeException {
return getList(null, QueryModifier.DEFAULT, responseType);
protected <T> List<T> getAll(Class<T> responseType) throws ConnectivityException, StatusCodeException {
return getAll(null, QueryModifier.DEFAULT, responseType);
}


Expand All @@ -304,8 +304,8 @@ protected <T> List<T> getList(Class<T> responseType) throws ConnectivityExceptio
* @throws StatusCodeException if HTTP request returns invalid statsu code
* @throws InvalidPayloadException if deserializing the payload fails
*/
protected <T> List<T> getList(String path, Class<T> responseType) throws ConnectivityException, StatusCodeException {
return getList(path, QueryModifier.DEFAULT, responseType);
protected <T> List<T> getAll(String path, Class<T> responseType) throws ConnectivityException, StatusCodeException {
return getAll(path, QueryModifier.DEFAULT, responseType);
}


Expand All @@ -321,8 +321,8 @@ protected <T> List<T> getList(String path, Class<T> responseType) throws Connect
* @throws StatusCodeException if HTTP request returns invalid statsu code
* @throws InvalidPayloadException if deserializing the payload fails
*/
protected <T> List<T> getList(String path, QueryModifier modifier, Class<T> responseType) throws ConnectivityException, StatusCodeException {
return getList(path, SearchCriteria.DEFAULT, Content.DEFAULT, modifier, responseType);
protected <T> List<T> getAll(String path, QueryModifier modifier, Class<T> responseType) throws ConnectivityException, StatusCodeException {
return getAll(path, SearchCriteria.DEFAULT, Content.DEFAULT, modifier, responseType);
}


Expand All @@ -339,8 +339,8 @@ protected <T> List<T> getList(String path, QueryModifier modifier, Class<T> resp
* @throws StatusCodeException if HTTP request returns invalid statsu code
* @throws InvalidPayloadException if deserializing the payload fails
*/
protected <T> List<T> getList(SearchCriteria searchCriteria, Content content, QueryModifier modifier, Class<T> responseType) throws ConnectivityException, StatusCodeException {
return getList(null, searchCriteria, content, modifier, responseType);
protected <T> List<T> getAll(SearchCriteria searchCriteria, Content content, QueryModifier modifier, Class<T> responseType) throws ConnectivityException, StatusCodeException {
return getAll(null, searchCriteria, content, modifier, responseType);
}


Expand All @@ -358,15 +358,15 @@ protected <T> List<T> getList(SearchCriteria searchCriteria, Content content, Qu
* @throws StatusCodeException if HTTP request returns invalid statsu code
* @throws InvalidPayloadException if deserializing the payload fails
*/
protected <T> List<T> getList(String path, SearchCriteria searchCriteria, Content content, QueryModifier modifier, Class<T> responseType)
protected <T> List<T> getAll(String path, SearchCriteria searchCriteria, Content content, QueryModifier modifier, Class<T> responseType)
throws ConnectivityException, StatusCodeException {
HttpRequest request = HttpHelper.createGetRequest(resolve(QueryHelper.apply(path, content, modifier, PagingInfo.ALL, searchCriteria)));
HttpResponse<String> response = HttpHelper.send(httpClient, request);
validateStatusCode(HttpMethod.GET, response, HttpStatus.OK);
try {
return new JsonApiDeserializer().readList(response.body(), responseType);
return deserializePage(response.body(), responseType).getContent();
}
catch (DeserializationException e) {
catch (DeserializationException | JSONException e) {
throw new InvalidPayloadException(e);
}
}
Expand Down Expand Up @@ -848,7 +848,15 @@ private static URI sanitizeEndpoint(URI endpoint) {
}


private static <T> T parseBody(HttpResponse<String> response, Class<T> responseType) {
/**
* Parses body of HTTP response.
*
* @param <T> result type
* @param response the response
* @param responseType the type of the payload to parse
* @return parsed body of response
*/
protected static <T> T parseBody(HttpResponse<String> response, Class<T> responseType) {
try {
return new JsonApiDeserializer().read(response.body(), responseType);
}
Expand Down Expand Up @@ -892,7 +900,15 @@ private static <T> Page<T> deserializePage(String responseBody, Class<T> respons
}


private static void validateStatusCode(HttpMethod method, HttpResponse<String> response, HttpStatus expected) throws StatusCodeException {
/**
* Checks if a given response matches the expected HTTP status code.
*
* @param method the HTTP method
* @param response the response to check
* @param expected the expected HTTP status code
* @throws StatusCodeException if the HTTP status code of the response is invlid/not supported
*/
protected static void validateStatusCode(HttpMethod method, HttpResponse<String> response, HttpStatus expected) throws StatusCodeException {
if (Objects.isNull(response)) {
throw new IllegalArgumentException("response must be non-null");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ConceptDescriptionRepositoryInterface extends BaseInterface {
*
* @param endpoint Uri used to communicate with the FA³ST service
*/
protected ConceptDescriptionRepositoryInterface(URI endpoint) {
public ConceptDescriptionRepositoryInterface(URI endpoint) {
super(resolve(endpoint, API_PATH));
}

Expand All @@ -56,7 +56,7 @@ protected ConceptDescriptionRepositoryInterface(URI endpoint) {
* @param user String to enable basic authentication
* @param password String to enable basic authentication
*/
protected ConceptDescriptionRepositoryInterface(URI endpoint, String user, String password) {
public ConceptDescriptionRepositoryInterface(URI endpoint, String user, String password) {
super(resolve(endpoint, API_PATH), user, password);
}

Expand All @@ -67,7 +67,7 @@ protected ConceptDescriptionRepositoryInterface(URI endpoint, String user, Strin
* @param endpoint the endpoint
* @param httpClient allows user to specify custom http-client
*/
protected ConceptDescriptionRepositoryInterface(URI endpoint, HttpClient httpClient) {
public ConceptDescriptionRepositoryInterface(URI endpoint, HttpClient httpClient) {
super(resolve(endpoint, API_PATH), httpClient);
}

Expand Down Expand Up @@ -111,7 +111,7 @@ public List<ConceptDescription> getAll() throws StatusCodeException, Connectivit
* @throws ConnectivityException if the connection to the server cannot be established
*/
public List<ConceptDescription> getAll(ConceptDescriptionSearchCriteria conceptDescriptionSearchCriteria) throws StatusCodeException, ConnectivityException {
return getList(conceptDescriptionSearchCriteria, Content.DEFAULT, QueryModifier.DEFAULT, ConceptDescription.class);
return getAll(conceptDescriptionSearchCriteria, Content.DEFAULT, QueryModifier.DEFAULT, ConceptDescription.class);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@

import de.fraunhofer.iosb.ilt.faaast.client.exception.ConnectivityException;
import de.fraunhofer.iosb.ilt.faaast.client.exception.StatusCodeException;
import de.fraunhofer.iosb.ilt.faaast.client.http.HttpMethod;
import de.fraunhofer.iosb.ilt.faaast.client.http.HttpStatus;
import de.fraunhofer.iosb.ilt.faaast.client.util.HttpHelper;
import de.fraunhofer.iosb.ilt.faaast.service.model.ServiceDescription;

import java.net.URI;
import java.net.http.HttpClient;
import java.util.List;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;


/**
Expand Down Expand Up @@ -83,8 +88,11 @@ public DescriptionInterface(URI endpoint, HttpClient httpClient) {
* </div>
* @throws ConnectivityException if the connection to the server cannot be established
*/
public List<String> get() throws StatusCodeException, ConnectivityException {
return getList(String.class);
public ServiceDescription get() throws StatusCodeException, ConnectivityException {
HttpRequest request = HttpHelper.createGetRequest(endpoint);
HttpResponse<String> response = HttpHelper.send(httpClient, request);
validateStatusCode(HttpMethod.GET, response, HttpStatus.OK);
return parseBody(response, ServiceDescription.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public String getPath(QueryModifier modifier) throws StatusCodeException, Connec
* @throws ConnectivityException if the connection to the server cannot be established
*/
public List<SubmodelElement> getAllElements() throws StatusCodeException, ConnectivityException {
return getList(submodelElementsPath(), SubmodelElement.class);
return getAll(submodelElementsPath(), SubmodelElement.class);
}


Expand All @@ -336,7 +336,7 @@ public List<SubmodelElement> getAllElements() throws StatusCodeException, Connec
* @throws ConnectivityException if the connection to the server cannot be established
*/
public List<SubmodelElement> getAllElements(QueryModifier modifier) throws StatusCodeException, ConnectivityException {
return getList(submodelElementsPath(), modifier, SubmodelElement.class);
return getAll(submodelElementsPath(), modifier, SubmodelElement.class);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public SubmodelRegistryInterface(URI endpoint, HttpClient httpClient) {
* @throws ConnectivityException if the connection to the server cannot be established
*/
public List<DefaultSubmodelDescriptor> getAll() throws StatusCodeException, ConnectivityException {
return getList(DefaultSubmodelDescriptor.class);
return getAll(DefaultSubmodelDescriptor.class);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public List<Submodel> getAll(SubmodelSearchCriteria submodelSearchCriteria) thro
* @throws ConnectivityException if the connection to the server cannot be established
*/
public List<Submodel> getAll(QueryModifier modifier, SubmodelSearchCriteria submodelSearchCriteria) throws StatusCodeException, ConnectivityException {
return getList(submodelSearchCriteria, Content.DEFAULT, modifier, Submodel.class);
return getAll(submodelSearchCriteria, Content.DEFAULT, modifier, Submodel.class);
}


Expand Down Expand Up @@ -272,7 +272,7 @@ public Page<Submodel> get(PagingInfo pagingInfo, QueryModifier modifier, Submode
* @throws ConnectivityException if the connection to the server cannot be established
*/
public List<Submodel> getAllMetadata(QueryModifier modifier, SubmodelSearchCriteria submodelSearchCriteria) throws StatusCodeException, ConnectivityException {
return getList(submodelSearchCriteria, Content.METADATA, modifier, Submodel.class);
return getAll(submodelSearchCriteria, Content.METADATA, modifier, Submodel.class);
}


Expand Down Expand Up @@ -320,7 +320,7 @@ public Page<Submodel> getMetadata(PagingInfo pagingInfo, QueryModifier modifier,
* @throws ConnectivityException if the connection to the server cannot be established
*/
public List<Submodel> getAllValues(QueryModifier modifier, SubmodelSearchCriteria submodelSearchCriteria) throws StatusCodeException, ConnectivityException {
return getList(submodelSearchCriteria, Content.VALUE, modifier, Submodel.class);
return getAll(submodelSearchCriteria, Content.VALUE, modifier, Submodel.class);
}


Expand Down Expand Up @@ -366,7 +366,7 @@ public Page<Submodel> getValue(PagingInfo pagingInfo, QueryModifier modifier, Su
* @throws ConnectivityException if the connection to the server cannot be established
*/
public List<Reference> getAllReferences(SubmodelSearchCriteria submodelSearchCriteria) throws StatusCodeException, ConnectivityException {
return getList(submodelSearchCriteria, Content.REFERENCE, QueryModifier.MINIMAL, Reference.class);
return getAll(submodelSearchCriteria, Content.REFERENCE, QueryModifier.MINIMAL, Reference.class);
}


Expand Down Expand Up @@ -412,7 +412,7 @@ public Page<Reference> getReference(PagingInfo pagingInfo, SubmodelSearchCriteri
* @throws ConnectivityException if the connection to the server cannot be established
*/
public List<Reference> getAllPaths(QueryModifier modifier, SubmodelSearchCriteria submodelSearchCriteria) throws StatusCodeException, ConnectivityException {
return getList(submodelSearchCriteria, Content.PATH, modifier, Reference.class);
return getAll(submodelSearchCriteria, Content.PATH, modifier, Reference.class);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
import de.fraunhofer.iosb.ilt.faaast.service.dataformat.json.JsonApiSerializer;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import de.fraunhofer.iosb.ilt.faaast.service.model.api.paging.Page;
import de.fraunhofer.iosb.ilt.faaast.service.model.api.paging.PagingMetadata;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.UnsupportedModifierException;
import de.fraunhofer.iosb.ilt.faaast.service.util.EncodingHelper;
import okhttp3.mockwebserver.MockResponse;
Expand Down Expand Up @@ -201,9 +202,11 @@ public void testDeleteThumbnail() throws InterruptedException, ClientException {

@Test
public void testGetAllSubmodelReferences() throws SerializationException, InterruptedException, ClientException, UnsupportedModifierException {
List<Reference> requestSubmodelReferenceList = new ArrayList<>();
requestSubmodelReferenceList.add(new DefaultReference());
server.enqueue(new MockResponse().setBody(serializer.write(requestSubmodelReferenceList)));
Page<Reference> requestSubmodelReferencePage = Page.<Reference> builder()
.result(new DefaultReference())
.metadata(new PagingMetadata.Builder().build())
.build();
server.enqueue(new MockResponse().setBody(serializer.write(requestSubmodelReferencePage)));

List<Reference> responseList = aasInterface.getAllSubmodelReferences();
RecordedRequest request = server.takeRequest();
Expand All @@ -212,7 +215,7 @@ public void testGetAllSubmodelReferences() throws SerializationException, Interr
assertEquals(0, request.getBodySize());
assertEquals("/api/v3.0/aas/submodel-refs", request.getPath());

assertEquals(requestSubmodelReferenceList, responseList);
assertEquals(requestSubmodelReferencePage.getContent(), responseList);
}


Expand Down
Loading

0 comments on commit 84986e2

Please sign in to comment.