View Javadoc

1   package org.nhindirect.monitor.dao.impl;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertNotNull;
5   import static org.junit.Assert.assertNull;
6   import static org.junit.Assert.assertTrue;
7   import static org.mockito.Matchers.any;
8   import static org.mockito.Mockito.doThrow;
9   import static org.mockito.Mockito.mock;
10  import static org.mockito.Mockito.when;
11  import static org.mockito.Mockito.verify;
12  import static org.mockito.Mockito.times;
13  import static org.mockito.Mockito.never;
14  
15  import java.util.List;
16  
17  import javax.persistence.EntityManager;
18  
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  import org.nhindirect.monitor.dao.AggregationDAO;
23  import org.nhindirect.monitor.dao.AggregationDAOException;
24  import org.nhindirect.monitor.dao.entity.Aggregation;
25  import org.nhindirect.monitor.dao.impl.AggregationDAOImpl;
26  import org.springframework.beans.factory.annotation.Autowired;
27  import org.springframework.test.context.ContextConfiguration;
28  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
29  import org.springframework.test.context.transaction.TransactionConfiguration;
30  import org.springframework.transaction.annotation.Transactional;
31  
32  
33  
34  @RunWith(SpringJUnit4ClassRunner.class)
35  @ContextConfiguration(locations = { "file:src/test/resources/aggregationStore.xml" })
36  @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
37  @Transactional
38  public class AggregationDAOImpl_confirmAggregationTest 
39  {
40  	@Autowired
41  	private AggregationDAO notifDao;
42  	
43  	@Before
44  	public void setUp() throws Exception
45  	{
46  		notifDao.purgeAll();
47  		
48  		List<String> keys = notifDao.getAggregationKeys();
49  		assertEquals(0, keys.size());
50  		
51  		keys = notifDao.getAggregationCompletedKeys();
52  		assertEquals(0, keys.size());
53  	}
54  	
55  	@Test
56  	public void testConfirmAggregationTest_aggregationRemoved() throws Exception
57  	{
58  		final Aggregation insert = new Aggregation();
59  		insert.setExchangeBlob(new byte[] {0,3,2});
60  		insert.setId("12345");
61  		insert.setVersion(0);
62  		
63  		notifDao.addUpdateAggregation(insert);
64  		assertNotNull(notifDao.getAggregation("12345"));
65  
66  		final Aggregation remove = new Aggregation();
67  		remove.setExchangeBlob(new byte[] {0,3,2});
68  		remove.setId("12345");
69  		remove.setVersion(1);
70  		
71  		notifDao.removeAggregation(remove, "12345");
72  		
73  		assertNotNull(notifDao.getAggregationCompleted("12345", true));
74  
75  		notifDao.confirmAggregation("12345");
76  		
77  		assertNull(notifDao.getAggregationCompleted("12345", true));
78  	}
79  	
80  	@Test
81  	public void testConfirmAggregationTest_aggregationNotFound_nothingRemoved() throws Exception
82  	{
83  		EntityManager mgr = mock(EntityManager.class);
84  		when(mgr.find((Class<?>)any(), any())).thenReturn(null);
85  		
86  		final AggregationDAOImpl dao = new AggregationDAOImpl();
87  		dao.setEntityManager(mgr);
88  		
89  		dao.confirmAggregation("12345");
90  		verify(mgr, times(1)).find((Class<?>)any(), any());
91  		verify(mgr, never()).remove(any());
92  	}
93  	
94  	@Test
95  	public void testConfirmAggregationTest_exceptionInRemove_assertException() throws Exception
96  	{
97  		EntityManager mgr = mock(EntityManager.class);
98  		doThrow(new RuntimeException()).when(mgr).find((Class<?>)any(), any());
99  		
100 		
101 		final AggregationDAOImpl dao = new AggregationDAOImpl();
102 		dao.setEntityManager(mgr);
103 		
104 		boolean exceptionOccured = false;
105 		try
106 		{
107 			dao.confirmAggregation("12345");
108 		}
109 		catch(AggregationDAOException e)
110 		{
111 			exceptionOccured = true;
112 		}
113 		
114 		assertTrue(exceptionOccured);
115 	}
116 }