Package com.rila.chat.mongo

Source Code of com.rila.chat.mongo.MessagesHistoryMongoTest

package com.rila.chat.mongo;

import java.net.UnknownHostException;
import org.junit.Assert;
import org.junit.Test;

import com.google.gson.Gson;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;

import fr.cesi.chat.domain.Message;
import fr.cesi.chat.domain.Statut;
import fr.cesi.chat.domain.User;

public class MessagesHistoryMongoTest {

DB chatDatabase;
 
 
  public MessagesHistoryMongoTest(){
    MongoClient mongoClient;
    try {
      mongoClient = new MongoClient(new MongoClientURI(
          "mongodb://localhost"));
      chatDatabase = mongoClient.getDB("rilachatjavaTest");
     
    } catch (UnknownHostException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
   
  }
 
 
  public void dropCollection() {
    DBCollection messagesCollection = chatDatabase.getCollection("historymessages");
    messagesCollection.drop();
  }
 
  @Test
  public void testSaveMessage() {
    this.dropCollection();

    // on utilise notre fonction
    Statut statut = Statut.EN_LIGNE;
    Message message1 = new Message();
    message1.setMessage("comment vas-tu?");
    message1.setUserFrom(new User("benoit", statut, "192.168.1.1"));
    message1.setUserTo("florian");
    Gson gson = new Gson();
    String json = gson.toJson(message1);
    MessagesHistoryMongo conect;
    conect = new MessagesHistoryMongo();
    conect.saveMessage(json);

    // on test si on a bien le message insérer
    DBCollection messagesCollection = chatDatabase
        .getCollection("historymessages");
    DBObject messageReturn = messagesCollection.findOne();
    Assert.assertNotNull(messageReturn);
  }

  @Test
  public void testOnlyTenMessages() {
    this.dropCollection();
//    insertion de 10 messages
    MessagesHistoryMongo conect = new MessagesHistoryMongo();
 
   
    Statut statut = Statut.EN_LIGNE;
    Message message1 = new Message();
    message1.setMessage("comment vas-tu?");
    message1.setUserFrom(new User("benoit", statut, "192.168.1.1"));
    message1.setUserTo("florian");
   
   
    Gson gson = new Gson();
    String json1 = gson.toJson(message1);
    conect.saveMessage(json1);
   
    Statut statut2 = Statut.ABSENT;
    Message message2 = new Message();
    message2.setMessage("bien et toi");
    message2.setUserFrom(new User("florian", statut2, "192.168.1.2"));
    message2.setUserTo("Benoit");
   
    Gson gson2 = new Gson();
    String json2 = gson2.toJson(message2);
    conect.saveMessage(json2);
   
    Message message3 = new Message();
    message3.setMessage("cool raoul");
    message3.setUserFrom(new User("antho", statut, "192.168.1.3"));
    message3.setUserTo("All");
   
    Gson gson3 = new Gson();
    String json3 = gson3.toJson(message3);
    conect.saveMessage(json3);
   
    Message message4 = new Message();
    message4.setMessage("on se fait un mac DO");
    message4.setUserFrom(new User("benoit", statut, "192.168.1.1"));
    message4.setUserTo("florian");
   
    Gson gson4 = new Gson();
    String json4 = gson4.toJson(message4);
    conect.saveMessage(json4);
   
    Message message5 = new Message();
    message5.setMessage("ou un kebab");
    message5.setUserFrom(new User("florian", statut, "192.168.1.2"));
    message5.setUserTo("all");
   
    Gson gson5 = new Gson();
    String json5 = gson5.toJson(message5);
    conect.saveMessage(json5);
   
    Message message6 = new Message();
    message6.setMessage("pour moi mc do c'est cool!");
    message6.setUserFrom(new User("antho", statut, "192.168.1.3"));
    message6.setUserTo("all");
   
    Gson gson6 = new Gson();
    String json6 = gson6.toJson(message6);
    conect.saveMessage(json6);
   
    Message message7 = new Message();
    message7.setMessage("bon ben mc do");
    message7.setUserFrom(new User("florian", statut, "192.168.1.2"));
    message7.setUserTo("all");
   
    Gson gson7 = new Gson();
    String json7 = gson7.toJson(message7);
    conect.saveMessage(json7);

    Message message8 = new Message();
    message8.setMessage("on prend quelle voiture?");
    message8.setUserFrom(new User("florian", statut, "192.168.1.2"));
    message8.setUserTo("all");
   
    Gson gson8 = new Gson();
    String json8 = gson8.toJson(message8);
    conect.saveMessage(json8);
   
    Message message9 = new Message();
    message9.setMessage("la mienne c'est bon");
    message9.setUserFrom(new User("benoit", statut, "192.168.1.1"));
    message9.setUserTo("all");
   
    Gson gson9 = new Gson();
    String json9 = gson9.toJson(message9);
    conect.saveMessage(json9);
   
    Message message10 = new Message();
    message10.setMessage("allez go en B");
    message10.setUserFrom(new User("florian", statut, "192.168.1.2"));
    message10.setUserTo("all");
   
    Gson gson10 = new Gson();
    String json10 = gson10.toJson(message10);
    conect.saveMessage(json10);
   
    DBCollection messagesCollection = chatDatabase.getCollection("historymessages");
    Long numberOfMessages = messagesCollection.count();
   
    Assert.assertEquals(new Long(10) , numberOfMessages);
   
    Message message11 = new Message();
    message11.setMessage("un best OF!");
    message11.setUserFrom(new User("antho", statut, "192.168.1.3"));
    message11.setUserTo("all");
   
    Gson gson11 = new Gson();
    String json11 = gson11.toJson(message11);
    conect.saveMessage(json11);
   
    Long numberOfMessagesNew = messagesCollection.count();
    Assert.assertEquals(new Long(10) , numberOfMessagesNew);
   
    //todo test si c'est bien le plus ancien qui a été supprimé
   
  }

}
TOP

Related Classes of com.rila.chat.mongo.MessagesHistoryMongoTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.