Preview:
package com.infonow.crux.dao.orm.hibernate;

import com.infonow.crux.*;
import com.infonow.crux.dao.DaoException;
import com.infonow.crux.dao.InowProfileCustomerDao;
import com.infonow.crux.dao.OrchestrationInserts;
import com.infonow.crux.dao.jdbc.mapper.InowProfileRowMapper;
import com.infonow.crux.impl.ClassificationTypeImpl;
import com.infonow.crux.impl.CountryImpl;
import com.infonow.crux.impl.CustomerImpl;
import com.infonow.crux.impl.InowProfileImpl;
import com.infonow.crux.learning.LearnedName;
import com.infonow.crux.query.PagedQueryResult;
import com.infonow.crux.svcClient.InowProfileCountryCode;
import com.infonow.crux.svcClient.InowProfileCustomerType;
import com.infonow.framework.util.spring.*;
import java.sql.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.*;
import org.mockito.*;
import org.springframework.jdbc.core.*;
import org.springframework.jdbc.core.namedparam.*;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.DefaultTransactionStatus;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.*;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

public class InowProfileDaoTest
{

    @Mock
    private JdbcTemplateProxy jdbcTemplate;
    
    @Mock
    private NamedParameterJdbcTemplateProxy namedParameterJdbcTemplate;

    @Mock
    private CountryDao countryDao;

    @Mock
    private DataSource dataSource;

    @Mock
    private PlatformTransactionManager platformTransactionManager;

    @Mock
    private TransactionTemplate transactionTemplate;

    @InjectMocks
    private InowProfileDao inowProfileDao;

    private InowProfileDao.ComponentSubSegmentationRowMapper rowMapper;

    @Before
    public void setUp()
    {
        MockitoAnnotations.initMocks(this);
        when(jdbcTemplate.getDataSource()).thenReturn(dataSource);
    }

    @Test
    public void testNextId()
    {
        List<Long> mockIdList = new ArrayList<Long>(); // A valid list of Longs
        
        mockIdList.add(1L);
        mockIdList.add(2L);
        mockIdList.add(3L);
        when(jdbcTemplate.queryForList(anyString(), any(), eq(Long.class)))
        .thenReturn(mockIdList);
        
        Long nextId = inowProfileDao.nextId();
        assertNotNull(nextId);
    }

    @Test
    public void testCreateIdentifier()
    {
        String identifier = inowProfileDao.createIdentifier(1L);
        assertNotNull(identifier);
        assertEquals("INOW-00000000000001", identifier);
    }

    @Test
    public void testGetInowProfiles() throws DaoException
    {
        List<String> identifiers = Arrays.asList("INOW-12345");
        String customerId = "customer1";
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setIdentifier("INOW-12345");
        List<InowProfile> inowProfiles = Arrays.asList(inowProfile);
        when(jdbcTemplate.query(anyString(), any(Object[].class),
            any(RowMapper.class))).thenReturn(inowProfiles);

        Map<String, InowProfile> result = inowProfileDao.
            getInowProfiles(identifiers, customerId);
        assertNotNull(result);
        assertEquals(1, result.size());
        assertEquals(inowProfile, result.get("INOW-12345"));
    }

    @Test
    public void testGetBySid()
    {
        Long sid = 1L;
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setSid(sid);
        List<InowProfile> inowProfiles = Arrays.asList(inowProfile);
        when(jdbcTemplate.query(anyString(), any(Object[].class),
            any(RowMapper.class))).thenReturn(inowProfiles);

        InowProfile result = inowProfileDao.getBySid(sid);
        assertNotNull(result);
        assertEquals(inowProfile, result);

        sid = null;
        assertNull(inowProfileDao.getBySid(sid));

        sid = -1L;
        assertNull(inowProfileDao.getBySid(sid));
    }

    @Test
    public void testFindById()
    {
        String id = "INOW-00000000010002";
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setIdentifier(id);
        List<InowProfile> inowProfiles = Arrays.asList(inowProfile);
        when(jdbcTemplate.query(anyString(), any(Object[].class),
            any(RowMapper.class))).thenReturn(inowProfiles);

        InowProfile result = inowProfileDao.findById(id);
        assertNotNull(result);
        assertEquals(inowProfile, result);

        id = "";
        assertNull(inowProfileDao.findById(id));
    }

    @Test
    public void testFindWhereIndividualIsNull() throws SQLException
    {
        com.infonow.crux.dao.InowProfileDao.InowProfileResultHandler
            resultHandler = mock(
            com.infonow.crux.dao.InowProfileDao.
                InowProfileResultHandler.class);
        
        // Mock ResultSet and ResultSetMetaData
        ResultSet rs = mock(ResultSet.class);
        ResultSetMetaData metaData = mock(ResultSetMetaData.class);
        
        // Mock ResultSetMetaData behavior
        when(metaData.getColumnCount()).thenReturn(3); // Adjust based on actual column count
        when(metaData.getColumnName(1)).thenReturn("ENTITY_NAME");
        when(metaData.getColumnName(2)).thenReturn("SID");
        when(metaData.getColumnName(3)).thenReturn("CLASSIFICATION_TYPE_SID");
        when(rs.getMetaData()).thenReturn(metaData);
        
        // Mock ResultSet behavior
        when(rs.getString("ENTITY_NAME")).thenReturn("BASE REGISTERAPI");
        when(rs.getLong("SID")).thenReturn(10001L);
        when(rs.getLong("CLASSIFICATION_TYPE_SID")).thenReturn(15L);
    
        doAnswer(invocation -> {
            InowProfileRowMapper mapper = new InowProfileRowMapper();
            InowProfile inowProfile = (InowProfile) mapper.mapRow(rs, 1);
            resultHandler.onResult(inowProfile);
            return null;
        }).when(jdbcTemplate).query(anyString(), any(RowCallbackHandler.class));


        inowProfileDao.findWhereIndividualIsNull(resultHandler);
        verify(resultHandler, times(1)).onResult(any(InowProfile.class));
    }

    @Test
    public void testUpdateIndividualAndClassification()
    {
        List<InowProfile> inowProfiles = new ArrayList<>();
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setSid(1L);
        inowProfiles.add(inowProfile);

        inowProfileDao.updateIndividualAndClassification(inowProfiles);
        verify(jdbcTemplate, times(1)).batchUpdate(anyString(),
            any(BatchPreparedStatementSetter.class));
    }

    @Test
    public void testGetRandomInowProfile()
    {
        InowProfile inowProfile = new InowProfileImpl();
        List<InowProfile> inowProfiles = Arrays.asList(inowProfile);
        when(jdbcTemplate.query(anyString(), any(RowMapper.class)))
            .thenReturn(inowProfiles);

        InowProfile result = inowProfileDao.getRandomInowProfile();
        assertNotNull(result);
        assertEquals(inowProfile, result);
    }

    @Test
    public void testGetRecordCountForSearch() throws DaoException
    {
        when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class))).thenReturn(1);
        int count = inowProfileDao.getRecordCountForSearch(
            "INOW-00000000010002", "BASE REGISTERAPI", "Test Street 1",
            "TestStreet2", "Test City", "Test State Province",
            "Test Postal Code", "Test Country", false);

        assertNotNull(count);

        assertNull(inowProfileDao.getRecordCountForSearch("", "", "", "", "",
            "", "", "", false));

        int countWithoutId = inowProfileDao.getRecordCountForSearch("",
            "BASE REGISTERAPI", "", "", "", "", "", "", false);

        assertNotNull(countWithoutId);

        int countWithStreet1 = inowProfileDao.getRecordCountForSearch("",
            "", "street1", "", "", "", "", "", false);

        assertNotNull(countWithStreet1);

        int countWithStreet2 = inowProfileDao.getRecordCountForSearch("",
            "", "", "street2", "", "", "", "", false);

        assertNotNull(countWithStreet2);

        int countWithCity = inowProfileDao.getRecordCountForSearch("",
            "", "", "", "city", "", "", "", false);

        assertNotNull(countWithCity);

        int countWithStateProvince = inowProfileDao.getRecordCountForSearch("",
            "", "", "", "", "stateProvince", "", "", false);

        assertNotNull(countWithStateProvince);

        int countWithPostalCode = inowProfileDao.getRecordCountForSearch("",
            "", "", "", "", "", "postalCode", "", false);

        assertNotNull(countWithPostalCode);

        int countWithCountry = inowProfileDao.getRecordCountForSearch("",
            "", "", "", "", "", "", "country", false);

        assertNotNull(countWithCountry);
    }

    @Test
    public void testFindByWildcard() throws DaoException
    {
        List<InowProfile> inowProfiles = new ArrayList<>();
        InowProfile inowProfile = new InowProfileImpl();
        inowProfiles.add(inowProfile);
        when(jdbcTemplate.query(anyString(), any(Object[].class),
            any(RowMapper.class))).thenReturn(inowProfiles);

        List<InowProfile> result = inowProfileDao.findByWildcard("id",
            "name", "street1", "street2", "city", "state", "postal",
            "country", false, 0, 10);

        assertNotNull(result);
        assertEquals(1, result.size());
        assertEquals(inowProfile, result.get(0));
        assertNull(inowProfileDao.findByWildcard("", "", "", "", "",
            "", "", "", false, 0, 10));

        List<InowProfile> resultwithNegativePageStart = inowProfileDao.
            findByWildcard("id", "name", "street1", "street2",
                "city", "state", "postal", "country", false, -1, 10);

        assertNotNull(resultwithNegativePageStart);

        List<InowProfile> resultwithNameWildCard = inowProfileDao.findByWildcard(
            "", "name", "", "", "", "", "", "", false, 1, 10);

        assertNotNull(resultwithNameWildCard);

        List<InowProfile> resultwithStreet1WildCard = inowProfileDao.
            findByWildcard("", "", "Street1", "", "", "", "", "", false,
                1, 10);

        assertNotNull(resultwithStreet1WildCard);

        List<InowProfile> resultwithStreet2WildCard = inowProfileDao.
            findByWildcard("", "", "", "Street2", "", "", "", "", false, 1,
                10);

        assertNotNull(resultwithStreet2WildCard);

        List<InowProfile> resultwithCityWildCard = inowProfileDao.
            findByWildcard("", "", "", "", "city", "", "", "", false, 1,
                10);

        assertNotNull(resultwithCityWildCard);

        List<InowProfile> resultwithStateProvinceWildCard = inowProfileDao.
            findByWildcard("", "", "", "", "", "stateProvince", "", "", false, 1, 10);

        assertNotNull(resultwithStateProvinceWildCard);

        List<InowProfile> resultwithPostalCodeWildCard = inowProfileDao.
            findByWildcard("", "", "", "", "", "",
                "postalCode", "", false, 1, 10);

        assertNotNull(resultwithPostalCodeWildCard);

        List<InowProfile> resultwithCountryWildCard = inowProfileDao.
            findByWildcard("", "", "", "", "", "", "",
                "Country", false, 1, 10);

        assertNotNull(resultwithCountryWildCard);
    }

    @Test
    public void testUpdateCountries() throws DaoException
    {

        List<InowProfileCountryCode> inowProfileCountryCodes = new ArrayList<>();
        InowProfileCountryCode inowProfileCountryCode = new
            InowProfileCountryCode(10002L, "US");
        inowProfileCountryCodes.add(inowProfileCountryCode);

        DefaultTransactionStatus transactionStatus =
            mock(DefaultTransactionStatus.class);

        when(platformTransactionManager.getTransaction
            (any(DefaultTransactionDefinition.class))).thenReturn
            (transactionStatus);

        when(transactionTemplate.getTransactionManager()).
            thenReturn(platformTransactionManager);

        inowProfileDao.updateCountries(inowProfileCountryCodes);

        verify(jdbcTemplate, times(3)).
            batchUpdate(anyString(), any(BatchPreparedStatementSetter.class));

        verify(platformTransactionManager, times(1)).
            commit(transactionStatus);
    }

    @Test
    public void testUpdateGrade() throws DaoException
    {
        when(namedParameterJdbcTemplate.update(anyString(), any(Map.class))).thenReturn(1);

        inowProfileDao.updateGrade(10002L, 90L);

        verify(namedParameterJdbcTemplate, times(1)).update(anyString(), any(Map.class));
    }

    @Test
    public void testUpdateGradeAndIsValidFields() throws DaoException
    {
        when(jdbcTemplate.update(anyString(), any(Object[].class))).thenReturn(1);
        inowProfileDao.updateGradeAndIsValidFields(10002L, 80L, true,
            true, true);
        verify(jdbcTemplate, times(1)).update(
            eq("update inow_profile set grade = ?, update_date = sysdate, " +
                "valid_postalcode = ?, valid_stateprovince = ?, " +
                "valid_city = ?  where sid = ? "),
            eq(new Object[] { 80L, 1, 1, 1, 10002L })
        );
    }

    @Test
    public void testUpdateAddressFields()
    {
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setSid(3616L);
        inowProfile.setName("Test Name");
        inowProfile.setStreet1("Street 1");
        inowProfile.setStreet2("Street 2");
        inowProfile.setCity("City");
        inowProfile.setStateProvince("State");
        inowProfile.setStateProvinceCode("SPC");
        inowProfile.setPostalCode("Postal");
        inowProfile.setValidPostalCode(true);
        inowProfile.setValidStateProvince(true);
        inowProfile.setValidCity(true);
        inowProfile.setCountry("Country");
        inowProfile.setCountryObject(new CountryImpl());

        when(jdbcTemplate.update(anyString(), any(Object[].class))).
            thenReturn(1);
        int result = inowProfileDao.updateAddressFields(inowProfile);
        assertEquals(1, result);
    }

    @Test
    public void testFindUnclassified() throws DaoException
    {
        List<Customer> customers = new ArrayList<>();
        Customer customer = new CustomerImpl(2L, "QCOM");
        customers.add(customer);

        List<InowProfile> inowProfiles = new ArrayList<>();
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setSid(1L);
        inowProfile.setName("BASE REGISTERAPI");
        inowProfiles.add(inowProfile);

        when(jdbcTemplate.query(anyString(), any(Object[].class),
            any(RowMapper.class))).thenReturn(inowProfiles);

        PagedQueryResult<InowProfile> result = inowProfileDao.
            findUnclassified(customers, 0, 10, "NAME", true);
        result.setTotalCount(result.getItems().size());
        assertNotNull(result);
        assertEquals(1, result.getTotalCount());
    }

    @Test
    public void testFindUnclassifiedNegativePageStart() throws DaoException
    {
        List<Customer> customers = new ArrayList<>();
        Customer customer = new CustomerImpl(2L, "QCOM");
        customers.add(customer);

        List<InowProfile> inowProfiles = new ArrayList<>();
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setSid(1L);
        inowProfile.setName("BASE REGISTERAPI");
        inowProfiles.add(inowProfile);

        when(jdbcTemplate.query(anyString(), any(Object[].class),
            any(RowMapper.class))).thenReturn(inowProfiles);

        PagedQueryResult<InowProfile> result = inowProfileDao.
            findUnclassified(customers, -1, 10, "NAME", true);
        result.setTotalCount(result.getItems().size());
        assertNotNull(result);
        assertEquals(1, result.getTotalCount());
    }

    @Test
    public void testFindUnclassifiedNegativePageEnd() throws DaoException
    {
        List<Customer> customers = new ArrayList<>();
        Customer customer = new CustomerImpl(2L, "QCOM");
        customers.add(customer);

        List<InowProfile> inowProfiles = new ArrayList<>();
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setSid(1L);
        inowProfile.setName("BASE REGISTERAPI");
        inowProfiles.add(inowProfile);

        when(jdbcTemplate.query(anyString(), any(Object[].class),
            any(RowMapper.class))).thenReturn(inowProfiles);

        PagedQueryResult<InowProfile> result = inowProfileDao.
            findUnclassified(customers, 0, -200, "NAME", true);
        result.setTotalCount(result.getItems().size());
        assertNotNull(result);
        assertEquals(1, result.getTotalCount());
    }

    @Test
    public void testGetClassifiedEntityCount() throws DaoException
    {
        when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class))).
            thenReturn(1);
        int count = inowProfileDao.getClassifiedEntityCount();
        assertNotNull(count);
        assertTrue(count >= 0);
    }

    @Test
    public void testGetClassifiedEntityCountByCode() throws DaoException
    {
        when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class),
            any(Object[].class))).thenReturn(1);
        Integer count = inowProfileDao.
            getClassifiedEntityCount("DESIGN SERVICES");
        assertNotNull(count);
    }

    @Test
    public void testGetClassifications() throws DaoException
    {
        List<ClassificationType> classificationTypes = new ArrayList<>();
        ClassificationType classificationType = new ClassificationTypeImpl
            (15L, "DESIGN SERVICESs", "Design Services");
        classificationTypes.add(classificationType);

        when(jdbcTemplate.query(anyString(), any(RowMapper.class))).
            thenReturn(classificationTypes);

        List<ClassificationType> result = inowProfileDao.getClassifications();
        assertNotNull(result);
        assertEquals(1, result.size());
        assertEquals(classificationType, result.get(0));
    }

    @Test
    public void testGetClassificationType() throws DaoException
    {
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setSid(1L);

        ClassificationType classificationType = new ClassificationTypeImpl();
        List<ClassificationType> classificationTypes = Arrays.
            asList(classificationType);

        when(jdbcTemplate.query(anyString(), any(Object[].class),
            any(RowMapper.class))).thenReturn(classificationTypes);

        ClassificationType result = inowProfileDao.
            getClassificationType(inowProfile);
        assertNotNull(result);
        assertEquals(classificationType, result);
    }

    @Test
    public void testGetClassificationTypeByCode() throws DaoException
    {
        ClassificationType classificationType = new ClassificationTypeImpl();
        List<ClassificationType> classificationTypes = Arrays.
            asList(classificationType);

        when(jdbcTemplate.query(anyString(), any(Object[].class),
            any(RowMapper.class))).thenReturn(classificationTypes);

        ClassificationType result = inowProfileDao.
            getClassificationTypeByCode("code");
        assertNotNull(result);
        assertEquals(classificationType, result);
    }

    @Test
    public void testFindClassified() throws DaoException
    {
        List<InowProfile> inowProfiles = new ArrayList<>();
        InowProfile inowProfile = new InowProfileImpl();
        inowProfiles.add(inowProfile);

        when(jdbcTemplate.query(anyString(), any(Object[].class),
            any(RowMapper.class))).thenReturn(inowProfiles);

        List<InowProfile> result = inowProfileDao.findClassified(0, 10,
            "CITY", true);
        assertNotNull(result);
        assertEquals(1, result.size());
        assertEquals(inowProfile, result.get(0));
    }

    @Test
    public void testClassify() throws DaoException
    {
        List<InowProfileCustomerType> customerTypes = new ArrayList<>();
        InowProfileCustomerType customerType = new InowProfileCustomerType();
        customerType.setInowProfileSid(1L);
        customerType.setCustomerType("type");
        customerTypes.add(customerType);

        when(jdbcTemplate.queryForObject(anyString(), any(Object[].class),
            eq(String.class))).thenReturn("type");

        List<Long> result = inowProfileDao.classify(customerTypes);
        assertNotNull(result);
        assertEquals(1, result.size());
        assertEquals((Long) 1L, result.get(0));
    }

    @Test
    public void testSaveR2rReporter() throws DaoException
    {
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setSid(1L);

        when(jdbcTemplate.update(anyString(), any(Object[].class))).
            thenReturn(1);

        inowProfileDao.saveR2rReporter(inowProfile, "value");

        verify(jdbcTemplate, times(1)).update(anyString(),
            any(Object[].class));
    }

    @Test
    public void testGetBySalesLineItemAndAddressType() throws DaoException
    {
        InowProfile inowProfile = new InowProfileImpl();
        List<InowProfile> inowProfiles = Arrays.asList(inowProfile);

        when(jdbcTemplate.query(anyString(), any(Object[].class),
            any(RowMapper.class))).thenReturn(inowProfiles);

        InowProfile result = inowProfileDao.getBySalesLineItemAndAddressType
            ("CTH", 1L, "SOLD TO");
        assertNotNull(result);
        assertEquals(inowProfile, result);

        assertThrows(DaoException.class, () -> inowProfileDao.
            getBySalesLineItemAndAddressType("", 1L, "SOLD TO"));

        assertThrows(DaoException.class, () -> inowProfileDao.
            getBySalesLineItemAndAddressType("CTH", -1L, "SOLD TO"));

        assertThrows(DaoException.class, () -> inowProfileDao.
            getBySalesLineItemAndAddressType("CTH", 1L, ""));
    }

    @Test
    public void testDeleteDuplicate() throws DaoException
    {
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setIdentifier("10002");

        when(namedParameterJdbcTemplate.update(anyString(), any(Map.class))).thenReturn(1);

        inowProfileDao.deleteDuplicate(inowProfile);
        verify(namedParameterJdbcTemplate, times(1)).update(anyString(), any(Map.class));
    }

    @Test
    public void testSaveDuplicate() throws DaoException
    {
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setIdentifier("INOW-12345");
        InowProfile duplicateInowProfile = new InowProfileImpl();
        duplicateInowProfile.setIdentifier("INOW-54321");

        Map<InowProfile, List<InowProfile>> inowProfileDuplicateMap =
            new HashMap<>();
        List<InowProfile> duplicates = new ArrayList<>();
        duplicates.add(duplicateInowProfile);
        inowProfileDuplicateMap.put(inowProfile, duplicates);

        when(jdbcTemplate.batchUpdate(anyString(),
            any(BatchPreparedStatementSetter.class))).thenReturn(new int[] { 1 });

        inowProfileDao.saveDuplicate(inowProfileDuplicateMap, true);

        verify(jdbcTemplate, times(1)).batchUpdate(anyString(),
            any(BatchPreparedStatementSetter.class));

        inowProfileDao.saveDuplicate(inowProfileDuplicateMap, false);

        verify(jdbcTemplate, atLeastOnce()).batchUpdate(anyString(),
            any(BatchPreparedStatementSetter.class));
    }

    @Test
    public void testGetCount() throws DaoException
    {
        when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class))).
            thenReturn(1);
        Integer count = inowProfileDao.getCount();
        assertNotNull(count);
    }

    @Test
    public void testGetRecords() throws DaoException
    {
        List<InowProfile> inowProfiles = new ArrayList<>();
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setName("D&H CANADAAAAA");
        inowProfile.setStreet1("255 COURTNEY PARK DRIVE W");
        inowProfile.setStreet2("SUITE 101");
        inowProfile.setCity("MISSISSAUGA");
        inowProfile.setStateProvince("ONTARIO");
        inowProfile.setPostalCode("L5W 0A5");
        inowProfiles.add(inowProfile);

        when(jdbcTemplate.query(anyString(), any(Object[].class),
            any(RowMapper.class))).thenReturn(inowProfiles);

        List<InowProfile> result = inowProfileDao.getRecords(0, 10);
        assertNotNull(result);
        assertEquals(1, result.size());
        assertEquals(inowProfile, result.get(0));
    }

    @Test
    public void testFindByFields()
    {
        InowProfile inowProfile = new InowProfileImpl();
        ClassificationType classificationType =
            new ClassificationTypeImpl(15L, "DESIGN SERVICES",
                "Design Services");
        inowProfile.setName("BASE REGISTERAPI");
        inowProfile.setClassificationType(classificationType);
        
        when(namedParameterJdbcTemplate.queryForList(anyString(), any(Map.class),
            eq(String.class))).thenReturn(Arrays.asList("INOW-12345"));
        
        String result = inowProfileDao.findByFields(inowProfile, true);
        assertNotNull(result);
        assertEquals("INOW-12345", result);
    }

    @Test
    public void testFindExactAddressDupes()
    {
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setName("Test Name");
        inowProfile.setStreet1("Street 1");
        inowProfile.setStreet2("Street 2");
        inowProfile.setCity("City");
        inowProfile.setStateProvince("State");
        inowProfile.setPostalCode("Postal");
        inowProfile.setCountry("Country");

        List<InowProfile> inowProfiles = new ArrayList<>();
        inowProfiles.add(inowProfile);

        when(jdbcTemplate.query(anyString(), any(Object[].class),
            any(RowMapper.class))).thenReturn(inowProfiles);

        List<InowProfile> result = inowProfileDao.
            findExactAddressDupes(inowProfile);
        assertNotNull(result);
        assertEquals(1, result.size());
        assertEquals(inowProfile, result.get(0));
    }

    @Test
    public void testFindByFieldsWithNullResults()
    {
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setName("D&H CANADAAAAA");
        inowProfile.setStreet1("255 COURTNEY PARK DRIVE W");
        inowProfile.setStreet2("SUITE 101");
        inowProfile.setCity("MISSISSAUGA");
        inowProfile.setStateProvince("ONTARIO");
        inowProfile.setPostalCode("L5W 0A5");

        when(jdbcTemplate.queryForList(anyString(), any(Map.class),
            eq(String.class))).thenReturn(Collections.emptyList());

        String result = inowProfileDao.findByFields(inowProfile, true);
        assertNull(result);
    }

    @Test
    public void testFindByFieldsWithException()
    {
        InowProfile inowProfile = new InowProfileImpl();
        inowProfile.setName("Illegal Argument");

        when(namedParameterJdbcTemplate.queryForList(anyString(), any(Map.class),
            eq(String.class))).thenThrow(new RuntimeException("Database error"));

        assertThrows(RuntimeException.class, () -> inowProfileDao.
            findByFields(inowProfile, true));
    }

    @Test
    public void testFindClassificationType()
    {
        String code = "Code";
        
        List<ClassificationType> classificationTypes = new ArrayList<>();
        classificationTypes.add(new ClassificationTypeImpl(1L, "CT01", "Test Classification"));
        
        when(jdbcTemplate.query(anyString(), any(Object[].class),
            any(RowMapper.class))).thenReturn(classificationTypes);
        
        assertNotNull(inowProfileDao.findClassificationType(code));
        
        List<ClassificationType> classificationTypesMultiple = new ArrayList<>();
        
        classificationTypesMultiple.add(new ClassificationTypeImpl(1L, "CT01", "Test Classification"));
        
        classificationTypesMultiple.add(new ClassificationTypeImpl(2L, "CT02", "Test Classification 2"));
        
        when(jdbcTemplate.query(anyString(), any(Object[].class),
            any(RowMapper.class))).thenReturn(classificationTypesMultiple);
        
        ClassificationType classificationType = inowProfileDao.findClassificationType(code);
        
        assertEquals(classificationTypesMultiple.get(0), classificationType);
    }

    @Test
    public void testFindComponentSegmentation()
    {
        String name = "Name";
        assertNull(inowProfileDao.findComponentSegmentation(name));
    }

    @Test
    public void testFindComponentSubSegmentation()
    {
        String name = "Name";
        assertNull(inowProfileDao.findComponentSubSegmentation(name));
    }

    @Test
    public void testFindMatchedProfilesWithLearnedName()
    {
        LearnedName learnedName = new LearnedName("fromName", "toName");
        assertTrue(inowProfileDao.findMatchedProfilesWithLearnedName(learnedName).isEmpty());
    }

    @Test
    public void testCreateInfoNowId()
    {
        InowProfile inowProfile = new InowProfileImpl();
        String id = "12345";
        String expectedInfoNowId = "INOW-00000000012345";
        inowProfile.setIdentifier(expectedInfoNowId);
        inowProfile.setSid(1L);

        assertThrows(DaoException.class, () -> inowProfileDao.
            createInfoNowId(inowProfile));

        assertNotNull(inowProfile.getIdentifier());
        assertEquals(expectedInfoNowId, inowProfile.getIdentifier());
    }

    @Test
    public void testCreateInfoNowIdNull()
    {
        InowProfile inowProfile = null;
        assertThrows(IllegalArgumentException.class, () -> inowProfileDao.
            createInfoNowId(inowProfile));
    }

    @Test
    public void testSetInowProfileCustomerDao()
    {
        InowProfileCustomerDao inowProfileCustomerDao =
            mock(InowProfileCustomerDao.class);
        inowProfileCustomerDao.createInowProfileCustomer(1L, 1L);
        inowProfileDao.setInowProfileCustomerDao(inowProfileCustomerDao);

        assertNotNull(inowProfileDao.getInowProfileCustomerDao());
        assertEquals(inowProfileCustomerDao, inowProfileDao.
            getInowProfileCustomerDao());
    }

    @Test
    public void testSetOrchestrationDao()
    {
        OrchestrationInserts inowProfileOrchestrationDao =
            mock(OrchestrationInserts.class);
        inowProfileOrchestrationDao.insert(1L);

        inowProfileDao.setOrchestrationDao(inowProfileOrchestrationDao);
        assertNotNull(inowProfileDao.getOrchestrationDao());
        assertEquals(inowProfileOrchestrationDao, inowProfileDao.
            getOrchestrationDao());
    }
    
    @Test
    public void testBulkInsert() throws SQLException
    {
        PreparedStatement preparedStatement = mock(PreparedStatement.class);

        InowProfile inowProfileNegativeSid = new InowProfileImpl();
        inowProfileNegativeSid.setName("D&H CANADAAAAA");
        inowProfileNegativeSid.setStreet1("255 COURTNEY PARK DRIVE W");
        inowProfileNegativeSid.setStreet2("SUITE 101");
        inowProfileNegativeSid.setCity("MISSISSAUGA");
        inowProfileNegativeSid.setStateProvince("ONTARIO");
        inowProfileNegativeSid.setPostalCode("L5W 0A5");
        inowProfileNegativeSid.setSid(-1L);
        inowProfileNegativeSid.setIdentifier("1");

        InowProfile inowProfileEmptyIdentifier = new InowProfileImpl();
        inowProfileEmptyIdentifier.setSid(2L);
        inowProfileEmptyIdentifier.setName("Test Name 2");
        inowProfileEmptyIdentifier.setStreet1("Street 3");
        inowProfileEmptyIdentifier.setStreet2("Street 4");
        inowProfileEmptyIdentifier.setCity("City 2");
        inowProfileEmptyIdentifier.setStateProvince("State 2");
        inowProfileEmptyIdentifier.setPostalCode("Postal 2");
        inowProfileEmptyIdentifier.setCountry("Country 2");
        inowProfileNegativeSid.setIdentifier("");

        List<InowProfile> values = Arrays.asList(inowProfileNegativeSid,
            inowProfileEmptyIdentifier);
        InowProfileDao.BulkInsert bulkInsert =
            inowProfileDao.new BulkInsert(values);
        
        List<Long> mockIdList = new ArrayList<Long>(); // A valid list of Longs
        
        mockIdList.add(1L);
        mockIdList.add(2L);
        mockIdList.add(3L);
        
        when(jdbcTemplate.
            queryForList(anyString(), any(), eq(Long.class))).thenReturn(mockIdList);
        for (InowProfile inowProfile : values)
        {
            bulkInsert.setValues(preparedStatement, inowProfile);
        }

        verify(preparedStatement, atLeastOnce()).setObject(anyInt(), anyDouble());

    }

    @Test
    public void testInowProfileClassificationRowMapper_MapRow() throws SQLException
    {
        ResultSet resultSet = mock(ResultSet.class);
        when(resultSet.getString("TWO_CHAR_CODE")).thenReturn("US");
        when(resultSet.getLong("SID")).thenReturn(1L);
        when(resultSet.getString("IDENTIFIER")).
            thenReturn("INOW-00000000000001");
        when(resultSet.getString("ENTITY_NAME")).thenReturn("Test Entity");
        when(resultSet.getString("STREET_1")).thenReturn("123 Test St");
        when(resultSet.getString("STREET_2")).thenReturn("Suite 100");
        when(resultSet.getString("CITY")).thenReturn("Test City");
        when(resultSet.getString("STATEPROVINCE")).thenReturn("Test State");
        when(resultSet.getString("STATE_PROVINCE_ISO_CODE")).thenReturn("TS");
        when(resultSet.getString("POSTALCODE")).thenReturn("12345");
        when(resultSet.getString("COUNTRY")).thenReturn("Test Country");
        when(resultSet.getLong("CT_SID")).thenReturn(1L);
        when(resultSet.getString("CT_CODE")).thenReturn("CT01");
        when(resultSet.getString("CT_DESCRIPTION")).
            thenReturn("Test Classification");

        InowProfileDao.InowProfileClassificationRowMapper rowMapper = new InowProfileDao()
            .new InowProfileClassificationRowMapper();

        InowProfileImpl inowProfile = (InowProfileImpl) rowMapper.mapRow(resultSet, 1);

        assertEquals("US", inowProfile.getCountryObject().getTwoCharCode());
        assertEquals("INOW-00000000000001", inowProfile.getIdentifier());
        assertEquals("Test Entity", inowProfile.getName());
        assertEquals("123 Test St", inowProfile.getStreet1());
        assertEquals("Suite 100", inowProfile.getStreet2());
        assertEquals("Test City", inowProfile.getCity());
        assertEquals("Test State", inowProfile.getStateProvince());
        assertEquals("TS", inowProfile.getStateProvinceCode());
        assertEquals("12345", inowProfile.getPostalCode());
        assertEquals("Test Country", inowProfile.getCountry());
        assertEquals("CT01", inowProfile.getClassificationType().getCode());
        assertEquals("Test Classification", inowProfile.getClassificationType()
            .getDescription());
    }
    
    @Test
    public void testBulkUpdate() throws SQLException
    {
        PreparedStatement preparedStatement = mock(PreparedStatement.class);

        InowProfile inowProfileNegativeSid = new InowProfileImpl();
        inowProfileNegativeSid.setName("D&H CANADAAAAA");
        inowProfileNegativeSid.setStreet1("255 COURTNEY PARK DRIVE W");
        inowProfileNegativeSid.setStreet2("SUITE 101");
        inowProfileNegativeSid.setCity("MISSISSAUGA");
        inowProfileNegativeSid.setStateProvince("ONTARIO");
        inowProfileNegativeSid.setPostalCode("L5W 0A5");
        inowProfileNegativeSid.setSid(-1L);
        inowProfileNegativeSid.setIdentifier("1");

        InowProfile inowProfileEmptyIdentifier = new InowProfileImpl();
        inowProfileEmptyIdentifier.setSid(2L);
        inowProfileEmptyIdentifier.setName("Test Name 2");
        inowProfileEmptyIdentifier.setStreet1("Street 3");
        inowProfileEmptyIdentifier.setStreet2("Street 4");
        inowProfileEmptyIdentifier.setCity("City 2");
        inowProfileEmptyIdentifier.setStateProvince("State 2");
        inowProfileEmptyIdentifier.setPostalCode("Postal 2");
        inowProfileEmptyIdentifier.setCountry("Country 2");
        inowProfileNegativeSid.setIdentifier("");

        List<InowProfile> values = Arrays.asList(inowProfileNegativeSid,
            inowProfileEmptyIdentifier);
        InowProfileDao.BulkUpdate bulkUpdate = inowProfileDao.new BulkUpdate(values);

        for (InowProfile inowProfile : values)
        {
            bulkUpdate.setValues(preparedStatement, inowProfile);
        }

        verify(preparedStatement, atLeastOnce()).setObject(anyInt(), anyLong());
    }
    
    @Test
    public void testComponentSegmentationRowMapper_MapRow() throws SQLException {
        // Mock the ResultSet
        ResultSet resultSet = mock(ResultSet.class);
        when(resultSet.getLong("SID")).thenReturn(1L);
        when(resultSet.getString("NAME")).thenReturn("Test Name");
        when(resultSet.getString("DESCRIPTION")).thenReturn("Test Description");
    
        // Instantiate the RowMapper
        InowProfileDao.ComponentSegmentationRowMapper rowMapper = new InowProfileDao().new ComponentSegmentationRowMapper();
    
        // Call the mapRow method
        ComponentSegmentation result = (ComponentSegmentation) rowMapper.mapRow(resultSet, 1);
    
        // Assert the results
        assertNotNull(result);
        assertEquals(0, result.getSid().compareTo(1L));
        assertEquals("Test Name", result.getName());
        assertEquals("Test Description", result.getDescription());
    }
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter