/*
 * Licensed to Elastic Search and Shay Banon under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Elastic Search licenses this
 * file to you 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.elasticsearch.common.lucene.uid;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.RAMDirectory;
import org.elasticsearch.common.lucene.Lucene;
import org.testng.annotations.Test;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

/**
 * @author kimchy (shay.banon)
 */
public class UidFieldTests {

    @Test public void testUidField() throws Exception {
        IndexWriter writer = new IndexWriter(new RAMDirectory(), Lucene.STANDARD_ANALYZER, IndexWriter.MaxFieldLength.UNLIMITED);

        IndexReader reader = writer.getReader();
        assertThat(UidField.loadVersion(reader, new Term("_uid", "1")), equalTo(-1l));

        Document doc = new Document();
        doc.add(new Field("_uid", "1", Field.Store.YES, Field.Index.NOT_ANALYZED));
        writer.addDocument(doc);
        reader = writer.getReader();
        assertThat(UidField.loadVersion(reader, new Term("_uid", "1")), equalTo(-2l));
        assertThat(UidField.loadDocIdAndVersion(reader, new Term("_uid", "1")).version, equalTo(-2l));

        doc = new Document();
        doc.add(new UidField("_uid", "1", 1));
        writer.updateDocument(new Term("_uid", "1"), doc);
        reader = writer.getReader();
        assertThat(UidField.loadVersion(reader, new Term("_uid", "1")), equalTo(1l));
        assertThat(UidField.loadDocIdAndVersion(reader, new Term("_uid", "1")).version, equalTo(1l));

        doc = new Document();
        doc.add(new UidField("_uid", "1", 2));
        writer.updateDocument(new Term("_uid", "1"), doc);
        reader = writer.getReader();
        assertThat(UidField.loadVersion(reader, new Term("_uid", "1")), equalTo(2l));
        assertThat(UidField.loadDocIdAndVersion(reader, new Term("_uid", "1")).version, equalTo(2l));
    }
}

@@ -2763,21 +2763,304 @@
 qualTo(2l));
+%0A%0A        writer.deleteDocuments(new Term(%22_uid%22, %221%22));%0A        reader = writer.getReader();%0A        assertThat(UidField.loadVersion(reader, new Term(%22_uid%22, %221%22)), equalTo(-1l));%0A        assertThat(UidField.loadDocIdAndVersion(reader, new Term(%22_uid%22, %221%22)).version, equalTo(-1l));
 %0A    %7D%0A%7D%0A

add a test for term docs version deletes