use of org.onosproject.store.service.ConsistentMap in project onos by opennetworkinglab.
the class DefaultConsistentMapTest method testBehavior.
/**
* Tests the behavior of public APIs of the default consistent map
* implmentation.
*/
@Test
public void testBehavior() {
Map<String, String> baseMap = new HashMap<>();
AsyncConsistentMapMock<String, String> asyncMap = new AsyncConsistentMapMock<>(baseMap);
ConsistentMap<String, String> newMap = new DefaultConsistentMap<>(asyncMap, 11);
assertThat(newMap.size(), is(0));
assertThat(newMap.isEmpty(), is(true));
newMap.put(KEY1, VALUE1);
assertThat(newMap.size(), is(1));
assertThat(newMap.get(KEY1).value(), is(VALUE1));
assertThat(newMap.containsKey(KEY1), is(true));
assertThat(newMap.containsKey(VALUE1), is(false));
assertThat(newMap.containsValue(VALUE1), is(true));
assertThat(newMap.containsValue(KEY1), is(false));
assertThat(newMap.keySet(), hasSize(1));
assertThat(newMap.keySet(), hasItem(KEY1));
assertThat(newMap.values(), hasSize(1));
assertThat(newMap.values(), hasItem(new Versioned<>(VALUE1, 0, 0)));
assertThat(newMap.entrySet(), hasSize(1));
Map.Entry<String, Versioned<String>> entry = newMap.entrySet().iterator().next();
assertThat(entry.getKey(), is(KEY1));
assertThat(entry.getValue().value(), is(VALUE1));
newMap.putIfAbsent(KEY2, VALUE2);
assertThat(newMap.entrySet(), hasSize(2));
assertThat(newMap.get(KEY2).value(), is(VALUE2));
newMap.putIfAbsent(KEY2, VALUE1);
assertThat(newMap.entrySet(), hasSize(2));
assertThat(newMap.get(KEY2).value(), is(VALUE2));
newMap.putAndGet(KEY3, VALUE3);
assertThat(newMap.entrySet(), hasSize(3));
assertThat(newMap.get(KEY3).value(), is(VALUE3));
newMap.putIfAbsent(KEY3, VALUE1);
assertThat(newMap.entrySet(), hasSize(3));
assertThat(newMap.get(KEY3).value(), is(VALUE3));
assertThat(newMap.computeIfAbsent(KEY4, this::computeFunction).value(), is(VALUE4));
assertThat(computeFunctionCalls, is(1));
assertThat(newMap.entrySet(), hasSize(4));
assertThat(newMap.computeIfAbsent(KEY4, this::computeFunction).value(), is(VALUE4));
assertThat(computeFunctionCalls, is(1));
Map javaMap = newMap.asJavaMap();
assertThat(javaMap.size(), is(newMap.size()));
assertThat(javaMap.get(KEY1), is(VALUE1));
assertThat(newMap.toString(), containsString(KEY4 + "=" + VALUE4));
assertThat(newMap.remove(KEY4).value(), is(VALUE4));
assertThat(newMap.entrySet(), hasSize(3));
assertThat(newMap.remove(KEY4).value(), nullValue());
assertThat(newMap.entrySet(), hasSize(3));
assertThat(newMap.remove(KEY3, DEFAULT_VERSION), is(true));
assertThat(newMap.entrySet(), hasSize(2));
assertThat(newMap.remove(KEY3, DEFAULT_VERSION), is(false));
assertThat(newMap.entrySet(), hasSize(2));
assertThat(newMap.remove(KEY2, VALUE2), is(true));
assertThat(newMap.entrySet(), hasSize(1));
assertThat(newMap.remove(KEY2, VALUE2), is(false));
assertThat(newMap.entrySet(), hasSize(1));
assertThat(newMap.replace(KEY1, VALUE4).value(), is(VALUE1));
assertThat(newMap.get(KEY1).value(), is(VALUE4));
assertThat(newMap.replace(KEY1, VALUE4, VALUE2), is(true));
assertThat(newMap.get(KEY1).value(), is(VALUE2));
assertThat(newMap.replace(KEY1, DEFAULT_VERSION, VALUE1), is(true));
assertThat(newMap.get(KEY1).value(), is(VALUE1));
newMap.clear();
assertThat(newMap.size(), is(0));
newMap.compute(KEY1, (a, b) -> VALUE1);
assertThat(newMap.get(KEY1).value(), is(VALUE1));
newMap.computeIfPresent(KEY1, (a, b) -> VALUE2);
assertThat(newMap.get(KEY1).value(), is(VALUE2));
Listener listener1 = new Listener(1);
newMap.addListener(listener1, null);
assertThat(asyncMap.listeners, hasSize(1));
assertThat(asyncMap.listeners, hasItem(listener1));
newMap.removeListener(listener1);
assertThat(asyncMap.listeners, hasSize(0));
Consumer<DistributedPrimitive.Status> consumer = status -> {
};
newMap.addStatusChangeListener(consumer);
assertThat(newMap.statusChangeListeners(), hasSize(1));
assertThat(newMap.statusChangeListeners(), hasItem(consumer));
newMap.removeStatusChangeListener(consumer);
assertThat(newMap.statusChangeListeners(), hasSize(0));
assertThat(newMap.statusChangeListeners(), not(hasItem(consumer)));
}
use of org.onosproject.store.service.ConsistentMap in project onos by opennetworkinglab.
the class ConsistentMapTestCommand method doExecute.
@Override
protected void doExecute() {
StorageService storageService = get(StorageService.class);
map = storageService.<String, String>consistentMapBuilder().withName(name).withSerializer(Serializer.using(KryoNamespaces.BASIC)).withVersion(Version.version("1.0.0")).withCompatibilityFunction((value, version) -> version + ":" + value).build();
if ("get".equals(operation)) {
print(map.get(arg1));
} else if ("put".equals(operation)) {
print(map.put(arg1, arg2));
} else if ("size".equals(operation)) {
print("%d", map.size());
} else if ("isEmpty".equals(operation)) {
print("%b", map.isEmpty());
} else if ("putIfAbsent".equals(operation)) {
print(map.putIfAbsent(arg1, arg2));
} else if ("putAndGet".equals(operation)) {
print(map.putAndGet(arg1, arg2));
} else if ("clear".equals(operation)) {
map.clear();
} else if ("remove".equals(operation)) {
if (arg2 == null) {
print(map.remove(arg1));
} else {
print("%b", map.remove(arg1, arg2));
}
} else if ("containsKey".equals(operation)) {
print("%b", map.containsKey(arg1));
} else if ("containsValue".equals(operation)) {
print("%b", map.containsValue(arg1));
} else if ("replace".equals(operation)) {
if (arg3 == null) {
print(map.replace(arg1, arg2));
} else {
print("%b", map.replace(arg1, arg2, arg3));
}
} else if ("compatiblePut".equals(operation)) {
ConsistentMap<String, String> map = storageService.<String, String>consistentMapBuilder().withName(name).withSerializer(Serializer.using(KryoNamespaces.BASIC)).withCompatibilityFunction((value, version) -> version + ":" + value).withVersion(Version.version("2.0.0")).build();
print(map.put(arg1, arg2));
} else if ("compatibleGet".equals(operation)) {
ConsistentMap<String, String> map = storageService.<String, String>consistentMapBuilder().withName(name).withSerializer(Serializer.using(KryoNamespaces.BASIC)).withCompatibilityFunction((value, version) -> version + ":" + value).withVersion(Version.version("2.0.0")).build();
print(map.get(arg1));
}
}
use of org.onosproject.store.service.ConsistentMap in project onos by opennetworkinglab.
the class ECFlowRuleStoreTest method setUp.
@Before
public void setUp() throws Exception {
flowStoreImpl = new ECFlowRuleStore();
flowStoreImpl.storageService = new TestStorageService() {
@Override
public <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder() {
return new ConsistentMapBuilder<K, V>() {
@Override
public AsyncConsistentMap<K, V> buildAsyncMap() {
return new AsyncConsistentMapAdapter<K, V>();
}
@Override
public ConsistentMap<K, V> build() {
return null;
}
};
}
};
ReplicaInfoManager replicaInfoManager = new ReplicaInfoManager();
replicaInfoManager.mastershipService = new MasterOfAll();
flowStoreImpl.replicaInfoManager = replicaInfoManager;
mockClusterService = createMock(ClusterService.class);
flowStoreImpl.clusterService = mockClusterService;
nodeId = new NodeId("1");
mockControllerNode = new MockControllerNode(nodeId);
expect(mockClusterService.getLocalNode()).andReturn(mockControllerNode).anyTimes();
replay(mockClusterService);
flowStoreImpl.clusterCommunicator = new ClusterCommunicationServiceAdapter();
flowStoreImpl.mastershipService = new MasterOfAll();
flowStoreImpl.deviceService = new DeviceServiceAdapter();
flowStoreImpl.coreService = new CoreServiceAdapter();
flowStoreImpl.configService = new ComponentConfigAdapter();
flowStoreImpl.persistenceService = new PersistenceServiceAdapter();
flowStoreImpl.activate(context);
}
Aggregations