Project

General

Profile

Download (2.33 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.remote.service;
2

    
3
import java.io.ByteArrayInputStream;
4
import java.io.InputStream;
5

    
6

    
7
import org.easymock.EasyMock;
8
import org.junit.Assert;
9
import org.junit.Before;
10
import org.junit.Test;
11
import org.springframework.mock.web.MockHttpServletResponse;
12
import org.springframework.web.servlet.ModelAndView;
13
import org.unitils.UnitilsJUnit4;
14
import org.unitils.easymock.annotation.Mock;
15
import org.unitils.inject.annotation.InjectInto;
16
import org.unitils.inject.annotation.TestedObject;
17

    
18
import com.ibm.lsid.LSIDException;
19
import com.ibm.lsid.MalformedLSIDException;
20
import com.ibm.lsid.server.LSIDServerException;
21

    
22
import eu.etaxonomy.cdm.api.service.lsid.LSIDDataService;
23
import eu.etaxonomy.cdm.model.common.LSID;
24
import eu.etaxonomy.cdm.test.util.LSIDMatchers;
25

    
26
public class DataControllerTest extends UnitilsJUnit4 {
27
	
28
	@Mock
29
	@InjectInto(property = "lsidDataService")
30
	LSIDDataService dataService;
31
	
32
	@TestedObject
33
    private DataController dataController;
34
	
35
	private static String expectedData = "Acherontia Laspeyres 1809 sensu example.org 2007";
36
	private InputStream inputStream;
37
	private LSID lsid;
38
	private MockHttpServletResponse response;
39

    
40

    
41
	@Before
42
    public void setUp() {
43
		try {
44
			lsid = new LSID("urn:lsid:example.org:taxonconcepts:1");
45
		} catch (MalformedLSIDException e) { }
46
		
47
    	dataController = new DataController();
48
    	inputStream = new ByteArrayInputStream(expectedData.getBytes());
49
    	dataService = EasyMock.createMock(LSIDDataService.class);
50
    	response = new MockHttpServletResponse();
51
    }
52
	
53
	@Test
54
    public void testGetData() throws Exception {
55
    	EasyMock.expect(dataService.getData(LSIDMatchers.eqLSID(lsid))).andReturn(inputStream);
56
		EasyMock.replay(dataService);
57

    
58
		ModelAndView modelAndView = dataController.getData(lsid,response);
59
		EasyMock.verify(dataService); 
60
		
61
		Assert.assertNull(modelAndView);
62
		Assert.assertEquals(response.getContentAsString(),expectedData);
63
	}
64
    
65
	@Test(expected = LSIDServerException.class)
66
    public void testGetDataWithUnknownLSID() throws Exception {;
67
    	LSIDServerException lse = new LSIDServerException(LSIDException.UNKNOWN_LSID, "Unknown LSID");
68
    	
69
    	EasyMock.expect(dataService.getData(LSIDMatchers.eqLSID(lsid))).andThrow(lse);
70
		EasyMock.replay(dataService);
71
		
72
		dataController.setLsidDataService(dataService);
73
        dataController.getData(lsid, response);
74
	}
75
}
(2-2/3)