From 8f9348bbd6c7a4cc20e677642d574e79baf4c6ad Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Wed, 15 Aug 2012 18:00:34 -0700 Subject: [PATCH] CSPACE-5271: Added rudimentary test class for the URI template registry. --- .../common/test/UriTemplateRegistryTest.java | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateRegistryTest.java 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 new file mode 100644 index 000000000..165e73fd4 --- /dev/null +++ b/services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateRegistryTest.java @@ -0,0 +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()); + + } + } +} -- 2.47.3