All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lordofthejars.nosqlunit.infinispan.InfinispanAssertion Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.lordofthejars.nosqlunit.infinispan;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.infinispan.api.BasicCache;

import com.lordofthejars.nosqlunit.core.FailureHandler;

public class InfinispanAssertion {

	public static void strictAssertEquals(BasicCache cache, Map expectedMap) {

		int expectedNumberOfKeys = expectedMap.size();
		int currentNumberOfKeys = cache.size();

		checkNumberOfElements(expectedNumberOfKeys, currentNumberOfKeys);
		checkElements(cache, expectedMap);
	}

	private static void checkElements(BasicCache cache, Map expectedMap) throws Error {
		
		Set> expectedElements = expectedMap.entrySet();

		for (Entry expectedElement : expectedElements) {
			checkElement(cache, expectedElement);
		}
	}

	private static void checkElement(BasicCache cache, Entry expectedElement) throws Error {
		Object expectedKey = expectedElement.getKey();

		if (cache.containsKey(expectedKey)) {
			Object currentValue = cache.get(expectedKey);
			Object expectedValue = expectedElement.getValue();
			
			if(!currentValue.equals(expectedValue)) {
				throw FailureHandler.createFailure("Object for key %s should be %s but was found %s.", expectedKey,
						expectedValue, currentValue);
			}

		} else {
			throw FailureHandler.createFailure("Key %s was not found.", expectedKey);
		}
	}

	private static void checkNumberOfElements(int expectedNumberOfKeys, int currentNumberOfKeys) throws Error {
		if (expectedNumberOfKeys != currentNumberOfKeys) {
			throw FailureHandler.createFailure("Number of expected keys are %s but was found %s.",
					expectedNumberOfKeys, currentNumberOfKeys);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy