From 1c48c052a937c502a5e08e881ffeaf373521ddc2 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 8 Aug 2019 13:16:00 +0200 Subject: [PATCH] Update for foreign key for search MODINVSTOR-339 / RMB 26.3.1 --- pom.xml | 2 +- .../folio/rest/impl/InstanceStorageAPI.java | 36 -------- .../templates/db_scripts/schema.json | 2 + .../folio/rest/api/InstanceStorageTest.java | 14 +-- .../org/folio/rest/api/StorageTestSuite.java | 2 - .../rest/impl/InstanceStorageAPITest.java | 86 ------------------- 6 files changed, 10 insertions(+), 132 deletions(-) delete mode 100644 src/test/java/org/folio/rest/impl/InstanceStorageAPITest.java diff --git a/pom.xml b/pom.xml index b55376967..31f0c5ab4 100644 --- a/pom.xml +++ b/pom.xml @@ -60,7 +60,7 @@ UTF-8 UTF-8 ${basedir}/ramls/ - 26.2.4 + 26.3.1 diff --git a/src/main/java/org/folio/rest/impl/InstanceStorageAPI.java b/src/main/java/org/folio/rest/impl/InstanceStorageAPI.java index 517a42aa4..08c22a1b2 100644 --- a/src/main/java/org/folio/rest/impl/InstanceStorageAPI.java +++ b/src/main/java/org/folio/rest/impl/InstanceStorageAPI.java @@ -44,48 +44,12 @@ public class InstanceStorageAPI implements InstanceStorage { // lower case headers private static final String TENANT_HEADER = "x-okapi-tenant"; public static final String MODULE = "mod_inventory_storage"; - public static final String INSTANCE_HOLDINGS_VIEW = "instance_holding_view"; - public static final String INSTANCE_HOLDINGS_ITEMS_VIEW = "instance_holding_item_view"; public static final String INSTANCE_TABLE = "instance"; private static final String INSTANCE_SOURCE_MARC_TABLE = "instance_source_marc"; private static final String INSTANCE_RELATIONSHIP_TABLE = "instance_relationship"; private final Messages messages = Messages.getInstance(); PreparedCQL handleCQL(String query, int limit, int offset) throws FieldException { - boolean containsHoldingsRecordProperties = query != null && query.contains("holdingsRecords."); - boolean containsItemsRecordProperties = query != null && query.contains("item."); - - if(containsItemsRecordProperties && containsHoldingsRecordProperties) { - //it_jsonb is the alias given items in the view in the DB - query = query.replaceAll("item\\.", INSTANCE_HOLDINGS_ITEMS_VIEW+".it_jsonb."); - - //ho_jsonb is the alias given holdings in the view in the DB - query = query.replaceAll("holdingsRecords\\.", INSTANCE_HOLDINGS_ITEMS_VIEW+".ho_jsonb."); - - return new PreparedCQL(INSTANCE_HOLDINGS_ITEMS_VIEW, query, limit, offset, Arrays.asList( - INSTANCE_HOLDINGS_ITEMS_VIEW + ".jsonb", - INSTANCE_HOLDINGS_ITEMS_VIEW + ".it_jsonb", - INSTANCE_HOLDINGS_ITEMS_VIEW + ".ho_jsonb"), INSTANCE_TABLE + ".jsonb"); - } - - if(containsItemsRecordProperties) { - //it_jsonb is the alias given items in the view in the DB - query = query.replaceAll("item\\.", INSTANCE_HOLDINGS_ITEMS_VIEW+".it_jsonb."); - - return new PreparedCQL(INSTANCE_HOLDINGS_ITEMS_VIEW, query, limit, offset, Arrays.asList( - INSTANCE_HOLDINGS_ITEMS_VIEW + ".jsonb", - INSTANCE_HOLDINGS_ITEMS_VIEW + ".it_jsonb"), INSTANCE_TABLE + ".jsonb"); - } - - if(containsHoldingsRecordProperties) { - //ho_jsonb is the alias given holdings in the view in the DB - query = query.replaceAll("holdingsRecords\\.", INSTANCE_HOLDINGS_VIEW+".ho_jsonb."); - - return new PreparedCQL(INSTANCE_HOLDINGS_VIEW, query, limit, offset, Arrays.asList( - INSTANCE_HOLDINGS_VIEW+".jsonb", - INSTANCE_HOLDINGS_VIEW + ".ho_jsonb"), INSTANCE_TABLE + ".jsonb"); - } - return new PreparedCQL(INSTANCE_TABLE, query, limit, offset, Arrays.asList(INSTANCE_TABLE + ".jsonb"), INSTANCE_TABLE + ".jsonb"); } diff --git a/src/main/resources/templates/db_scripts/schema.json b/src/main/resources/templates/db_scripts/schema.json index 83bc08b7a..6bf850021 100644 --- a/src/main/resources/templates/db_scripts/schema.json +++ b/src/main/resources/templates/db_scripts/schema.json @@ -632,6 +632,7 @@ { "fieldName": "instanceId", "targetTable": "instance", + "tableAlias" : "holdingsRecords", "tOps": "ADD" }, { @@ -721,6 +722,7 @@ { "fieldName": "holdingsRecordId", "targetTable": "holdings_record", + "targetTableAlias" : "holdingsRecords", "tOps": "ADD" }, { diff --git a/src/test/java/org/folio/rest/api/InstanceStorageTest.java b/src/test/java/org/folio/rest/api/InstanceStorageTest.java index e14c8dd0c..dbd734ce0 100644 --- a/src/test/java/org/folio/rest/api/InstanceStorageTest.java +++ b/src/test/java/org/folio/rest/api/InstanceStorageTest.java @@ -1325,12 +1325,12 @@ public void testCrossTableQueries() throws Exception { createHoldings(jho3); ////////////////////////done ////////////////////////////////////// - String url1 = url+URLEncoder.encode("title=Long Way to a Small Angry Planet* sortBy holdingsRecords.permanentLocationId/sort.descending title", "UTF-8"); - String url2 = url+URLEncoder.encode("title=cql.allRecords=1 sortBy holdingsRecords.permanentLocationId/sort.ascending", "UTF-8"); - String url3 = url+URLEncoder.encode("holdingsRecords.permanentLocationId=99999999-dee7-48eb-b03f-d02fdf0debd0 sortBy holdingsRecords.permanentLocationId/sort.descending title", "UTF-8"); - String url4 = url+URLEncoder.encode("title=cql.allRecords=1 sortby holdingsRecords.permanentLocationId title", "UTF-8"); - String url5 = url+URLEncoder.encode("title=cql.allRecords=1 and holdingsRecords.permanentLocationId=99999999-dee7-48eb-b03f-d02fdf0debd0 " - + "sortby holdingsRecords.permanentLocationId", "UTF-8"); + String url1 = url+ URLEncoder.encode("title=Long Way to a Small Angry Planet* sortby title", "UTF-8"); + String url2 = url+ URLEncoder.encode("title=cql.allRecords=1 sortBy title", "UTF-8"); + String url3 = url+ URLEncoder.encode("holdingsRecords.permanentLocationId=99999999-dee7-48eb-b03f-d02fdf0debd0 sortBy title", "UTF-8"); + String url4 = url+ URLEncoder.encode("title=cql.allRecords=1 sortby title", "UTF-8"); + String url5 = url+ URLEncoder.encode("title=cql.allRecords=1 and holdingsRecords.permanentLocationId=99999999-dee7-48eb-b03f-d02fdf0debd0 " + + "sortby title", "UTF-8"); //non existant - 0 results String url6 = url+URLEncoder.encode("title=cql.allRecords=1 and holdingsRecords.permanentLocationId=abc* sortby holdingsRecords.permanentLocationId", "UTF-8"); @@ -1357,7 +1357,7 @@ public void testCrossTableQueries() throws Exception { if(i==0){ assertThat(3, is(cqlResponse.getJson().getInteger("totalRecords"))); - assertThat("TEST2" , is(cqlResponse.getJson().getJsonArray("instances").getJsonObject(0).getString("source"))); + assertThat("TEST1" , is(cqlResponse.getJson().getJsonArray("instances").getJsonObject(0).getString("source"))); } else if(i==1){ assertThat(3, is(cqlResponse.getJson().getInteger("totalRecords"))); assertThat("TEST1" , is(cqlResponse.getJson().getJsonArray("instances").getJsonObject(0).getString("source"))); diff --git a/src/test/java/org/folio/rest/api/StorageTestSuite.java b/src/test/java/org/folio/rest/api/StorageTestSuite.java index 6983ce691..60097e8fd 100644 --- a/src/test/java/org/folio/rest/api/StorageTestSuite.java +++ b/src/test/java/org/folio/rest/api/StorageTestSuite.java @@ -12,7 +12,6 @@ import java.util.concurrent.TimeoutException; import org.folio.rest.RestVerticle; -import org.folio.rest.impl.InstanceStorageAPITest; import org.folio.rest.impl.StorageHelperTest; import org.folio.rest.persist.PostgresClient; import org.folio.rest.support.HttpClient; @@ -35,7 +34,6 @@ @Suite.SuiteClasses({ InstanceStorageTest.class, - InstanceStorageAPITest.class, HoldingsStorageTest.class, ItemStorageTest.class, LoanTypeTest.class, diff --git a/src/test/java/org/folio/rest/impl/InstanceStorageAPITest.java b/src/test/java/org/folio/rest/impl/InstanceStorageAPITest.java deleted file mode 100644 index 4846eb7c3..000000000 --- a/src/test/java/org/folio/rest/impl/InstanceStorageAPITest.java +++ /dev/null @@ -1,86 +0,0 @@ -package org.folio.rest.impl; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import org.folio.rest.impl.InstanceStorageAPI.PreparedCQL; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.Timeout; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnit; -import org.mockito.junit.MockitoRule; -import org.mockito.quality.Strictness; -import org.folio.cql2pgjson.exception.FieldException; -import io.vertx.ext.unit.junit.VertxUnitRunner; - -/** - * @see org.folio.rest.api.InstanceStorageTest - */ -@RunWith(VertxUnitRunner.class) -public class InstanceStorageAPITest { - @Rule - public MockitoRule mockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); - @Rule - public Timeout timeoutRule = Timeout.seconds(5); - - private InstanceStorageAPI instanceStorageApi = new InstanceStorageAPI(); - - private void handleCQL(String cql, String sql, String table) { - try { - PreparedCQL preparedCql = instanceStorageApi.handleCQL(cql, 1, 0); - String actualSql = preparedCql.getCqlWrapper().toString(); - actualSql = actualSql - .substring(" WHERE ".length(), actualSql.length() - " LIMIT 1 OFFSET 0".length()) - .replace("(", "").replace(")", ""); - assertThat(actualSql, is(sql)); - assertThat(preparedCql.getTableName(), is(table)); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - @Test - public void handleCQLBasic() throws FieldException { - handleCQL("a=\"\"", "instance.jsonb->>'a' ~ ''", "instance"); - handleCQL("holdingsRecords.a=\"\"", "instance_holding_view.ho_jsonb->>'a' ~ ''", "instance_holding_view"); - handleCQL("item.a=\"\"", "instance_holding_item_view.it_jsonb->>'a' ~ ''", "instance_holding_item_view"); - } - - @Test - public void handleCQLBasic2() throws FieldException { - handleCQLBasic(); - handleCQLBasic(); - } - - @Test - public void handleCQLMulti1() throws FieldException { - handleCQL("holdingsRecords.a=\"\" AND item.b=\"\"", - "instance_holding_item_view.ho_jsonb->>'a' ~ '' AND " - + "instance_holding_item_view.it_jsonb->>'b' ~ ''", - "instance_holding_item_view"); - } - - @Test - public void handleCQLMulti2() throws FieldException { - handleCQL("a=\"\" AND holdingsRecords.b=\"\" AND item.c=\"\" AND " - + "d=\"\" AND holdingsRecords.e=\"\" AND item.f=\"\"", - "instance_holding_item_view.jsonb->>'a' ~ '' AND " - + "instance_holding_item_view.ho_jsonb->>'b' ~ '' AND " - + "instance_holding_item_view.it_jsonb->>'c' ~ '' AND " - + "instance_holding_item_view.jsonb->>'d' ~ '' AND " - + "instance_holding_item_view.ho_jsonb->>'e' ~ '' AND " - + "instance_holding_item_view.it_jsonb->>'f' ~ ''", - "instance_holding_item_view"); - } - - @Test - public void handleCQLMulti3() throws FieldException { - handleCQL("holdingsRecords.a=\"\" AND b=\"\" AND " - + "holdingsRecords.c=\"\" AND d=\"\"", - "instance_holding_view.ho_jsonb->>'a' ~ '' AND " - + "instance_holding_view.jsonb->>'b' ~ '' AND " - + "instance_holding_view.ho_jsonb->>'c' ~ '' AND " - + "instance_holding_view.jsonb->>'d' ~ ''", - "instance_holding_view"); - } -}