From f60edf1179900e5ba7a3587e0cd7aab70f78dbf4 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Wed, 15 Aug 2012 18:06:33 -0700 Subject: [PATCH] CSPACE-5271,CSPACE-5453: RefObjs code now uses URI Template Registry. --- .../common/UriTemplateRegistryKey.java | 8 +- .../vocabulary/RefNameServiceUtils.java | 9 +- .../common/test/UriTemplateRegistryTest.java | 206 +++++++++--------- 3 files changed, 113 insertions(+), 110 deletions(-) diff --git a/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistryKey.java b/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistryKey.java index f7e85bd67..a0560dce9 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistryKey.java +++ b/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistryKey.java @@ -63,9 +63,11 @@ public class UriTemplateRegistryKey { // Cast the compared-to object to an object of this type UriTemplateRegistryKey key = (UriTemplateRegistryKey) o; - // If either the tenant ID or docType values dont't match, whether - // only one is null, or via a failure of an 'equals' test of their - // values, return false + // FIXME: Look into possible case-sensitivity issues for registry keys - ADR 2012-08-15 + + // If either the tenant ID or docType values don't match, + // whether only one is null, or via a failure of an 'equals' + // test of their values, return false if (tenantId == null ? key.tenantId != null : !tenantId.equals(key.tenantId)) { return false; } diff --git a/services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameServiceUtils.java b/services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameServiceUtils.java index cc9aa87a9..265e969bf 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameServiceUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameServiceUtils.java @@ -573,10 +573,11 @@ public class RefNameServiceUtils { ilistItem = new AuthorityRefDocList.AuthorityRefDocItem(); String csid = NuxeoUtils.getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString()); ilistItem.setDocId(csid); - UriTemplateRegistry uriTemplateRegistry = ServiceMain.getInstance().getUriTemplateRegistry(); - StoredValuesUriTemplate storedValuesResourceTemplate = uriTemplateRegistry.get(new UriTemplateRegistryKey(docType, tenantId)); + UriTemplateRegistry registry = ServiceMain.getInstance().getUriTemplateRegistry(); + UriTemplateRegistryKey key = new UriTemplateRegistryKey(tenantId, docType); + StoredValuesUriTemplate template = registry.get(key); Map additionalValues = new HashMap(); - if (storedValuesResourceTemplate.getUriTemplateType() == UriTemplateFactory.ITEM) { + if (template.getUriTemplateType() == UriTemplateFactory.ITEM) { try { String inAuthorityCsid = (String) docModel.getPropertyValue("inAuthority"); // AuthorityItemJAXBSchema.IN_AUTHORITY additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, inAuthorityCsid); @@ -587,7 +588,7 @@ public class RefNameServiceUtils { } else { additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, csid); } - String uriStr = storedValuesResourceTemplate.buildUri(additionalValues); + String uriStr = template.buildUri(additionalValues); ilistItem.setUri(uriStr); try { ilistItem.setWorkflowState(docModel.getCurrentLifeCycleState()); diff --git a/services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateRegistryTest.java b/services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateRegistryTest.java index 165e73fd4..90c5a8c5c 100644 --- a/services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateRegistryTest.java +++ b/services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateRegistryTest.java @@ -1,103 +1,103 @@ -/** - * This document is a part of the source code and related artifacts for - * CollectionSpace, an open source collections management system for museums and - * related institutions: - * - * http://www.collectionspace.org http://wiki.collectionspace.org - * - * Copyright (c) 2012 Regents of the University of California - * - * Licensed under the Educational Community License (ECL), Version 2.0. You may - * not use this file except in compliance with this License. - * - * You may obtain a copy of the ECL 2.0 License at - * https://source.collectionspace.org/collection-space/LICENSE.txt - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package org.collectionspace.services.common.test; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import org.collectionspace.services.common.ServiceMain; -import org.collectionspace.services.common.StoredValuesUriTemplate; -import org.collectionspace.services.common.UriTemplateFactory; -import org.collectionspace.services.common.UriTemplateRegistry; -import org.collectionspace.services.common.UriTemplateRegistryKey; -import org.collectionspace.services.common.api.Tools; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.BeforeSuite; -import org.testng.annotations.Test; - -public class UriTemplateRegistryTest { - - private static final Logger logger = LoggerFactory.getLogger(UriTemplateRegistryTest.class); - UriTemplateRegistry registry; - final static String TEST_TENANT_ID = "1"; - final static String TEST_DOCTYPE_NAME = "Doctype"; - final static String TEST_TEMPATE = "/doctypes"; - final static UriTemplateFactory.UriTemplateType TEST_URI_TEMPLATE_TYPE = - UriTemplateFactory.RESOURCE; - - private void testBanner(String msg) { - String BANNER = "-------------------------------------------------------"; - logger.debug("\r" + BANNER + "\r\n" + this.getClass().getName() + "\r\n" + msg + "\r\n" + BANNER); - } - - /** - * Create a test entry in the registry. - */ - @BeforeSuite - private void setUp() { - UriTemplateRegistryKey key = new UriTemplateRegistryKey(TEST_TENANT_ID, TEST_DOCTYPE_NAME); - Map storedValues = new HashMap(); - StoredValuesUriTemplate template = new StoredValuesUriTemplate(TEST_URI_TEMPLATE_TYPE, TEST_TEMPATE, storedValues); - registry = new UriTemplateRegistry(); - registry.put(key, template); - } - - @Test - public void registryContainsEntries() { - testBanner("registryContainsEntries"); - Assert.assertNotNull(registry); - Assert.assertFalse(registry.isEmpty()); - } - - /** - * Identify a valid entry in the registry, then use its key to successfully - * retrieve the entry once again. - */ - @Test(dependsOnMethods = {"registryContainsEntries"}) - public void getRegistryEntryByKey() { - testBanner("getRegistryEntryByKey"); - UriTemplateRegistryKey key; - StoredValuesUriTemplate template; - boolean hasValidKey = false; - boolean hasValidTemplate = false; - for (Map.Entry entry : registry.entrySet()) { - key = entry.getKey(); - template = entry.getValue(); - if (key != null && Tools.notBlank(key.getTenantId()) && Tools.notBlank(key.getDocType())) { - hasValidKey = true; - } - if (template != null && template.getUriTemplateType() != null && Tools.notBlank(template.toString())) { - hasValidTemplate = true; - } - if (hasValidKey && hasValidTemplate) { - break; - } - Assert.assertTrue(hasValidKey && hasValidTemplate); - StoredValuesUriTemplate retrievedTemplate = registry.get(key); - Assert.assertNotNull(retrievedTemplate); - Assert.assertEquals(template.toString(), retrievedTemplate.toString()); - - } - } -} +/** + * This document is a part of the source code and related artifacts for + * CollectionSpace, an open source collections management system for museums and + * related institutions: + * + * http://www.collectionspace.org http://wiki.collectionspace.org + * + * Copyright (c) 2012 Regents of the University of California + * + * Licensed under the Educational Community License (ECL), Version 2.0. You may + * not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * https://source.collectionspace.org/collection-space/LICENSE.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.collectionspace.services.common.test; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import org.collectionspace.services.common.ServiceMain; +import org.collectionspace.services.common.StoredValuesUriTemplate; +import org.collectionspace.services.common.UriTemplateFactory; +import org.collectionspace.services.common.UriTemplateRegistry; +import org.collectionspace.services.common.UriTemplateRegistryKey; +import org.collectionspace.services.common.api.Tools; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.Assert; +import org.testng.annotations.BeforeSuite; +import org.testng.annotations.Test; + +public class UriTemplateRegistryTest { + + private static final Logger logger = LoggerFactory.getLogger(UriTemplateRegistryTest.class); + UriTemplateRegistry registry; + final static String TEST_TENANT_ID = "1"; + final static String TEST_DOCTYPE_NAME = "Doctype"; + final static String TEST_TEMPATE = "/doctypes"; + final static UriTemplateFactory.UriTemplateType TEST_URI_TEMPLATE_TYPE = + UriTemplateFactory.RESOURCE; + + private void testBanner(String msg) { + String BANNER = "-------------------------------------------------------"; + logger.debug("\r" + BANNER + "\r\n" + this.getClass().getName() + "\r\n" + msg + "\r\n" + BANNER); + } + + /** + * Create a test entry in the registry. + */ + @BeforeSuite + private void setUp() { + UriTemplateRegistryKey key = new UriTemplateRegistryKey(TEST_TENANT_ID, TEST_DOCTYPE_NAME); + Map storedValues = new HashMap(); + StoredValuesUriTemplate template = new StoredValuesUriTemplate(TEST_URI_TEMPLATE_TYPE, TEST_TEMPATE, storedValues); + registry = new UriTemplateRegistry(); + registry.put(key, template); + } + + @Test + public void registryContainsEntries() { + testBanner("registryContainsEntries"); + Assert.assertNotNull(registry); + Assert.assertFalse(registry.isEmpty()); + } + + /** + * Identify a valid entry in the registry, then use its key to successfully + * retrieve the entry once again. + */ + @Test(dependsOnMethods = {"registryContainsEntries"}) + public void getRegistryEntryByKey() { + testBanner("getRegistryEntryByKey"); + UriTemplateRegistryKey key; + StoredValuesUriTemplate template; + boolean hasValidKey = false; + boolean hasValidTemplate = false; + for (Map.Entry entry : registry.entrySet()) { + key = entry.getKey(); + template = entry.getValue(); + if (key != null && Tools.notBlank(key.getTenantId()) && Tools.notBlank(key.getDocType())) { + hasValidKey = true; + } + if (template != null && template.getUriTemplateType() != null && Tools.notBlank(template.toString())) { + hasValidTemplate = true; + } + if (hasValidKey && hasValidTemplate) { + break; + } + Assert.assertTrue(hasValidKey && hasValidTemplate); + StoredValuesUriTemplate retrievedTemplate = registry.get(key); + Assert.assertNotNull(retrievedTemplate); + Assert.assertEquals(template.toString(), retrievedTemplate.toString()); + + } + } +} -- 2.47.3