/*
 * Copyright 2014 Global Biodiversity Information Facility (GBIF)
 * 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.gbif.api.model.occurrence;

import org.gbif.api.model.occurrence.predicate.EqualsPredicate;
import org.gbif.api.model.occurrence.predicate.Predicate;
import org.gbif.api.model.occurrence.search.OccurrenceSearchParameter;

import java.io.ByteArrayOutputStream;
import java.util.Date;

import com.google.common.collect.Lists;
import com.google.common.io.Closeables;
import org.codehaus.jackson.map.ObjectMapper;
import org.hamcrest.core.IsCollectionContaining;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.matchers.JUnitMatchers.both;
import static org.mockito.Mockito.mock;

public class DownloadRequestTest {

  private static final String TEST_USER = "user@gbif.org";
  private static final String TEST_EMAIL = "test@gbif.org";

  private static DownloadRequest newDownload(Predicate p) {
    return newDownload(p, TEST_USER);
  }

  private static DownloadRequest newDownload(Predicate p, String user) {
    return newDownload(p, user, null);
  }

  private static DownloadRequest newDownload(Predicate p, String user, Date completed) {
    return new DownloadRequest(p, user, Lists.newArrayList(TEST_EMAIL), false);
  }

  @Test
  public void testAvailable() {
    Download download = new Download();
    download.setStatus(Download.Status.RUNNING);
    assertFalse(download.isAvailable());

    download.setStatus(Download.Status.SUCCEEDED);
    assertTrue(download.isAvailable());
  }

  @Test
  public void testBasic() {
    Predicate p = mock(Predicate.class);
    DownloadRequest dl = newDownload(p);

    assertThat(dl.getNotificationAddresses(), IsCollectionContaining.hasItem(TEST_EMAIL));
    assertThat(dl.getPredicate(), equalTo(p));
  }


  @Test
  public void testEquals() {
    Predicate p = mock(Predicate.class);

    DownloadRequest dl1 = newDownload(p);
    DownloadRequest dl2 = newDownload(p);

    assertThat(dl1, both(equalTo(dl1)).and(equalTo(dl2)));
  }


  @Test
  public void testHashcode() {
    Predicate p = mock(Predicate.class);

    DownloadRequest dl1 = newDownload(p);
    DownloadRequest dl2 = newDownload(p);

    assertThat(dl1.hashCode(), both(equalTo(dl1.hashCode())).and(equalTo(dl2.hashCode())));

    dl2 = newDownload(p, TEST_EMAIL);
    assertThat(dl1.hashCode(), not(equalTo(dl2.hashCode())));
  }

  @Test
  public void testSerde() {
    ObjectMapper mapper = new ObjectMapper();
    DownloadRequest d = newDownload(new EqualsPredicate(OccurrenceSearchParameter.CATALOG_NUMBER, "b"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
      mapper.writeValue(baos, d);
      baos.flush();
      DownloadRequest d2 = mapper.readValue(baos.toByteArray(), DownloadRequest.class);
      assertEquals(d, d2);

    } catch (Exception e) {
      fail(e.getMessage());
    } finally {
      Closeables.closeQuietly(baos);
    }
  }

}

@@ -883,16 +883,44 @@
 Stream;%0A
+import java.io.IOException;%0A
 import j
@@ -1008,21 +1008,17 @@
 io.Close
-ables
+r
 ;%0Aimport
@@ -3232,24 +3232,43 @@
  testSerde()
+ throws IOException
  %7B%0A    Objec
@@ -3405,24 +3405,74 @@
 BER, %22b%22));%0A
+%0A    Closer closer = Closer.create();%0A    try %7B%0A  
     ByteArra
@@ -3492,16 +3492,32 @@
  baos = 
+closer.register(
 new Byte
@@ -3535,27 +3535,18 @@
 Stream()
+)
 ;
-%0A    try %7B
 %0A      m
@@ -3725,50 +3725,113 @@
 ch (
-Exception e) %7B%0A      fail(e.getMessage());
+Throwable e) %7B // closer must catch Throwable%0A      fail(e.getMessage());%0A      throw closer.rethrow(e);%0A
 %0A   
@@ -3853,36 +3853,21 @@
     
-C
+c
 lose
-ables
+r
 .close
-Quietly(baos
+(
 );%0A 

Use a guava closer