package org.opencommercesearch;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.util.Set;

import org.apache.solr.common.cloud.SolrZkClient;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.opencommercesearch.repository.SynonymListProperty;
import org.opencommercesearch.repository.SynonymProperty;

import com.google.common.collect.Sets;

import atg.repository.RepositoryItem;

public class CloudSearchServerUnitTest {

    CloudSearchServer cloudSearchServer;
    
    @Before
    public void setUp() throws Exception {
        cloudSearchServer = new CloudSearchServer();
        cloudSearchServer.setLoggingInfo(false);
        cloudSearchServer.setLoggingError(false);
        cloudSearchServer.setLoggingError(false);
        cloudSearchServer.setLoggingWarning(false);
        cloudSearchServer.setLoggingTrace(false);
    }

    @Test
    public void testExportSynonymList() throws SearchServerException, KeeperException, InterruptedException {
        
        SolrZkClient zkClient = mock(SolrZkClient.class);
        RepositoryItem synonymList = initExportSynonyms(zkClient);
        when(zkClient.exists(anyString(), anyBoolean())).thenReturn(false);
        
        cloudSearchServer.exportSynonymList(synonymList);
        
        ArgumentCaptor<byte[]> dataCaptor = ArgumentCaptor.forClass(byte[].class);
        ArgumentCaptor<String> pathCaptor = ArgumentCaptor.forClass(String.class);
        verify(zkClient, times(2)).makePath(pathCaptor.capture(), dataCaptor.capture(), any(CreateMode.class), eq(true));
        
        assertNotNull(dataCaptor.getAllValues());
        byte[] capturedData = dataCaptor.getAllValues().get(0);
        String capturedDataStr = new String(capturedData);
        
        assertTrue(capturedDataStr.contains("synonym1 > mapping1"));
        assertTrue(capturedDataStr.contains("synonym2 > mapping2"));
        
        String path = pathCaptor.getAllValues().get(0);
        assertTrue(path.contains("synonymlist"));
    }

    private RepositoryItem initExportSynonyms(SolrZkClient zkClient) throws SearchServerException {
        cloudSearchServer.setSolrZkClient(zkClient);
        
        RepositoryItem synonymList = mock(RepositoryItem.class);
        when(synonymList.getItemDisplayName()).thenReturn("synonymList");
        
                
        Set<RepositoryItem> mappings = 
                Sets.newHashSet(mockMapping("synonym1 > mapping1"), 
                                mockMapping("synonym2 > mapping2"));
        when(synonymList.getPropertyValue(SynonymListProperty.MAPPINGS)).thenReturn(mappings);
        
        cloudSearchServer.setCatalogCollection("catalogCollection");
        cloudSearchServer.setRulesCollection("ruleCollection");
        
        return synonymList;
    }
    
    private RepositoryItem mockMapping(String mappingStr){
        RepositoryItem mapping = mock(RepositoryItem.class);
        when(mapping.getPropertyValue(SynonymProperty.MAPPING)).thenReturn(mappingStr);
        return mapping;
                
    }

}

@@ -1040,16 +1040,23 @@
 onymList
+NewFile
 () throw
@@ -1112,32 +1112,32 @@
 ptedException %7B%0A
-
         %0A       
@@ -1695,32 +1695,955 @@
 rue));%0A        %0A
+        verifySynonymExport(dataCaptor, pathCaptor);%0A    %7D%0A    %0A    @Test%0A    public void testExportSynonymListExistingFile() throws SearchServerException, KeeperException, InterruptedException %7B%0A        %0A        SolrZkClient zkClient = mock(SolrZkClient.class);%0A        RepositoryItem synonymList = initExportSynonyms(zkClient);%0A        when(zkClient.exists(anyString(), anyBoolean())).thenReturn(true);%0A        %0A        cloudSearchServer.exportSynonymList(synonymList);%0A        %0A        ArgumentCaptor%3Cbyte%5B%5D%3E dataCaptor = ArgumentCaptor.forClass(byte%5B%5D.class);%0A        ArgumentCaptor%3CString%3E pathCaptor = ArgumentCaptor.forClass(String.class);%0A        verify(zkClient, times(2)).setData(pathCaptor.capture(), dataCaptor.capture(), eq(true));%0A        %0A        verifySynonymExport(dataCaptor, pathCaptor);%0A    %7D%0A%0A    private void verifySynonymExport(ArgumentCaptor%3Cbyte%5B%5D%3E dataCaptor, ArgumentCaptor%3CString%3E pathCaptor) %7B%0A
         assertNo
@@ -2967,22 +2967,19 @@
     
-String path = 
+assertTrue(
 path
@@ -3010,44 +3010,170 @@
 t(0)
-;%0A        assertTrue(path.contains(%22
+.contains(%22/configs/catalogCollection/synonyms/synonymlist.txt%22));%0A        assertTrue(pathCaptor.getAllValues().get(1).contains(%22/configs/ruleCollection/synonyms/
 syno
@@ -3179,16 +3179,20 @@
 onymlist
+.txt
 %22));%0A   

add various scenarios for the export synonyms functionality within the Cloud Search Server unit test