/*
 */
package org.datadryad.rest.storage.rdbms;

import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import org.datadryad.rest.models.Organization;
import org.datadryad.rest.storage.StoragePath;
import org.datadryad.test.ContextUnitTest;
import org.dspace.core.Context;
import org.dspace.storage.rdbms.DatabaseManager;
import org.dspace.storage.rdbms.TableRow;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Dan Leehr <dan.leehr@nescent.org>
 */
public class OrganizationDatabaseStorageImplTest extends ContextUnitTest {
    private static Logger log = Logger.getLogger(OrganizationDatabaseStorageImplTest.class);
    private static final Integer TEST_ORGANIZATION_ID = 1;
    private static final String TEST_ORGANIZATION_CODE_1 = "test1";
    private static final String TEST_ORGANIZATION_NAME_1 = "Test Organization 1";
    private static final String TEST_ORGANIZATION_CODE_2 = "test2";
    private static final String TEST_ORGANIZATION_NAME_2 = "Test Organization 2";

    @Before
    public void setUp() {
        super.setUp();
        // Create a row in the database
        Organization organization = new Organization();
        organization.organizationCode = TEST_ORGANIZATION_CODE_1;
        organization.organizationName = TEST_ORGANIZATION_NAME_1;
        TableRow row = OrganizationDatabaseStorageImpl.tableRowFromOrganization(organization);
        row.setColumn(OrganizationDatabaseStorageImpl.COLUMN_ID, TEST_ORGANIZATION_ID);
        try {
            DatabaseManager.deleteByValue(context, OrganizationDatabaseStorageImpl.ORGANIZATION_TABLE, OrganizationDatabaseStorageImpl.COLUMN_ID, TEST_ORGANIZATION_ID);
            DatabaseManager.insert(context, row);
            context.commit();
        } catch (SQLException ex) {
            fail("Exception setting up test organization: " + ex);
        }
    }

    @Override
    public void tearDown() {
        try {
            DatabaseManager.deleteByValue(context, OrganizationDatabaseStorageImpl.ORGANIZATION_TABLE, OrganizationDatabaseStorageImpl.COLUMN_ID, 1);
            context.complete();
        } catch (SQLException ex) {
            fail("Exception clearing test organization: " + ex);
        }
        super.tearDown();
    }

    /**
     * Test of objectExists method, of class OrganizationDatabaseStorageImpl.
     */
    @Test
    public void testObjectExists() throws Exception {
        log.info("objectExists");
        StoragePath path = new StoragePath();
        Organization organization = new Organization();
        organization.organizationCode = TEST_ORGANIZATION_CODE_1;
        organization.organizationName = TEST_ORGANIZATION_NAME_1;
        OrganizationDatabaseStorageImpl instance = new OrganizationDatabaseStorageImpl();
        Boolean expResult = true;
        Boolean result = instance.objectExists(path, organization);
        assertEquals("Object should exist", expResult, result);
    }

    /**
     * Test of addAll method, of class OrganizationDatabaseStorageImpl.
     */
    @Test
    public void testAddAll() throws Exception {
        log.info("addAll");
        StoragePath path = new StoragePath();
        List<Organization> organizations = new ArrayList<Organization>();
        OrganizationDatabaseStorageImpl instance = new OrganizationDatabaseStorageImpl();
        instance.addAll(path, organizations);
        Integer expectedSize = 1;
        Integer actualSize = organizations.size();
        assertEquals("There should be 1 organization", expectedSize, actualSize);
    }

    /**
     * Test of saveObject method, of class OrganizationDatabaseStorageImpl.
     */
    @Test
    public void testSaveObject() throws Exception {
        log.info("saveObject");
        StoragePath path = new StoragePath();
        Organization organization = new Organization();
        organization.organizationCode = TEST_ORGANIZATION_CODE_2;
        organization.organizationName = TEST_ORGANIZATION_NAME_2;
        OrganizationDatabaseStorageImpl instance = new OrganizationDatabaseStorageImpl();
        instance.saveObject(path, organization);
        Boolean exists = instance.objectExists(path, organization);
        assertTrue("Newly saved object should exist", exists);
    }

    /**
     * Test of readObject method, of class OrganizationDatabaseStorageImpl.
     */
    @Test
    public void testReadObject() throws Exception {
        log.info("readObject");
        // Read object requires full storage path
        StoragePath path = new StoragePath();
        path.addPathElement("organizationCode", TEST_ORGANIZATION_CODE_1);
        OrganizationDatabaseStorageImpl instance = new OrganizationDatabaseStorageImpl();
        String expectedName = TEST_ORGANIZATION_NAME_1;
        Organization result = instance.readObject(path);
        String resultName = result.organizationName;
        assertEquals("Read object should have same name as original", expectedName, resultName);
    }

    /**
     * Test of deleteObject method, of class OrganizationDatabaseStorageImpl.
     */
    @Test
    public void testDeleteObject() throws Exception {
        log.info("deleteObject");
        // Delete object requires full storage path
        StoragePath path = new StoragePath();
        path.addPathElement("organizationCode", TEST_ORGANIZATION_CODE_1);
        OrganizationDatabaseStorageImpl instance = new OrganizationDatabaseStorageImpl();
        instance.deleteObject(path);
        Organization dummyOrganization = new Organization();
        dummyOrganization.organizationCode = TEST_ORGANIZATION_CODE_1;
        Boolean exists = instance.objectExists(path, dummyOrganization);
        assertFalse("Deleted object should not exist", exists);
    }
}

@@ -297,40 +297,8 @@
 st;%0A
-import org.dspace.core.Context;%0A
 impo
@@ -405,97 +405,14 @@
 nit.
-After;%0Aimport org.junit.AfterClass;%0Aimport org.junit.Before;%0Aimport org.junit.BeforeClass
+Before
 ;%0Aim
@@ -695,67 +695,8 @@
 s);%0A
-    private static final Integer TEST_ORGANIZATION_ID = 1;%0A
     
@@ -1388,22 +1388,117 @@
     
-row.setColumn(
+try %7B%0A            DatabaseManager.deleteByValue(context, OrganizationDatabaseStorageImpl.ORGANIZATION_TABLE, 
 Orga
@@ -1524,34 +1524,36 @@
 rageImpl.COLUMN_
-ID
+CODE
 , TEST_ORGANIZAT
@@ -1560,26 +1560,16 @@
 ION_
-ID);%0A        try %7B
+CODE_1);
 %0A   
@@ -1707,18 +1707,20 @@
 .COLUMN_
-ID
+CODE
 , TEST_O
@@ -1731,18 +1731,22 @@
 IZATION_
-ID
+CODE_2
 );%0A     
@@ -2143,45 +2143,213 @@
 UMN_
-ID, 1);%0A            context.complete(
+CODE, TEST_ORGANIZATION_CODE_1);%0A            DatabaseManager.deleteByValue(context, OrganizationDatabaseStorageImpl.ORGANIZATION_TABLE, OrganizationDatabaseStorageImpl.COLUMN_CODE, TEST_ORGANIZATION_CODE_2
 );%0A 

Tidy up organization setUp/tearDown