package com.napa.pulse.dao.impl; import com.napa.pulse.dao.interfaces.AcoDAO; import com.napa.pulse.dao.interfaces.SiteGroupDAO; import com.napa.pulse.dto.LeadTimeStatusDTO; import com.napa.pulse.dto.ProductLinesData; import com.napa.pulse.entity.pulseui.CustomGroupCodeProductGroup; import com.napa.pulse.entity.pulseui.CustomProductGroup; import com.napa.pulse.entity.pulseui.ProductGroupOrderReturn; import com.napa.pulse.entity.security.User; import com.napa.pulse.enums.AccessItem; import com.napa.pulse.rowmappers.IntegerRowMapper; import com.napa.pulse.service.interfaces.AdminService; import org.junit.Test; import org.junit.jupiter.api.BeforeEach; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.*; import org.mockito.invocation.InvocationOnMock; import org.mockito.runners.MockitoJUnitRunner; import org.mockito.stubbing.Answer; //import org.powermock.api.mockito.PowerMockito; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.InvalidResultSetAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementCreator; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.jdbc.core.simple.SimpleJdbcCall; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; import org.springframework.jdbc.support.rowset.SqlRowSet; import org.springframework.jdbc.support.rowset.SqlRowSetMetaData; import org.springframework.test.util.ReflectionTestUtils; import javax.sql.DataSource; import java.math.BigDecimal; import java.sql.*; import java.util.*; import java.util.Date; import java.util.concurrent.*; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.*; import static org.mockito.Mockito.*; import static org.mockito.Mockito.when; /** * @author Rahul Chaudhary * 6/1/2022 */ @SpringBootTest @RunWith(MockitoJUnitRunner.class) public class SessionDAOImplUnitTest { @InjectMocks SessionDAOImpl sessionDAOImplMock; @Mock private JdbcTemplate jdbcTemplate; @Mock private AcoDAO acoDAO; @Mock private AdminService adminService; @Mock private SiteGroupDAOImpl siteGroupDAO; @BeforeEach public void setup() throws Exception { // assertNotNull(dataSource); MockitoAnnotations.initMocks(this); } @Test public void testRecalculateSessionTotals() { sessionDAOImplMock = mock(SessionDAOImpl.class); Map inParamMap = new HashMap<>(); inParamMap.put("SESSIONID_IN", 0); inParamMap.put("SITEID_IN", 2); sessionDAOImplMock.reCalculateSessionTotals(0, 2); verify(sessionDAOImplMock, times(1)).reCalculateSessionTotals(0, 2); } @Test(expected = Exception.class) public void testRecalculateSessionTotalsForException() { sessionDAOImplMock = mock(SessionDAOImpl.class); Map inParamMap = new HashMap<>(); inParamMap.put("SESSIONID_IN", 0); inParamMap.put("SITEID_IN", null); SqlParameterSource in = new MapSqlParameterSource(inParamMap); SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName("RECALC_SESSION_TOTALS"); when(simpleJdbcCall.execute(in)).thenThrow(Exception.class); } @Test public void getMinMaxSessionProductsTest() { //sessionDAOImplMock = mock(SessionDAOImpl.class); Integer[] sites = {1309, 1346}; //String sql = "" List> obj = new ArrayList<>(); Map map = new HashMap<>(); map.put("ac", 1309); obj.add(map); Map>> ans = new HashMap<>(); ans.put("ad", obj); ReflectionTestUtils.setField(sessionDAOImplMock, "jdbcTemplate", jdbcTemplate); when(jdbcTemplate.queryForList(anyString())).thenReturn(obj); sessionDAOImplMock.getMinMaxSessionProducts(sites); assertThat(ans).isNotNull(); } @Test public void testGetMinMaxSessionUnselectedProducts() { int sessionId = 1; int siteId = 1; List> expectedResult = new ArrayList<>(); Map row = new HashMap<>(); row.put("id", 1); row.put("code", "CODE1"); row.put("line", "false"); row.put("orderCycle", 14); row.put("leadTime", 7); row.put("perCar", "false"); row.put("isStockEditOff", "true"); expectedResult.add(row); when(jdbcTemplate.queryForList(anyString())).thenReturn(expectedResult); List> result = sessionDAOImplMock.getMinMaxSessionUnselectedProducts(sessionId, siteId);assertNotNull(result); assertEquals(expectedResult, result); verify(jdbcTemplate, times(1)).queryForList(anyString()); } @Test public void testCreateNewSession() throws Exception { // Arrange User user = new User(); user.setUserId(1); user.setRoleId(1); Boolean perCarOrder = true; Boolean includeFleets = true; Boolean includeSystems = true; Boolean includeSpecials = true; Boolean includeObsoletes = true; Boolean includeOverstocks = true; Boolean includeSupersede = true; Boolean includeKeylinesOrders = true; Boolean includeKeylinesReturns = true; Boolean includeLessThanStdPkg = true; Double includeWeekSupplyOption = 2.0; String weekSupplyOptionName = "weekSupplyOption"; Boolean useInvestmentCollections = true; Boolean provideStoreDepth = true; Boolean includeSoldNotStocked = true; Boolean includeNewNumbers = true; Boolean orderAllSkus = true; Boolean returnAllSKUs = true; Integer dollarLimit = 1000; String comment = "Test comment"; Integer siteGroupId = 1; Integer[] sitesArray = {1, 2, 3}; ProductGroupOrderReturn[] productGroupsArray = {}; ProductGroupOrderReturn[] groupCodeArray = {}; Integer[] fleetGroupsIds = {1, 2, 3}; CustomProductGroup customProductGroup = new CustomProductGroup(); customProductGroup.setOrders(true); customProductGroup.setReturns(true); String[] newHierarchyIds = {"1", "2"}; customProductGroup.setHierarchyIds(newHierarchyIds); CustomGroupCodeProductGroup customGroupCodeProductGroupObj = new CustomGroupCodeProductGroup(); Boolean isGroupCode = false; String savedGroup = "savedGroup"; String userAccessSites = "1,2,3"; Integer[] skuArray = {1, 2, 3}; ProductLinesData[] productLineIdArray = {}; ProductLinesData[] allProductLines = {}; ProductLinesData p = new ProductLinesData(); p.setSiteId(1); p.setSiteId(2); ProductLinesData[] siteProductLines = {p}; Integer includeOrders = 1; Integer includeReturns = 1; Date expirationDate = new Date(System.currentTimeMillis()); String includeDaysCount = "30"; Integer perCarMinMaxValue = 1; String salesHistoryValue = "salesHistoryValue"; String displayQty = "displayQty"; String sessionType = "MM"; String includeHubSpokeOrderOption = "includeHubSpokeOrderOption"; String includeHubSpokeReturnOption = "includeHubSpokeReturnOption"; Boolean applicationPartsOnly = true; LeadTimeStatusDTO[] leadTime = {}; int DEFER_SITE_LIMIT = 1; KeyHolder keyHolder = new GeneratedKeyHolder(); // when(user.getAccessItemIds()).thenReturn(Arrays.asList(AccessItem.DELAY_SESSION_CREATION.getNumVal())); when(siteGroupDAO.getSiteCount(anyInt(), any(User.class), anyString(), anyString())).thenReturn(DEFER_SITE_LIMIT + 1); when(jdbcTemplate.update(any(PreparedStatementCreator.class), any(KeyHolder.class))).thenReturn(1); when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class))).thenReturn(1); Map keyMap = new HashMap<>(); keyMap.put("SESSION_ID", BigDecimal.valueOf(1234)); doAnswer(invocation -> { KeyHolder keyHolderArg = invocation.getArgument(1); keyHolderArg.getKeyList().add(keyMap); return 1; }).when(jdbcTemplate).update(any(PreparedStatementCreator.class), any(KeyHolder.class)); BigDecimal sessionId = sessionDAOImplMock.createNewSession( user, perCarOrder, includeFleets, includeSystems, includeSpecials, includeObsoletes, includeOverstocks, includeSupersede, includeKeylinesOrders, includeKeylinesReturns, includeLessThanStdPkg, includeWeekSupplyOption, weekSupplyOptionName, useInvestmentCollections, provideStoreDepth, includeSoldNotStocked, includeNewNumbers, orderAllSkus, returnAllSKUs, dollarLimit, comment, siteGroupId, sitesArray, productGroupsArray, groupCodeArray, fleetGroupsIds, customProductGroup, customGroupCodeProductGroupObj, isGroupCode, savedGroup, userAccessSites, skuArray, productLineIdArray, allProductLines, siteProductLines, includeOrders, includeReturns, expirationDate, includeDaysCount, perCarMinMaxValue, salesHistoryValue, displayQty, sessionType, includeHubSpokeOrderOption, includeHubSpokeReturnOption, applicationPartsOnly, leadTime ); if (sessionId == null) { sessionId = BigDecimal.valueOf(9999); } // Assert assertNotNull(sessionId); assertEquals(BigDecimal.valueOf(1234), sessionId); verify(jdbcTemplate, times(1)).update(any(PreparedStatementCreator.class), any(KeyHolder.class)); verify(jdbcTemplate, times(1)).queryForObject(anyString(), eq(Integer.class)); } @Test public void testCreateNewSession4() throws Exception { // Arrange User user = new User(); user.setUserId(1); user.setRoleId(10); List list = new ArrayList<>(); list.add(69); user.setAccessItemIds(list); Boolean perCarOrder = true; Boolean includeFleets = true; Boolean includeSystems = true; Boolean includeSpecials = true; Boolean includeObsoletes = true; Boolean includeOverstocks = true; Boolean includeSupersede = true; Boolean includeKeylinesOrders = true; Boolean includeKeylinesReturns = true; Boolean includeLessThanStdPkg = true; Double includeWeekSupplyOption = 2.0; String weekSupplyOptionName = "weekSupplyOption"; Boolean useInvestmentCollections = true; Boolean provideStoreDepth = true; Boolean includeSoldNotStocked = true; Boolean includeNewNumbers = true; Boolean orderAllSkus = true; Boolean returnAllSKUs = true; Integer dollarLimit = 1000; String comment = "Test comment"; Integer siteGroupId = 1; Integer[] sitesArray = {1, 2, 3}; ProductGroupOrderReturn[] productGroupsArray = {}; ProductGroupOrderReturn[] groupCodeArray = {}; Integer[] fleetGroupsIds = {1, 2, 3}; CustomProductGroup customProductGroup = new CustomProductGroup(); customProductGroup.setOrders(true); customProductGroup.setReturns(true); String[] newHierarchyIds = {"1", "2"}; customProductGroup.setHierarchyIds(newHierarchyIds); CustomGroupCodeProductGroup customGroupCodeProductGroupObj = new CustomGroupCodeProductGroup(); Boolean isGroupCode = false; String savedGroup = "savedGroup"; String userAccessSites = "69"; Integer[] skuArray = {1, 2, 3}; ProductLinesData[] productLineIdArray = {}; ProductLinesData[] allProductLines = {}; ProductLinesData p = new ProductLinesData(); p.setSiteId(1); p.setSiteId(2); ProductLinesData[] siteProductLines = {p}; Integer includeOrders = 1; Integer includeReturns = 1; Date expirationDate = new Date(System.currentTimeMillis()); String includeDaysCount = "30"; Integer perCarMinMaxValue = 1; String salesHistoryValue = "salesHistoryValue"; String displayQty = "displayQty"; String sessionType = "MM"; String includeHubSpokeOrderOption = "includeHubSpokeOrderOption"; String includeHubSpokeReturnOption = "includeHubSpokeReturnOption"; Boolean applicationPartsOnly = true; LeadTimeStatusDTO[] leadTime = {}; int DEFER_SITE_LIMIT = 1; KeyHolder keyHolder = new GeneratedKeyHolder(); // when(user.getAccessItemIds()).thenReturn(Arrays.asList(AccessItem.DELAY_SESSION_CREATION.getNumVal())); when(siteGroupDAO.getSiteCount(anyInt(), any(User.class), anyString(), anyString())).thenReturn(DEFER_SITE_LIMIT + 1); when(jdbcTemplate.update(any(PreparedStatementCreator.class), any(KeyHolder.class))).thenReturn(1); when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class))).thenReturn(1); Map keyMap = new HashMap<>(); keyMap.put("SESSION_ID", BigDecimal.valueOf(1234)); // Act doAnswer(invocation -> { KeyHolder keyHolderArg = invocation.getArgument(1); // You can set a dummy key here to simulate a successful insertion keyHolderArg.getKeyList().add(keyMap); // keyHolderArg.getKeyList().add(Collections.singletonMap("SESSION_ID", BigDecimal.valueOf(1234))); // Simulated session ID return 1; }).when(jdbcTemplate).update(any(PreparedStatementCreator.class), any(KeyHolder.class)); BigDecimal sessionId = sessionDAOImplMock.createNewSession( user, perCarOrder, includeFleets, includeSystems, includeSpecials, includeObsoletes, includeOverstocks, includeSupersede, includeKeylinesOrders, includeKeylinesReturns, includeLessThanStdPkg, includeWeekSupplyOption, weekSupplyOptionName, useInvestmentCollections, provideStoreDepth, includeSoldNotStocked, includeNewNumbers, orderAllSkus, returnAllSKUs, dollarLimit, comment, siteGroupId, sitesArray, productGroupsArray, groupCodeArray, fleetGroupsIds, customProductGroup, customGroupCodeProductGroupObj, isGroupCode, savedGroup, userAccessSites, skuArray, productLineIdArray, allProductLines, siteProductLines, includeOrders, includeReturns, expirationDate, includeDaysCount, perCarMinMaxValue, salesHistoryValue, displayQty, sessionType, includeHubSpokeOrderOption, includeHubSpokeReturnOption, applicationPartsOnly, leadTime ); if (sessionId == null) { sessionId = BigDecimal.valueOf(9999); } // Assert assertNotNull(sessionId); assertEquals(BigDecimal.valueOf(1234), sessionId); verify(jdbcTemplate, times(1)).update(any(PreparedStatementCreator.class), any(KeyHolder.class)); verify(jdbcTemplate, times(1)).queryForObject(anyString(), eq(Integer.class)); } @Test public void testCreateNewSession1() throws Exception { // Arrange User user = new User(); user.setUserId(1); user.setRoleId(1); Boolean perCarOrder = true; Boolean includeFleets = true; Boolean includeSystems = true; Boolean includeSpecials = true; Boolean includeObsoletes = true; Boolean includeOverstocks = true; Boolean includeSupersede = true; Boolean includeKeylinesOrders = true; Boolean includeKeylinesReturns = true; Boolean includeLessThanStdPkg = true; Double includeWeekSupplyOption = 2.0; String weekSupplyOptionName = "weekSupplyOption"; Boolean useInvestmentCollections = true; Boolean provideStoreDepth = true; Boolean includeSoldNotStocked = true; Boolean includeNewNumbers = true; Boolean orderAllSkus = true; Boolean returnAllSKUs = true; Integer dollarLimit = 1000; String comment = "Test comment"; Integer siteGroupId = 1; Integer[] sitesArray = {1, 2, 3}; ProductGroupOrderReturn prg = new ProductGroupOrderReturn(); prg.setReturns(true); prg.setOrders(true); prg.setProductGroupId(1); prg.setProductGroupName("product"); ProductGroupOrderReturn[] productGroupsArray = {prg}; ProductGroupOrderReturn[] groupCodeArray = {prg}; Integer[] fleetGroupsIds = {1, 2, 3}; CustomProductGroup customProductGroup = new CustomProductGroup(); customProductGroup.setOrders(true); customProductGroup.setReturns(true); String[] newHierarchyIds = {"1", "2"}; customProductGroup.setHierarchyIds(newHierarchyIds); CustomGroupCodeProductGroup customGroupCodeProductGroupObj = new CustomGroupCodeProductGroup(); Boolean isGroupCode = true; String savedGroup = "savedGroup"; String userAccessSites = "1,2,3"; Integer[] skuArray = {1, 2, 3}; ProductLinesData[] productLineIdArray = {}; ProductLinesData[] allProductLines = {}; ProductLinesData p = new ProductLinesData(); p.setSiteId(1); p.setSiteId(2); ProductLinesData[] siteProductLines = {p}; Integer includeOrders = 1; Integer includeReturns = 1; Date expirationDate = new Date(System.currentTimeMillis()); String includeDaysCount = "30"; Integer perCarMinMaxValue = 1; String salesHistoryValue = "salesHistoryValue"; String displayQty = "displayQty"; String sessionType = "SS"; String includeHubSpokeOrderOption = "includeHubSpokeOrderOption"; String includeHubSpokeReturnOption = "includeHubSpokeReturnOption"; Boolean applicationPartsOnly = true; LeadTimeStatusDTO[] leadTime = {}; KeyHolder keyHolder = new GeneratedKeyHolder(); when(jdbcTemplate.update(any(PreparedStatementCreator.class), any(KeyHolder.class))).thenReturn(1); when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class))).thenReturn(1); Map keyMap = new HashMap<>(); keyMap.put("SESSION_ID", BigDecimal.valueOf(1234)); // Act doAnswer(invocation -> { KeyHolder keyHolderArg = invocation.getArgument(1); // You can set a dummy key here to simulate a successful insertion keyHolderArg.getKeyList().add(keyMap); // keyHolderArg.getKeyList().add(Collections.singletonMap("SESSION_ID", BigDecimal.valueOf(1234))); // Simulated session ID return 1; }).when(jdbcTemplate).update(any(PreparedStatementCreator.class), any(KeyHolder.class)); //List siteList = Arrays.asList(1, 2, 3); // Sample list of site IDs //BigDecimal sessionId = BigDecimal.valueOf(1234); //when(jdbcTemplate.query(anyString(), eq(new IntegerRowMapper()), eq(sessionId))) // .thenReturn(siteList); BigDecimal sessionId = sessionDAOImplMock.createNewSession( user, perCarOrder, includeFleets, includeSystems, includeSpecials, includeObsoletes, includeOverstocks, includeSupersede, includeKeylinesOrders, includeKeylinesReturns, includeLessThanStdPkg, includeWeekSupplyOption, weekSupplyOptionName, useInvestmentCollections, provideStoreDepth, includeSoldNotStocked, includeNewNumbers, orderAllSkus, returnAllSKUs, dollarLimit, comment, siteGroupId, sitesArray, productGroupsArray, groupCodeArray, fleetGroupsIds, customProductGroup, customGroupCodeProductGroupObj, isGroupCode, savedGroup, userAccessSites, skuArray, productLineIdArray, allProductLines, siteProductLines, includeOrders, includeReturns, expirationDate, includeDaysCount, perCarMinMaxValue, salesHistoryValue, displayQty, sessionType, includeHubSpokeOrderOption, includeHubSpokeReturnOption, applicationPartsOnly, leadTime ); if (sessionId == null) { sessionId = BigDecimal.valueOf(9999); } // Assert assertNotNull(sessionId); assertEquals(BigDecimal.valueOf(1234), sessionId); // assertNotNull(keyHolder); // assertNotNull(keyHolder.getKey()); verify(jdbcTemplate, times(1)).update(any(PreparedStatementCreator.class), any(KeyHolder.class)); //verify(jdbcTemplate, times(1)).queryForObject(anyString(), eq(Integer.class)); // for (Integer site : siteList) { // verify(jdbcTemplate, times(1)).query(anyString(), eq(new IntegerRowMapper()), eq(sessionId)); //} } @Test public void testCreateNewSessionAC() throws Exception { // Arrange // SqlRowSet mockResult = mock(SqlRowSet.class); User user = new User(); user.setUserId(1); user.setRoleId(1); Boolean perCarOrder = true; Boolean includeFleets = true; Boolean includeSystems = true; Boolean includeSpecials = true; Boolean includeObsoletes = true; Boolean includeOverstocks = true; Boolean includeSupersede = true; Boolean includeKeylinesOrders = true; Boolean includeKeylinesReturns = true; Boolean includeLessThanStdPkg = true; Double includeWeekSupplyOption = 2.0; String weekSupplyOptionName = "weekSupplyOption"; Boolean useInvestmentCollections = true; Boolean provideStoreDepth = true; Boolean includeSoldNotStocked = true; Boolean includeNewNumbers = true; Boolean orderAllSkus = true; Boolean returnAllSKUs = true; Integer dollarLimit = 1000; String comment = "Test comment"; Integer siteGroupId = 1; Integer[] sitesArray = {1, 2, 3}; ProductGroupOrderReturn prg = new ProductGroupOrderReturn(); prg.setReturns(true); prg.setOrders(true); prg.setProductGroupId(1); prg.setProductGroupName("product"); ProductGroupOrderReturn[] productGroupsArray = {prg}; ProductGroupOrderReturn[] groupCodeArray = {}; Integer[] fleetGroupsIds = {1, 2, 3}; CustomProductGroup customProductGroup = new CustomProductGroup(); customProductGroup.setOrders(true); customProductGroup.setReturns(true); String[] newHierarchyIds = {"1", "2"}; customProductGroup.setHierarchyIds(newHierarchyIds); CustomGroupCodeProductGroup customGroupCodeProductGroupObj = new CustomGroupCodeProductGroup(); Boolean isGroupCode = true; String savedGroup = "savedGroup"; String userAccessSites = "1,2,3"; Integer[] skuArray = {1, 2, 3}; ProductLinesData[] productLineIdArray = {}; ProductLinesData[] allProductLines = {}; ProductLinesData p = new ProductLinesData(); p.setSiteId(1); p.setSiteId(2); ProductLinesData[] siteProductLines = {p}; Integer includeOrders = 1; Integer includeReturns = 1; Date expirationDate = new Date(System.currentTimeMillis()); String includeDaysCount = "30"; Integer perCarMinMaxValue = 1; String salesHistoryValue = "salesHistoryValue"; String displayQty = "displayQty"; String sessionType = "AC"; String includeHubSpokeOrderOption = "includeHubSpokeOrderOption"; String includeHubSpokeReturnOption = "includeHubSpokeReturnOption"; Boolean applicationPartsOnly = true; LeadTimeStatusDTO[] leadTime = {}; KeyHolder keyHolder = new GeneratedKeyHolder(); when(jdbcTemplate.update(any(PreparedStatementCreator.class), any(KeyHolder.class))).thenReturn(1); when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class))).thenReturn(1); Map keyMap = new HashMap<>(); keyMap.put("SESSION_ID", BigDecimal.valueOf(1234)); // Act doAnswer(invocation -> { KeyHolder keyHolderArg = invocation.getArgument(1); // You can set a dummy key here to simulate a successful insertion keyHolderArg.getKeyList().add(keyMap); // keyHolderArg.getKeyList().add(Collections.singletonMap("SESSION_ID", BigDecimal.valueOf(1234))); // Simulated session ID return 1; }).when(jdbcTemplate).update(any(PreparedStatementCreator.class), any(KeyHolder.class)); //List siteList = Arrays.asList(1, 2, 3); // Sample list of site IDs //BigDecimal sessionId = BigDecimal.valueOf(1234); //when(jdbcTemplate.query(anyString(), eq(new IntegerRowMapper()), eq(sessionId))) // .thenReturn(siteList); SqlRowSet mockHierarchyRowSet = mock(SqlRowSet.class); when(mockHierarchyRowSet.next()).thenReturn(true, false); when(mockHierarchyRowSet.getInt(1)).thenReturn(1); SqlRowSet mockSiteIdRowSet = mock(SqlRowSet.class); when(mockSiteIdRowSet.next()).thenReturn(true, false); when(mockSiteIdRowSet.getInt(1)).thenReturn(1); // Mocking the jdbcTemplate to return the mock SqlRowSet when(jdbcTemplate.queryForRowSet(eq("select sh.HIERARCHY_ID from SESSION_HIERARCHY sh, product_hierarchy ph where session_id=? and sh.hierarchy_id=ph.hierarchy_id and ACO_ENABLED=1"), any())).thenReturn(mockHierarchyRowSet); when(jdbcTemplate.queryForRowSet(eq("select site_id from session_site where session_id=?"), any())).thenReturn(mockSiteIdRowSet); when(jdbcTemplate.update(anyString(), any(Object[].class))).thenReturn(1); // Mock AcoDAO methods doNothing().when(acoDAO).xferBlendingData(anyInt(), anyInt(), anyInt()); doNothing().when(acoDAO).createAcoSiteHierarchy(anyInt(), anyInt(), anyInt()); // BigDecimal resultSessionId = yourService.createNewSession(sessionType, user); BigDecimal sessionId = sessionDAOImplMock.createNewSession( user, perCarOrder, includeFleets, includeSystems, includeSpecials, includeObsoletes, includeOverstocks, includeSupersede, includeKeylinesOrders, includeKeylinesReturns, includeLessThanStdPkg, includeWeekSupplyOption, weekSupplyOptionName, useInvestmentCollections, provideStoreDepth, includeSoldNotStocked, includeNewNumbers, orderAllSkus, returnAllSKUs, dollarLimit, comment, siteGroupId, sitesArray, productGroupsArray, groupCodeArray, fleetGroupsIds, customProductGroup, customGroupCodeProductGroupObj, isGroupCode, savedGroup, userAccessSites, skuArray, productLineIdArray, allProductLines, siteProductLines, includeOrders, includeReturns, expirationDate, includeDaysCount, perCarMinMaxValue, salesHistoryValue, displayQty, sessionType, includeHubSpokeOrderOption, includeHubSpokeReturnOption, applicationPartsOnly, leadTime ); if (sessionId == null) { sessionId = BigDecimal.valueOf(9999); } // Assert assertNotNull(sessionId); assertEquals(BigDecimal.valueOf(1234), sessionId); // assertNotNull(keyHolder); // assertNotNull(keyHolder.getKey()); verify(jdbcTemplate, times(1)).update(any(PreparedStatementCreator.class), any(KeyHolder.class)); //verify(jdbcTemplate, times(1)).queryForObject(anyString(), eq(Integer.class)); // for (Integer site : siteList) { //verify(jdbcTemplate, times(1)).queryForRowSet(anyString(), any(Object[].class)); //verify(jdbcTemplate, times(1)).update(anyString(), any(Object[].class)); verify(acoDAO, times(1)).xferBlendingData(anyInt(), anyInt(), anyInt()); verify(acoDAO, times(1)).createAcoSiteHierarchy(anyInt(), anyInt(), anyInt()); //} } @Test public void testCreateNewSessionSC() throws Exception { // Arrange // SqlRowSet mockResult = mock(SqlRowSet.class); User user = new User(); user.setUserId(1); user.setRoleId(1); Boolean perCarOrder = true; Boolean includeFleets = true; Boolean includeSystems = true; Boolean includeSpecials = true; Boolean includeObsoletes = true; Boolean includeOverstocks = true; Boolean includeSupersede = true; Boolean includeKeylinesOrders = true; Boolean includeKeylinesReturns = true; Boolean includeLessThanStdPkg = true; Double includeWeekSupplyOption = 2.0; String weekSupplyOptionName = "weekSupplyOption"; Boolean useInvestmentCollections = true; Boolean provideStoreDepth = true; Boolean includeSoldNotStocked = true; Boolean includeNewNumbers = true; Boolean orderAllSkus = true; Boolean returnAllSKUs = true; Integer dollarLimit = 1000; String comment = "Test comment"; Integer siteGroupId = 1; Integer[] sitesArray = {1, 2, 3}; ProductGroupOrderReturn prg = new ProductGroupOrderReturn(); prg.setReturns(true); prg.setOrders(true); prg.setProductGroupId(1); prg.setProductGroupName("product"); ProductGroupOrderReturn[] productGroupsArray = {prg}; ProductGroupOrderReturn[] groupCodeArray = {}; Integer[] fleetGroupsIds = {1, 2, 3}; CustomProductGroup customProductGroup = new CustomProductGroup(); customProductGroup.setOrders(true); customProductGroup.setReturns(true); String[] newHierarchyIds = {"1", "2"}; customProductGroup.setHierarchyIds(newHierarchyIds); CustomGroupCodeProductGroup customGroupCodeProductGroupObj = new CustomGroupCodeProductGroup(); Boolean isGroupCode = true; String savedGroup = "savedGroup"; String userAccessSites = "1,2,3"; Integer[] skuArray = {1, 2, 3}; ProductLinesData[] productLineIdArray = {}; ProductLinesData[] allProductLines = {}; ProductLinesData p = new ProductLinesData(); p.setSiteId(1); p.setSiteId(2); ProductLinesData[] siteProductLines = {p}; Integer includeOrders = 1; Integer includeReturns = 1; Date expirationDate = new Date(System.currentTimeMillis()); String includeDaysCount = "30"; Integer perCarMinMaxValue = 1; String salesHistoryValue = "salesHistoryValue"; String displayQty = "displayQty"; String sessionType = "SC"; String includeHubSpokeOrderOption = "includeHubSpokeOrderOption"; String includeHubSpokeReturnOption = "includeHubSpokeReturnOption"; Boolean applicationPartsOnly = true; LeadTimeStatusDTO[] leadTime = {}; KeyHolder keyHolder = new GeneratedKeyHolder(); when(jdbcTemplate.update(any(PreparedStatementCreator.class), any(KeyHolder.class))).thenReturn(1); when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class))).thenReturn(1); Map keyMap = new HashMap<>(); keyMap.put("SESSION_ID", BigDecimal.valueOf(1234)); // Act doAnswer(invocation -> { KeyHolder keyHolderArg = invocation.getArgument(1); // You can set a dummy key here to simulate a successful insertion keyHolderArg.getKeyList().add(keyMap); // keyHolderArg.getKeyList().add(Collections.singletonMap("SESSION_ID", BigDecimal.valueOf(1234))); // Simulated session ID return 1; }).when(jdbcTemplate).update(any(PreparedStatementCreator.class), any(KeyHolder.class)); SqlRowSet mockHierarchyRowSet = mock(SqlRowSet.class); when(mockHierarchyRowSet.next()).thenReturn(true, false); when(mockHierarchyRowSet.getInt(1)).thenReturn(1); SqlRowSet mockSiteIdRowSet = mock(SqlRowSet.class); when(mockSiteIdRowSet.next()).thenReturn(true, false); when(mockSiteIdRowSet.getInt(1)).thenReturn(1); // Mocking the jdbcTemplate to return the mock SqlRowSet when(jdbcTemplate.queryForRowSet(eq("select sh.HIERARCHY_ID from SESSION_HIERARCHY sh, product_hierarchy ph where session_id=? and sh.hierarchy_id=ph.hierarchy_id and ACO_ENABLED=1"), any())).thenReturn(mockHierarchyRowSet); when(jdbcTemplate.queryForRowSet(eq("select site_id from session_site where session_id=?"), any())).thenReturn(mockSiteIdRowSet); when(jdbcTemplate.update(anyString(), any(Object[].class))).thenReturn(1); // Mock AcoDAO methods doNothing().when(acoDAO).xferBlendingData(anyInt(), anyInt(), anyInt()); doNothing().when(acoDAO).createAcoSiteHierarchy(anyInt(), anyInt(), anyInt()); // BigDecimal resultSessionId = yourService.createNewSession(sessionType, user); BigDecimal sessionId = sessionDAOImplMock.createNewSession( user, perCarOrder, includeFleets, includeSystems, includeSpecials, includeObsoletes, includeOverstocks, includeSupersede, includeKeylinesOrders, includeKeylinesReturns, includeLessThanStdPkg, includeWeekSupplyOption, weekSupplyOptionName, useInvestmentCollections, provideStoreDepth, includeSoldNotStocked, includeNewNumbers, orderAllSkus, returnAllSKUs, dollarLimit, comment, siteGroupId, sitesArray, productGroupsArray, groupCodeArray, fleetGroupsIds, customProductGroup, customGroupCodeProductGroupObj, isGroupCode, savedGroup, userAccessSites, skuArray, productLineIdArray, allProductLines, siteProductLines, includeOrders, includeReturns, expirationDate, includeDaysCount, perCarMinMaxValue, salesHistoryValue, displayQty, sessionType, includeHubSpokeOrderOption, includeHubSpokeReturnOption, applicationPartsOnly, leadTime ); if (sessionId == null) { sessionId = BigDecimal.valueOf(9999); } // Assert assertNotNull(sessionId); assertEquals(BigDecimal.valueOf(1234), sessionId); verify(jdbcTemplate, times(1)).update(any(PreparedStatementCreator.class), any(KeyHolder.class)); } @Test public void testCreateNewSessionException() throws Exception { // Arrange User user = new User(); user.setUserId(1); user.setRoleId(1); Boolean perCarOrder = true; Boolean includeFleets = true; Boolean includeSystems = true; Boolean includeSpecials = true; Boolean includeObsoletes = true; Boolean includeOverstocks = true; Boolean includeSupersede = true; Boolean includeKeylinesOrders = true; Boolean includeKeylinesReturns = true; Boolean includeLessThanStdPkg = true; Double includeWeekSupplyOption = 2.0; String weekSupplyOptionName = "weekSupplyOption"; Boolean useInvestmentCollections = true; Boolean provideStoreDepth = true; Boolean includeSoldNotStocked = true; Boolean includeNewNumbers = true; Boolean orderAllSkus = true; Boolean returnAllSKUs = true; Integer dollarLimit = 1000; String comment = "Test comment"; Integer siteGroupId = 1; Integer[] sitesArray = {1, 2, 3}; ProductGroupOrderReturn prg = new ProductGroupOrderReturn(); prg.setReturns(true); prg.setOrders(true); prg.setProductGroupId(1); prg.setProductGroupName("product"); ProductGroupOrderReturn[] productGroupsArray = {prg}; ProductGroupOrderReturn[] groupCodeArray = {}; Integer[] fleetGroupsIds = {1, 2, 3}; CustomProductGroup customProductGroup = new CustomProductGroup(); customProductGroup.setOrders(true); customProductGroup.setReturns(true); String[] newHierarchyIds = {"1", "2"}; customProductGroup.setHierarchyIds(newHierarchyIds); CustomGroupCodeProductGroup customGroupCodeProductGroupObj = new CustomGroupCodeProductGroup(); Boolean isGroupCode = true; String savedGroup = "savedGroup"; String userAccessSites = "1,2,3"; Integer[] skuArray = {1, 2, 3}; ProductLinesData[] productLineIdArray = {}; ProductLinesData[] allProductLines = {}; ProductLinesData p = new ProductLinesData(); p.setSiteId(1); p.setSiteId(2); ProductLinesData[] siteProductLines = {p}; Integer includeOrders = 1; Integer includeReturns = 1; Date expirationDate = new Date(System.currentTimeMillis()); String includeDaysCount = "30"; Integer perCarMinMaxValue = 1; String salesHistoryValue = "salesHistoryValue"; String displayQty = "displayQty"; String sessionType = "AC"; String includeHubSpokeOrderOption = "includeHubSpokeOrderOption"; String includeHubSpokeReturnOption = "includeHubSpokeReturnOption"; Boolean applicationPartsOnly = true; LeadTimeStatusDTO[] leadTime = {}; when(jdbcTemplate.update(any(PreparedStatementCreator.class), any(KeyHolder.class))) .thenThrow(new RuntimeException("Database error")); BigDecimal sessionId = null; try { sessionId = sessionDAOImplMock.createNewSession( user, perCarOrder, includeFleets, includeSystems, includeSpecials, includeObsoletes, includeOverstocks, includeSupersede, includeKeylinesOrders, includeKeylinesReturns, includeLessThanStdPkg, includeWeekSupplyOption, weekSupplyOptionName, useInvestmentCollections, provideStoreDepth, includeSoldNotStocked, includeNewNumbers, orderAllSkus, returnAllSKUs, dollarLimit, comment, siteGroupId, sitesArray, productGroupsArray, groupCodeArray, fleetGroupsIds, customProductGroup, customGroupCodeProductGroupObj, isGroupCode, savedGroup, userAccessSites, skuArray, productLineIdArray, allProductLines, siteProductLines, includeOrders, includeReturns, expirationDate, includeDaysCount, perCarMinMaxValue, salesHistoryValue, displayQty, sessionType, includeHubSpokeOrderOption, includeHubSpokeReturnOption, applicationPartsOnly, leadTime ); }catch(RuntimeException e){ assertEquals("Database error",e.getMessage()); } assertNull(sessionId); verify(jdbcTemplate, times(1)).update(any(PreparedStatementCreator.class), any(KeyHolder.class)); } // Additional tests for edge cases and exceptions }