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

import org.assertj.core.util.Lists;
import org.hisp.dhis.android.core.dataset.DataSet;
import org.hisp.dhis.android.core.program.Program;
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.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.mockito.Mockito.when;

@RunWith(JUnit4.class)
public class OrganisationUnitModelBuilderShould {

    @Mock
    private OrganisationUnit parent;

    @Mock
    private OrganisationUnit grandparent;

    @Mock
    private OrganisationUnit pojo;

    @Before
    @SuppressWarnings("unchecked")
    public void setUp() throws IOException {
        MockitoAnnotations.initMocks(this);


        pojo = OrganisationUnit.create(
                "uid",
                "code",
                "name",
                "displayName",
                new Date(),
                new Date(),
                "shortName",
                "displayShortName",
                "description",
                "displayDescription",
                parent,
                "path",
                new Date(),
                new Date(),
                3,
                new ArrayList<Program>(),
                new ArrayList<DataSet>(),
                Lists.newArrayList(grandparent, parent),
                false
        );

        when(parent.uid()).thenReturn("parentUid");
        when(parent.displayName()).thenReturn("parentDisplayName");
        when(grandparent.displayName()).thenReturn("grandparentDisplayName");
    }

    @Test
    public void copy_pojo_identifiable_properties() {
        OrganisationUnitModel model = new OrganisationUnitModelBuilder().buildModel(pojo);

        assertThat(model.uid()).isEqualTo(pojo.uid());
        assertThat(model.code()).isEqualTo(pojo.code());
        assertThat(model.name()).isEqualTo(pojo.name());
        assertThat(model.displayName()).isEqualTo(pojo.displayName());
        assertThat(model.created()).isEqualTo(pojo.created());
        assertThat(model.lastUpdated()).isEqualTo(pojo.lastUpdated());
    }

    @Test
    public void copy_pojo_nameable_properties() {
        OrganisationUnitModel model = new OrganisationUnitModelBuilder().buildModel(pojo);

        assertThat(model.shortName()).isEqualTo(pojo.shortName());
        assertThat(model.displayShortName()).isEqualTo(pojo.displayShortName());
        assertThat(model.description()).isEqualTo(pojo.description());
        assertThat(model.displayDescription()).isEqualTo(pojo.displayDescription());
    }

    @Test
    public void copy_pojo_organisation_unit_properties() {
        OrganisationUnitModel model = new OrganisationUnitModelBuilder().buildModel(pojo);

        assertThat(model.path()).isEqualTo(pojo.path());
        assertThat(model.openingDate()).isEqualTo(pojo.openingDate());
        assertThat(model.closedDate()).isEqualTo(pojo.closedDate());
        assertThat(model.level()).isEqualTo(pojo.level());
    }

    @Test
    public void copy_pojo_organisation_parent_uid() {
        OrganisationUnitModel model = new OrganisationUnitModelBuilder().buildModel(pojo);

        assertThat(model.parent()).isEqualTo(parent.uid());
    }

    @Test
    public void build_display_name_path_from_ancestors() {
        OrganisationUnitModel model = new OrganisationUnitModelBuilder().buildModel(pojo);

        String expectedDisplayNamePath = "/" + grandparent.displayName() + "/" + parent.displayName() + "/" +
                pojo.displayName();
        assertThat(model.displayNamePath()).isEqualTo(expectedDisplayNamePath);
    }
}

@@ -1636,16 +1636,97 @@
 .Lists;%0A
+import org.hisp.dhis.android.core.common.IdentifiableModelBuilderAbstractShould;%0A
 import o
@@ -2253,17 +2253,191 @@
 rShould 
-%7B
+extends IdentifiableModelBuilderAbstractShould%3COrganisationUnit,%0A        OrganisationUnitModel%3E %7B%0A%0A    private OrganisationUnit pojo;%0A%0A    private OrganisationUnitModel model;
 %0A%0A    @M
@@ -2535,54 +2535,8 @@
 t;%0A%0A
-    @Mock%0A    private OrganisationUnit pojo;%0A%0A
     
@@ -2672,23 +2672,345 @@
 );%0A%0A
-%0A        pojo =
+        when(parent.uid()).thenReturn(%22parentUid%22);%0A        when(parent.displayName()).thenReturn(%22parentDisplayName%22);%0A        when(grandparent.displayName()).thenReturn(%22grandparentDisplayName%22);%0A%0A        pojo = buildPojo();%0A        model = buildModel();%0A    %7D%0A%0A    @Override%0A    protected OrganisationUnit buildPojo() %7B%0A        return
  Org
@@ -3624,319 +3624,42 @@
  );%0A
-%0A
     
-    when(parent.uid()).thenReturn(%22parentUid%22);%0A        when(parent.displayName()).thenReturn(%22parentDisplayName%22);%0A        when(grandparent.displayName()).thenReturn(%22grandparentDisplayName%22);%0A    %7D%0A%0A    @Test%0A    public void copy_pojo_identifiable_properties() %7B%0A        OrganisationUnitModel model = new
+%7D%0A%0A    @Override%0A    protected
  Org
@@ -3672,34 +3672,25 @@
 ionUnitModel
-Builder().
+ 
 buildModel(p
@@ -3692,548 +3692,146 @@
 del(
-pojo);%0A%0A        assertThat(model.uid()).isEqualTo(pojo.uid());%0A        assertThat(model.code()).isEqualTo(pojo.code());%0A        assertThat(model.name()).isEqualTo(pojo.name());%0A        assertThat(model.displayName()).isEqualTo(pojo.displayName());%0A        assertThat(model.created()).isEqualTo(pojo.created());%0A        assertThat(model.lastUpdated()).isEqualTo(pojo.lastUpdated());%0A    %7D%0A%0A    @Test%0A    public void copy_pojo_nameable_properties() %7B%0A        OrganisationUnitModel model = new OrganisationUnitModelBuilder().buildModel(pojo);%0A
+) %7B%0A        return new OrganisationUnitModelBuilder().buildModel(pojo);%0A    %7D%0A%0A    @Test%0A    public void copy_pojo_nameable_properties() %7B
 %0A   
@@ -4211,100 +4211,8 @@
 ) %7B%0A
-        OrganisationUnitModel model = new OrganisationUnitModelBuilder().buildModel(pojo);%0A%0A
     
@@ -4538,100 +4538,8 @@
 ) %7B%0A
-        OrganisationUnitModel model = new OrganisationUnitModelBuilder().buildModel(pojo);%0A%0A
     
@@ -4674,100 +4674,8 @@
 ) %7B%0A
-        OrganisationUnitModel model = new OrganisationUnitModelBuilder().buildModel(pojo);%0A%0A
     

use IdentifiableModelBuilderAbstractShould in OrganisationUnitModelBuilderShould