/*
 * Copyright 2016 Needham Software LLC
 *
 * 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 org.jesterj.ingest.model.impl;

import com.copyright.easiertest.Mock;
import com.copyright.easiertest.ObjectUnderTest;
import org.apache.commons.codec.digest.DigestUtils;
import org.jesterj.ingest.model.Document;
import org.jesterj.ingest.model.Plan;
import org.jesterj.ingest.model.Scanner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import static com.copyright.easiertest.EasierMocks.prepareMocks;
import static com.copyright.easiertest.EasierMocks.replay;
import static com.copyright.easiertest.EasierMocks.reset;
import static com.copyright.easiertest.EasierMocks.verify;
import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertEquals;


public class DocumentImplTest {

  @ObjectUnderTest private DocumentImpl obj;
  @Mock private Scanner scannerMock;
  @Mock private Plan planMock;


  public DocumentImplTest() {
    prepareMocks(this);
  }

  @Before
  public void setUp() {
    reset();
  }

  @After
  public void tearDown() {
    verify();
  }

  @Test
  public void testGetFirstValue() {
    expect(scannerMock.getName()).andReturn("my_name").anyTimes();
    expect(planMock.getDocIdField()).andReturn("id").anyTimes();
    replay();
    DocumentImpl document = new DocumentImpl(new byte[]{}, "foo", planMock, Document.Operation.NEW, scannerMock);
    document.put("string", "stringvalue");
    DocumentImpl document2 = new DocumentImpl(new byte[]{}, "foo", planMock, Document.Operation.NEW, scannerMock);
    document2.put("string", "stringvalue");
    System.out.println(document.getHash());
    System.out.println(document2.getHash());

    assertEquals("stringvalue", document.getFirstValue("string"));
    assertEquals(null, document.getFirstValue("unknown"));

  }

  @Test
  public void testHash() throws UnsupportedEncodingException, NoSuchAlgorithmException {
    expect(obj.getDelegateString()).andReturn("CAFE");
    expect(obj.getRawData()).andReturn("BABE".getBytes("UTF-8")).anyTimes();
    replay();
    assertEquals(DigestUtils.md5Hex("CAFEBABE".getBytes("UTF-8")).toUpperCase(), obj.getHash());
  }

  @Test
  public void testHashRawDataNull() throws UnsupportedEncodingException, NoSuchAlgorithmException {
    expect(obj.getDelegateString()).andReturn("CAFE");
    expect(obj.getRawData()).andReturn(null).anyTimes();
    replay();
    assertEquals(DigestUtils.md5Hex("CAFE".getBytes("UTF-8")).toUpperCase(), obj.getHash());
  }
}

@@ -725,16 +725,68 @@
 erTest;%0A
+import com.google.common.collect.ArrayListMultimap;%0A
 import o
@@ -829,16 +829,16 @@
 tUtils;%0A
-
 import o
@@ -946,24 +946,64 @@
 el.Scanner;%0A
+import org.jesterj.ingest.model.Status;%0A
 import org.j
@@ -1634,16 +1634,51 @@
 anMock;%0A
+  @Mock private StepImpl stepMock;%0A
 %0A%0A  publ
@@ -2339,24 +2339,26 @@
 ingvalue%22);%0A
+//
     System.o
@@ -2385,24 +2385,26 @@
 getHash());%0A
+//
     System.o
@@ -3140,32 +3140,32 @@
 ;%0A    replay();%0A
-
     assertEquals
@@ -3244,11 +3244,979 @@
 ());%0A  %7D
+%0A%0A  @Test%0A  public void testToEntry() %7B%0A    ArrayListMultimap%3CString, String%3E stupidFinalClass = ArrayListMultimap.create();%0A    expect(obj.getDelegate()).andReturn(stupidFinalClass);%0A    expect(obj.getSourceScannerName()).andReturn(%22foo%22);%0A    expect(obj.getStatus()).andReturn(Status.PROCESSING);%0A    expect(obj.getStatusMessage()).andReturn(%22ok%22);%0A    byte%5B%5D value = %7B%7D;%0A    expect(obj.getRawData()).andReturn(value);%0A    expect(stepMock.getName()).andReturn(%22bar%22);%0A    expect(obj.getOperation()).andReturn(Document.Operation.UPDATE);%0A    replay();%0A    DocumentImpl.DocumentEntry e = (DocumentImpl.DocumentEntry) obj.toEntry(stepMock);%0A    assertEquals(stupidFinalClass, e.contents);%0A    assertEquals(%22foo%22, e.scannerName);%0A    assertEquals(Status.PROCESSING, e.status);%0A    assertEquals(%22ok%22, e.statusMessage);%0A    assertEquals(value, e.data.data);%0A    assertEquals(%22bar%22, e.nextStepName);%0A    assertEquals(Document.Operation.UPDATE.toString(), e.operation);%0A%0A  %7D
 %0A%7D%0A

Test entry creation