diff --git a/solid/src/test/java/com/inrupt/client/solid/SolidClientTest.java b/solid/src/test/java/com/inrupt/client/solid/SolidClientTest.java index 0536702e1a..292552cb66 100644 --- a/solid/src/test/java/com/inrupt/client/solid/SolidClientTest.java +++ b/solid/src/test/java/com/inrupt/client/solid/SolidClientTest.java @@ -132,15 +132,18 @@ void testCustomHeaders() throws Exception { @Test void testJsonResource() { final URI uri = URI.create(config.get("solid_resource_uri") + "/transaction"); - client.read(uri, Transaction.class).thenAccept(transaction -> { - try (final Transaction t = transaction) { - assertEquals("sample description", t.getDescription()); - assertEquals(TransactionType.CREDIT, t.getType()); - final var err = assertThrows(CompletionException.class, client.update(t).toCompletableFuture()::join); + client.read(uri, TransactionResource.class).thenAccept(res -> { + try (final TransactionResource tr = res) { + final var transaction = tr.getTransaction(); + assertEquals("sample description", transaction.description()); + assertEquals(TransactionResource.TransactionType.CREDIT, transaction.type()); + final var err = assertThrows(CompletionException.class, client.update(tr).toCompletableFuture()::join); assertTrue(err.getCause() instanceof ForbiddenException); - t.setType(TransactionType.DEBIT); - t.setDescription("different description"); - assertDoesNotThrow(client.update(t).toCompletableFuture()::join); + tr.setTransaction(TransactionResource.Transaction.newBuilder(transaction) + .type(TransactionResource.TransactionType.DEBIT) + .description("different description") + .build()); + assertDoesNotThrow(client.update(tr).toCompletableFuture()::join); } }).toCompletableFuture().join(); } diff --git a/solid/src/test/java/com/inrupt/client/solid/Transaction.java b/solid/src/test/java/com/inrupt/client/solid/Transaction.java deleted file mode 100644 index 7e7c805e08..0000000000 --- a/solid/src/test/java/com/inrupt/client/solid/Transaction.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright Inrupt Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the - * Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -package com.inrupt.client.solid; - -import com.inrupt.client.spi.JsonService; -import com.inrupt.client.spi.ServiceProvider; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.UncheckedIOException; -import java.net.URI; -import java.time.Instant; - -class Transaction extends SolidNonRDFSource { - - private final JsonService jsonService; - private final TransactionData data; - - public Transaction(final URI identifier, final String contentType, final InputStream entity) { - super(identifier, contentType, entity); - - this.jsonService = ServiceProvider.getJsonService(); - try { - this.data = jsonService.fromJson(super.getEntity(), TransactionData.class); - } catch (IOException ex) { - throw new UncheckedIOException("Unable to parse Transaction data", ex); - } - } - - public String getId() { - return data.getId(); - } - - public void setId(final String id) { - data.setId(id); - } - - public String getDescription() { - return data.getDescription(); - } - - public void setDescription(final String description) { - data.setDescription(description); - } - - public Instant getDate() { - return data.getDate(); - } - - public void setDate(final Instant date) { - data.setDate(date); - } - - public double getAmount() { - return data.getAmount(); - } - - public void setAmount(final double amount) { - data.setAmount(amount); - } - - public String getCategory() { - return data.getCategory(); - } - - public void setCategory(final String category) { - data.setCategory(category); - } - - public TransactionType getType() { - return data.getType(); - } - - public void setType(final TransactionType type) { - data.setType(type); - } - - @Override - public InputStream getEntity() throws IOException { - try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) { - jsonService.toJson(data, output); - return new ByteArrayInputStream(output.toByteArray()); - } - } -} diff --git a/solid/src/test/java/com/inrupt/client/solid/TransactionData.java b/solid/src/test/java/com/inrupt/client/solid/TransactionData.java deleted file mode 100644 index 9f8b551724..0000000000 --- a/solid/src/test/java/com/inrupt/client/solid/TransactionData.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright Inrupt Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the - * Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -package com.inrupt.client.solid; - -import java.time.Instant; - -class TransactionData { - - private String id; - private String description; - private Instant date; - private double amount; - private TransactionType type; - private String category; - - public String getId() { - return id; - } - - public void setId(final String id) { - this.id = id; - } - - public String getDescription() { - return description; - } - - public void setDescription(final String description) { - this.description = description; - } - - public Instant getDate() { - return date; - } - - public void setDate(final Instant date) { - this.date = date; - } - - public double getAmount() { - return amount; - } - - public void setAmount(final double amount) { - this.amount = amount; - } - - public TransactionType getType() { - return type; - } - - public void setType(final TransactionType type) { - this.type = type; - } - - public String getCategory() { - return category; - } - - public void setCategory(final String category) { - this.category = category; - } -} diff --git a/solid/src/test/java/com/inrupt/client/solid/TransactionResource.java b/solid/src/test/java/com/inrupt/client/solid/TransactionResource.java new file mode 100644 index 0000000000..0e5eef7905 --- /dev/null +++ b/solid/src/test/java/com/inrupt/client/solid/TransactionResource.java @@ -0,0 +1,132 @@ +/* + * Copyright Inrupt Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.inrupt.client.solid; + +import com.inrupt.client.spi.JsonService; +import com.inrupt.client.spi.ServiceProvider; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.UncheckedIOException; +import java.net.URI; +import java.time.Instant; +import java.util.Objects; + +class TransactionResource extends SolidNonRDFSource { + + private final JsonService jsonService; + + private Transaction transaction; + + public TransactionResource(final URI identifier, final String contentType, final InputStream entity) { + super(identifier, contentType, entity); + + this.jsonService = ServiceProvider.getJsonService(); + try { + this.transaction = jsonService.fromJson(super.getEntity(), Transaction.class); + } catch (IOException ex) { + throw new UncheckedIOException("Unable to parse Transaction data", ex); + } + } + + public Transaction getTransaction() { + return transaction; + } + + public void setTransaction(final Transaction transaction) { + this.transaction = Objects.requireNonNull(transaction, "transaction may not be null!"); + } + + @Override + public InputStream getEntity() throws IOException { + try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) { + jsonService.toJson(transaction, output); + return new ByteArrayInputStream(output.toByteArray()); + } + } + + record Transaction(String id, String description, Instant date, double amount, TransactionType type, + String category) { + static class Builder { + private String transactionId; + private String transactionDescription; + private Instant transactionDate; + private double transactionAmount; + private TransactionType transactionType; + private String transactionCategory; + + public Builder id(final String id) { + this.transactionId = id; + return this; + } + + public Builder description(final String description) { + this.transactionDescription = description; + return this; + } + + public Builder date(final Instant date) { + this.transactionDate = date; + return this; + } + + public Builder amount(final double amount) { + this.transactionAmount = amount; + return this; + } + + public Builder type(final TransactionType type) { + this.transactionType = type; + return this; + } + + public Builder category(final String category) { + this.transactionCategory = category; + return this; + } + + public Transaction build() { + return new Transaction(this.transactionId, this.transactionDescription, this.transactionDate, + this.transactionAmount, this.transactionType, this.transactionCategory); + } + } + + static Builder newBuilder() { + return new Builder(); + } + + static Builder newBuilder(final Transaction transaction) { + return new Builder() + .id(transaction.id()) + .description(transaction.description()) + .date(transaction.date()) + .amount(transaction.amount()) + .type(transaction.type()) + .category(transaction.category()); + } + } + + enum TransactionType { + CREDIT, DEBIT + } +} diff --git a/solid/src/test/java/com/inrupt/client/solid/TransactionType.java b/solid/src/test/java/com/inrupt/client/solid/TransactionType.java deleted file mode 100644 index ec9e2c29f1..0000000000 --- a/solid/src/test/java/com/inrupt/client/solid/TransactionType.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright Inrupt Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the - * Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -package com.inrupt.client.solid; - -enum TransactionType { - CREDIT, DEBIT -}