source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/JdbcVersionDaoTest.java @ 3380

Last change on this file since 3380 was 3380, checked in by olhsha, 11 years ago

"put updated body" is implemented and tested. Big refactring: simplifying DAO's and pushing their composition to rest methods. add-methods in DAO return now not the classes but internalID-s of the added resources. Still 2 test errors and 2 test failures.

File size: 8.0 KB
Line 
1/*
2 * Copyright (C) 2013 DASISH
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18package eu.dasish.annotation.backend.dao.impl;
19
20import eu.dasish.annotation.backend.TestBackendConstants;
21import eu.dasish.annotation.backend.dao.CachedRepresentationDao;
22import eu.dasish.annotation.backend.identifiers.VersionIdentifier;
23import eu.dasish.annotation.schema.Version;
24import java.util.ArrayList;
25import java.util.List;
26import org.jmock.Expectations;
27import org.jmock.Mockery;
28import org.junit.Test;
29import static org.junit.Assert.*;
30import org.junit.runner.RunWith;
31import org.springframework.beans.factory.annotation.Autowired;
32import org.springframework.test.context.ContextConfiguration;
33import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
34
35/**
36 *
37 * @author olhsha
38 */
39@RunWith(SpringJUnit4ClassRunner.class)
40@ContextConfiguration({"/spring-test-config/dataSource.xml", "/spring-test-config/mockery.xml", "/spring-test-config/mockAnnotationDao.xml",
41    "/spring-test-config/mockUserDao.xml", "/spring-test-config/mockPermissionsDao.xml", "/spring-test-config/mockNotebookDao.xml",
42    "/spring-test-config/mockSourceDao.xml", "/spring-test-config/mockCachedRepresentationDao.xml", "/spring-config/versionDao.xml"})
43
44public class JdbcVersionDaoTest extends JdbcResourceDaoTest{
45   
46    @Autowired
47    JdbcVersionDao jdbcVersionDao;
48   
49    @Autowired
50    private CachedRepresentationDao cachedRepresentationDao;   
51   
52    @Autowired
53    private Mockery mockery;
54   
55   
56    /**
57     * Test of getExternalId method, of class JdbcVersionDao.
58     */
59    @Test
60    public void testGetExternalId() {
61        System.out.println("getExternalId");
62        Number internalID = 1;
63        VersionIdentifier result = jdbcVersionDao.getExternalID(internalID);
64        assertEquals(TestBackendConstants._TEST_VERSION_1_EXT_ID, result.toString());
65    }
66
67    /**
68     * Test of getInternalId method, of class JdbcVersionDao.
69     */
70    @Test
71    public void testGetInternalId() {
72        System.out.println("getInternalId");
73        VersionIdentifier externalID = new VersionIdentifier(TestBackendConstants._TEST_VERSION_1_EXT_ID);
74        Number expResult = 1;
75        Number result = jdbcVersionDao.getInternalID(externalID);
76        assertEquals(expResult, result);
77    }
78
79    /**
80     * Test of getVersion method, of class JdbcVersionDao.
81     */
82    @Test
83    public void testGetVersion() {
84        System.out.println("getVersion");
85        Number internalID = 1;
86        Version result = jdbcVersionDao.getVersion(internalID);
87        assertEquals(TestBackendConstants._TEST_VERSION_1_EXT_ID, result.getVersion());
88        //TODO: once the schems is fixed, test "version" and "URI/external-id" separately
89        // at the moment "version" corresponds "external_id"
90    }
91
92    /**
93     * Test of retrieveVersionList method, of class JdbcVersionDao.
94     */
95    @Test
96    public void testRetrieveVersionList() {
97        System.out.println("retrieveVersionList");
98        Number sourceID = 1;
99        // INSERT INTO sources_versions (source_id, version_id) VALUES (1, 1);
100        // INSERT INTO sources_versions (source_id, version_id) VALUES (1, 2);
101        List<Number> result = jdbcVersionDao.retrieveVersionList(sourceID);
102        assertEquals(1, result.get(0));
103        assertEquals(2, result.get(1));
104    }
105
106     /**
107     * Test of deleteVersionCachedRepresentationRows method, of class JdbcVersionDao.
108     */
109    @Test
110    public void testDeleteVersionCachedRepresentationRows() {
111        System.out.println("deleteVersion");
112        Number internalID = 6; // there is no sources (in target_source and sources_versions - sibling table) connected to this version in the test table
113        int result = jdbcVersionDao.deleteVersionCachedRepresentationRow(internalID);
114        assertEquals(1, result);
115    }
116   
117    /**
118     * Test of deleteVersion method, of class JdbcVersionDao.
119     */
120    @Test
121    public void testDeleteVersion() {
122        System.out.println("deleteVersion");
123        final Number internalID = 6; // there is no sources (in target_source and sources_versions - sibling table) connected to this version in the test table
124        final Number cachedID =5;
125        final List<Number> versions = new ArrayList<Number>();
126        versions.add(cachedID);
127       
128        //jdbcCachedRepresentationDao.retrieveCachedRepresentationList(internalID);
129        //jdbcCachedRepresentationDao.deleteCachedRepresentationInfo(cachedID);
130       
131        mockery.checking(new Expectations() {
132            { 
133                oneOf(cachedRepresentationDao).retrieveCachedRepresentationList(internalID);
134                will(returnValue(versions));
135               
136                oneOf(cachedRepresentationDao).deleteCachedRepresentationInfo(cachedID);
137                will(returnValue(0));
138               
139            }
140        });
141       
142        int result = jdbcVersionDao.deleteVersion(internalID); 
143        assertEquals(1, result);
144       
145        // try to delete one more time
146         mockery.checking(new Expectations() {
147            { 
148                oneOf(cachedRepresentationDao).retrieveCachedRepresentationList(internalID);
149                will(returnValue(new ArrayList<Number>()));
150               
151            }
152        });
153        int resultTwo = jdbcVersionDao.deleteVersion(internalID);
154        assertEquals(0, resultTwo);
155       
156        // the version in use, shoul not be deleted
157        final Number internalIDNotToDelete = 4;         
158        int resultThree = jdbcVersionDao.deleteVersion(internalIDNotToDelete);
159        assertEquals(0, resultThree);
160    }
161
162    /**
163     * Test of addVersion method, of class JdbcVersionDao.
164     */
165    @Test
166    public void testAddVersion() {
167        System.out.println("addVersion");
168       
169        Version freshVersion = new Version();   
170       
171        Number result = jdbcVersionDao.addVersion(freshVersion);
172        assertEquals(8, result);
173        Version addedVersion = jdbcVersionDao.getVersion(result);
174        assertFalse(null==addedVersion.getVersion());
175       
176    }
177
178 
179    /**
180     * test
181     * public int deleteCachedRepresentationForSource(Number sourceID, Number cachedRepresentationID)
182     *
183     **/
184 
185   
186    @Test
187    public void tesDeleteCachedRepresentationForSource() {
188         System.out.println("test delete CachedRepresentationForSource");
189         
190         /// TEST 1 /////
191          mockery.checking(new Expectations() {
192            { 
193                oneOf(cachedRepresentationDao).deleteCachedRepresentationInfo(5);
194                will(returnValue(0));
195               
196            }
197         });         
198         int[] result = jdbcVersionDao.deleteCachedRepresentationForSource(1, 5); // source 1 has versions {1,2}, versions 1 and 2 have cached representations {1, 5} and {3} respectively
199         assertEquals(1, result[0]); 
200         assertEquals(0, result[1]); //cahced representation 5 is also connected version 6, therefore cannot be removed
201         
202         
203         // TEST 2 ////////
204          mockery.checking(new Expectations() {
205            { 
206                oneOf(cachedRepresentationDao).deleteCachedRepresentationInfo(4);
207                will(returnValue(1));
208               
209            }
210         });     
211         int[] result_2 = jdbcVersionDao.deleteCachedRepresentationForSource(3, 4); //source 1 --> version 4 --> cached 4
212         assertEquals(1, result_2[0]); 
213         assertEquals(1, result_2[1]); 
214    }
215}
Note: See TracBrowser for help on using the repository browser.