/*
 * 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.option;

import org.hisp.dhis.android.core.common.GenericHandler;
import org.hisp.dhis.android.core.common.HandleAction;
import org.hisp.dhis.android.core.common.IdentifiableHandlerImpl;
import org.hisp.dhis.android.core.common.IdentifiableObjectStore;
import org.hisp.dhis.android.core.common.OrphanCleaner;
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.List;

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

@RunWith(JUnit4.class)
public class OptionSetHandlerShould {
    @Mock
    private IdentifiableObjectStore<OptionSetModel> optionSetStore;

    @Mock
    private OptionSet optionSet;

    @Mock
    private GenericHandler<Option, OptionModel> optionHandler;

    @Mock
    private OrphanCleaner<OptionSet, Option> optionCleaner;

    @Mock
    private List<Option> options;

    // object to test
    private OptionSetHandler optionSetHandler;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        optionSetHandler = new OptionSetHandler(optionSetStore, optionHandler, optionCleaner);
        when(optionSet.uid()).thenReturn("test_option_set_uid");
        when(optionSet.options()).thenReturn(options);
    }

    @Test
    public void handle_options() {
        optionSetHandler.handle(optionSet, new OptionSetModelBuilder());
        verify(optionHandler).handleMany(same(options), any(OptionModelBuilder.class));
    }

    @Test
    public void clean_orphan_options_after_update() {
        when(optionSetStore.updateOrInsert(any(OptionSetModel.class))).thenReturn(HandleAction.Update);
        optionSetHandler.handle(optionSet, new OptionSetModelBuilder());
        verify(optionCleaner).deleteOrphan(optionSet, options);
    }

    @Test
    public void not_clean_orphan_options_after_insert() {
        when(optionSetStore.updateOrInsert(any(OptionSetModel.class))).thenReturn(HandleAction.Insert);
        optionSetHandler.handle(optionSet, new OptionSetModelBuilder());
        verify(optionCleaner, never()).deleteOrphan(optionSet, options);
    }

    @Test
    public void extend_identifiable_handler_impl() {
        IdentifiableHandlerImpl<OptionSet, OptionSetModel> genericHandler =
                new OptionSetHandler(null,null, null);
    }
}

@@ -1627,29 +1627,45 @@
 ore.
-common.Generi
+arch.handlers.IdentifiableSyn
 cHandler
 ;%0Aim
@@ -1660,16 +1660,20 @@
 cHandler
+Impl
 ;%0Aimport
@@ -2088,16 +2088,46 @@
 tions;%0A%0A
+import java.util.Collections;%0A
 import j
@@ -2186,49 +2186,8 @@
 ny;%0A
-import static org.mockito.Matchers.same;%0A
 impo
@@ -2516,42 +2516,42 @@
 ate 
-GenericHandler%3COption, 
+IdentifiableSyncHandlerImpl%3C
 Option
-Model
 %3E op
@@ -2637,32 +2637,60 @@
 ner;%0A%0A    @Mock%0A
+    private Option option;%0A%0A
     private List
@@ -3037,16 +3037,69 @@
 _uid%22);%0A
+        options = Collections.singletonList(option);%0A
         
@@ -3315,21 +3315,16 @@
 any(
-same(
 options)
 , an
@@ -3323,40 +3323,8 @@
 ons)
-, any(OptionModelBuilder.class))
 ;%0A  

fix OptionSetHandlerShould