/*
 * Copyright 2013 Cloudera Inc.
 *
 * Licensed 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 com.cloudera.cdk.morphline.solr;

import java.util.Arrays;

import org.junit.Test;

import com.cloudera.cdk.morphline.api.MorphlineContext;
import com.cloudera.cdk.morphline.api.Record;
import com.cloudera.cdk.morphline.base.Fields;
import com.cloudera.cdk.morphline.base.Notifications;

public class SolrMorphlineTest extends AbstractSolrMorphlineTest {

  @Test
  public void testLoadSchema() throws Exception {
    SolrLocator locator = new SolrLocator(new MorphlineContext.Builder().build());
    locator.setCollectionName("collection1");
    locator.setSolrHomeDir("solr/collection1");
    assertNotNull(locator.getIndexSchema());
  }
  
  @Test
  public void testLoadSolrBasic() throws Exception {
    //System.setProperty("ENV_SOLR_HOME", testSolrHome + "/collection1");
    morphline = createMorphline("test-morphlines/loadSolrBasic");    
    //System.clearProperty("ENV_SOLR_HOME");
    Record record = new Record();
    record.put(Fields.ID, "id0");
    record.put("first_name", "Nadja"); // will be sanitized
    startSession();
    Notifications.notifyBeginTransaction(morphline);
    assertTrue(morphline.process(record));
    assertEquals(1, collector.getNumStartEvents());
    Notifications.notifyCommitTransaction(morphline);
    Record expected = new Record();
    expected.put(Fields.ID, "id0");
    assertEquals(Arrays.asList(expected), collector.getRecords());
    assertEquals(1, queryResultSetSize("*:*"));
    Notifications.notifyRollbackTransaction(morphline);
    Notifications.notifyShutdown(morphline);
  }
    
  @Test
  public void testTokenizeText() throws Exception {
    morphline = createMorphline("test-morphlines/tokenizeText");    
    Record record = new Record();
    record.put(Fields.MESSAGE, "Hello World!");
    record.put(Fields.MESSAGE, "\nFoo@Bar.com #%()123");
    Record expected = record.copy();
    expected.getFields().putAll("tokens", Arrays.asList("hello", "world", "foo", "bar.com", "123"));
    startSession();
    Notifications.notifyBeginTransaction(morphline);
    assertTrue(morphline.process(record));
    assertEquals(1, collector.getNumStartEvents());
    Notifications.notifyCommitTransaction(morphline);
    assertEquals(expected, collector.getFirstRecord());
  }
    
}

@@ -2258,21 +2258,53 @@
 eText%22);
+%0A
     
-%0A
+for (int i = 0; i %3C 3; i++) %7B%0A  
     Reco
@@ -2325,32 +2325,34 @@
 w Record();%0A    
+  
 record.put(Field
@@ -2375,24 +2375,26 @@
 o World!%22);%0A
+  
     record.p
@@ -2438,24 +2438,26 @@
 )123%22);%0A    
+ 
+ 
 Record expec
@@ -2473,24 +2473,26 @@
 ord.copy();%0A
+  
     expected
@@ -2576,24 +2576,51 @@
 %22, %22123%22));%0A
+      collector.reset();%0A  
     startSes
@@ -2623,32 +2623,34 @@
 tSession();%0A    
+ 
+ 
 Notifications.no
@@ -2678,32 +2678,34 @@
 morphline);%0A    
+  
 assertTrue(morph
@@ -2719,32 +2719,34 @@
 ocess(record));%0A
+  
     assertEquals
@@ -2777,32 +2777,34 @@
 tEvents());%0A    
+  
 Notifications.no
@@ -2829,32 +2829,34 @@
 ion(morphline);%0A
+  
     assertEquals
@@ -2891,23 +2891,29 @@
 tRecord());%0A
+    %7D%0A
   %7D%0A    %0A%7D%0A

add unit test that uses stateful API