|
| 1 | +/* |
| 2 | + * Copyright 2022 Inrupt Inc. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal in |
| 6 | + * the Software without restriction, including without limitation the rights to use, |
| 7 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 8 | + * Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | + * subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 15 | + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 16 | + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 17 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 18 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 19 | + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | +package com.inrupt.client.jackson; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | + |
| 25 | +import com.inrupt.client.spi.JsonProcessor; |
| 26 | +import com.inrupt.client.spi.ServiceProvider; |
| 27 | +import com.inrupt.client.spi.VerifiableCredential; |
| 28 | + |
| 29 | +import java.io.File; |
| 30 | +import java.io.FileInputStream; |
| 31 | +import java.io.FileOutputStream; |
| 32 | +import java.io.IOException; |
| 33 | +import java.nio.file.Files; |
| 34 | +import java.util.UUID; |
| 35 | + |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | + |
| 38 | +public class JacksonVCTest { |
| 39 | + |
| 40 | + private static final JsonProcessor processor = ServiceProvider.getJsonProcessor(); |
| 41 | + private static VerifiableCredential vc; |
| 42 | + private static VerifiableCredential vcCopy; |
| 43 | + |
| 44 | + @Test |
| 45 | + void roundtripVC() throws IOException { |
| 46 | + try (final var res = JacksonVCTest.class.getResourceAsStream("/verifiableCredential.json")) { |
| 47 | + vc = processor.fromJson(res, VerifiableCredential.class); |
| 48 | + } |
| 49 | + |
| 50 | + final var targetPath = new File("target").toPath(); |
| 51 | + final var testFolderName = UUID.randomUUID().toString(); |
| 52 | + final var testFolderPath = Files.createTempDirectory(targetPath, testFolderName); |
| 53 | + final var testFile = Files.createTempFile(testFolderPath, UUID.randomUUID().toString(), ".json"); |
| 54 | + |
| 55 | + try (final var out = new FileOutputStream(testFile.toString())) { |
| 56 | + processor.toJson(vc, out); |
| 57 | + } |
| 58 | + |
| 59 | + try (final var in = new FileInputStream(testFile.toString())) { |
| 60 | + vcCopy = processor.fromJson(in, VerifiableCredential.class); |
| 61 | + } |
| 62 | + |
| 63 | + assertEquals(vc.context, vcCopy.context); |
| 64 | + assertEquals(vc.id, vcCopy.id); |
| 65 | + assertEquals(vc.type, vcCopy.type); |
| 66 | + assertEquals(vc.issuer, vcCopy.issuer); |
| 67 | + assertEquals(vc.issuanceDate, vcCopy.issuanceDate); |
| 68 | + assertEquals(vc.expirationDate, vcCopy.expirationDate); |
| 69 | + assertEquals(vc.credentialSubject, vcCopy.credentialSubject); |
| 70 | + assertEquals(vc.credentialStatus, vcCopy.credentialStatus); |
| 71 | + assertEquals(vc.proof, vcCopy.proof); |
| 72 | + |
| 73 | + Files.deleteIfExists(testFile); |
| 74 | + Files.deleteIfExists(testFolderPath); |
| 75 | + |
| 76 | + } |
| 77 | +} |
0 commit comments