/* 
 * Copyright 2014 OpenMarket Ltd
 * Copyright 2018 New Vector Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.matrix.androidsdk.rest.model.bingrules;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.matrix.androidsdk.data.Room;
import org.matrix.androidsdk.rest.model.RoomMember;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;

import java.util.ArrayList;
import java.util.List;

@RunWith(RobolectricTestRunner.class)
public class RoomMemberCountConditionTest {

    private RoomMemberCountCondition condition = new RoomMemberCountCondition();

    @Mock
    private Room mockRoom;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        setUpThreeRoomMembers();
    }

    private void setUpThreeRoomMembers() {
        List<RoomMember> members = new ArrayList<>();

        RoomMember rm1 = new RoomMember();
        rm1.membership = RoomMember.MEMBERSHIP_JOIN;
        members.add(rm1);

        RoomMember rm2 = new RoomMember();
        rm2.membership = RoomMember.MEMBERSHIP_JOIN;
        members.add(rm2);

        RoomMember rm3 = new RoomMember();
        rm3.membership = RoomMember.MEMBERSHIP_JOIN;
        members.add(rm3);

        // This one shouldn't count because they're not joined
        RoomMember rm4 = new RoomMember();
        rm4.membership = RoomMember.MEMBERSHIP_LEAVE;
        members.add(rm4);

        Mockito.when(mockRoom.getMembers()).thenReturn(members);
    }

    @Test
    public void testRoomMemberCount() {
        condition.is = "3";
        condition.parseIsField();
        Assert.assertTrue(condition.isSatisfied(mockRoom));

        condition.is = "4";
        condition.parseIsField();
        Assert.assertFalse(condition.isSatisfied(mockRoom));

        condition.is = "2";
        condition.parseIsField();
        Assert.assertFalse(condition.isSatisfied(mockRoom));
    }

    @Test
    public void testEquals() {
        condition.is = "==3";
        condition.parseIsField();
        Assert.assertTrue(condition.isSatisfied(mockRoom));

        condition.is = "==5";
        condition.parseIsField();
        Assert.assertFalse(condition.isSatisfied(mockRoom));
    }

    @Test
    public void testLess() {
        condition.is = "<5";
        condition.parseIsField();
        Assert.assertTrue(condition.isSatisfied(mockRoom));

        condition.is = "<3";
        condition.parseIsField();
        Assert.assertFalse(condition.isSatisfied(mockRoom));
    }

    @Test
    public void testGreater() {
        condition.is = ">2";
        condition.parseIsField();
        Assert.assertTrue(condition.isSatisfied(mockRoom));

        condition.is = ">3";
        condition.parseIsField();
        Assert.assertFalse(condition.isSatisfied(mockRoom));
    }

    @Test
    public void testLessOrEqual() {
        condition.is = "<=5";
        condition.parseIsField();
        Assert.assertTrue(condition.isSatisfied(mockRoom));

        condition.is = "<=3";
        condition.parseIsField();
        Assert.assertTrue(condition.isSatisfied(mockRoom));

        condition.is = "<=2";
        condition.parseIsField();
        Assert.assertFalse(condition.isSatisfied(mockRoom));
    }

    @Test
    public void testGreaterOrEqual() {
        condition.is = ">=2";
        condition.parseIsField();
        Assert.assertTrue(condition.isSatisfied(mockRoom));

        condition.is = ">=3";
        condition.parseIsField();
        Assert.assertTrue(condition.isSatisfied(mockRoom));

        condition.is = ">=5";
        condition.parseIsField();
        Assert.assertFalse(condition.isSatisfied(mockRoom));
    }

    @Test
    public void testParseError() {
        condition.is = "f2";
        condition.parseIsField();
        Assert.assertFalse(condition.isSatisfied(mockRoom));

        condition.is = "x";
        condition.parseIsField();
        Assert.assertFalse(condition.isSatisfied(mockRoom));
    }
}

@@ -830,60 +830,8 @@
 om;%0A
-import org.matrix.androidsdk.rest.model.RoomMember;%0A
 impo
@@ -851,16 +851,16 @@
 o.Mock;%0A
+
 import o
@@ -969,60 +969,8 @@
 r;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 @Run
@@ -1345,619 +1345,8 @@
     
-List%3CRoomMember%3E members = new ArrayList%3C%3E();%0A%0A        RoomMember rm1 = new RoomMember();%0A        rm1.membership = RoomMember.MEMBERSHIP_JOIN;%0A        members.add(rm1);%0A%0A        RoomMember rm2 = new RoomMember();%0A        rm2.membership = RoomMember.MEMBERSHIP_JOIN;%0A        members.add(rm2);%0A%0A        RoomMember rm3 = new RoomMember();%0A        rm3.membership = RoomMember.MEMBERSHIP_JOIN;%0A        members.add(rm3);%0A%0A        // This one shouldn't count because they're not joined%0A        RoomMember rm4 = new RoomMember();%0A        rm4.membership = RoomMember.MEMBERSHIP_LEAVE;%0A        members.add(rm4);%0A%0A        
 Mock
@@ -1366,16 +1366,30 @@
 Room.get
+NumberOfJoined
 Members(
@@ -1406,15 +1406,9 @@
 urn(
-members
+3
 );%0A 

Fix test compilation issue