/*
 * Copyright (c) 2017, University of Oslo
 *
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.hisp.dhis.android.core.common;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.Arrays;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(JUnit4.class)
public class IdentifiableHandlerShould {

    private interface TestCall<A> {
        void call(A a);
    }
    
    abstract class NameableMockModelInterface extends BaseIdentifiableObjectModel implements StatementBinder {
    }

    @Mock
    private TestCall<BaseIdentifiableObject> testCall;

    @Mock
    private IdentifiableObjectStore<NameableMockModelInterface> store;

    @Mock
    private BaseIdentifiableObject pojo;

    @Mock
    private BaseIdentifiableObject pojo2;
    
    @Mock
    private NameableMockModelInterface model;

    private GenericHandler<BaseIdentifiableObject> genericHandler;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        when(pojo.uid()).thenReturn("uid");
        when(pojo2.uid()).thenReturn("uid2");
        when(model.uid()).thenReturn("uid");

        genericHandler = new IdentifiableHandlerImpl<BaseIdentifiableObject, NameableMockModelInterface>(store) {
            @Override
            protected NameableMockModelInterface pojoToModel(BaseIdentifiableObject BaseIdentifiableObject) {
                return model;
            }

            @Override
            protected void afterObjectPersisted(BaseIdentifiableObject BaseIdentifiableObject) {
                testCall.call(BaseIdentifiableObject);
            }
        };
    }

    @Test
    public void do_nothing_for_null() throws Exception {
        genericHandler.handle(null);

        verify(store, never()).delete(anyString());
        verify(store, never()).update(any(NameableMockModelInterface.class));
        verify(store, never()).insert(any(NameableMockModelInterface.class));
        verify(store, never()).updateOrInsert(any(NameableMockModelInterface.class));
    }

    @Test
    public void delete_when_deleted_is_true() throws Exception {
        when(pojo.deleted()).thenReturn(Boolean.TRUE);

        genericHandler.handle(pojo);

        verify(store).delete(pojo.uid());
        verify(store, never()).update(any(NameableMockModelInterface.class));
        verify(store, never()).insert(any(NameableMockModelInterface.class));
        verify(store, never()).updateOrInsert(any(NameableMockModelInterface.class));
    }

    @Test
    public void call_update_or_insert_when_deleted_is_false() throws Exception {
        when(pojo.deleted()).thenReturn(Boolean.FALSE);

        genericHandler.handle(pojo);

        verify(store).updateOrInsert(any(NameableMockModelInterface.class));
        verify(store, never()).update(any(NameableMockModelInterface.class));
        verify(store, never()).insert(any(NameableMockModelInterface.class));
        verify(store, never()).delete(anyString());
    }

    @Test
    public void call_after_object_persisted() throws Exception {
        genericHandler.handle(pojo);
        verify(testCall).call(pojo);
    }

    @Test
    public void handle_multiple_pojos() throws Exception {
        genericHandler.handleMany(Arrays.asList(pojo, pojo2));

        verify(store, times(2)).updateOrInsert(any(NameableMockModelInterface.class));
        verify(store, never()).update(any(NameableMockModelInterface.class));
        verify(store, never()).insert(any(NameableMockModelInterface.class));
        verify(store, never()).delete(anyString());

        verify(testCall).call(pojo);
        verify(testCall).call(pojo2);
    }
}

@@ -2551,21 +2551,17 @@
  pojo2;%0A
-    
 %0A
+
     @Moc
@@ -2605,24 +2605,126 @@
 ace model;%0A%0A
+    @Mock%0A    private ModelBuilder%3CBaseIdentifiableObject, NameableMockModelInterface%3E modelBuilder;%0A%0A
     private 
@@ -2760,16 +2760,44 @@
 leObject
+, NameableMockModelInterface
 %3E generi
@@ -3161,185 +3161,8 @@
 ) %7B%0A
-            @Override%0A            protected NameableMockModelInterface pojoToModel(BaseIdentifiableObject BaseIdentifiableObject) %7B%0A                return model;%0A            %7D%0A%0A
     
@@ -3460,16 +3460,22 @@
 .handle(
+null, 
 null);%0A%0A
@@ -3932,32 +3932,46 @@
 dler.handle(pojo
+, modelBuilder
 );%0A%0A        veri
@@ -4431,16 +4431,30 @@
 dle(pojo
+, modelBuilder
 );%0A%0A    
@@ -4846,24 +4846,38 @@
 .handle(pojo
+, modelBuilder
 );%0A        v
@@ -4972,32 +4972,32 @@
 ows Exception %7B%0A
-
         genericH
@@ -5040,16 +5040,30 @@
 , pojo2)
+, modelBuilder
 );%0A%0A    

fix IdentifiableHandlerShould