-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add deserialization in DynawoModelsSupplier/DynawoEventModelsSupplier (…
…#363) * Add deserialization in DynawoModelsSupplier and DynawoEventModelsSupplier through a load static method * Add integration test * Add unit test Signed-off-by: lisrte <[email protected]>
- Loading branch information
Showing
9 changed files
with
273 additions
and
32 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
dynawaltz/src/main/java/com/powsybl/dynawaltz/suppliers/SupplierJsonDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.powsybl.dynawaltz.suppliers; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import com.powsybl.commons.PowsyblException; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>} | ||
*/ | ||
public class SupplierJsonDeserializer<T> { | ||
|
||
private final StdDeserializer<List<T>> deserializer; | ||
|
||
public SupplierJsonDeserializer(StdDeserializer<List<T>> deserializer) { | ||
this.deserializer = deserializer; | ||
} | ||
|
||
public List<T> deserialize(Path path) { | ||
try { | ||
Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8); | ||
return setupObjectMapper().readValue(reader, new TypeReference<>() { | ||
}); | ||
} catch (IOException e) { | ||
throw new PowsyblException("JSON input cannot be read", e); | ||
} | ||
} | ||
|
||
public List<T> deserialize(InputStream is) { | ||
try { | ||
return setupObjectMapper().readValue(is, new TypeReference<>() { | ||
}); | ||
} catch (IOException e) { | ||
throw new PowsyblException("JSON input cannot be read", e); | ||
} | ||
} | ||
|
||
private ObjectMapper setupObjectMapper() { | ||
return new ObjectMapper().registerModule(new SimpleModule().addDeserializer(List.class, deserializer)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
dynawaltz/src/test/java/com/powsybl/dynawaltz/suppliers/SupplierJsonDeserializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com/) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
package com.powsybl.dynawaltz.suppliers; | ||
|
||
import com.powsybl.commons.PowsyblException; | ||
import com.powsybl.dynawaltz.suppliers.events.EventModelConfig; | ||
import com.powsybl.dynawaltz.suppliers.events.EventModelConfigsJsonDeserializer; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Path; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
/** | ||
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>} | ||
*/ | ||
class SupplierJsonDeserializerTest { | ||
|
||
@Test | ||
void testPathException() { | ||
Path path = Path.of("wrongURI"); | ||
SupplierJsonDeserializer<EventModelConfig> deserializer = new SupplierJsonDeserializer<>(new EventModelConfigsJsonDeserializer()); | ||
Exception e = assertThrows(PowsyblException.class, () -> deserializer.deserialize(path)); | ||
assertEquals("JSON input cannot be read", e.getMessage()); | ||
} | ||
|
||
@Test | ||
void testInputStreamException() throws IOException { | ||
try (InputStream is = InputStream.nullInputStream()) { | ||
SupplierJsonDeserializer<EventModelConfig> deserializer = new SupplierJsonDeserializer<>(new EventModelConfigsJsonDeserializer()); | ||
Exception e = assertThrows(PowsyblException.class, () -> deserializer.deserialize(is)); | ||
assertEquals("JSON input cannot be read", e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.