Skip to content

Commit

Permalink
devonfw#634: added DependencyInfoDeserializer (devonfw#641)
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille authored Sep 20, 2024
1 parent 90d6b8d commit 57f3629
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.devonfw.tools.ide.json;

import java.io.IOException;

import com.devonfw.tools.ide.url.model.file.json.DependencyInfo;
import com.devonfw.tools.ide.version.VersionRange;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

/**
* {@link JsonDeserializer} for {@link DependencyInfo}.
*/
public class DependencyInfoDeserializer extends JsonDeserializer<DependencyInfo> {

@Override
public DependencyInfo deserialize(JsonParser p, DeserializationContext context) throws IOException, JacksonException {

JsonToken token = p.getCurrentToken();
if (token == JsonToken.START_OBJECT) {
token = p.nextToken();
String tool = null;
VersionRange versionRange = null;
while (token == JsonToken.FIELD_NAME) {
String property = p.currentName();
if (property.equals("tool")) {
token = p.nextToken();
assert token == JsonToken.VALUE_STRING;
tool = p.getValueAsString();
} else if (property.equals("versionRange")) {
token = p.nextToken();
assert (token == JsonToken.START_OBJECT) || (token == JsonToken.VALUE_STRING);
versionRange = p.readValueAs(VersionRange.class);
} else {
// ignore unknown property
// currently cannot log here due to https://github.com/devonfw/IDEasy/issues/404
}
token = p.nextToken();
}
if ((tool != null) && (versionRange != null)) {
return new DependencyInfo(tool, versionRange);
}
}
throw new IllegalArgumentException("Invalid JSON for DependencyInfo!");
}

}
2 changes: 2 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/json/JsonMapping.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.devonfw.tools.ide.json;

import com.devonfw.tools.ide.url.model.file.json.DependencyInfo;
import com.devonfw.tools.ide.version.VersionIdentifier;
import com.devonfw.tools.ide.version.VersionRange;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down Expand Up @@ -28,6 +29,7 @@ public static ObjectMapper create() {
SimpleModule customModule = new SimpleModule();
customModule.addDeserializer(VersionIdentifier.class, new VersionIdentifierDeserializer());
customModule.addDeserializer(VersionRange.class, new VersionRangeDeserializer());
customModule.addDeserializer(DependencyInfo.class, new DependencyInfoDeserializer());
customModule.addKeyDeserializer(VersionRange.class, new VersionRangeKeyDeserializer());
mapper = mapper.registerModule(customModule);
return mapper;
Expand Down
30 changes: 24 additions & 6 deletions cli/src/test/java/com/devonfw/tools/ide/json/JsonMappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import com.devonfw.tools.ide.url.model.file.json.DependencyInfo;
import com.devonfw.tools.ide.version.BoundaryType;
import com.devonfw.tools.ide.version.VersionIdentifier;
import com.devonfw.tools.ide.version.VersionRange;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
Expand All @@ -26,12 +30,12 @@ public class JsonMappingTest extends Assertions {
@Test
public void testReadInstant() throws Exception {

// given
// arrange
String value = INSTANT_VALUE_JSON;
// when
// act
ObjectMapper mapper = JsonMapping.create();
Instant instant = mapper.readValue(value, Instant.class);
// then
// assert
assertThat(instant).isEqualTo(INSTANT_VALUE);
}

Expand All @@ -43,13 +47,27 @@ public void testReadInstant() throws Exception {
@Test
public void testWriteInstant() throws Exception {

// given
// arrange
Instant value = INSTANT_VALUE;
// when
// act
ObjectMapper mapper = JsonMapping.create();
String json = mapper.writeValueAsString(value);
// then
// assert
assertThat(json).isEqualTo(INSTANT_VALUE_JSON);
}

@Test
public void testReadDependencies() throws Exception {

// arrange
String json = "{\"tool\": \"java\",\"versionRange\": \"[11,21_35]\"}";
VersionRange expectedVersionRange = VersionRange.of(VersionIdentifier.of("11"), VersionIdentifier.of("21_35"), BoundaryType.CLOSED);
// act
ObjectMapper mapper = JsonMapping.create();
DependencyInfo dependencyInfo = mapper.readValue(json, DependencyInfo.class);
// assert
assertThat(dependencyInfo.tool()).isEqualTo("java");
assertThat(dependencyInfo.versionRange()).isEqualTo(expectedVersionRange);
}

}

0 comments on commit 57f3629

Please sign in to comment.