package umm3601.deck;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.*;
import org.bson.codecs.*;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.json.JsonReader;
import org.bson.types.ObjectId;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;

public class DeckControllerSpec {
    private DeckController deckController;
    private ObjectId testDeckId;
    @Before
    public void clearAndPopulateDB() throws IOException {
        MongoClient mongoClient = new MongoClient();
        MongoDatabase db = mongoClient.getDatabase("test");
        MongoCollection<Document> deckDocuments = db.getCollection("decks");
        deckDocuments.drop();
        List<Document> testDecks = new ArrayList<>();
        testDecks.add(Document.parse("{\n" +
            "   name: \"Deck1\", \n" +
            "   cards: \"[" +
            "{\n" +
            "   word: \"Cool\", \n" +
            "   synonym: \"rad\", \n" +
            "   antonym: \"bogus\", \n" +
            "   general_sense: \"to be radical or something\", \n" +
            "   example: \"Todd is as cool as ice\", \n" +
            "   }, \n" +
            "{\n" +
            "   word: \"Warm\", \n" +
            "   synonym: \"hot\", \n" +
            "   antonym: \"cold\", \n" +
            "   general_sense: \"to feel heat or something\", \n" +
            "   example: \"Todd is warm as hell\", \n" +
            "   } \n" +
            "              ]\n" +
            "                           }"
        ));

        testDecks.add(Document.parse("{\n" +
            "   name: \"Deck2\", \n" +
            "   cards: \"[" +
            "{\n" +
            "   word: \"Rad\", \n" +
            "   synonym: \"cool\", \n" +
            "   antonym: \"bogus\", \n" +
            "   general_sense: \"to be cool or something\", \n" +
            "   example: \"Todd is totally rad\", \n" +
            "   }, \n" +
            "{\n" +
            "   word: \"Hot\", \n" +
            "   synonym: \"warm\", \n" +
            "   antonym: \"cold\", \n" +
            "   general_sense: \"to feel warm or something\", \n" +
            "   example: \"Todd is hot as hell\", \n" +
            "   } \n" +
            "              ]\n" +
            "                           }"
        ));

        testDeckId = new ObjectId();
        Object[] cards = new Object[2];
        BasicDBObject testDeck = new BasicDBObject("_id", testDeckId);
        Object card1 = deckController.addNewCard("swell", "pretty alright", "uncool","this is pretty cool, my dude", "Angular 4 is pretty swell");
        Object card2 = deckController.addNewCard("swell", "pretty alright", "uncool","this is pretty cool, my dude", "Angular 4 is pretty swell");
        cards[0] = card1;
        cards[1] = card2;
        testDeck = testDeck.append("name", "TesterDeck")
                            .append("cards", cards);
    }


}

@@ -3190,11 +3190,1557 @@
 %0A    %7D%0A%0A
+    private BsonArray parseJsonArray(String json) %7B%0A        final CodecRegistry codecRegistry%0A            = CodecRegistries.fromProviders(Arrays.asList(%0A            new ValueCodecProvider(),%0A            new BsonValueCodecProvider(),%0A            new DocumentCodecProvider()));%0A%0A        JsonReader reader = new JsonReader(json);%0A        BsonArrayCodec arrayReader = new BsonArrayCodec(codecRegistry);%0A%0A        return arrayReader.decode(reader, DecoderContext.builder().build());%0A    %7D%0A%0A    private static String getName(BsonValue val) %7B%0A        BsonDocument doc = val.asDocument();%0A        return ((BsonString) doc.get(%22name%22)).getValue();%0A    %7D%0A%0A    // These both kinda fail atm%0A    @Test%0A    public void getAllDecks() %7B%0A        Map%3CString, String%5B%5D%3E emptyMap = new HashMap%3C%3E();%0A        String jsonResult = deckController.getDecks(emptyMap);%0A        BsonArray docs = parseJsonArray(jsonResult);%0A%0A        assertEquals(%22Should be 3 decks%22, 3, docs.size());%0A        List%3CString%3E names = docs%0A            .stream()%0A            .map(DeckControllerSpec::getName)%0A            .sorted()%0A            .collect(Collectors.toList());%0A        List%3CString%3E expectedNames = Arrays.asList(%22Deck1%22, %22Deck2%22, %22TesterDeck%22);%0A        assertEquals(%22Names should match%22, expectedNames, names);%0A    %7D%0A%0A    @Test%0A    public void getTesterDeckById() %7B%0A        String jsonResult = deckController.getDeck(testDeckId.toHexString());%0A        Document testDeck = Document.parse(jsonResult);%0A        assertEquals(%22Name should match%22, %22TesterDeck%22, testDeck.get(%22name%22));%0A    %7D%0A%0A
 %0A%7D%0A

Add some tests for deckController (that both fail) for setup