package io.kamax.matrix.json;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import io.kamax.matrix.MatrixID;
import io.kamax.matrix._MatrixID;
import io.kamax.matrix.event.EventKey;
import io.kamax.matrix.json.event.MatrixJsonDirectEvent;

import org.junit.Test;

import static junit.framework.TestCase.assertTrue;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class MatrixJsonDirectEventTest {

    private final _MatrixID userId = MatrixID.from("user", "example.org").valid();

    private MatrixJsonDirectEvent makeEvent(JsonObject content) {
        JsonObject obj = new JsonObject();
        obj.addProperty(EventKey.Type.get(), "m.direct");
        obj.add(EventKey.Content.get(), content);
        return new MatrixJsonDirectEvent(obj);
    }

    @Test
    public void contentEmpty() {
        MatrixJsonDirectEvent event = makeEvent(new JsonObject());
        assertTrue(event.getMappings().isEmpty());
    }

    @Test(expected = InvalidJsonException.class)
    public void valueNull() {
        makeEvent(GsonUtil.makeObj(userId.getId(), null));
    }

    @Test
    public void valueEmpty() {
        JsonObject content = GsonUtil.makeObj(userId.getId(), new JsonArray());
        MatrixJsonDirectEvent event = makeEvent(content);
        assertEquals(1, event.getMappings().size());
        assertTrue(event.getMappings().get(userId).isEmpty());
    }

    @Test
    public void singleValuePresent() {
        String roomId = "!roomId:example.org";
        JsonObject content = GsonUtil.makeObj(userId.getId(), GsonUtil.asArray(roomId));
        MatrixJsonDirectEvent event = makeEvent(content);
        assertEquals(1, event.getMappings().size());
        assertEquals(1, event.getMappings().get(userId).size());
        assertEquals(roomId, event.getMappings().get(userId).get(0));
    }

    @Test(expected = InvalidJsonException.class)
    public void nullObject() {
        new MatrixJsonDirectEvent(null);
    }

    @Test(expected = InvalidJsonException.class)
    public void emptyObject() {
        new MatrixJsonDirectEvent(new JsonObject());
    }

    @Test(expected = InvalidJsonException.class)
    public void invalidType() {
        JsonObject obj = new JsonObject();
        obj.addProperty(EventKey.Type.get(), "m");
        obj.add(EventKey.Content.get(), new JsonObject());
        new MatrixJsonDirectEvent(obj);
    }

}

@@ -1,8 +1,793 @@
+/*%0A * matrix-java-sdk - Matrix Client SDK for Java%0A * Copyright (C) 2018 Kamax Sarl%0A *%0A * https://www.kamax.io/%0A *%0A * This program is free software: you can redistribute it and/or modify%0A * it under the terms of the GNU Affero General Public License as%0A * published by the Free Software Foundation, either version 3 of the%0A * License, or (at your option) any later version.%0A *%0A * This program is distributed in the hope that it will be useful,%0A * but WITHOUT ANY WARRANTY; without even the implied warranty of%0A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the%0A * GNU Affero General Public License for more details.%0A *%0A * You should have received a copy of the GNU Affero General Public License%0A * along with this program. If not, see %3Chttp://www.gnu.org/licenses/%3E.%0A */%0A%0A
 package 

Add missing header