import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.RedisConnection;
import com.lambdaworks.redis.RedisURI;
import eu.dnetlib.data.mapreduce.hbase.lodExport.build.BlockNoCacheReducer;
import eu.dnetlib.data.mapreduce.hbase.lodExport.build.SourceBuildNoCacheMapper;
import eu.dnetlib.data.mapreduce.hbase.lodExport.build.TargetBuildNoCacheMapper;
import eu.dnetlib.data.mapreduce.hbase.lodExport.linkage.LinkMapper;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mrunit.mapreduce.MapDriver;
import org.apache.hadoop.mrunit.mapreduce.MapReduceDriver;
import org.apache.hadoop.mrunit.mapreduce.ReduceDriver;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BuildTest {

    MapDriver<LongWritable, Text, Text, Text> targetBuildMapper;
    MapDriver<LongWritable, Text, Text, Text> sourceBuildMapper;
    ReduceDriver<Text, Text, Text, Text> blockReducer;

    @Before
    public void setUp() {


        Configuration configuration = new Configuration();
        String sourceMappings = "{\"type\":\"result\", \"fields\":[\"<http://purl.org/dc/terms/identifier>\",\"<http://www.eurocris.org/ontologies/cerif/1.3#name>\",\"<http://lod.openaire.eu/vocab/year>\"]}";
        configuration.set("lod.sourceMappings", sourceMappings);
        String stopwords = "a,able,about,above,abst,accordance,according,accordingly,across,act,actually,added,adj,affected,affecting,affects,after,afterwards,again,against,ah,all,almost,alone,along,already,also,although,always,am,among,amongst,an,and,announce,another,any,anybody,anyhow,anymore,anyone,anything,anyway,anyways,anywhere,apparently,approximately,are,aren,arent,arise,around,as,aside,ask,asking,at,auth,available,away,awfully,b,back,be,became,because,become,becomes,becoming,been,before,beforehand,begin,beginning,beginnings,begins,behind,being,believe,below,beside,besides,between,beyond,biol,both,brief,briefly,but,by,c,ca,came,can,cannot,cant,cause,causes,certain,certainly,co,com,come,comes,contain,containing,contains,could,couldnt,d,date,did,didnt,different,do,does,doesnt,doing,done,dont,down,downwards,due,during,e,each,ed,edu,effect,eg,eight,eighty,either,else,elsewhere,end,ending,enough,especially,et,et-al,etc,even,ever,every,everybody,everyone,everything,everywhere,ex,except,f,far,few,ff,fifth,first,five,fix,followed,following,follows,for,former,formerly,forth,found,four,from,further,furthermore,g,gave,get,gets,getting,give,given,gives,giving,go,goes,gone,got,gotten,h,had,happens,hardly,has,hasnt,have,havent,having,he,hed,hence,her,here,hereafter,hereby,herein,heres,hereupon,hers,herself,hes,hi,hid,him,himself,his,hither,home,how,howbeit,however,hundred,i,id,ie,if,ill,im,immediate,immediately,importance,important,in,inc,indeed,index,information,instead,into,invention,inward,is,isnt,it,itd,itll,its,itself,ive,j,just,k,keep,      keeps,kept,kg,km,know,known,knows,l,largely,last,lately,later,latter,latterly,least,less,lest,let,lets,like,liked,likely,line,little,ll,look,looking,looks,ltd,m,made,mainly,make,makes,many,may,maybe,me,mean,means,meantime,meanwhile,merely,mg,might,million,miss,ml,more,moreover,most,mostly,mr,mrs,much,mug,must,my,myself,n,na,name,namely,nay,nd,near,nearly,necessarily,necessary,need,needs,neither,never,nevertheless,new,next,nine,ninety,no,nobody,non,none,nonetheless,noone,nor,normally,nos,not,noted,nothing,now,nowhere,o,obtain,obtained,obviously,of,off,often,oh,ok,okay,old,omitted,on,once,one,ones,only,onto,or,ord,other,others,otherwise,ought,our,ours,ourselves,out,outside,over,overall,owing,own,p,page,pages,part,particular,particularly,past,per,perhaps,placed,please,plus,poorly,possible,possibly,potentially,pp,predominantly,present,previously,primarily,probably,promptly,proud,provides,put,q,que,quickly,quite,qv,r,ran,rather,rd,re,readily,really,recent,recently,ref,refs,regarding,regardless,regards,related,relatively,research,respectively,resulted,resulting,results,right,run,s,said,same,saw,say,saying,says,sec,section,see,seeing,seem,seemed,seeming,seems,seen,self,selves,sent,seven,several,shall,she,shed,shell,shes,should,shouldnt,show,showed,shown,showns,shows,significant,significantly,similar,similarly,since,six,slightly,so,some,somebody,somehow,someone,somethan,something,sometime,sometimes,somewhat,somewhere,soon,sorry,specifically,specified,specify,specifying,still,stop,strongly,sub,substantially,successfully,such,sufficiently,suggest,sup,sure,t,take,taken,taking,tell,tends,th,than,thank,thanks,thanx,that,thatll,thats,thatve,the,their,theirs,them,themselves,then,thence,there,thereafter,thereby,thered,therefore,therein,therell,thereof,therere,theres,thereto,thereupon,thereve,these,they,theyd,theyll,theyre,theyve,think,this,those,thou,though,thoughh,thousand,throug,through,throughout,thru,thus,til,tip,to,together,too,took,toward,towards,tried,tries,truly,try,trying,ts,twice,two,u,un,under,unfortunately,unless,unlike,unlikely,until,unto,up,upon,ups,us,use,used,useful,usefully,usefulness,uses,using,usually,v,value,various,ve,very,via,viz,vol,vols,vs,w,want,wants,was,wasnt,way,we,wed,welcome,well,went,were,werent,weve,what,whatever,whatll,whats,when,whence,whenever,where,whereafter,whereas,whereby,wherein,wheres,whereupon,wherever,whether,which,while,whim,whither,who,whod,whoever,whole,wholl,whom,whomever,whos,whose,why,widely,willing,wish,with,within,without,wont,words,world,would,wouldnt,www,x,y,yes,yet,you,youd,youll,your,youre,yours,yourself,yourselves,youve,z,zero\n";
        configuration.set("lod.stopwords", stopwords);
        configuration.set("lod.redisHost", "83.212.96.39");
        configuration.set("lod.redisPort", "6379");
        configuration.set("lod.redisPass", "TOXxhc0!53-A");
        String targetMappings = "{\"type\":\"publications\",\"fields\":[\"<http://purl.org/dc/terms/issued>\",\"<http://www.w3.org/2000/01/rdf-schema#label>\",\"<http://purl.org/dc/terms/identifier>\"]}";
        configuration.set("lod.targetMappings", targetMappings);


        targetBuildMapper = MapDriver.newMapDriver(new TargetBuildNoCacheMapper())
                .withConfiguration(configuration);
        sourceBuildMapper = MapDriver.newMapDriver(new SourceBuildNoCacheMapper())
                .withConfiguration(configuration);
        blockReducer = ReduceDriver.newReduceDriver(new BlockNoCacheReducer())
                .withConfiguration(configuration);

    }

    @Test
    public void testSourceMapper() throws IOException {
        sourceBuildMapper.withInput(new LongWritable(1), new Text("<http://lod.openaire.eu/data/result/acm_________::0e9512ea58c7ddf4115f6a56dee86abc>\t<http://purl.org/dc/terms/identifier>\t\"10.1145/1877937.1877943\"\t.\t<http://lod.openaire.eu/data/result/acm_________::0e9512ea58c7ddf4115f6a56dee86abc>\t<http://purl.org/dc/terms/identifier>\t\"acm_________::0e9512ea58c7ddf4115f6a56dee86abc\"\t.\t<http://lod.openaire.eu/data/result/acm_________::0e9512ea58c7ddf4115f6a56dee86abc>\t<http://purl.org/dc/terms/identifier>\t\"1877943\"\t.\t<http://lod.openaire.eu/data/result/acm_________::0e9512ea58c7ddf4115f6a56dee86abc>\t<http://www.eurocris.org/ontologies/cerif/1.3#name>\t\"Automatic event based indexing of multimedia content using a joint content event model\"\t.\t<http://lod.openaire.eu/data/result/acm_________::0e9512ea58c7ddf4115f6a56dee86abc>\t<http://lod.openaire.eu/vocab/year>\t\"2010\"\t.\t"));
        sourceBuildMapper.run();
    }


    @Test
    public void testTargetMapper() throws IOException {
        targetBuildMapper.withInput(new LongWritable(1), new Text("conf/3dic/HedlerSSKR10\t<http://www.w3.org/2000/01/rdf-schema#label>\tHigh performance 3D interconnects based on electrochemical etch and liquid metal fill.\t.\tconf/3dic/HedlerSSKR10\t<http://purl.org/dc/terms/issued>\t2010\t.\t\t"));
        targetBuildMapper.run();
    }


    @Test
    public void test() throws IOException {
        List l = new ArrayList<>();
        l.add("E");
        l.add("B");
        l.add("F");
        Collections.sort(l);
        System.out.println(l);
    }

    @Test
    public void redisTest() {
        RedisURI redisUri = RedisURI.Builder.redis("83.212.96.39")
                .withPassword("TOXxhc0!53-A")
                .withPort(6379)
                .build();
        RedisClient client = RedisClient.create(redisUri);
        RedisConnection connection = client.connect();
        System.out.println(connection.keys("*"));
        connection.close();
    }

}


