Project

General

Profile

1
package eu.dnetlib.parthenos.publisher;
2

    
3
import java.io.IOException;
4

    
5
import eu.dnetlib.parthenos.ParthenosPublisherApplication;
6
import org.apache.commons.io.IOUtils;
7
import org.apache.jena.graph.Triple;
8
import org.apache.jena.util.iterator.ExtendedIterator;
9
import org.junit.Ignore;
10
import org.junit.Test;
11
import org.junit.runner.RunWith;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Value;
14
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
15
import org.springframework.boot.test.context.SpringBootTest;
16
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
17
import org.springframework.core.io.ClassPathResource;
18
import org.springframework.test.context.TestPropertySource;
19
import org.springframework.test.context.junit4.SpringRunner;
20
import org.springframework.test.web.servlet.MockMvc;
21
import virtuoso.jena.driver.VirtGraph;
22

    
23
import static org.junit.Assert.assertEquals;
24
import static org.junit.Assert.assertNotEquals;
25
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
26
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
27

    
28
/**
29
 * Created by Alessia Bardi on 29/08/2017.
30
 * Integration test.
31
 *
32
 * @author Alessia Bardi
33
 */
34
@RunWith(SpringRunner.class)
35
@SpringBootTest(
36
		webEnvironment = WebEnvironment.RANDOM_PORT,
37
		classes = ParthenosPublisherApplication.class)
38
@AutoConfigureMockMvc
39
@TestPropertySource(locations = "classpath:application-integrationtest.properties")
40
//Ignore test because ot requires a running virtuoso server
41
@Ignore
42
public class ParthenosPublisherTest {
43

    
44
	private String recordPath = "eu/dnetlib/parthenos/virtuoso/test_record_plain.xml";
45
	@Value("${virtuoso.connectionstring}")
46
	private String connectionString;
47
	@Value("${virtuoso.usr}")
48
	private String testUser ;
49
	@Value("${virtuoso.pwd}")
50
	private String testPwd ;
51
	@Value("${virtuoso.uri.base.default}")
52
	private String defaultURIBaseURl;
53

    
54
	@Autowired
55
	private MockMvc mvc;
56

    
57

    
58
	@Test
59
	public void publishOK() throws Exception {
60
		String graphName = defaultURIBaseURl + "api_________::parthenos___::parthenos::topLevel/parthenos___::8d777f385d3dfec8815d20f7496026dc";
61
		VirtGraph graph = new VirtGraph(graphName, connectionString, testUser, testPwd);
62
		//ensure we have an empty graph
63
		graph.clear();
64
		assertEquals(0, graph.getCount());
65

    
66
		mvc.perform(post("/publish").param("record",  getString(recordPath)).param("serializationMode", "plain"))
67
				.andExpect(status().isOk());
68

    
69
		int count = graph.getCount();
70
		System.out.println(graphName +"has "+count+" triples:");
71
		assertNotEquals(0, graph.getCount());
72
		final ExtendedIterator<Triple> triples = graph.find();
73
		while(triples.hasNext()){
74
			System.out.println(triples.next());
75
		}
76
		//cleanup the virtuoso graph
77
		graph.clear();
78
		assertEquals(0, graph.getCount());
79

    
80
	}
81

    
82
	private String getString(final String classpath) {
83
		try {
84
			final ClassPathResource resource = new ClassPathResource(classpath);
85
			return IOUtils.toString(resource.getInputStream(), "UTF-8");
86
		}catch(IOException e){
87
			return null;
88
		}
89
	}
90

    
91
}
(2-2/2)