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

com.lordofthejars.nosqlunit.couchdb.CouchDbAssertion Maven / Gradle / Ivy

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

import static com.lordofthejars.nosqlunit.util.DeepEquals.deepEquals;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.ektorp.CouchDbConnector;

import com.lordofthejars.nosqlunit.core.FailureHandler;

public class CouchDbAssertion {

	private static final String _REV = "_rev";
	private static final String _ID = "_id";

	private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

	private CouchDbAssertion() {
		super();
	}

	public static final void strictAssertEquals(InputStream expectedData, CouchDbConnector couchDb) {

		try {

			List> expectedDocuments = DataLoader.getDocuments(expectedData);
			List allDocIds = couchDb.getAllDocIds();

			checkNumberOfDocuments(expectedDocuments, allDocIds);

			List> remainingExpectedElementsWithoutIds = checkDocumentsWithId(expectedDocuments,
					couchDb, allDocIds);
			checkDocumentsWithoutId(couchDb, allDocIds, remainingExpectedElementsWithoutIds);

		} catch (JsonProcessingException e) {
			throw new IllegalArgumentException(e);
		} catch (IOException e) {
			throw new IllegalArgumentException(e);
		}

	}

	private static void checkNumberOfDocuments(List> expectedDocuments, List allDocIds) {
		int expectedSize = expectedDocuments.size();
		int currentSize = allDocIds.size();
		if (expectedSize != currentSize) {
			throw FailureHandler.createFailure("Expected number of elements are %s but insert are %s.",
					expectedSize, currentSize);
		}
	}

	private static void checkDocumentsWithoutId(CouchDbConnector couchDb, List allDocIds,
			List> remainingExpectedElementsWithoutIds) {

		List> storedElementsWihtoutExpectedId = loadDocuments(allDocIds, couchDb);

		for (Map expectedElement : remainingExpectedElementsWithoutIds) {

			if (!isExpectedElementPresent(expectedElement, storedElementsWihtoutExpectedId)) {
				throw FailureHandler.createFailure("Expected element # %s # is not found.",
						jsonStringify(expectedElement));
			}
		}
	}

	private static boolean isExpectedElementPresent(Map expectedElement,
			List> storedElementsWihtoutExpectedId) {

		for (Map element : storedElementsWihtoutExpectedId) {
			if (deepEquals(expectedElement, element)) {
				return true;
			}
		}

		return false;
	}

	private static List> loadDocuments(List allDocIds, CouchDbConnector couchDb) {
		List> loadedDocuments = new ArrayList>();

		for (String id : allDocIds) {
			Map element = couchDb.get(Map.class, id);

			removeIdTag(element);
			removeRevisionTag(element);

			loadedDocuments.add(element);
		}

		return loadedDocuments;
	}

	private static List> checkDocumentsWithId(List> expectedDocuments,
			CouchDbConnector couchDb, List allDocIds) throws IOException, JsonProcessingException {

		Iterator> expectedDocumentsIterator = expectedDocuments.iterator();
		List> remainingElements = new ArrayList>();

		while (expectedDocumentsIterator.hasNext()) {

			Map expectedDocument = expectedDocumentsIterator.next();

			Object idNode = expectedDocument.get(_ID);

			if (isIdDefined(idNode)) {
				String expectedId = toStringValue(idNode);

				if (allDocIds.remove(expectedId)) {
					checkDocument(couchDb, expectedDocument, expectedId);
				} else {
					throw FailureHandler.createFailure("Document with id %s is not found.", expectedId);
				}

				expectedDocumentsIterator.remove();

			} else {
				remainingElements.add(expectedDocument);
			}

		}
		return remainingElements;
	}

	private static void checkDocument(CouchDbConnector couchDb, Map expectedDocument, String expectedId)
			throws Error {
		// Element must be in database
		Map element = couchDb.get(Map.class, expectedId);
		// We remove rev because it is impossible to know by the end
		// user
		removeRevisionTag(element);

		if (!deepEquals(element, expectedDocument)) {
			throw FailureHandler.createFailure(
					"Expected element # %s # is not found but # %s # was found.",
					jsonStringify(expectedDocument), jsonStringify(element));
		}
	}

	private static boolean isIdDefined(Object idNode) {
		return idNode != null;
	}

	private static void removeRevisionTag(Map element) {
		element.remove(_REV);
	}

	private static void removeIdTag(Map element) {
		element.remove(_ID);
	}

	private static String toStringValue(Object value) {
		if (value instanceof String) {
			return (String) value;
		} else {
			throw new IllegalArgumentException("An String value was expected.");
		}
	}

	private static String jsonStringify(Map document) {
		try {
			return OBJECT_MAPPER.writeValueAsString(document);
		} catch (JsonGenerationException e) {
			throw new IllegalStateException(e);
		} catch (JsonMappingException e) {
			throw new IllegalStateException(e);
		} catch (IOException e) {
			throw new IllegalStateException(e);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy