ONOS-2431 - Unit tests for DistributedNetworkConfigStore

Change-Id: I7a3f6bd5d39c90e7ad2247fb56a14fa29227ad91
diff --git a/core/api/src/test/java/org/onosproject/store/service/ConsistentMapAdapter.java b/core/api/src/test/java/org/onosproject/store/service/ConsistentMapAdapter.java
new file mode 100644
index 0000000..e6df572
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/store/service/ConsistentMapAdapter.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.onosproject.store.service;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+import java.util.function.Predicate;
+
+/**
+ * Testing adapter for the consistent map.
+ */
+public class ConsistentMapAdapter<K, V> implements ConsistentMap<K, V> {
+    @Override
+    public int size() {
+        return 0;
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return false;
+    }
+
+    @Override
+    public boolean containsKey(K key) {
+        return false;
+    }
+
+    @Override
+    public boolean containsValue(V value) {
+        return false;
+    }
+
+    @Override
+    public Versioned<V> get(K key) {
+        return null;
+    }
+
+    @Override
+    public Versioned<V> computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
+        return null;
+    }
+
+    @Override
+    public Versioned<V> compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
+        return null;
+    }
+
+    @Override
+    public Versioned<V> computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
+        return null;
+    }
+
+    @Override
+    public Versioned<V> computeIf(K key, Predicate<? super V> condition,
+                                  BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
+        return null;
+    }
+
+    @Override
+    public Versioned<V> put(K key, V value) {
+        return null;
+    }
+
+    @Override
+    public Versioned<V> putAndGet(K key, V value) {
+        return null;
+    }
+
+    @Override
+    public Versioned<V> remove(K key) {
+        return null;
+    }
+
+    @Override
+    public void clear() {
+
+    }
+
+    @Override
+    public Set<K> keySet() {
+        return null;
+    }
+
+    @Override
+    public Collection<Versioned<V>> values() {
+        return null;
+    }
+
+    @Override
+    public Set<Map.Entry<K, Versioned<V>>> entrySet() {
+        return null;
+    }
+
+    @Override
+    public Versioned<V> putIfAbsent(K key, V value) {
+        return null;
+    }
+
+    @Override
+    public boolean remove(K key, V value) {
+        return false;
+    }
+
+    @Override
+    public boolean remove(K key, long version) {
+        return false;
+    }
+
+    @Override
+    public boolean replace(K key, V oldValue, V newValue) {
+        return false;
+    }
+
+    @Override
+    public boolean replace(K key, long oldVersion, V newValue) {
+        return false;
+    }
+
+    @Override
+    public void addListener(MapEventListener<K, V> listener) {
+
+    }
+
+    @Override
+    public void removeListener(MapEventListener<K, V> listener) {
+
+    }
+}
diff --git a/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java b/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java
new file mode 100644
index 0000000..1494b8a
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java
@@ -0,0 +1,261 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.onosproject.store.service;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+import org.onosproject.core.ApplicationId;
+import static org.onosproject.store.service.MapEvent.Type;
+import static org.onosproject.store.service.MapEvent.Type.*;
+
+/**
+ * Test implementation of the consistent map.
+ */
+public final class TestConsistentMap<K, V> extends ConsistentMapAdapter<K, V> {
+
+    private final List<MapEventListener<K, V>> listeners;
+    private final HashMap<K, V> map;
+    private final String mapName;
+
+    private TestConsistentMap(String mapName) {
+        map = new HashMap<>();
+        listeners = new LinkedList<>();
+        this.mapName = mapName;
+    }
+
+    private Versioned<V> version(V v) {
+        return new Versioned<>(v, 1, System.currentTimeMillis());
+    }
+
+    /**
+     * Notify all listeners of an event.
+     */
+    private void notifyListeners(String mapName, Type type,
+                                 K key, Versioned<V> value) {
+        MapEvent<K, V> event = new MapEvent<>(mapName, type, key, value);
+        listeners.forEach(
+                listener -> listener.event(event)
+        );
+    }
+
+    @Override
+    public int size() {
+        return map.size();
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return map.isEmpty();
+    }
+
+    @Override
+    public boolean containsKey(K key) {
+        return map.containsKey(key);
+    }
+
+    @Override
+    public boolean containsValue(V value) {
+        return map.containsValue(value);
+    }
+
+    @Override
+    public Versioned<V> get(K key) {
+        return version(map.get(key));
+    }
+
+    @Override
+    public Versioned<V> computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
+        Versioned<V> result = version(map.computeIfAbsent(key, mappingFunction));
+        notifyListeners(mapName, INSERT, key, result);
+        return result;
+    }
+
+    @Override
+    public Versioned<V> compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
+        return version(map.compute(key, remappingFunction));
+    }
+
+    @Override
+    public Versioned<V> computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
+        return version(map.computeIfPresent(key, remappingFunction));
+    }
+
+    @Override
+    public Versioned<V> computeIf(K key, Predicate<? super V> condition,
+                                  BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
+        return null;
+    }
+
+    @Override
+    public Versioned<V> put(K key, V value) {
+        Versioned<V> result = version(map.put(key, value));
+        notifyListeners(mapName, INSERT, key, result);
+        return result;
+    }
+
+    @Override
+    public Versioned<V> putAndGet(K key, V value) {
+        Versioned<V> result = version(map.put(key, value));
+        notifyListeners(mapName, UPDATE, key, result);
+        return result;
+    }
+
+    @Override
+    public Versioned<V> remove(K key) {
+        Versioned<V> result = version(map.remove(key));
+        notifyListeners(mapName, REMOVE, key, result);
+        return result;
+    }
+
+    @Override
+    public void clear() {
+        map.clear();
+    }
+
+    @Override
+    public Set<K> keySet() {
+        return map.keySet();
+    }
+
+    @Override
+    public Collection<Versioned<V>> values() {
+        return map
+                .values()
+                .stream()
+                .map(this::version)
+                .collect(Collectors.toList());
+    }
+
+    @Override
+    public Set<Map.Entry<K, Versioned<V>>> entrySet() {
+        return super.entrySet();
+    }
+
+    @Override
+    public Versioned<V> putIfAbsent(K key, V value) {
+        Versioned<V> result =  version(map.putIfAbsent(key, value));
+        if (map.get(key).equals(value)) {
+            notifyListeners(mapName, INSERT, key, result);
+        }
+        return result;
+    }
+
+    @Override
+    public boolean remove(K key, V value) {
+        boolean removed = map.remove(key, value);
+        if (removed) {
+            notifyListeners(mapName, REMOVE, key, null);
+        }
+        return removed;
+    }
+
+    @Override
+    public boolean remove(K key, long version) {
+        boolean removed =  map.remove(key, version);
+        if (removed) {
+            notifyListeners(mapName, REMOVE, key, null);
+        }
+        return removed;
+    }
+
+    @Override
+    public boolean replace(K key, V oldValue, V newValue) {
+        boolean replaced = map.replace(key, oldValue, newValue);
+        if (replaced) {
+            notifyListeners(mapName, REMOVE, key, null);
+        }
+        return replaced;
+    }
+
+    @Override
+    public boolean replace(K key, long oldVersion, V newValue) {
+        boolean replaced =  map.replace(key, map.get(key), newValue);
+        if (replaced) {
+            notifyListeners(mapName, REMOVE, key, null);
+        }
+        return replaced;
+    }
+
+    @Override
+    public void addListener(MapEventListener<K, V> listener) {
+        listeners.add(listener);
+    }
+
+    @Override
+    public void removeListener(MapEventListener<K, V> listener) {
+        listeners.remove(listener);
+    }
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    public static class Builder<K, V> implements ConsistentMapBuilder<K, V> {
+        String mapName = "map";
+
+        @Override
+        public ConsistentMapBuilder<K, V> withName(String mapName) {
+            this.mapName = mapName;
+            return this;
+        }
+
+        @Override
+        public ConsistentMapBuilder<K, V> withApplicationId(ApplicationId id) {
+            return this;
+        }
+
+        @Override
+        public ConsistentMapBuilder<K, V> withSerializer(Serializer serializer) {
+            return this;
+        }
+
+        @Override
+        public ConsistentMapBuilder<K, V> withPartitionsDisabled() {
+            return this;
+        }
+
+        @Override
+        public ConsistentMapBuilder<K, V> withUpdatesDisabled() {
+            return this;
+        }
+
+        @Override
+        public ConsistentMapBuilder<K, V> withPurgeOnUninstall() {
+            return this;
+        }
+
+        @Override
+        public ConsistentMap<K, V> build() {
+            return new TestConsistentMap<>(mapName);
+        }
+
+        @Override
+        public AsyncConsistentMap<K, V> buildAsyncMap() {
+            return null;
+        }
+
+    }
+
+}
diff --git a/core/api/src/test/java/org/onosproject/store/service/TestStorageService.java b/core/api/src/test/java/org/onosproject/store/service/TestStorageService.java
index d79796c..4901cfa 100644
--- a/core/api/src/test/java/org/onosproject/store/service/TestStorageService.java
+++ b/core/api/src/test/java/org/onosproject/store/service/TestStorageService.java
@@ -25,7 +25,7 @@
 
     @Override
     public <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder() {
-        throw new UnsupportedOperationException("consistentMapBuilder");
+        return TestConsistentMap.builder();
     }
 
     @Override
diff --git a/incubator/store/src/test/java/org/onosproject/incubator/store/config/impl/DistributedNetworkConfigStoreTest.java b/incubator/store/src/test/java/org/onosproject/incubator/store/config/impl/DistributedNetworkConfigStoreTest.java
new file mode 100644
index 0000000..9f37d78
--- /dev/null
+++ b/incubator/store/src/test/java/org/onosproject/incubator/store/config/impl/DistributedNetworkConfigStoreTest.java
@@ -0,0 +1,127 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.onosproject.incubator.store.config.impl;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.incubator.net.config.Config;
+import org.onosproject.incubator.net.config.ConfigFactory;
+import org.onosproject.incubator.net.config.SubjectFactory;
+import org.onosproject.store.service.TestStorageService;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+public class DistributedNetworkConfigStoreTest {
+    private DistributedNetworkConfigStore configStore;
+
+    /**
+     * Sets up the config store and the storage service test harness.
+     */
+    @Before
+    public void setUp() {
+        configStore = new DistributedNetworkConfigStore();
+        configStore.storageService = new TestStorageService();
+        configStore.setDelegate(event -> { });
+        configStore.activate();
+    }
+
+    /**
+     * Tears down the config store.
+     */
+    @After
+    public void tearDown() {
+        configStore.deactivate();
+    }
+
+    /**
+     * Config class for testing.
+     */
+    public class BasicConfig extends Config<String> { }
+
+    /**
+     * Config factory class for testing.
+     */
+    public class MockConfigFactory extends ConfigFactory<String, BasicConfig> {
+        protected MockConfigFactory(SubjectFactory<String> subjectFactory,
+                                Class<BasicConfig> configClass, String configKey) {
+            super(subjectFactory, configClass, configKey);
+        }
+        @Override
+        public BasicConfig createConfig() {
+            return new BasicConfig();
+        }
+    }
+
+    /**
+     * Tests creation, query and removal of a config.
+     */
+    @Test
+    public void testCreateConfig() {
+        configStore.addConfigFactory(new MockConfigFactory(null, BasicConfig.class, "config1"));
+
+        configStore.createConfig("config1", BasicConfig.class);
+        assertThat(configStore.getConfigClasses("config1"), hasSize(1));
+        assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(1));
+        assertThat(configStore.getSubjects(String.class), hasSize(1));
+
+        BasicConfig queried = configStore.getConfig("config1", BasicConfig.class);
+        assertThat(queried, notNullValue());
+
+        configStore.clearConfig("config1", BasicConfig.class);
+        assertThat(configStore.getConfigClasses("config1"), hasSize(0));
+        assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(0));
+        assertThat(configStore.getSubjects(String.class), hasSize(0));
+
+        BasicConfig queriedAfterClear = configStore.getConfig("config1", BasicConfig.class);
+        assertThat(queriedAfterClear, nullValue());
+    }
+
+    /**
+     * Tests creation, query and removal of a factory.
+     */
+    @Test
+    public void testCreateFactory() {
+        MockConfigFactory mockFactory = new MockConfigFactory(null, BasicConfig.class, "config1");
+
+        assertThat(configStore.getConfigFactory(BasicConfig.class), nullValue());
+
+        configStore.addConfigFactory(mockFactory);
+        assertThat(configStore.getConfigFactory(BasicConfig.class), is(mockFactory));
+
+        configStore.removeConfigFactory(mockFactory);
+        assertThat(configStore.getConfigFactory(BasicConfig.class), nullValue());
+    }
+
+    /**
+     * Tests applying a config.
+     */
+    @Test
+    public void testApplyConfig() {
+        configStore.addConfigFactory(new MockConfigFactory(null, BasicConfig.class, "config1"));
+
+        configStore.applyConfig("config1", BasicConfig.class, new ObjectMapper().createObjectNode());
+        assertThat(configStore.getConfigClasses("config1"), hasSize(1));
+        assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(1));
+        assertThat(configStore.getSubjects(String.class), hasSize(1));
+    }
+}