Project

General

Profile

1
package eu.dnetlib.data.claims;
2

    
3
/**
4
 * Created by Eri on 18/11/2015.
5
 */
6

    
7

    
8
import eu.dnetlib.data.claims.entity.Claim;
9
import eu.dnetlib.data.claims.entity.OpenaireEntity;
10
import eu.dnetlib.data.claims.entity.Project;
11
import eu.dnetlib.data.claims.entity.Result;
12
import eu.dnetlib.data.claims.handler.*;
13
import eu.dnetlib.data.claims.parser.ExternalRecordParser;
14
import eu.dnetlib.data.claims.sql.SQLStoreException;
15
import eu.dnetlib.data.claims.sql.SqlDAO;
16
import eu.dnetlib.data.claims.sql.SqlStore;
17
import eu.dnetlib.data.claims.utils.*;
18
import eu.dnetlib.data.emailSender.EmailSender;
19
import org.apache.logging.log4j.Level;
20
import org.apache.logging.log4j.LogManager;
21
import org.apache.logging.log4j.Logger;
22
import org.apache.logging.log4j.core.config.Configurator;
23
import org.apache.logging.log4j.core.config.DefaultConfiguration;
24
import org.junit.Before;
25
import org.junit.Test;
26
import org.junit.runner.RunWith;
27
import org.springframework.context.ApplicationContext;
28
import org.springframework.context.annotation.PropertySource;
29
import org.springframework.context.support.ClassPathXmlApplicationContext;
30
import org.springframework.test.context.ContextConfiguration;
31
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32

    
33
import java.sql.ResultSet;
34
import java.util.ArrayList;
35
import java.util.Calendar;
36
import java.util.List;
37

    
38
import static junit.framework.Assert.assertNotNull;
39

    
40
/**
41
 * @author eri
42
 */
43
@RunWith(SpringJUnit4ClassRunner.class)
44
@ContextConfiguration(locations = {"springContext-claims.xml"})
45
@PropertySource("springContext-claims.properties")
46
public class TestClass {
47
	private Logger log = LogManager.getLogger(this.getClass());
48

    
49

    
50
	private SqlDAO sqlDAO;
51
    private SqlStore sqlStore;
52

    
53
    private QueryGenerator queryGenerator;
54

    
55
    ProjectHandler projectHandler = null;
56
    DirectIndexHandler directIndexHandler = null;
57
    FetchProjectHandler fetchProjectHandler = null;
58
    IndexResultHandler indexResultHandler = null;
59
    ExternalRecordHandler externalRecordHandler = null;
60
    ResultHandler resultHandler = null;
61

    
62
    FetchClaimHandler fetchClaimHandler = null;
63
    ClaimHandler claimHandler = null;
64
    ClaimValidation claimValidation = null;
65

    
66
    EmailSender emailSender = null;
67
    ContextUtils contextUtils = null;
68

    
69

    
70
    @Before
71
	public void init() throws Exception {
72
        Configurator.initialize(new DefaultConfiguration());
73
        Configurator.setRootLevel(Level.INFO);
74
        ApplicationContext context = new ClassPathXmlApplicationContext("eu/dnetlib/data/claims/springContext-claims.xml");
75

    
76
        directIndexHandler = context.getBean(DirectIndexHandler.class);
77
        projectHandler = context.getBean(ProjectHandler.class);
78
        fetchProjectHandler = context.getBean(FetchProjectHandler.class);
79

    
80
        externalRecordHandler = context.getBean(ExternalRecordHandler.class);
81
        indexResultHandler = context.getBean(IndexResultHandler.class);
82
        resultHandler = context.getBean(ResultHandler.class);
83
        claimHandler = context.getBean(ClaimHandler.class);
84
        sqlDAO = context.getBean(SqlDAO.class);
85
        sqlStore = context.getBean(SqlStore.class);
86
        queryGenerator = context.getBean(QueryGenerator.class);
87
        fetchClaimHandler = context.getBean(FetchClaimHandler.class);
88
        claimValidation = context.getBean(ClaimValidation.class);
89

    
90
        emailSender = context.getBean(EmailSender.class);
91
        contextUtils = context.getBean(ContextUtils.class);
92

    
93
        assertNotNull(sqlDAO);
94

    
95
	}
96
    @Test
97
    public void testRollback(){
98
        List <String> commands = new ArrayList<String>();
99
        commands.add("UPDATE claim SET claimedBy = 'argirok1' WHERE  id = 93");
100
        commands.add("EXCEPTION");
101
        commands.add("UPDATE claim SET claimedBy = 'argirok2' WHERE  id = 95");
102
        try {
103
            sqlStore.executeUpdateWithRollback(commands);
104
        } catch (Exception e) {
105
            log.error("",e);
106
        }
107
    }
108
    @Test
109
    public void testJson(){
110
        Claim claim = new Claim();
111
        Project project= new Project();
112
        claim.setDate(Calendar.getInstance().getTime());
113
        claim.setId("claimId");
114
        claim.setUserMail("claim mail");
115

    
116
        project.setFunderName("EC");
117
        project.setOpenaireId("openaireID1234");
118
        project.setName("PrTitle");
119
        claim.setSource(project);
120
        Result result = new Result();
121
        result.setAccessRights("accessRights");
122
        result.setCollectedFrom("collectedFrom");
123
        result.setExternalUrl("external_url");
124
        result.setOpenaireId("openaire_id");
125
        result.setTitle("title");
126
        result.setResultType("type");
127
        result.setMetadataRecord("xmlString");
128
        claim.setTarget(result);
129
        System.out.println("test");
130
        System.out.println(JsonldBuilder.toJsonld(claim));
131
    }
132

    
133
    @Test
134
    public void testFetch() throws Exception {
135

    
136
        try {
137
            List<Claim> claims = null;
138
            List<String> types= new ArrayList<String>();
139
//            types.add(ClaimUtils.CONTEXT);
140
//            types.add(ClaimUtils.PROJECT);
141
//          types.add(ClaimUtils.DATASET);
142
            types.add(ClaimUtils.PUBLICATION);
143

    
144

    
145
//            claims = fetchClaimHandler.fetchClaimsByUser("argirok_1@hotmail.com",1,0,types, false);
146
//            claims = fetchClaimHandler.fetchClaimsByContext("egi",100,0,null,"claim.claim_date",false,types,false);
147
//            claims = fetchClaimHandler.fetchClaimsByContext("egi",100,0,null,"claim.claim_date",false,types,false);
148
//            (String openaireId, Integer limit, Integer offset, String keyword, String orderField, boolean descending, List<String> types, boolean addCurationInfo) throws Exception, SQLStoreException {
149
//            claims = fetchClaimHandler.fetchClaimsByUser("argirok@di.uoa.gr",3,0,null,"claim.claim_date",false,types,false);
150
//
151
//            claims = fetchClaimHandler.fetchClaimsByProject("corda_______::ab9c77ce02967b24fc9c1a74276e4677",5,0);
152
//            claims = fetchClaimHandler.fetchClaimsByProject("corda__h2020::94c962e736df90a5075a7f660ba3d7f6",12,0,null, "claim.claim_date",false,types,true);
153
            //claims = fetchClaimHandler.fetchClaimsByToken("corda__h2020::94c962e736df90a5075a7f660ba3d7f6","argiro@gmail.com",12,0,null, "claim.claim_date",false,types,false);
154

    
155
//            claims = fetchClaimHandler.fetchClaimsByDate("2016-01-14 14:53:50","2016-04-21",10,0);
156
//            claims = fetchClaimHandler.fetchClaimsByDate("2016-01-14 14:53:50","2017-04-21",10,0,"", "source",true,types, false);
157
//            // 2016-04-20 14:53:50.276
158
//            claims = fetchClaimHandler.fetchClaimsByContext("egi::country::gr",5,0,null,"claim.claim_date",false,types);
159
//            claims = fetchClaimHandler.fetchAllClaims(5,3);
160
//            claims = fetchClaimHandler.fetchClaimsByResult("od_______233::235960bb401207cb9595a79a1bc8e867",5,0,null, "claim.claim_date",false,types);
161
//            claims = fetchClaimHandler.fetchClaimsByFunder("fct_________::FCT",5,0);
162
//            claims= fetchClaimHandler.fetchClaimsByUser("kiatrop@di.uoa.gr",20,0,types);
163
//            claims = fetchClaimHandler.fetchAllClaims(10,0,"","date",true,types);
164

    
165
            System.out.println(fetchClaimHandler.claims2JSON(claims)+"\n\n\n "+claims.size());
166
//          Claim claim = fetchClaimHandler.fetchClaimById("5821");
167
//          System.out.println(fetchClaimHandler.claim2JSON(claim) );
168
//          System.out.println("ALL:"+fetchClaimHandler.countClaimsByUser("argirok@di.uoa.gr","EGI", types));
169
          System.out.println("ALL:"+fetchClaimHandler.countClaimsByContext("egi",null, types));
170
//            System.out.println("ALL:"+fetchClaimHandler.countClaimsByProject("corda_______::ab9c77ce02967b24fc9c1a74276e4677",null,types));
171

    
172
//        System.out.println("ALL:"+fetchClaimHandler.countAllClaims("", types));
173
//                +"\n Project: "+fetchClaimHandler.countClaimsByProject("corda_______::ab9c77ce02967b24fc9c1a74276e4677")+
174
//                " \n Result:"+fetchClaimHandler.countClaimsByResult("dedup_wf_001::541b1cef0d38d519e98a3b7a5b60bc75")+" \n Context:"+fetchClaimHandler.countClaimsByContext("egi::country::de")
175
//        +" \n Date:"+fetchClaimHandler.countClaimsByDate("2014-04-14","2015-04-14")+" \n Funder:"+fetchClaimHandler.countClaimsByFunder("fct_________::FCT"));
176
        } catch (Exception e) {
177
            log.error("",e);
178
        } catch (SQLStoreException e) {
179
            e.printStackTrace();
180
        }
181
    }
182
    @Test
183
    public void testContext(){
184

    
185
        System.out.print(contextUtils.fetchContextById("egi::organisation::uom"));
186
    }
187
    @Test
188
    public void testInsert() throws Exception, SQLStoreException {
189
 //acm_________::31e0e5e7cbd6e2556336d7e795f55c49
190
//dedup_wf_001::64ca1e3a4d1590456ad3dd7df8a18cd7
191
 /*
192
claimedBy	argirok_1@hotmail.com
193
sourceId	corda_______::2c37878a0cede85dbbd1081bb9b4a2f8
194
sourceType	project
195
sourceCollectedFrom	openaire
196
sourceAccessRights	OPEN
197
sourceEmbargoEndDate
198
targetId	10.1007/978-3-540-76323-9_4
199
targetType	software
200
targetCollectedFrom	crossref
201
targetAccessRights	EMBARGO
202
targetEmbargoEndDate	2028-11-10T13:55:47.935Z
203
  */
204
//        String id=claimHandler.buildAndInsertClaim("argirok_1@hotmail.com", "project", "corda__h2020::94c962e736df90a5075a7f660ba3d7f6", "openaire", "OPEN", null,"software", "10.1007/978-3-540-76323-9_4", "crossref", "EMBARGO","2028-11-10T13:55:47.935Z");
205
//        String id=claimHandler.buildAndInsertClaim("argirok@di.uoa.gr", "project", "corda__h2020::94c962e736df90a5075a7f660ba3d7f6", "openaire", "OPEN", null,"publication", "0000-0002-9414-646X-21755327", "orcid", "EMBARGO","2028-11-10T13:55:47.935Z");
206
//        {user:argirok@di.uoa.gr, sourceType:project, sourceId:corda_______::2c37878a0cede85dbbd1081bb9b4a2f8, sourceCollectedFrom:openaire, sourceAccessRights:OPEN, sourceEmbargoDate:null,
207
//        targetType:publication, targetId:0000-0002-3477-3082-5288650, targetCollectedFrom:orcid, targetAccessRights:OPEN, targetEmbargoDate:null}
208
//        String id=claimHandler.buildAndInsertClaim("argirok@di.uoa.gr", "project", "corda_______::2c37878a0cede85dbbd1081bb9b4a2f8",  "openaire", "OPEN", null,"publication",                "0000-0002-3477-3082-5288651", "orcid", "OPEN",null, "development_explore");
209

    
210

    
211
//        String id=claimHandler.buildAndInsertClaim("argirok@di.uoa.gr", "context", "egi::country::gr",
212
//                "openaire", null, null,"dataset", "10.4225/41/55A111BDE741C", "datacite", "OPEN","2017-05-01","development_explore");
213
    }
214
    @Test
215
    public void testDelete() throws Exception, SQLStoreException {
216

    
217
//            claimHandler.deleteClaim("argirok@di.uoa.gr","2876");
218
//        claimHandler.deleteClaim("argirok@di.uoa.gr","3101");
219
        }
220

    
221
    //@Test fails due to org.postgresql.util.PSQLException: FATAL: database "claimsnewschema" does not exist
222
	public void testJob() throws Exception, SQLStoreException {
223
        String selectcountDMFClaims =" Select count(*) from claims  where type='dmf2actions' and xml NOT ILIKE '%<oaf:concept%' ";
224
        String selectCountConceptClaims =" Select count(*) from claims  where ( type='dmf2actions' or type='updates2actions' ) and xml LIKE '%<oaf:concept%' ";
225
        String selectCountRelationClaims =" Select count(*) from claims  where type='rels2actions'  ";
226

    
227
        ResultSet rs = sqlDAO.executePreparedQuery(selectCountConceptClaims);
228

    
229
        while(rs.next()) {
230
            log.info("Concept claims: "+rs.getInt(1));
231
        }
232
         rs = sqlDAO.executePreparedQuery(selectCountRelationClaims);
233

    
234
        while(rs.next()) {
235
            log.info("Relation claims: "+rs.getInt(1));
236
        }
237
         rs = sqlDAO.executePreparedQuery("Select count(*) from claim");
238

    
239
        while(rs.next()) {
240
            log.info("Migrated claims: "+rs.getInt(1));
241
        }
242
        rs = sqlDAO.executePreparedQuery("Select count(*) from result");
243

    
244
        while(rs.next()) {
245
            log.info("Results: "+rs.getInt(1));
246
        }
247

    
248
	}
249

    
250
    @Test
251
    public void testHttpRequest() throws Exception {
252
        SearchUtils searchUtils= new SearchUtils();
253
        log.info(searchUtils.fetchPublicationXmlFromIndex("doajarticles::eb8a123eb82b25013b0001d0e2d1842b"));
254
        log.info(searchUtils.fetchDatasetXmlFromIndex("doajarticles::eb8a123eb82b25013b0001d0e2d1842b"));
255
    }
256

    
257
    @Test
258
    public void testXpath() throws Exception {
259
            Result r=indexResultHandler.fetchResultById("dedup_wf_001::88a1eedd1ffce63dccf51d8ce2053c85");
260
     }
261

    
262
    @Test
263
    public void testSoftwareXpath() throws Exception {
264
//        http://scoobydoo.di.uoa.gr:5000/search/software?softwareId=datacite____%3A%3Aabe9e916b29d028789d7ae3c6f79f254
265
        Result r=indexResultHandler.fetchSoftwareById("datacite____::abe9e916b29d028789d7ae3c6f79f254");
266
        if(r!=null){
267
            System.out.println(r.toString());
268
        }
269

    
270
    }
271
    @Test
272
    public void testPublicationXpath() throws Exception {
273
        Result r=indexResultHandler.fetchPublicationById("od________18::5acd89ab4df2b877a7a208ea2a123a87");
274
        if(r!=null){
275
            System.out.println(r.toString());
276
        }
277

    
278
    }
279

    
280
    @Test
281
    public void dropClaimTables() throws Exception {
282
//        sqlDAO.executeUpdateQuery(queryGenerator.generateDeleteClaimTablesQuery());
283
    }
284
    @Test
285
    public void createClaimTables() throws Exception {
286
//        sqlDAO.executeUpdateQuery(queryGenerator.generateCreateClaimTablesQuery());
287
    }
288

    
289

    
290
    @Test
291
    public void buildProject() throws Exception {
292
        String id = "arc_________::089188bbc5db213fb2b00a0d93043fc4";
293
        Project pr = projectHandler.fetchProjectByID(id);
294
        log.info(pr.toString());
295
        id="corda_______::404d91e07cd4d32fddd8fc636ad4daf7";
296
        pr = projectHandler.fetchProjectByID(id);
297
        log.info(pr.toString());
298

    
299
    }
300

    
301

    
302
    @Test
303
    public void buildOpenaireResult() throws Exception {
304

    
305

    
306
        OpenaireEntity r= claimHandler.buildOpenaireEntity("dedup_wf_001::9e19149f2ece7129e63638a6a52cf468",ClaimUtils.DATASET,ClaimUtils.OPENAIRE,null, null);
307
        log.info(r);
308

    
309
    }
310
    @Test
311
    public void buildOpenaireProject() throws Exception {
312
        String projetcId = "corda_______::2c37878a0cede85dbbd1081bb9b4a2f8";
313
        OpenaireEntity r= claimHandler.buildOpenaireEntity(projetcId,ClaimUtils.PROJECT,ClaimUtils.OPENAIRE,null, null);
314
        log.info(r);
315

    
316
    }
317
    //@Test org.postgresql.util.PSQLException: FATAL: database "claimsnewschema" does not exist
318
    public void testProject() throws Exception, SQLStoreException {
319
//        List<String> mails = new ArrayList<String>();
320
//        mails.add("test1@mail.com");
321
//        mails.add("test2@mail.com");
322
//        mails.add("test3@mail.com");
323
//        sqlDAO.executePreparedQuery(queryGenerator.generateInsertProjectQuery("123","test project","TEstP","","","",mails));
324
        System.out.println("\n"+this.fetchProjectHandler.fetchContactEmailsByProjectId("corda__h2020::94c962e736df90a5075a7f660ba3d7f6")+"\n");
325
        List<String> mails = new ArrayList<String>();
326
        mails.add("test11111@mail.com");
327
        mails.add("test122222@mail.com");
328
        this.projectHandler.updateContactEmailsByProjectId("123",mails);
329
//        System.out.println("\n"+this.projectHandler.fetchContactEmailsByProjectId("123")+"\n");
330
//        String token = UUID.randomUUID().toString();
331
//        this.projectHandler.updateTokenByProjectId("123",token);
332

    
333

    
334

    
335
    }
336
    @Test
337
    public void testMD5() throws Exception {
338
        String id = "10.1016/j.engstruct.2013.03.014";
339
        String createdId = ExternalRecordParser.createOpenaireId(id);
340
        String openaireId = "userclaim___::07ec9eb2278a11e352e3fa93a621411d";
341
        System.out.println(createdId );
342

    
343
    }
344
    @Test
345
    public void testCuration() throws Exception, SQLStoreException {
346
//        boolean updated = claimHandler.updateClaimCurationInfo("argiro@gmail.com","1",true);
347
//        System.out.println(updated);
348
    }
349
/*
350
    @Test
351
    public void testFetchProjectIdByTokenAndEmail() throws Exception, SQLStoreException {
352
        String openaireId = this.fetchProjectHandler.fetchProjectIdByTokenAndEmail("dedc6979-1967-4ea2-a2ad-5135f95285ae", "test11111@mail.com");
353
        System.out.println(openaireId);
354
    }
355

    
356
    @Test
357
    public void testFetchProjectIdByToken() throws Exception, SQLStoreException {
358
        String openaireId = this.fetchProjectHandler.fetchProjectIdByToken("dedc6979-1967-4ea2-a2ad-5135f95285ae");
359
        System.out.println(openaireId);
360
    }
361
*/
362
    @Test
363
    public void directIndexClaim() throws Exception {
364
        System.out.println(this.claimValidation.getPathToSaveReport());
365
        String json = "{\"originalId\":\"userclaim____::d1e668dc81fa714aa98a558d9ce515fa\",\"title\":\"Expression of Ik6 and Ik8 Isoforms and Their Association with Relapse and Death in Mexican Children with Acute Lymphoblastic Leukemia\",\"authors\":[\"Reyes-León Adriana\",\"Juárez-Velázquez Rocío\",\"Medrano-Hernández Alma\",\"Cuenca-Roldán Teresa\",\"Salas-Labadía Consuelo\",\"del Pilar Navarrete-Meneses María\",\"Rivera-Luna Roberto\",\"López-Hernández Gerardo\",\"Paredes-Aguilera Rogelio\",\"Pérez-Vera Patricia\"],\"publisher\":\"Public Library of Science (PLoS)\",\"type\":\"publication\",\"pids\":[{\"type\":\"doi\",\"value\":\"10.1371/journal.pone.0130756\"}],\"licenseCode\":\"OPEN\",\"resourceType\":\"0001\",\"url\":\"http://dx.doi.org/10.1371/journal.pone.0130756\",\"hostedById\":\"openaire____::1256f046-bf1f-4afc-8b47-d0b147148b18\",\"collectedFromId\":\"openaire____::crossref\",\"contexts\":[\"egi::country::gr\"]}";
366
//        System.out.println("\n\nFeed Response is: "+directIndexHandler.insertRecord(json));
367

    
368
//        System.out.println("\n\nDelete Response is: "+directIndexHandler.deleteRecord("userclaim___::d1e668dc81fa714aa98a558d9ce515fa","openaire____::crossref"));
369
    }
370

    
371
    // To run following tests about email: claim notifications, set all the properties inside project folder
372
    @Test
373
    public void forceSendEmailNotifications() throws Exception {
374
        //emailSender.run();
375
    }
376

    
377
    @Test
378
    public void testEmail() throws Exception {
379
        ArrayList<String> list = new ArrayList<String>();
380
        list.add("konstantinagalouni@gmail.com");
381
        list.add("argirok@di.uoa.gr");
382
//        emailSender.send("openaire_id_test", "openaire_name_test", "community", list);
383
    }
384

    
385
}
    (1-1/1)