Skip to content

Commit

Permalink
Refactoring of the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emildinchev committed Feb 2, 2024
1 parent c55601f commit fa2f191
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,19 @@ public <T> T read(String value, Class<T> valueType) throws DeserializationExcept
}

/**
* Generic method to deserialize a given JSON node into instance of an AAS type
* Generic method to deserialize a given string into a list of AAS instances
*
* @param node the node to parse
* @param valueType the class type of the AAS instance. Not null.
* @param value a string representation of the AAS instances list
* @param valueType the class type of the instance. Not null.
* @param <T> the AAS type
* @return an AAS instance
*
* @return a list of AAS instances
* @throws DeserializationException if deserialization fails
*/
public <T> T read(JsonNode node, Class<T> valueType) throws DeserializationException {
public <T> List<T> readList(String value, Class<T> valueType) throws DeserializationException {
try {
return mapper.treeToValue(node, valueType);
return mapper.readValue(value, mapper.getTypeFactory().constructCollectionLikeType(List.class, valueType));
} catch (JsonProcessingException ex) {
throw new DeserializationException("error deserializing " + valueType.getSimpleName(), ex);
throw new DeserializationException("error deserializing list of " + valueType.getSimpleName(), ex);
}
}

Expand Down Expand Up @@ -128,67 +127,68 @@ public <T> T read(InputStream stream, Charset charset, Class<T> valueType) throw
}

/**
* Generic method to deserialize a given string into a list of AAS instances
* Deserializes a given input stream into a list of AAS instances using the default UTF-8 charset
*
* @param value a string representation of the AAS instances list
* @param valueType the class type of the instance. Not null.
* @param stream An InputStream containing the string representation of the AAS instances list
* @param valueType the class type of the AAS instance. Not null.
* @param <T> the AAS type
* @return a list of AAS instances
* @throws DeserializationException if deserialization fails
*/
public <T> List<T> readList(String value, Class<T> valueType) throws DeserializationException {
try {
return mapper.readValue(value, mapper.getTypeFactory().constructCollectionLikeType(List.class, valueType));
} catch (JsonProcessingException ex) {
throw new DeserializationException("error deserializing list of " + valueType.getSimpleName(), ex);
}
public <T> List<T> readList(InputStream stream, Class<T> valueType) throws DeserializationException {
return readList(stream, StandardCharsets.UTF_8, valueType);
}

/**
* Deserializes a given JsonArray into a list of AAS instances
* Deserializes a given input stream into a list of AAS instances
*
* @param node a JsonArray representing the AAS instances list
* @param valueType the class type of the instance. Not null.
* @param stream An InputStream containing the string representation of the AAS instances list
* @param charset the charset to use for deserialization
* @param valueType the class type of the AAS instance. Not null.
* @param <T> the AAS type
* @return a list of AAS instances
* @throws DeserializationException if deserialization fails
*/
public <T> List<T> readList(JsonNode node, Class<T> valueType) throws DeserializationException {
public <T> List<T> readList(InputStream stream, Charset charset, Class<T> valueType) throws DeserializationException {
try {
return mapper.treeToValue(node, mapper.getTypeFactory().constructCollectionLikeType(List.class, valueType));
} catch (JsonProcessingException ex) {
return mapper.readValue(new InputStreamReader(stream, charset),
mapper.getTypeFactory().constructCollectionLikeType(List.class, valueType));
} catch (Exception ex) {
throw new DeserializationException("error deserializing list of " + valueType.getSimpleName(), ex);
}
}

/**
* Deserializes a given input stream into a list of AAS instances using the default UTF-8 charset
* Generic method to deserialize a given JSON node into instance of an AAS type
*
* @param stream An InputStream containing the string representation of the AAS instances list
* @param node the node to parse
* @param valueType the class type of the AAS instance. Not null.
* @param <T> the AAS type
* @return a list of AAS instances
* @return an AAS instance
*
* @throws DeserializationException if deserialization fails
*/
public <T> List<T> readList(InputStream stream, Class<T> valueType) throws DeserializationException {
return readList(stream, StandardCharsets.UTF_8, valueType);
public <T> T read(JsonNode node, Class<T> valueType) throws DeserializationException {
try {
return mapper.treeToValue(node, valueType);
} catch (JsonProcessingException ex) {
throw new DeserializationException("error deserializing " + valueType.getSimpleName(), ex);
}
}

/**
* Deserializes a given input stream into a list of AAS instances
* Deserializes a given JsonArray into a list of AAS instances
*
* @param stream An InputStream containing the string representation of the AAS instances list
* @param charset the charset to use for deserialization
* @param valueType the class type of the AAS instance. Not null.
* @param node a JsonArray representing the AAS instances list
* @param valueType the class type of the instance. Not null.
* @param <T> the AAS type
* @return a list of AAS instances
* @throws DeserializationException if deserialization fails
*/
public <T> List<T> readList(InputStream stream, Charset charset, Class<T> valueType) throws DeserializationException {
public <T> List<T> readList(JsonNode node, Class<T> valueType) throws DeserializationException {
try {
return mapper.readValue(new InputStreamReader(stream, charset),
mapper.getTypeFactory().constructCollectionLikeType(List.class, valueType));
} catch (Exception ex) {
return mapper.treeToValue(node, mapper.getTypeFactory().constructCollectionLikeType(List.class, valueType));
} catch (JsonProcessingException ex) {
throw new DeserializationException("error deserializing list of " + valueType.getSimpleName(), ex);
}
}
Expand Down
Loading

0 comments on commit fa2f191

Please sign in to comment.