/*
 * Copyright (C) 2010 Teleal GmbH, Switzerland
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.teleal.cling.test.ssdp;

import org.teleal.cling.model.meta.RemoteDevice;
import org.teleal.cling.test.MockUpnpService;
import org.teleal.cling.test.data.SampleData;
import org.testng.Assert;
import org.testng.annotations.Test;


public class RemoteDeviceExpirationTest {

    @Test
    public void addAndExpire() throws Exception {

        MockUpnpService upnpService = new MockUpnpService(false, true);

        RemoteDevice rd = SampleData.createRemoteDevice(SampleData.createRemoteDeviceIdentity(1));
        upnpService.getRegistry().addDevice(rd);
        
        Assert.assertEquals(upnpService.getRegistry().getRemoteDevices(true).size(), 1);

        Thread.sleep(3000);

        Assert.assertEquals(upnpService.getRegistry().getRemoteDevices(true).size(), 0);

        upnpService.shutdown();
    }


    @Test
    public void addAndUpdateAndExpire() throws Exception {

        MockUpnpService upnpService = new MockUpnpService(false, true);

        RemoteDevice rd = SampleData.createRemoteDevice(SampleData.createRemoteDeviceIdentity(1));

        // Add it to registry
        upnpService.getRegistry().addDevice(rd);
        Thread.sleep(500);
        Assert.assertEquals(upnpService.getRegistry().getRemoteDevices(true).size(), 1);

        // Update it in registry
        upnpService.getRegistry().addDevice(rd);
        Thread.sleep(500);
        Assert.assertEquals(upnpService.getRegistry().getRemoteDevices(true).size(), 1);

        // Update again
        upnpService.getRegistry().addDevice(rd);
        Thread.sleep(500);
        Assert.assertEquals(upnpService.getRegistry().getRemoteDevices(true).size(), 1);

        // Wait for expiration
        Thread.sleep(3000);
        Assert.assertEquals(upnpService.getRegistry().getRemoteDevices(true).size(), 0);

        upnpService.shutdown();
    }

}
