1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.metapath.item.atomic;
7   
8   import static org.junit.jupiter.api.Assertions.assertAll;
9   import static org.junit.jupiter.api.Assertions.assertArrayEquals;
10  import static org.junit.jupiter.api.Assertions.assertEquals;
11  
12  import org.junit.jupiter.api.Assertions;
13  import org.junit.jupiter.api.Test;
14  import org.junit.jupiter.params.ParameterizedTest;
15  import org.junit.jupiter.params.provider.Arguments;
16  import org.junit.jupiter.params.provider.MethodSource;
17  
18  import java.nio.ByteBuffer;
19  import java.util.stream.Stream;
20  
21  import dev.metaschema.core.util.ObjectUtils;
22  import edu.umd.cs.findbugs.annotations.NonNull;
23  
24  class IBase64BinaryItemTest {
25    private static final long MIN_LONG = -9_223_372_036_854_775_808L;
26    private static final long MAX_LONG = 9_223_372_036_854_775_807L;
27    private static final String BASE_64 = "gAAAAAAAAAB//////////w==";
28  
29    @Test
30    void testValueOf() {
31      IBase64BinaryItem item = IBase64BinaryItem.encode(ObjectUtils.notNull(
32          ByteBuffer.allocate(16)
33              .putLong(MIN_LONG)
34              .putLong(MAX_LONG)));
35      assertEquals(BASE_64, item.asString());
36    }
37  
38    @Test
39    void testCastSame() {
40      ByteBuffer buf = ObjectUtils.notNull(ByteBuffer.allocate(16)
41          .putLong(MIN_LONG)
42          .putLong(MAX_LONG));
43      IBase64BinaryItem item = IBase64BinaryItem.encode(buf);
44      assertEquals(IBase64BinaryItem.cast(item), item);
45    }
46  
47    @Test
48    void testCastString() {
49      ByteBuffer buf
50          = ObjectUtils.notNull(ByteBuffer.allocate(16).putLong(MIN_LONG).putLong(MAX_LONG));
51      IBase64BinaryItem expected = IBase64BinaryItem.encode(buf);
52      IBase64BinaryItem actual = IBase64BinaryItem.cast(IStringItem.valueOf(BASE_64));
53      Assertions.assertAll(
54          // TODO: use equals method?
55          () -> assertArrayEquals(actual.asByteBuffer().array(), expected.asByteBuffer().array()),
56          () -> assertEquals(actual.asString(), expected.asString()));
57    }
58  
59    private static Stream<Arguments> provideValuesForEncodeDecode() {
60      return Stream.of(
61          Arguments.of("The quick brown fox jumps over the lazy dog",
62              "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw=="),
63          Arguments.of("Mr. Watson, come here, I need you", "TXIuIFdhdHNvbiwgY29tZSBoZXJlLCBJIG5lZWQgeW91"),
64          Arguments.of(
65              "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam eu luctus lorem. Aliquam malesuada lorem"
66                  + " nisi, ut tincidunt neque feugiat vitae. Fusce pretium nunc ac sapien feugiat accumsan. Proin eget"
67                  + " ligula non turpis laoreet fermentum. Aliquam mi justo, gravida id vulputate id, venenatis eu felis."
68                  + " Vestibulum commodo, magna quis sollicitudin consectetur, eros erat elementum libero, nec euismod"
69                  + " elit arcu non diam. Sed iaculis dui lacus, vitae placerat velit iaculis quis. Sed in ligula in eros"
70                  + " luctus porttitor. Nullam in laoreet leo. Cras sed nisl eget turpis sollicitudin molestie et eget"
71                  + " tellus. Vivamus aliquam odio et dui mattis, in rhoncus mauris hendrerit. Nam viverra mattis risus"
72                  + " non tristique.",
73              "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gRXRpYW0gZXUgbHVjdHVzIGxvcmVtLi"
74                  + "BBbGlxdWFtIG1hbGVzdWFkYSBsb3JlbSBuaXNpLCB1dCB0aW5jaWR1bnQgbmVxdWUgZmV1Z2lhdCB2aXRhZS4gRnVzY2UgcHJldG"
75                  + "l1bSBudW5jIGFjIHNhcGllbiBmZXVnaWF0IGFjY3Vtc2FuLiBQcm9pbiBlZ2V0IGxpZ3VsYSBub24gdHVycGlzIGxhb3JlZXQgZm"
76                  + "VybWVudHVtLiBBbGlxdWFtIG1pIGp1c3RvLCBncmF2aWRhIGlkIHZ1bHB1dGF0ZSBpZCwgdmVuZW5hdGlzIGV1IGZlbGlzLiBWZX"
77                  + "N0aWJ1bHVtIGNvbW1vZG8sIG1hZ25hIHF1aXMgc29sbGljaXR1ZGluIGNvbnNlY3RldHVyLCBlcm9zIGVyYXQgZWxlbWVudHVtIG"
78                  + "xpYmVybywgbmVjIGV1aXNtb2QgZWxpdCBhcmN1IG5vbiBkaWFtLiBTZWQgaWFjdWxpcyBkdWkgbGFjdXMsIHZpdGFlIHBsYWNlcm"
79                  + "F0IHZlbGl0IGlhY3VsaXMgcXVpcy4gU2VkIGluIGxpZ3VsYSBpbiBlcm9zIGx1Y3R1cyBwb3J0dGl0b3IuIE51bGxhbSBpbiBsYW"
80                  + "9yZWV0IGxlby4gQ3JhcyBzZWQgbmlzbCBlZ2V0IHR1cnBpcyBzb2xsaWNpdHVkaW4gbW9sZXN0aWUgZXQgZWdldCB0ZWxsdXMuIF"
81                  + "ZpdmFtdXMgYWxpcXVhbSBvZGlvIGV0IGR1aSBtYXR0aXMsIGluIHJob25jdXMgbWF1cmlzIGhlbmRyZXJpdC4gTmFtIHZpdmVycm"
82                  + "EgbWF0dGlzIHJpc3VzIG5vbiB0cmlzdGlxdWUu"));
83    }
84  
85    @ParameterizedTest
86    @MethodSource("provideValuesForEncodeDecode")
87    void testEncodeDecodeEncode(@NonNull String expectedDecodedString, @NonNull String expectedEncodedString) {
88      IStringItem decodedText = IStringItem.valueOf(expectedDecodedString);
89  
90      // test encode
91      IBase64BinaryItem encodedText = IBase64BinaryItem.encode(decodedText);
92      assertAll(
93          () -> assertEquals(expectedDecodedString, decodedText.asString(), "Expected same initial decoded text"),
94          () -> assertEquals(expectedEncodedString, encodedText.asString(), "Expected same encoded text"),
95          // test decode
96          () -> assertEquals(expectedDecodedString, encodedText.decodeAsString().asString(),
97              "Expected same decoded text on first attempt"),
98          // test decode #2
99          () -> assertEquals(expectedDecodedString, encodedText.decodeAsString().asString(),
100             "Expected same decoded text on second attempt"));
101   }
102 }