Search in sources :

Example 1 with RegistryChangeListener

use of org.openhab.core.common.registry.RegistryChangeListener in project openhab-addons by openhab.

the class BaseIntegrationTest method newService.

/**
 * Create new persistence service. Either pointing to real DynamoDB (given credentials as java properties) or local
 * in-memory server
 *
 * @param legacy whether to create config that implies legacy or new schema. Use null for MAYBE_LEGACY
 * @param cleanLocal when creating local DB, whether to create new DB
 * @param overrideLocalURI URI to use when using local DB
 * @param table
 * @param tablePrefix
 * @return new persistence service
 */
protected static synchronized DynamoDBPersistenceService newService(@Nullable Boolean legacy, boolean cleanLocal, @Nullable URI overrideLocalURI, @Nullable String table, @Nullable String tablePrefix) {
    final DynamoDBPersistenceService service;
    Map<String, Object> config = getConfig(legacy, table, tablePrefix);
    if (cleanLocal && overrideLocalURI != null) {
        throw new IllegalArgumentException("cannot specify both cleanLocal=true and overrideLocalURI");
    }
    if (legacy == null && (table != null || tablePrefix != null)) {
        throw new IllegalArgumentException("cannot specify both legacy=null and unambiguous table configuration");
    }
    URI localEndpointOverride = overrideLocalURI == null ? endpointOverride : overrideLocalURI;
    if (overrideLocalURI == null && !credentialsSet() && (cleanLocal || endpointOverride == null)) {
        // Local server not started yet, start it
        // endpointOverride static field has the URI
        LOGGER.info("Since credentials have not been defined, using embedded local AWS DynamoDB server");
        System.setProperty("sqlite4java.library.path", "src/test/resources/native-libs");
        int port = findFreeTCPPort();
        String endpoint = String.format("http://127.0.0.1:%d", port);
        try {
            localEndpointOverride = new URI(endpoint);
            DynamoDBProxyServer localEmbeddedServer = ServerRunner.createServerFromCommandLineArgs(new String[] { "-inMemory", "-port", String.valueOf(port) });
            localEmbeddedServer.start();
            embeddedServer = localEmbeddedServer;
        } catch (Exception e) {
            fail("Error with embedded DynamoDB server", e);
            throw new IllegalStateException();
        }
    }
    if (endpointOverride == null) {
        endpointOverride = localEndpointOverride;
    }
    service = new DynamoDBPersistenceService(new ItemRegistry() {

        @Override
        public Collection<Item> getItems(String pattern) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Collection<Item> getItems() {
            throw new UnsupportedOperationException();
        }

        @Override
        public Item getItemByPattern(String name) throws ItemNotFoundException, ItemNotUniqueException {
            throw new UnsupportedOperationException();
        }

        @Override
        public Item getItem(String name) throws ItemNotFoundException {
            Item item = ITEMS.get(name);
            if (item == null) {
                throw new ItemNotFoundException(name);
            }
            injectItemServices(item);
            return item;
        }

        @Override
        public void addRegistryChangeListener(RegistryChangeListener<Item> listener) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Collection<Item> getAll() {
            throw new UnsupportedOperationException();
        }

        @Override
        public Stream<Item> stream() {
            throw new UnsupportedOperationException();
        }

        @Override
        @Nullable
        public Item get(String key) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void removeRegistryChangeListener(RegistryChangeListener<Item> listener) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Item add(Item element) {
            throw new UnsupportedOperationException();
        }

        @Override
        @Nullable
        public Item update(Item element) {
            throw new UnsupportedOperationException();
        }

        @Override
        @Nullable
        public Item remove(String key) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Collection<Item> getItemsOfType(String type) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Collection<Item> getItemsByTag(String... tags) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Collection<Item> getItemsByTagAndType(String type, String... tags) {
            throw new UnsupportedOperationException();
        }

        @Override
        public <T extends Item> Collection<T> getItemsByTag(Class<T> typeFilter, String... tags) {
            throw new UnsupportedOperationException();
        }

        @Override
        @Nullable
        public Item remove(String itemName, boolean recursive) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void addRegistryHook(RegistryHook<Item> hook) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void removeRegistryHook(RegistryHook<Item> hook) {
            throw new UnsupportedOperationException();
        }
    }, localEndpointOverride);
    service.activate(null, config);
    return service;
}
Also used : ItemRegistry(org.openhab.core.items.ItemRegistry) URI(java.net.URI) RegistryHook(org.openhab.core.items.RegistryHook) DynamoDBProxyServer(com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer) ItemNotUniqueException(org.openhab.core.items.ItemNotUniqueException) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) DateTimeItem(org.openhab.core.library.items.DateTimeItem) CallItem(org.openhab.core.library.items.CallItem) GenericItem(org.openhab.core.items.GenericItem) NumberItem(org.openhab.core.library.items.NumberItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) ColorItem(org.openhab.core.library.items.ColorItem) LocationItem(org.openhab.core.library.items.LocationItem) GroupItem(org.openhab.core.items.GroupItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) PlayerItem(org.openhab.core.library.items.PlayerItem) ContactItem(org.openhab.core.library.items.ContactItem) RegistryChangeListener(org.openhab.core.common.registry.RegistryChangeListener) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Aggregations

DynamoDBProxyServer (com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer)1 URI (java.net.URI)1 ExecutionException (java.util.concurrent.ExecutionException)1 RegistryChangeListener (org.openhab.core.common.registry.RegistryChangeListener)1 GenericItem (org.openhab.core.items.GenericItem)1 GroupItem (org.openhab.core.items.GroupItem)1 Item (org.openhab.core.items.Item)1 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)1 ItemNotUniqueException (org.openhab.core.items.ItemNotUniqueException)1 ItemRegistry (org.openhab.core.items.ItemRegistry)1 RegistryHook (org.openhab.core.items.RegistryHook)1 CallItem (org.openhab.core.library.items.CallItem)1 ColorItem (org.openhab.core.library.items.ColorItem)1 ContactItem (org.openhab.core.library.items.ContactItem)1 DateTimeItem (org.openhab.core.library.items.DateTimeItem)1 DimmerItem (org.openhab.core.library.items.DimmerItem)1 LocationItem (org.openhab.core.library.items.LocationItem)1 NumberItem (org.openhab.core.library.items.NumberItem)1 PlayerItem (org.openhab.core.library.items.PlayerItem)1 RollershutterItem (org.openhab.core.library.items.RollershutterItem)1