// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.query.test;

import com.yahoo.prelude.query.AndItem;
import com.yahoo.prelude.query.SameElementItem;
import com.yahoo.prelude.query.WordItem;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class SameElementItemTestCase {

    @Test
    public void testAddItem() {
        SameElementItem s = new SameElementItem("structa");
        s.addItem(new WordItem("b", "f1"));
        s.addItem(new WordItem("c", "f2"));
        s.addItem(new WordItem("d", "f3"));
        assertEquals("structa:{f1:b f2:c f3:d}", s.toString());
    }

    @Test
    public void testClone() {
        SameElementItem s = new SameElementItem("structa");
        s.addItem(new WordItem("b", "f1"));
        s.addItem(new WordItem("c", "f2"));
        s.addItem(new WordItem("d", "f3"));
        assertEquals("structa:{f1:b f2:c f3:d}", s.toString());
        SameElementItem c = (SameElementItem)s.clone();
        assertEquals("structa:{f1:b f2:c f3:d}", c.toString());
    }

    @Test
    public void requireAllChildrenHaveStructMemberNameSet() {
        try {
            SameElementItem s = new SameElementItem("structa");
            s.addItem(new WordItem("b", "f1"));
            s.addItem(new WordItem("c"));
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) { // success
            assertEquals("Struct fieldname can not be empty", e.getMessage());
        }
    }

    @Test
    public void requireAllowCommonPrefix() {
        SameElementItem s = new SameElementItem("structa");
        s.addItem(new WordItem("b", "f1"));
        s.addItem(new WordItem("c", "structaf2"));
        assertEquals("structa:{f1:b structaf2:c}", s.toString());
    }

    @Test
    public void requireChildrenCanHavePrefixCommonWithParent() {
        SameElementItem s = new SameElementItem("structa");
        s.addItem(new WordItem("b", "f1"));
        s.addItem(new WordItem("c", "structa.f2"));
        assertEquals("structa:{f1:b structa.f2:c}", s.toString());
    }

    @Test
    public void requireAllChildrenHaveNonEmptyTerm() {
        try {
            SameElementItem s = new SameElementItem("structa");
            s.addItem(new WordItem("", "f2"));
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) { // Success
            assertEquals("The word of a word item can not be empty", e.getMessage());
        }
    }

    @Test
    public void requireAllChildrenAreTermItems() {
        try {
            SameElementItem s = new SameElementItem("structa");
            s.addItem(new AndItem());
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) { // Success
            assertEquals("Child item (AND ) should be an instance of class com.yahoo.prelude.query.TermItem but is class com.yahoo.prelude.query.AndItem",
                         e.getMessage());
        }
    }

}

@@ -187,24 +187,101 @@
 ry.AndItem;%0A
+import com.yahoo.prelude.query.IntItem;%0Aimport com.yahoo.prelude.query.Item;%0A
 import com.y
@@ -308,32 +308,73 @@
 ameElementItem;%0A
+import com.yahoo.prelude.query.TermItem;%0A
 import com.yahoo
@@ -418,24 +418,52 @@
 unit.Test;%0A%0A
+import java.util.Optional;%0A%0A
 import stati
@@ -495,16 +495,59 @@
 Equals;%0A
+import static org.junit.Assert.assertTrue;%0A
 import s
@@ -3270,10 +3270,873 @@
 %0A    %7D%0A%0A
+    private void verifyExtractSingle(TermItem term) %7B%0A        String subFieldName = term.getIndexName();%0A        SameElementItem s = new SameElementItem(%22structa%22);%0A        s.addItem(term);%0A        Optional%3CItem%3E single =s.extractSingleChild();%0A        assertTrue(single.isPresent());%0A        assertEquals(((TermItem)single.get()).getIndexName(), s.getFieldName() + %22.%22 + subFieldName);%0A    %7D%0A%0A    @Test%0A    public void requireExtractSingleItemToExtractSingles() %7B%0A        verifyExtractSingle(new WordItem(%22b%22, %22f1%22));%0A        verifyExtractSingle(new IntItem(%227%22, %22f1%22));%0A    %7D%0A%0A    @Test%0A    public void requireExtractSingleItemToExtractSinglesOnly() %7B%0A        SameElementItem s = new SameElementItem(%22structa%22);%0A        s.addItem(new WordItem(%22b%22, %22f1%22));%0A        s.addItem(new WordItem(%22c%22, %22f2%22));%0A        assertTrue(s.extractSingleChild().isEmpty());%0A    %7D%0A%0A
 %7D%0A

Add test from baldersheim