1
+ package io .kestra .plugin .serdes .json ;
2
+
3
+ import io .kestra .core .junit .annotations .KestraTest ;
4
+ import io .kestra .core .runners .RunContext ;
5
+ import io .kestra .core .runners .RunContextFactory ;
6
+ import io .kestra .core .storages .StorageInterface ;
7
+ import io .kestra .core .utils .IdUtils ;
8
+ import jakarta .inject .Inject ;
9
+ import static org .hamcrest .Matchers .is ;
10
+ import static org .hamcrest .MatcherAssert .assertThat ;
11
+
12
+ import static org .junit .jupiter .api .Assertions .fail ;
13
+
14
+ import java .io .ByteArrayInputStream ;
15
+ import java .io .InputStream ;
16
+ import java .net .URI ;
17
+ import java .nio .charset .StandardCharsets ;
18
+ import java .util .HashMap ;
19
+ import java .util .Map ;
20
+
21
+ import org .junit .jupiter .api .Test ;
22
+
23
+ import com .google .common .collect .ImmutableMap ;
24
+
25
+ @ KestraTest
26
+ public class IonToJsonTest {
27
+ @ Inject
28
+ private RunContextFactory runContextFactory ;
29
+
30
+ @ Inject
31
+ private StorageInterface storageInterface ;
32
+
33
+ private RunContext getContext (String content ) {
34
+ Map <String , String > kestraPath = new HashMap <>();
35
+ URI filePath ;
36
+ try {
37
+ filePath = storageInterface .put (
38
+ null ,
39
+ URI .create ("/" + IdUtils .create () + ".ion" ),
40
+ new ByteArrayInputStream (content .getBytes ())
41
+ );
42
+ kestraPath .put ("file" , filePath .toString ());
43
+ } catch (Exception e ) {
44
+ System .err .println (e .getMessage ());
45
+ fail ("Unable to load input file." );
46
+ return null ;
47
+ }
48
+ return runContextFactory .of (ImmutableMap .copyOf (kestraPath ));
49
+ }
50
+
51
+ private void assertEquality (String expected , URI file ) {
52
+ assertThat ("Result file should exist" , storageInterface .exists (null , file ), is (true ));
53
+ try (InputStream streamResult = storageInterface .get (null , file )) {
54
+ String result = new String (streamResult .readAllBytes (), StandardCharsets .UTF_8 ).replace ("\r \n " , "\n " );
55
+ System .out .println ("Got :\n " + result );
56
+ System .out .println ("Expecting :\n " + expected );
57
+ assertThat ("Result should match the reference" , result .equals (expected ));
58
+ } catch (Exception e ) {
59
+ System .err .println (e .getMessage ());
60
+ fail ("Unable to load results files." );
61
+ }
62
+ }
63
+
64
+ @ Test
65
+ void test_annotation_conversion () throws Exception {
66
+ String input = """
67
+ {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"]}}
68
+ """ ;
69
+ // Expectated result when should_keep_annotations==False | or not specified
70
+ String expectation_removed_annot = """
71
+ {"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"]}}
72
+ """ ;
73
+ // Expectated result when should_keep_annotations==True
74
+ // String expectation_indicated_annot = """
75
+ // {"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"]}}
76
+ // """;
77
+
78
+ RunContext runContext = getContext (input );
79
+ IonToJson task = IonToJson .builder ().from ("{{file}}" ).build ();//TODO: create two executions, with boolean "should_keep_annotations"
80
+ IonToJson .Output output = task .run (runContext );
81
+ 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
82
+ //assertEquality(expectation_indicated_annot, output.getUri());//TODO: if should_keep_annotations == true
83
+ }
84
+ }
0 commit comments