|
| 1 | +package com.lordofthejars.nosqlunit.elasticsearch6; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.LinkedHashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.codehaus.jackson.map.ObjectMapper; |
| 11 | +import org.elasticsearch.action.get.GetRequestBuilder; |
| 12 | +import org.elasticsearch.action.get.GetResponse; |
| 13 | +import org.elasticsearch.action.search.SearchResponse; |
| 14 | +import org.elasticsearch.client.Client; |
| 15 | +import org.elasticsearch.search.builder.SearchSourceBuilder; |
| 16 | + |
| 17 | +import com.lordofthejars.nosqlunit.core.FailureHandler; |
| 18 | +import com.lordofthejars.nosqlunit.elasticsearch6.parser.DataReader; |
| 19 | +import com.lordofthejars.nosqlunit.util.DeepEquals; |
| 20 | + |
| 21 | +public class ElasticsearchAssertion { |
| 22 | + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
| 23 | + |
| 24 | + private ElasticsearchAssertion() { |
| 25 | + super(); |
| 26 | + } |
| 27 | + |
| 28 | + public static void strictAssertEquals(final List<Map<String, Object>> expectedDocuments, final Client client) { |
| 29 | + |
| 30 | + checkNumberOfDocuments(expectedDocuments, client); |
| 31 | + |
| 32 | + for (Map<String, Object> document : expectedDocuments) { |
| 33 | + final Object object = document.get(DataReader.DOCUMENT_ELEMENT); |
| 34 | + |
| 35 | + if (object instanceof List) { |
| 36 | + @SuppressWarnings("unchecked") |
| 37 | + final List<Map<String, Object>> properties = (List<Map<String, Object>>) object; |
| 38 | + |
| 39 | + final List<GetRequestBuilder> indexes = new ArrayList<>(); |
| 40 | + Map<String, Object> expectedDataOfDocument = new HashMap<>(); |
| 41 | + |
| 42 | + for (Map<String, Object> property : properties) { |
| 43 | + |
| 44 | + if (property.containsKey(DataReader.INDEX_ELEMENT)) { |
| 45 | + indexes.add(prepareGetIndex(property.get(DataReader.INDEX_ELEMENT), client)); |
| 46 | + } else { |
| 47 | + if (property.containsKey(DataReader.DATA_ELEMENT)) { |
| 48 | + expectedDataOfDocument = dataOfDocument(property.get(DataReader.DATA_ELEMENT)); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + checkIndicesWithDocument(indexes, expectedDataOfDocument); |
| 55 | + |
| 56 | + } else { |
| 57 | + throw new IllegalArgumentException("Array of Indexes and Data are required."); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private static void checkIndicesWithDocument(final List<GetRequestBuilder> indexes, |
| 63 | + final Map<String, Object> expectedDataOfDocument) { |
| 64 | + for (GetRequestBuilder getRequestBuilder : indexes) { |
| 65 | + |
| 66 | + GetResponse dataOfDocumentResponse = getRequestBuilder.execute().actionGet(); |
| 67 | + |
| 68 | + checkExistenceOfDocument(getRequestBuilder, dataOfDocumentResponse); |
| 69 | + checkDocumentEquality(expectedDataOfDocument, getRequestBuilder, dataOfDocumentResponse); |
| 70 | + |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static void checkDocumentEquality(final Map<String, Object> expectedDataOfDocument, |
| 75 | + final GetRequestBuilder getRequestBuilder, final GetResponse dataOfDocumentResponse) { |
| 76 | + final Map<String, Object> dataOfDocument = new LinkedHashMap<>(dataOfDocumentResponse.getSource()); |
| 77 | + |
| 78 | + if (!DeepEquals.deepEquals(dataOfDocument, expectedDataOfDocument)) { |
| 79 | + try { |
| 80 | + throw FailureHandler.createFailure( |
| 81 | + "Expected document for index: %s - type: %s - id: %s is %s, but %s was found.", |
| 82 | + getRequestBuilder.request().index(), getRequestBuilder.request().type(), |
| 83 | + getRequestBuilder.request().id(), OBJECT_MAPPER.writeValueAsString(expectedDataOfDocument), |
| 84 | + OBJECT_MAPPER.writeValueAsString(dataOfDocument)); |
| 85 | + } catch (IOException e) { |
| 86 | + throw new IllegalArgumentException(e); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static void checkExistenceOfDocument(final GetRequestBuilder getRequestBuilder, |
| 92 | + final GetResponse dataOfDocumentResponse) { |
| 93 | + if (!dataOfDocumentResponse.isExists()) { |
| 94 | + throw FailureHandler.createFailure( |
| 95 | + "Document with index: %s - type: %s - id: %s has not returned any document.", |
| 96 | + getRequestBuilder.request().index(), getRequestBuilder.request().type(), |
| 97 | + getRequestBuilder.request().id()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static void checkNumberOfDocuments(final List<Map<String, Object>> expectedDocuments, final Client client) { |
| 102 | + int expectedNumberOfElements = expectedDocuments.size(); |
| 103 | + |
| 104 | + long numberOfInsertedDocuments = numberOfInsertedDocuments(client); |
| 105 | + |
| 106 | + if (expectedNumberOfElements != numberOfInsertedDocuments) { |
| 107 | + throw FailureHandler.createFailure("Expected number of documents are %s but %s has been found.", |
| 108 | + expectedNumberOfElements, numberOfInsertedDocuments); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private static GetRequestBuilder prepareGetIndex(final Object object, final Client client) { |
| 113 | + @SuppressWarnings("unchecked") |
| 114 | + Map<String, String> indexInformation = (Map<String, String>) object; |
| 115 | + |
| 116 | + GetRequestBuilder prepareGet = client.prepareGet(); |
| 117 | + |
| 118 | + if (indexInformation.containsKey(DataReader.INDEX_NAME_ELEMENT)) { |
| 119 | + prepareGet.setIndex(indexInformation.get(DataReader.INDEX_NAME_ELEMENT)); |
| 120 | + } |
| 121 | + |
| 122 | + if (indexInformation.containsKey(DataReader.INDEX_TYPE_ELEMENT)) { |
| 123 | + prepareGet.setType(indexInformation.get(DataReader.INDEX_TYPE_ELEMENT)); |
| 124 | + } |
| 125 | + |
| 126 | + if (indexInformation.containsKey(DataReader.INDEX_ID_ELEMENT)) { |
| 127 | + prepareGet.setId(indexInformation.get(DataReader.INDEX_ID_ELEMENT)); |
| 128 | + } |
| 129 | + |
| 130 | + return prepareGet; |
| 131 | + } |
| 132 | + |
| 133 | + @SuppressWarnings("unchecked") |
| 134 | + private static Map<String, Object> dataOfDocument(final Object object) { |
| 135 | + return (Map<String, Object>) object; |
| 136 | + } |
| 137 | + |
| 138 | + private static long numberOfInsertedDocuments(final Client client) { |
| 139 | + SearchResponse response = client.prepareSearch().setSource(new SearchSourceBuilder().size(0)).get(); |
| 140 | + return response.getHits().totalHits; |
| 141 | + } |
| 142 | +} |
0 commit comments