Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tests): test to check if Ion that contain annotation is transformed well in Json #140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/test/java/io/kestra/plugin/serdes/json/IonToJsonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.kestra.plugin.serdes.json;

import io.kestra.core.junit.annotations.KestraTest;
import io.kestra.core.runners.RunContext;
import io.kestra.core.runners.RunContextFactory;
import io.kestra.core.storages.StorageInterface;
import io.kestra.core.utils.IdUtils;
import jakarta.inject.Inject;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import static org.junit.jupiter.api.Assertions.fail;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;

import com.google.common.collect.ImmutableMap;

@KestraTest
public class IonToJsonTest {
@Inject
private RunContextFactory runContextFactory;

@Inject
private StorageInterface storageInterface;

private RunContext getContext(String content) {
Map<String, String> kestraPath = new HashMap<>();
URI filePath;
try {
filePath = storageInterface.put(
null,
URI.create("/" + IdUtils.create() + ".ion"),
new ByteArrayInputStream(content.getBytes())
);
kestraPath.put("file", filePath.toString());
} catch (Exception e) {
System.err.println(e.getMessage());
fail("Unable to load input file.");
return null;
}
return runContextFactory.of(ImmutableMap.copyOf(kestraPath));
}

private void assertEquality(String expected, URI file) {
assertThat("Result file should exist", storageInterface.exists(null, file), is(true));
try (InputStream streamResult = storageInterface.get(null, file)) {
String result = new String(streamResult.readAllBytes(), StandardCharsets.UTF_8).replace("\r\n", "\n");
System.out.println("Got :\n" + result);
System.out.println("Expecting :\n" + expected);
assertThat("Result should match the reference", result.equals(expected));
} catch (Exception e) {
System.err.println(e.getMessage());
fail("Unable to load results files.");
}
}

@Test
void test_annotation_conversion() throws Exception {
String input = """
{dn:"[email protected],ou=diffusion_list,dc=orga,dc=com",attributes:{description:["Some description 2",base64::"TGlzdGUgZCfDg8KpY2hhbmdlIHN1ciBsZSBzdWl2aSBkZSBsYSBtYXNzZSBzYWxhcmlhbGUgZGUgbCdJVVQ=","Melusine lover as well"],someOtherAttribute:["perhaps 2","perhapsAgain 2"]}}
""";
// Expectated result when should_keep_annotations==False | or not specified
String expectation_removed_annot = """
{"dn":"[email protected],ou=diffusion_list,dc=orga,dc=com","attributes":{"description":["Some description 2","TGlzdGUgZCfDg8KpY2hhbmdlIHN1ciBsZSBzdWl2aSBkZSBsYSBtYXNzZSBzYWxhcmlhbGUgZGUgbCdJVVQ=","Melusine lover as well"],"someOtherAttribute":["perhaps 2","perhapsAgain 2"]}}
""";
// Expectated result when should_keep_annotations==True
// String expectation_indicated_annot = """
// {"dn":"[email protected],ou=diffusion_list,dc=orga,dc=com","attributes":{"description":["Some description 2",{"ion_annotations":["base64"], "value":"TGlzdGUgZCfDg8KpY2hhbmdlIHN1ciBsZSBzdWl2aSBkZSBsYSBtYXNzZSBzYWxhcmlhbGUgZGUgbCdJVVQ="},"Melusine lover as well"],"someOtherAttribute":["perhaps 2","perhapsAgain 2"]}}
// """;

RunContext runContext = getContext(input);
IonToJson task = IonToJson.builder().from("{{file}}").build();//TODO: create two executions, with boolean "should_keep_annotations"
IonToJson.Output output = task.run(runContext);
assertEquality(expectation_removed_annot, output.getUri());//by defaults, annot shouldn't be kept since it's the default behaviour of the ION cookbook https://amazon-ion.github.io/ion-docs/guides/cookbook.html, see "Down-converting to JSON" section
//assertEquality(expectation_indicated_annot, output.getUri());//TODO: if should_keep_annotations == true
}
}