Project

General

Profile

1
package eu.dnetlib.parthenos.catalogue;
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.net.URISyntaxException;
6

    
7
import eu.dnetlib.parthenos.CRM;
8
import eu.dnetlib.parthenos.CRMdig;
9
import eu.dnetlib.parthenos.CRMpe;
10
import eu.dnetlib.parthenos.publisher.ParthenosPublisherException;
11
import org.apache.commons.lang3.StringUtils;
12
import org.apache.jena.ontology.OntModel;
13
import org.apache.jena.ontology.OntModelSpec;
14
import org.apache.jena.rdf.model.InfModel;
15
import org.apache.jena.rdf.model.ModelFactory;
16
import org.apache.jena.rdf.model.ResIterator;
17
import org.apache.jena.rdf.model.Resource;
18
import org.apache.jena.vocabulary.RDF;
19
import org.junit.After;
20
import org.junit.Before;
21
import org.junit.Ignore;
22
import org.junit.Test;
23
import org.junit.runner.RunWith;
24
import org.springframework.beans.factory.annotation.Autowired;
25
import org.springframework.boot.test.context.SpringBootTest;
26
import org.springframework.core.io.ClassPathResource;
27
import org.springframework.test.context.TestPropertySource;
28
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
29

    
30
import static org.junit.Assert.assertFalse;
31
import static org.junit.Assert.assertTrue;
32

    
33
/**
34
 * Created by Alessia Bardi on 15/03/2018.
35
 *
36
 * @author Alessia Bardi
37
 */
38
@Ignore
39
@RunWith(SpringJUnit4ClassRunner.class)
40
@SpringBootTest
41
@TestPropertySource(
42
		locations = "classpath:application-integrationtest.properties")
43
public class CatalogueRegistratorIntegrationTest {
44

    
45
	private String nakalaService = "eu/dnetlib/parthenos/registry/nakala.rdf";
46
	private String resourceCatName = "nakalatest";
47

    
48
	private String rdfWithProviders = "eu/dnetlib/parthenos/registry/withProviders.rdf";
49
	private boolean doPurge=false;
50

    
51
	@Autowired
52
	private CatalogueRegistrator reg;
53
	private OntModel baseModel;
54

    
55

    
56

    
57
	@Before
58
	public void setup() {
59
		baseModel = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_TRANS_INF);
60
		baseModel.read(CRMpe.RDFS_URL);
61
		baseModel.read(CRM.RDFS_URL);
62
		baseModel.read(CRMdig.RDFS_URL);
63
	}
64

    
65
	@After
66
	public void tearDown() throws URISyntaxException, IOException {
67
		//purge the item from the catalogue
68
		if(doPurge) reg.purge(resourceCatName);
69
	}
70

    
71

    
72
	@Test
73
	public void testRegistration() throws Exception{
74
		InfModel model = loadBaseModel();
75
		model.read(getStream(nakalaService), CRMpe.NS);
76
		ResIterator subjects = model.listSubjectsWithProperty(RDF.type, CRM.E7_Activity);
77
		Resource nakala = subjects.nextResource();
78
		String uuid = reg.register(nakala, CRM.E7_Activity);
79
		System.out.println(uuid);
80
		if(StringUtils.isNotBlank(uuid)) doPurge = true;
81
	}
82

    
83
	@Test
84
	public void testPurge() throws IOException, URISyntaxException {
85
		reg.purge(resourceCatName);
86
		doPurge = false;
87
	}
88

    
89
	@Test
90
	public void testAddGroup() throws IOException, URISyntaxException, ParthenosPublisherException {
91
		InfModel model = loadBaseModel();
92
		model.read(getStream(rdfWithProviders), CRMpe.NS);
93
		ResIterator it = model.listSubjectsWithProperty(CRMpe.PP2_provided_by);
94
		while(it.hasNext()){
95
			Resource withProvider = it.nextResource();
96
			reg.ensureGroups(withProvider);
97
		}
98
		doPurge = false;
99

    
100
	}
101

    
102
	@Test
103
	public void testGroup() throws ParthenosPublisherException, IOException, URISyntaxException {
104
		assertFalse(reg.getCatalogueAPIClient().groupExist("sismeltest"));
105
		String sismelJson = "{"
106
				+ "  \"name\":\"sismeltest\","
107
				+ "  \"id\":\"sismeltest\","
108
				+ "  \"title\": \"SISMEL TEST GROUP TITLE\""
109
				//+ "  \"description\": \"%s\""
110
				+ "}";
111
		reg.getCatalogueAPIClient().registerGroup(sismelJson, "sismeltest");
112
		assertTrue(reg.getCatalogueAPIClient().groupExist("sismeltest"));
113
		reg.getCatalogueAPIClient().purgeGroup("sismeltest");
114
		assertFalse(reg.getCatalogueAPIClient().groupExist("sismeltest"));
115
	}
116

    
117
	private static InputStream getStream(final String classpath) throws IOException {
118
		return new ClassPathResource(classpath).getInputStream();
119
	}
120

    
121
	protected InfModel loadBaseModel() {
122
		return ModelFactory.createRDFSModel(baseModel);
123
	}
124

    
125
}
(2-2/3)