From 36a8530ec4a3a318470814d9a6c47db287e1df91 Mon Sep 17 00:00:00 2001 From: amihaiemil Date: Fri, 7 May 2021 16:05:08 +0300 Subject: [PATCH 1/2] #296 core 81 and SelfJsonStorage sketch --- pom.xml | 2 +- .../java/com/selfxdsd/storage/SelfJooq.java | 6 ++ .../com/selfxdsd/storage/SelfJsonStorage.java | 76 +++++++++++++++++++ .../selfxdsd/storage/SelfJooqTestCase.java | 15 ++++ 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/selfxdsd/storage/SelfJsonStorage.java diff --git a/pom.xml b/pom.xml index 6b5adc1..866705a 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ Self XDSD Storage Module Storage Module for Self XDSD - 0.0.72 + 0.0.81 UTF-8 diff --git a/src/main/java/com/selfxdsd/storage/SelfJooq.java b/src/main/java/com/selfxdsd/storage/SelfJooq.java index 4a7bcc2..0258b3d 100644 --- a/src/main/java/com/selfxdsd/storage/SelfJooq.java +++ b/src/main/java/com/selfxdsd/storage/SelfJooq.java @@ -23,6 +23,7 @@ package com.selfxdsd.storage; import com.selfxdsd.api.*; +import com.selfxdsd.api.storage.JsonStorage; import com.selfxdsd.api.storage.Storage; /** @@ -135,6 +136,11 @@ public Payments payments() { return new SelfPayments(this, this.database); } + @Override + public JsonStorage jsonStorage() { + return new SelfJsonStorage(this, this.database); + } + @Override public void close() { this.database.close(); diff --git a/src/main/java/com/selfxdsd/storage/SelfJsonStorage.java b/src/main/java/com/selfxdsd/storage/SelfJsonStorage.java new file mode 100644 index 0000000..e59babd --- /dev/null +++ b/src/main/java/com/selfxdsd/storage/SelfJsonStorage.java @@ -0,0 +1,76 @@ +/** + * Copyright (c) 2020-2021, Self XDSD Contributors + * All rights reserved. + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), + * to read the Software only. Permission is hereby NOT GRANTED to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software. + *

+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package com.selfxdsd.storage; + +import com.selfxdsd.api.storage.JsonStorage; +import com.selfxdsd.api.storage.Storage; + +import java.net.URI; + +/** + * Storage for JsonResources in Self XDSD (we store JSONs received from + * providers' API for making conditional requests using E-Tag). + * @author Mihai Andronache (amihaiemil@gmail.com) + * @since 0.0.70 + * @version $Id$ + */ +public final class SelfJsonStorage implements JsonStorage { + + /** + * Parent Storage. + */ + private final Storage storage; + + /** + * Database. + */ + private final Database database; + + /** + * Ctor. + * @param storage Parent Storage. + * @param database Database. + */ + public SelfJsonStorage( + final Storage storage, + final Database database + ) { + this.storage = storage; + this.database = database; + } + + @Override + public String getEtag(final URI uri) { + return null; + } + + @Override + public String getResourceBody(final URI uri) { + return null; + } + + @Override + public void store(final URI uri, final String etag, final String resource) { + + } +} diff --git a/src/test/java/com/selfxdsd/storage/SelfJooqTestCase.java b/src/test/java/com/selfxdsd/storage/SelfJooqTestCase.java index dbf4e67..710cb75 100644 --- a/src/test/java/com/selfxdsd/storage/SelfJooqTestCase.java +++ b/src/test/java/com/selfxdsd/storage/SelfJooqTestCase.java @@ -270,4 +270,19 @@ public void returnsPayments() { ) ); } + + /** + * SelfJooq can return the JsonStorage. + */ + @Test + public void returnsJsonStorage() { + final Storage storage = new SelfJooq(Mockito.mock(Database.class)); + MatcherAssert.assertThat( + storage.jsonStorage(), + Matchers.allOf( + Matchers.notNullValue(), + Matchers.instanceOf(SelfJsonStorage.class) + ) + ); + } } From 2a8e0223bb08273343b87eca424bb2206dfa6f06 Mon Sep 17 00:00:00 2001 From: amihaiemil Date: Fri, 7 May 2021 16:51:30 +0300 Subject: [PATCH 2/2] #296 SelfJsonStorage implemented and tested --- .../com/selfxdsd/storage/SelfJsonStorage.java | 42 +++- .../selfxdsd/storage/generated/jooq/Keys.java | 4 + .../storage/generated/jooq/SelfXdsd.java | 9 +- .../storage/generated/jooq/Tables.java | 6 + .../jooq/tables/SlfJsonstorageXdsd.java | 146 ++++++++++++++ .../records/SlfJsonstorageXdsdRecord.java | 181 +++++++++++++++++ .../storage/SelfJsonStorageITCase.java | 187 ++++++++++++++++++ src/test/resources/createDb.sql | 12 +- src/test/resources/insertTestData.sql | 5 +- 9 files changed, 588 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/selfxdsd/storage/generated/jooq/tables/SlfJsonstorageXdsd.java create mode 100644 src/main/java/com/selfxdsd/storage/generated/jooq/tables/records/SlfJsonstorageXdsdRecord.java create mode 100644 src/test/java/com/selfxdsd/storage/SelfJsonStorageITCase.java diff --git a/src/main/java/com/selfxdsd/storage/SelfJsonStorage.java b/src/main/java/com/selfxdsd/storage/SelfJsonStorage.java index e59babd..58c57ec 100644 --- a/src/main/java/com/selfxdsd/storage/SelfJsonStorage.java +++ b/src/main/java/com/selfxdsd/storage/SelfJsonStorage.java @@ -24,8 +24,11 @@ import com.selfxdsd.api.storage.JsonStorage; import com.selfxdsd.api.storage.Storage; +import org.jooq.Record; +import org.jooq.Result; import java.net.URI; +import static com.selfxdsd.storage.generated.jooq.Tables.SLF_JSONSTORAGE_XDSD; /** * Storage for JsonResources in Self XDSD (we store JSONs received from @@ -61,16 +64,53 @@ public SelfJsonStorage( @Override public String getEtag(final URI uri) { + final Result result = this.database.jooq() + .select() + .from(SLF_JSONSTORAGE_XDSD) + .where(SLF_JSONSTORAGE_XDSD.URL.eq(uri.toString())) + .fetch(); + if(result.size() > 0) { + return result.get(0).get(SLF_JSONSTORAGE_XDSD.ETAG); + } return null; } @Override public String getResourceBody(final URI uri) { + final Result result = this.database.jooq() + .select() + .from(SLF_JSONSTORAGE_XDSD) + .where(SLF_JSONSTORAGE_XDSD.URL.eq(uri.toString())) + .fetch(); + if(result.size() > 0) { + return result.get(0).get(SLF_JSONSTORAGE_XDSD.JSONBODY); + } return null; } @Override public void store(final URI uri, final String etag, final String resource) { - + if(uri.toString().length() < 1024 + && !etag.isBlank() && !resource.isBlank()) { + if(this.getEtag(uri) == null) { + this.database.jooq().insertInto( + SLF_JSONSTORAGE_XDSD, + SLF_JSONSTORAGE_XDSD.URL, + SLF_JSONSTORAGE_XDSD.ETAG, + SLF_JSONSTORAGE_XDSD.JSONBODY + ).values( + uri.toString(), + etag, + resource + ).execute(); + } else { + this.database.jooq() + .update(SLF_JSONSTORAGE_XDSD) + .set(SLF_JSONSTORAGE_XDSD.ETAG, etag) + .set(SLF_JSONSTORAGE_XDSD.JSONBODY, resource) + .where(SLF_JSONSTORAGE_XDSD.URL.eq(uri.toString())) + .execute(); + } + } } } diff --git a/src/main/java/com/selfxdsd/storage/generated/jooq/Keys.java b/src/main/java/com/selfxdsd/storage/generated/jooq/Keys.java index 70cf2f8..56e907e 100644 --- a/src/main/java/com/selfxdsd/storage/generated/jooq/Keys.java +++ b/src/main/java/com/selfxdsd/storage/generated/jooq/Keys.java @@ -9,6 +9,7 @@ import com.selfxdsd.storage.generated.jooq.tables.SlfContributorsXdsd; import com.selfxdsd.storage.generated.jooq.tables.SlfInvoicedtasksXdsd; import com.selfxdsd.storage.generated.jooq.tables.SlfInvoicesXdsd; +import com.selfxdsd.storage.generated.jooq.tables.SlfJsonstorageXdsd; import com.selfxdsd.storage.generated.jooq.tables.SlfPaymentmethodsXdsd; import com.selfxdsd.storage.generated.jooq.tables.SlfPaymentsXdsd; import com.selfxdsd.storage.generated.jooq.tables.SlfPayoutmethodsXdsd; @@ -24,6 +25,7 @@ import com.selfxdsd.storage.generated.jooq.tables.records.SlfContributorsXdsdRecord; import com.selfxdsd.storage.generated.jooq.tables.records.SlfInvoicedtasksXdsdRecord; import com.selfxdsd.storage.generated.jooq.tables.records.SlfInvoicesXdsdRecord; +import com.selfxdsd.storage.generated.jooq.tables.records.SlfJsonstorageXdsdRecord; import com.selfxdsd.storage.generated.jooq.tables.records.SlfPaymentmethodsXdsdRecord; import com.selfxdsd.storage.generated.jooq.tables.records.SlfPaymentsXdsdRecord; import com.selfxdsd.storage.generated.jooq.tables.records.SlfPayoutmethodsXdsdRecord; @@ -68,6 +70,7 @@ public class Keys { public static final UniqueKey KEY_SLF_CONTRIBUTORS_XDSD_PRIMARY = UniqueKeys0.KEY_SLF_CONTRIBUTORS_XDSD_PRIMARY; public static final UniqueKey KEY_SLF_INVOICEDTASKS_XDSD_PRIMARY = UniqueKeys0.KEY_SLF_INVOICEDTASKS_XDSD_PRIMARY; public static final UniqueKey KEY_SLF_INVOICES_XDSD_PRIMARY = UniqueKeys0.KEY_SLF_INVOICES_XDSD_PRIMARY; + public static final UniqueKey KEY_SLF_JSONSTORAGE_XDSD_PRIMARY = UniqueKeys0.KEY_SLF_JSONSTORAGE_XDSD_PRIMARY; public static final UniqueKey KEY_SLF_PAYMENTMETHODS_XDSD_PRIMARY = UniqueKeys0.KEY_SLF_PAYMENTMETHODS_XDSD_PRIMARY; public static final UniqueKey KEY_SLF_PAYMENTS_XDSD_PRIMARY = UniqueKeys0.KEY_SLF_PAYMENTS_XDSD_PRIMARY; public static final UniqueKey KEY_SLF_PAYOUTMETHODS_XDSD_PRIMARY = UniqueKeys0.KEY_SLF_PAYOUTMETHODS_XDSD_PRIMARY; @@ -116,6 +119,7 @@ private static class UniqueKeys0 { public static final UniqueKey KEY_SLF_CONTRIBUTORS_XDSD_PRIMARY = Internal.createUniqueKey(SlfContributorsXdsd.SLF_CONTRIBUTORS_XDSD, "KEY_slf_contributors_xdsd_PRIMARY", new TableField[] { SlfContributorsXdsd.SLF_CONTRIBUTORS_XDSD.USERNAME, SlfContributorsXdsd.SLF_CONTRIBUTORS_XDSD.PROVIDER }, true); public static final UniqueKey KEY_SLF_INVOICEDTASKS_XDSD_PRIMARY = Internal.createUniqueKey(SlfInvoicedtasksXdsd.SLF_INVOICEDTASKS_XDSD, "KEY_slf_invoicedtasks_xdsd_PRIMARY", new TableField[] { SlfInvoicedtasksXdsd.SLF_INVOICEDTASKS_XDSD.ID }, true); public static final UniqueKey KEY_SLF_INVOICES_XDSD_PRIMARY = Internal.createUniqueKey(SlfInvoicesXdsd.SLF_INVOICES_XDSD, "KEY_slf_invoices_xdsd_PRIMARY", new TableField[] { SlfInvoicesXdsd.SLF_INVOICES_XDSD.INVOICEID, SlfInvoicesXdsd.SLF_INVOICES_XDSD.REPO_FULLNAME, SlfInvoicesXdsd.SLF_INVOICES_XDSD.USERNAME, SlfInvoicesXdsd.SLF_INVOICES_XDSD.PROVIDER, SlfInvoicesXdsd.SLF_INVOICES_XDSD.ROLE }, true); + public static final UniqueKey KEY_SLF_JSONSTORAGE_XDSD_PRIMARY = Internal.createUniqueKey(SlfJsonstorageXdsd.SLF_JSONSTORAGE_XDSD, "KEY_slf_jsonstorage_xdsd_PRIMARY", new TableField[] { SlfJsonstorageXdsd.SLF_JSONSTORAGE_XDSD.URL }, true); public static final UniqueKey KEY_SLF_PAYMENTMETHODS_XDSD_PRIMARY = Internal.createUniqueKey(SlfPaymentmethodsXdsd.SLF_PAYMENTMETHODS_XDSD, "KEY_slf_paymentmethods_xdsd_PRIMARY", new TableField[] { SlfPaymentmethodsXdsd.SLF_PAYMENTMETHODS_XDSD.PAYMENTMETHODID }, true); public static final UniqueKey KEY_SLF_PAYMENTS_XDSD_PRIMARY = Internal.createUniqueKey(SlfPaymentsXdsd.SLF_PAYMENTS_XDSD, "KEY_slf_payments_xdsd_PRIMARY", new TableField[] { SlfPaymentsXdsd.SLF_PAYMENTS_XDSD.INVOICEID, SlfPaymentsXdsd.SLF_PAYMENTS_XDSD.TRANSACTIONID, SlfPaymentsXdsd.SLF_PAYMENTS_XDSD.PAYMENT_TIMESTAMP }, true); public static final UniqueKey KEY_SLF_PAYOUTMETHODS_XDSD_PRIMARY = Internal.createUniqueKey(SlfPayoutmethodsXdsd.SLF_PAYOUTMETHODS_XDSD, "KEY_slf_payoutmethods_xdsd_PRIMARY", new TableField[] { SlfPayoutmethodsXdsd.SLF_PAYOUTMETHODS_XDSD.USERNAME, SlfPayoutmethodsXdsd.SLF_PAYOUTMETHODS_XDSD.PROVIDER, SlfPayoutmethodsXdsd.SLF_PAYOUTMETHODS_XDSD.TYPE }, true); diff --git a/src/main/java/com/selfxdsd/storage/generated/jooq/SelfXdsd.java b/src/main/java/com/selfxdsd/storage/generated/jooq/SelfXdsd.java index a9cddd0..f1a0b02 100644 --- a/src/main/java/com/selfxdsd/storage/generated/jooq/SelfXdsd.java +++ b/src/main/java/com/selfxdsd/storage/generated/jooq/SelfXdsd.java @@ -9,6 +9,7 @@ import com.selfxdsd.storage.generated.jooq.tables.SlfContributorsXdsd; import com.selfxdsd.storage.generated.jooq.tables.SlfInvoicedtasksXdsd; import com.selfxdsd.storage.generated.jooq.tables.SlfInvoicesXdsd; +import com.selfxdsd.storage.generated.jooq.tables.SlfJsonstorageXdsd; import com.selfxdsd.storage.generated.jooq.tables.SlfPaymentmethodsXdsd; import com.selfxdsd.storage.generated.jooq.tables.SlfPaymentsXdsd; import com.selfxdsd.storage.generated.jooq.tables.SlfPayoutmethodsXdsd; @@ -34,7 +35,7 @@ @SuppressWarnings({ "all", "unchecked", "rawtypes" }) public class SelfXdsd extends SchemaImpl { - private static final long serialVersionUID = -858571150; + private static final long serialVersionUID = 1768965955; /** * The reference instance of self_xdsd @@ -66,6 +67,11 @@ public class SelfXdsd extends SchemaImpl { */ public final SlfInvoicesXdsd SLF_INVOICES_XDSD = SlfInvoicesXdsd.SLF_INVOICES_XDSD; + /** + * The table self_xdsd.slf_jsonstorage_xdsd. + */ + public final SlfJsonstorageXdsd SLF_JSONSTORAGE_XDSD = SlfJsonstorageXdsd.SLF_JSONSTORAGE_XDSD; + /** * The table self_xdsd.slf_paymentmethods_xdsd. */ @@ -137,6 +143,7 @@ public final List> getTables() { SlfContributorsXdsd.SLF_CONTRIBUTORS_XDSD, SlfInvoicedtasksXdsd.SLF_INVOICEDTASKS_XDSD, SlfInvoicesXdsd.SLF_INVOICES_XDSD, + SlfJsonstorageXdsd.SLF_JSONSTORAGE_XDSD, SlfPaymentmethodsXdsd.SLF_PAYMENTMETHODS_XDSD, SlfPaymentsXdsd.SLF_PAYMENTS_XDSD, SlfPayoutmethodsXdsd.SLF_PAYOUTMETHODS_XDSD, diff --git a/src/main/java/com/selfxdsd/storage/generated/jooq/Tables.java b/src/main/java/com/selfxdsd/storage/generated/jooq/Tables.java index 1402f1c..bf79075 100644 --- a/src/main/java/com/selfxdsd/storage/generated/jooq/Tables.java +++ b/src/main/java/com/selfxdsd/storage/generated/jooq/Tables.java @@ -9,6 +9,7 @@ import com.selfxdsd.storage.generated.jooq.tables.SlfContributorsXdsd; import com.selfxdsd.storage.generated.jooq.tables.SlfInvoicedtasksXdsd; import com.selfxdsd.storage.generated.jooq.tables.SlfInvoicesXdsd; +import com.selfxdsd.storage.generated.jooq.tables.SlfJsonstorageXdsd; import com.selfxdsd.storage.generated.jooq.tables.SlfPaymentmethodsXdsd; import com.selfxdsd.storage.generated.jooq.tables.SlfPaymentsXdsd; import com.selfxdsd.storage.generated.jooq.tables.SlfPayoutmethodsXdsd; @@ -52,6 +53,11 @@ public class Tables { */ public static final SlfInvoicesXdsd SLF_INVOICES_XDSD = SlfInvoicesXdsd.SLF_INVOICES_XDSD; + /** + * The table self_xdsd.slf_jsonstorage_xdsd. + */ + public static final SlfJsonstorageXdsd SLF_JSONSTORAGE_XDSD = SlfJsonstorageXdsd.SLF_JSONSTORAGE_XDSD; + /** * The table self_xdsd.slf_paymentmethods_xdsd. */ diff --git a/src/main/java/com/selfxdsd/storage/generated/jooq/tables/SlfJsonstorageXdsd.java b/src/main/java/com/selfxdsd/storage/generated/jooq/tables/SlfJsonstorageXdsd.java new file mode 100644 index 0000000..3a10406 --- /dev/null +++ b/src/main/java/com/selfxdsd/storage/generated/jooq/tables/SlfJsonstorageXdsd.java @@ -0,0 +1,146 @@ +/* + * This file is generated by jOOQ. + */ +package com.selfxdsd.storage.generated.jooq.tables; + + +import com.selfxdsd.storage.generated.jooq.Keys; +import com.selfxdsd.storage.generated.jooq.SelfXdsd; +import com.selfxdsd.storage.generated.jooq.tables.records.SlfJsonstorageXdsdRecord; + +import java.util.Arrays; +import java.util.List; + +import org.jooq.Field; +import org.jooq.ForeignKey; +import org.jooq.Name; +import org.jooq.Record; +import org.jooq.Row3; +import org.jooq.Schema; +import org.jooq.Table; +import org.jooq.TableField; +import org.jooq.TableOptions; +import org.jooq.UniqueKey; +import org.jooq.impl.DSL; +import org.jooq.impl.TableImpl; + + +/** + * This class is generated by jOOQ. + */ +@SuppressWarnings({ "all", "unchecked", "rawtypes" }) +public class SlfJsonstorageXdsd extends TableImpl { + + private static final long serialVersionUID = -188559250; + + /** + * The reference instance of self_xdsd.slf_jsonstorage_xdsd + */ + public static final SlfJsonstorageXdsd SLF_JSONSTORAGE_XDSD = new SlfJsonstorageXdsd(); + + /** + * The class holding records for this type + */ + @Override + public Class getRecordType() { + return SlfJsonstorageXdsdRecord.class; + } + + /** + * The column self_xdsd.slf_jsonstorage_xdsd.url. + */ + public final TableField URL = createField(DSL.name("url"), org.jooq.impl.SQLDataType.VARCHAR(512).nullable(false), this, ""); + + /** + * The column self_xdsd.slf_jsonstorage_xdsd.etag. + */ + public final TableField ETAG = createField(DSL.name("etag"), org.jooq.impl.SQLDataType.VARCHAR(2048).nullable(false), this, ""); + + /** + * The column self_xdsd.slf_jsonstorage_xdsd.jsonBody. + */ + public final TableField JSONBODY = createField(DSL.name("jsonBody"), org.jooq.impl.SQLDataType.CLOB.nullable(false), this, ""); + + /** + * Create a self_xdsd.slf_jsonstorage_xdsd table reference + */ + public SlfJsonstorageXdsd() { + this(DSL.name("slf_jsonstorage_xdsd"), null); + } + + /** + * Create an aliased self_xdsd.slf_jsonstorage_xdsd table reference + */ + public SlfJsonstorageXdsd(String alias) { + this(DSL.name(alias), SLF_JSONSTORAGE_XDSD); + } + + /** + * Create an aliased self_xdsd.slf_jsonstorage_xdsd table reference + */ + public SlfJsonstorageXdsd(Name alias) { + this(alias, SLF_JSONSTORAGE_XDSD); + } + + private SlfJsonstorageXdsd(Name alias, Table aliased) { + this(alias, aliased, null); + } + + private SlfJsonstorageXdsd(Name alias, Table aliased, Field[] parameters) { + super(alias, null, aliased, parameters, DSL.comment(""), TableOptions.table()); + } + + public SlfJsonstorageXdsd(Table child, ForeignKey key) { + super(child, key, SLF_JSONSTORAGE_XDSD); + } + + @Override + public Schema getSchema() { + return SelfXdsd.SELF_XDSD; + } + + @Override + public UniqueKey getPrimaryKey() { + return Keys.KEY_SLF_JSONSTORAGE_XDSD_PRIMARY; + } + + @Override + public List> getKeys() { + return Arrays.>asList(Keys.KEY_SLF_JSONSTORAGE_XDSD_PRIMARY); + } + + @Override + public SlfJsonstorageXdsd as(String alias) { + return new SlfJsonstorageXdsd(DSL.name(alias), this); + } + + @Override + public SlfJsonstorageXdsd as(Name alias) { + return new SlfJsonstorageXdsd(alias, this); + } + + /** + * Rename this table + */ + @Override + public SlfJsonstorageXdsd rename(String name) { + return new SlfJsonstorageXdsd(DSL.name(name), null); + } + + /** + * Rename this table + */ + @Override + public SlfJsonstorageXdsd rename(Name name) { + return new SlfJsonstorageXdsd(name, null); + } + + // ------------------------------------------------------------------------- + // Row3 type methods + // ------------------------------------------------------------------------- + + @Override + public Row3 fieldsRow() { + return (Row3) super.fieldsRow(); + } +} diff --git a/src/main/java/com/selfxdsd/storage/generated/jooq/tables/records/SlfJsonstorageXdsdRecord.java b/src/main/java/com/selfxdsd/storage/generated/jooq/tables/records/SlfJsonstorageXdsdRecord.java new file mode 100644 index 0000000..c36a997 --- /dev/null +++ b/src/main/java/com/selfxdsd/storage/generated/jooq/tables/records/SlfJsonstorageXdsdRecord.java @@ -0,0 +1,181 @@ +/* + * This file is generated by jOOQ. + */ +package com.selfxdsd.storage.generated.jooq.tables.records; + + +import com.selfxdsd.storage.generated.jooq.tables.SlfJsonstorageXdsd; + +import org.jooq.Field; +import org.jooq.Record1; +import org.jooq.Record3; +import org.jooq.Row3; +import org.jooq.impl.UpdatableRecordImpl; + + +/** + * This class is generated by jOOQ. + */ +@SuppressWarnings({ "all", "unchecked", "rawtypes" }) +public class SlfJsonstorageXdsdRecord extends UpdatableRecordImpl implements Record3 { + + private static final long serialVersionUID = -1842010205; + + /** + * Setter for self_xdsd.slf_jsonstorage_xdsd.url. + */ + public void setUrl(String value) { + set(0, value); + } + + /** + * Getter for self_xdsd.slf_jsonstorage_xdsd.url. + */ + public String getUrl() { + return (String) get(0); + } + + /** + * Setter for self_xdsd.slf_jsonstorage_xdsd.etag. + */ + public void setEtag(String value) { + set(1, value); + } + + /** + * Getter for self_xdsd.slf_jsonstorage_xdsd.etag. + */ + public String getEtag() { + return (String) get(1); + } + + /** + * Setter for self_xdsd.slf_jsonstorage_xdsd.jsonBody. + */ + public void setJsonbody(String value) { + set(2, value); + } + + /** + * Getter for self_xdsd.slf_jsonstorage_xdsd.jsonBody. + */ + public String getJsonbody() { + return (String) get(2); + } + + // ------------------------------------------------------------------------- + // Primary key information + // ------------------------------------------------------------------------- + + @Override + public Record1 key() { + return (Record1) super.key(); + } + + // ------------------------------------------------------------------------- + // Record3 type implementation + // ------------------------------------------------------------------------- + + @Override + public Row3 fieldsRow() { + return (Row3) super.fieldsRow(); + } + + @Override + public Row3 valuesRow() { + return (Row3) super.valuesRow(); + } + + @Override + public Field field1() { + return SlfJsonstorageXdsd.SLF_JSONSTORAGE_XDSD.URL; + } + + @Override + public Field field2() { + return SlfJsonstorageXdsd.SLF_JSONSTORAGE_XDSD.ETAG; + } + + @Override + public Field field3() { + return SlfJsonstorageXdsd.SLF_JSONSTORAGE_XDSD.JSONBODY; + } + + @Override + public String component1() { + return getUrl(); + } + + @Override + public String component2() { + return getEtag(); + } + + @Override + public String component3() { + return getJsonbody(); + } + + @Override + public String value1() { + return getUrl(); + } + + @Override + public String value2() { + return getEtag(); + } + + @Override + public String value3() { + return getJsonbody(); + } + + @Override + public SlfJsonstorageXdsdRecord value1(String value) { + setUrl(value); + return this; + } + + @Override + public SlfJsonstorageXdsdRecord value2(String value) { + setEtag(value); + return this; + } + + @Override + public SlfJsonstorageXdsdRecord value3(String value) { + setJsonbody(value); + return this; + } + + @Override + public SlfJsonstorageXdsdRecord values(String value1, String value2, String value3) { + value1(value1); + value2(value2); + value3(value3); + return this; + } + + // ------------------------------------------------------------------------- + // Constructors + // ------------------------------------------------------------------------- + + /** + * Create a detached SlfJsonstorageXdsdRecord + */ + public SlfJsonstorageXdsdRecord() { + super(SlfJsonstorageXdsd.SLF_JSONSTORAGE_XDSD); + } + + /** + * Create a detached, initialised SlfJsonstorageXdsdRecord + */ + public SlfJsonstorageXdsdRecord(String url, String etag, String jsonbody) { + super(SlfJsonstorageXdsd.SLF_JSONSTORAGE_XDSD); + + set(0, url); + set(1, etag); + set(2, jsonbody); + } +} diff --git a/src/test/java/com/selfxdsd/storage/SelfJsonStorageITCase.java b/src/test/java/com/selfxdsd/storage/SelfJsonStorageITCase.java new file mode 100644 index 0000000..38572e2 --- /dev/null +++ b/src/test/java/com/selfxdsd/storage/SelfJsonStorageITCase.java @@ -0,0 +1,187 @@ +/** + * Copyright (c) 2020-2021, Self XDSD Contributors + * All rights reserved. + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), + * to read the Software only. Permission is hereby NOT GRANTED to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software. + *

+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package com.selfxdsd.storage; + +import com.selfxdsd.api.storage.JsonStorage; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; +import org.mockito.Mockito; + +import java.net.URI; +import java.util.UUID; + +/** + * Integration tests for {@link SelfJsonStorage}. + * @author Mihai Andronache (amihaiemil@gmail.com) + * @since 0.0.70 + * @version $Id$ + */ +public final class SelfJsonStorageITCase { + + /** + * Returns found Etag based on URL. + */ + @Test + public void returnsFoundETag() { + final JsonStorage jsonStorage = new SelfJooq(new H2Database()) + .jsonStorage(); + MatcherAssert.assertThat( + jsonStorage.getEtag( + URI.create( + "https://github.com/self-xdsd/self-storage/issues/123" + ) + ), + Matchers.equalTo("etag123321") + ); + } + + /** + * Returns null etag if no record is found for the given URL. + */ + @Test + public void returnsNullMissingEtag() { + final JsonStorage jsonStorage = new SelfJooq(new H2Database()) + .jsonStorage(); + MatcherAssert.assertThat( + jsonStorage.getEtag( + URI.create( + "https://github.com/self-xdsd/self-storage/issues/999" + ) + ), + Matchers.nullValue() + ); + } + + /** + * Returns found body based on URL. + */ + @Test + public void returnsFoundBody() { + final JsonStorage jsonStorage = new SelfJooq(new H2Database()) + .jsonStorage(); + MatcherAssert.assertThat( + jsonStorage.getResourceBody( + URI.create( + "https://github.com/self-xdsd/self-storage/issues/123" + ) + ), + Matchers.equalTo("{\"issueId\":\"123\"}") + ); + } + + /** + * Returns null body if no record is found for the given URL. + */ + @Test + public void returnsNullMissingBody() { + final JsonStorage jsonStorage = new SelfJooq(new H2Database()) + .jsonStorage(); + MatcherAssert.assertThat( + jsonStorage.getResourceBody( + URI.create( + "https://github.com/self-xdsd/self-storage/issues/999" + ) + ), + Matchers.nullValue() + ); + } + + /** + * Method store doesn't insert/update anything if the etag and body are + * blank. + */ + @Test + public void storeIgnoresBlankEtagAndBody() { + final Database mockDb = Mockito.mock(Database.class); + final JsonStorage jsonStorage = new SelfJooq(mockDb) + .jsonStorage(); + jsonStorage.store(URI.create("https://github.com"), "", ""); + Mockito.verify( + mockDb, Mockito.times(0) + ).jooq(); + } + + /** + * Method store inserts a new resource. + */ + @Test + public void storesNewResource() { + final JsonStorage jsonStorage = new SelfJooq(new H2Database()) + .jsonStorage(); + final String issueId = UUID.randomUUID().toString(); + final URI uri = URI.create( + "httos://github.com/self-xdsd/self-storage/issues/" + issueId + ); + + jsonStorage.store( + uri, "etag456654", "{\"issueId\":\"" + issueId + "\"}" + ); + + MatcherAssert.assertThat( + jsonStorage.getEtag(uri), + Matchers.equalTo("etag456654") + ); + } + + /** + * Method store updates an existing resource. + */ + @Test + public void updatesExistingResource() { + final JsonStorage jsonStorage = new SelfJooq(new H2Database()) + .jsonStorage(); + final String issueId = UUID.randomUUID().toString(); + final URI uri = URI.create( + "httos://github.com/self-xdsd/self-storage/issues/" + issueId + ); + + jsonStorage.store( + uri, "etag456654", "{\"body\":\"some body\"}" + ); + + MatcherAssert.assertThat( + jsonStorage.getEtag(uri), + Matchers.equalTo("etag456654") + ); + + MatcherAssert.assertThat( + jsonStorage.getResourceBody(uri), + Matchers.equalTo("{\"body\":\"some body\"}") + ); + + jsonStorage.store( + uri, "etag789987", "{\"body\":\"another body\"}" + ); + + MatcherAssert.assertThat( + jsonStorage.getEtag(uri), + Matchers.equalTo("etag789987") + ); + + MatcherAssert.assertThat( + jsonStorage.getResourceBody(uri), + Matchers.equalTo("{\"body\":\"another body\"}") + ); + } +} diff --git a/src/test/resources/createDb.sql b/src/test/resources/createDb.sql index 473d581..12517e2 100644 --- a/src/test/resources/createDb.sql +++ b/src/test/resources/createDb.sql @@ -264,4 +264,14 @@ CREATE TABLE `self_xdsd`.`slf_apitokens_xdsd` ( CONSTRAINT `ownerUser` FOREIGN KEY (`username` , `provider`) REFERENCES `self_xdsd`.`slf_users_xdsd` (`username` , `provider`) - ON DELETE CASCADE); \ No newline at end of file + ON DELETE CASCADE); + +-- ----------------------------------------------------- +-- Table `self_xdsd`.`slf_jsonstorage_xdsd` +-- ----------------------------------------------------- + +CREATE TABLE `self_xdsd`.`slf_jsonstorage_xdsd` ( + `url` VARCHAR(512) NOT NULL, + `etag` VARCHAR(2048) NOT NULL, + `jsonBody` LONGTEXT NOT NULL, + PRIMARY KEY (`url`)); diff --git a/src/test/resources/insertTestData.sql b/src/test/resources/insertTestData.sql index 240fde2..6f14f21 100644 --- a/src/test/resources/insertTestData.sql +++ b/src/test/resources/insertTestData.sql @@ -384,4 +384,7 @@ VALUES (1, '2021-01-09', 'mihai', 130, 19, 'transaction123', '2021-01-09', 487); INSERT INTO `self_xdsd`.`slf_platforminvoices_xdsd` (`id`, `createdAt`, `billedTo`, `commission`, `vat`, `transactionId`, `payment_timestamp`, `invoiceId`, `eurToRon`) -VALUES (2, '2021-01-09', 'vlad', 130, 19, 'transactionIdHere', '2021-01-09', 4, 487); \ No newline at end of file +VALUES (2, '2021-01-09', 'vlad', 130, 19, 'transactionIdHere', '2021-01-09', 4, 487); + +INSERT INTO `self_xdsd`.`slf_jsonstorage_xdsd` (`url`, `etag`, `jsonBody`) +VALUES ('https://github.com/self-xdsd/self-storage/issues/123', 'etag123321', '{"issueId":"123"}'); \ No newline at end of file