[RM] Feature JsonYamlDictionaryStructureWriter > fix writing dictionary attributes
RENAME
BackEnd/Core/sailfish-common/src/main/java/com/exactpro/sf/common/impl/messages/json/JsonDictionaryWriter.java
BackEnd/Core/sailfish-common/src/main/java/com/exactpro/sf/common/impl/messages/json/JsonYamlDictionaryWriter.java
@@ -17,10 +17,13 @@ package com.exactpro.sf.common.impl.messages.json;
import com.exactpro.sf.common.impl.messages.json.configuration.JsonYamlDictionary;
import com.exactpro.sf.common.util.EPSCommonException;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
+import com.fasterxml.jackson.dataformat.yaml.YAMLParser;
import java.io.File;
import java.io.FileNotFoundException;
@@ -28,16 +31,23 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
-public class JsonDictionaryWriter {
+public class JsonYamlDictionaryWriter {
- private static final ObjectMapper objectMapper = new ObjectMapper(
+ private static ObjectMapper objectMapper;
+
+
+ public static void write(JsonYamlDictionary dictionary, OutputStream output, boolean asYaml) {
+
+ if (asYaml) {
+ objectMapper = new ObjectMapper(
new YAMLFactory()
.disable(YAMLGenerator.Feature.USE_NATIVE_OBJECT_ID)
.disable(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID)
- )
- .enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
+ );
+ } else {
+ objectMapper = new ObjectMapper();
+ }
- public static void write(JsonYamlDictionary dictionary, OutputStream output) {
try {
objectMapper.writerFor(JsonYamlDictionary.class).writeValue(output, dictionary);
} catch (IOException exception) {
@@ -45,10 +55,10 @@ public class JsonDictionaryWriter {
}
}
- public static void write(JsonYamlDictionary dictionary, File outputFile) {
+ public static void write(JsonYamlDictionary dictionary, File outputFile, boolean asYaml) {
try (OutputStream outputStream = new FileOutputStream(outputFile)) {
- write(dictionary, outputStream);
+ write(dictionary, outputStream, asYaml);
} catch (IOException e) {
throw new EPSCommonException("Failed to write dictionary:" + dictionary.getName(), e);
}

MODIFY
BackEnd/Core/sailfish-common/src/main/java/com/exactpro/sf/common/impl/messages/json/configuration/JsonYamlDictionary.java
BackEnd/Core/sailfish-common/src/main/java/com/exactpro/sf/common/impl/messages/json/configuration/JsonYamlDictionary.java
@@ -119,6 +119,18 @@ public class JsonYamlDictionary implements IDictionary {
return attributes == null ? Collections.emptyList() : Collections.unmodifiableList(attributes);
}
+ /**
+ * Sets the value of the attribute property
+ *
+ * @param value
+ * allowed object is
+ * {@link List<JsonAttribute>}
+ *
+ */
+ public void setAttributes(List<JsonAttribute> value) {
+ this.attributes = value;
+ }
+
/**
* Gets the value of the fields property.
*

RENAME
FrontEnd/SailfishFrontEnd/src/main/java/com/exactpro/sf/testwebgui/structures/JsonDictionaryStructureWriter.java
FrontEnd/SailfishFrontEnd/src/main/java/com/exactpro/sf/testwebgui/structures/JsonYamlDictionaryStructureWriter.java
******************************************************************************/
package com.exactpro.sf.testwebgui.structures;
-import com.exactpro.sf.common.impl.messages.json.JsonDictionaryWriter;
+import com.exactpro.sf.common.impl.messages.json.JsonYamlDictionaryWriter;
import com.exactpro.sf.common.impl.messages.json.configuration.JsonAttribute;
import com.exactpro.sf.common.impl.messages.json.configuration.JsonField;
import com.exactpro.sf.common.impl.messages.json.configuration.JsonMessage;
@@ -33,19 +33,19 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
-public class JsonDictionaryStructureWriter {
+public class JsonYamlDictionaryStructureWriter {
- public static void write(ModifiableDictionaryStructure dictionaryStructure, OutputStream output) {
+ public static void write(ModifiableDictionaryStructure dictionaryStructure, OutputStream output, boolean asYaml) {
JsonYamlDictionary dictionary = convertStructureToJson(dictionaryStructure);
- JsonDictionaryWriter.write(dictionary, output);
+ JsonYamlDictionaryWriter.write(dictionary, output, asYaml);
}
- public static void write(ModifiableDictionaryStructure dictionaryStructure, File file) throws IOException {
+ public static void write(ModifiableDictionaryStructure dictionaryStructure, File file, boolean asYaml) throws IOException {
try (OutputStream output = new FileOutputStream(file)) {
- write(dictionaryStructure, output);
+ write(dictionaryStructure, output, asYaml);
} catch (FileNotFoundException e) {
throw new EPSCommonException("Failed to write dictionary: " + dictionaryStructure.getNamespace(), e);
}
@@ -54,9 +54,16 @@ public class JsonDictionaryStructureWriter {
private static JsonYamlDictionary convertStructureToJson(ModifiableDictionaryStructure dictionaryStructure) {
JsonYamlDictionary dictionary = new JsonYamlDictionary(dictionaryStructure.getNamespace());
-
dictionary.setDescription(dictionaryStructure.getDescription());
+ List<JsonAttribute> attributes = new ArrayList<>();
+ if (dictionaryStructure.getAttributes() != null) {
+ for (IAttributeStructure attributeStructure : dictionaryStructure.getAttributes().values()) {
+ attributes.add(createAttributeFromStructure(attributeStructure));
+ }
+ }
+ dictionary.setAttributes(attributes);
+
List<JsonField> fields = new ArrayList<>();
List<JsonMessage> messages = new ArrayList<>();
@@ -173,12 +180,18 @@ public class JsonDictionaryStructureWriter {
private static void addAttributes(ModifiableFieldStructure fieldStructure, JsonField jsonField) {
for (IAttributeStructure attributeStructure : fieldStructure.getImplAttributes()) {
- JsonAttribute jsonAttribute = new JsonAttribute();
- jsonAttribute.setName(attributeStructure.getName());
- jsonAttribute.setValue(attributeStructure.getValue());
- jsonAttribute.setType(attributeStructure.getType());
-
- jsonField.getAttributes().add(jsonAttribute);
+ jsonField.getAttributes().add(createAttributeFromStructure(attributeStructure));
}
}
+
+ private static JsonAttribute createAttributeFromStructure(IAttributeStructure attributeStructure) {
+ JsonAttribute attribute = new JsonAttribute();
+
+ attribute.setName(attributeStructure.getName());
+ attribute.setValue(attributeStructure.getValue());
+ attribute.setType(attributeStructure.getType());
+
+ return attribute;
+
+ }
}

RENAME
FrontEnd/SailfishFrontEnd/src/test/java/com/exactpro/sf/testwebgui/structures/JsonDictionaryStructureWriterTest.java
FrontEnd/SailfishFrontEnd/src/test/java/com/exactpro/sf/testwebgui/structures/JsonYamlDictionaryStructureWriterTest.java
@@ -22,7 +22,9 @@ import com.exactpro.sf.common.messages.structures.DictionaryComparator.IDiffList
import com.exactpro.sf.common.messages.structures.IDictionaryStructure;
import com.exactpro.sf.common.messages.structures.loaders.JsonYamlDictionaryStructureLoader;
import org.junit.Assert;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.FileInputStream;
@@ -33,12 +35,14 @@ import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
-public class JsonDictionaryStructureWriterTest {
+public class JsonYamlDictionaryStructureWriterTest {
private static final Path BASE_DIR = Paths.get((System.getProperty("basedir") == null) ? "." : System.getProperty("basedir")).toAbsolutePath().normalize();
- private static final String FILE_PATH = BASE_DIR + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator + "test_aml.xml";
+ private static final String FILE_PATH = BASE_DIR + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator + "writer-test-dictionary.xml";
+ @Rule
+ public TemporaryFolder folder = new TemporaryFolder();
@Test
public void testWriterWithoutDifference() throws IOException {
@@ -51,12 +55,11 @@ public class JsonDictionaryStructureWriterTest {
.load(inputStream);
}
- File tempFile = File.createTempFile("jsonDictionary", ".json");
+ File tempFile = folder.newFile("jsonDictionary.json");
- JsonDictionaryStructureWriter.write(modifiableDictionaryStructure, tempFile);
+ JsonYamlDictionaryStructureWriter.write(modifiableDictionaryStructure, tempFile, false);
IDictionaryStructure dictionaryStructureFromJson;
-
try (InputStream inputStream = new FileInputStream(tempFile)) {
JsonYamlDictionaryStructureLoader loader = new JsonYamlDictionaryStructureLoader();
@@ -82,14 +85,12 @@ public class JsonDictionaryStructureWriterTest {
.load(inputStream);
}
- File tempFile = File.createTempFile("jsonDictionary", ".json");
-
- JsonDictionaryStructureWriter.write(modifiableDictionaryStructure, tempFile);
+ File tempFile = folder.newFile("jsonDictionary.json");
+ JsonYamlDictionaryStructureWriter.write(modifiableDictionaryStructure, tempFile, false);
IDictionaryStructure dictionaryStructureFromJson;
-
try (InputStream inputStream = new FileInputStream(tempFile)) {
JsonYamlDictionaryStructureLoader loader = new JsonYamlDictionaryStructureLoader();

RENAME
FrontEnd/SailfishFrontEnd/src/test/resources/test_aml.xml
FrontEnd/SailfishFrontEnd/src/test/resources/writer-test-dictionary.xml
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<dictionary xmlns="http://exactprosystems.com/dictionary" name="TestAML">
+ <attribute name="Test" type="java.lang.String">TestString</attribute>
<fields>
<field id="F_CHAR" reference="F_TCharacter" name="CHAR"/>
<field id="F_STRING" reference="F_TString" name="STRING"/>

[RM] Feature #70790. JsonYamlDictionaryStructureWriter > fix writing dictionary attributes

Change-Id: I17d19f0651f765e6acbada710cdc2c446b8d6a01