<commit_before>package org.buddycloud.channelserver.packetHandler.iq;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import junit.framework.TestCase;

import org.apache.commons.io.IOUtils;
import org.buddycloud.channelserver.queue.InQueueConsumer;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Packet;

import redis.clients.jedis.Jedis;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class IQHandlerTest extends TestCase {

    public final static String STANZA_PATH = "src/test/java/resources/stanzas";
    
    public static void dropMongodb() {
        Properties conf = readConf();
        
        Mongo mongo = null;
        try {
            mongo = new Mongo(conf.getProperty("mongo.host"), 
                    Integer.parseInt(conf.getProperty("mongo.port")));
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MongoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        DB mdb = mongo.getDB(conf.getProperty("mongo.db"));
        //mdb.dropDatabase();
        DBCollection subscriptions = mdb.getCollection("subscriptions");
        subscriptions.drop();
        DBCollection entries = mdb.getCollection("entries");
        entries.drop();
    }
    
    /**
     * Side-effect: resets Jedis
     * @return
     */
    public static Jedis getJedis() {
        Properties conf = readConf();
        Jedis jedis = new Jedis(conf.getProperty("redis.host"), 
                Integer.valueOf(conf.getProperty("redis.port")));
        jedis.configSet("timeout", "0");
        
        jedis.flushAll();
        dropMongodb();
        
        return jedis;
    }
    
    public static Properties readConf() {
        Properties conf = new Properties();
        try {
            conf.load(new FileInputStream("src/test/java/resources/configuration.properties"));
        } catch (IOException e) {
            System.out.println(e.getMessage());
            System.exit(1);
        }
        return conf;
    }
    
    public static String readStanzaAsString(String stanzaPath) throws IOException, DocumentException {
        String stanzaStr = IOUtils.toString(
                new FileInputStream(STANZA_PATH + stanzaPath));<commit_msg>Fixed location of resources<commit_after>package org.buddycloud.channelserver.packetHandler.iq;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import junit.framework.TestCase;

import org.apache.commons.io.IOUtils;
import org.buddycloud.channelserver.queue.InQueueConsumer;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Packet;

import redis.clients.jedis.Jedis;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class IQHandlerTest extends TestCase {

    public final static String STANZA_PATH = "src/test/resources/stanzas";
    
    public static void dropMongodb() {
        Properties conf = readConf();
        
        Mongo mongo = null;
        try {
            mongo = new Mongo(conf.getProperty("mongo.host"), 
                    Integer.parseInt(conf.getProperty("mongo.port")));
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MongoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        DB mdb = mongo.getDB(conf.getProperty("mongo.db"));
        //mdb.dropDatabase();
        DBCollection subscriptions = mdb.getCollection("subscriptions");
        subscriptions.drop();
        DBCollection entries = mdb.getCollection("entries");
        entries.drop();
    }
    
    /**
     * Side-effect: resets Jedis
     * @return
     */
    public static Jedis getJedis() {
        Properties conf = readConf();
        Jedis jedis = new Jedis(conf.getProperty("redis.host"), 
                Integer.valueOf(conf.getProperty("redis.port")));
        jedis.configSet("timeout", "0");
        
        jedis.flushAll();
        dropMongodb();
        
        return jedis;
    }
    
    public static Properties readConf() {
        Properties conf = new Properties();
        try {
            conf.load(new FileInputStream("src/test/resources/configuration.properties"));
        } catch (IOException e) {
            System.out.println(e.getMessage());
            System.exit(1);
        }
        return conf;
    }
    
    public static String readStanzaAsString(String stanzaPath) throws IOException, DocumentException {
        String stanzaStr = IOUtils.toString(
                new FileInputStream(STANZA_PATH + stanzaPath));
{'fraction_non_alphanumeric': 0.10603355944305605, 'fraction_numerical': 0.001785076758300607, 'mean_word_length': 2.5417193426042983, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3838393', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1571, 'n_words': 318}