/*
 * 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.resources;

import org.teleal.cling.model.Resource;
import org.teleal.cling.model.meta.LocalDevice;
import org.teleal.cling.model.meta.RemoteDevice;
import org.teleal.cling.registry.RegistrationException;
import org.teleal.cling.test.MockUpnpService;
import org.teleal.cling.test.data.SampleData;
import org.teleal.cling.test.data.SampleDeviceRoot;
import org.teleal.cling.test.data.SampleDeviceRootLocal;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.net.URI;


public class RegistryNamespacingTest {

    @Test
    public void createLocalDevice() throws Exception {
        MockUpnpService upnpService = new MockUpnpService();

        LocalDevice device = SampleData.createLocalDevice();

        upnpService.getRegistry().addDevice(device);

        SampleDeviceRootLocal.assertLocalResourcesMatch(device);
    }

    @Test
    public void findLocalDevice() throws Exception {
        MockUpnpService upnpService = new MockUpnpService();

        LocalDevice deviceOne = SampleData.createLocalDevice();
        upnpService.getRegistry().addDevice(deviceOne);

        LocalDevice foundDevice =
                upnpService.getRegistry().getResourceModel(
                        LocalDevice.class,
                        Resource.Type.DEVICE_DESCRIPTOR,
                        SampleDeviceRoot.getDeviceDescriptorURI());

        Assert.assertNotNull(foundDevice);
    }

    @Test(expectedExceptions = IllegalArgumentException.class)
    public void findLocalDeviceInvalidRelativePath() throws Exception {
        MockUpnpService upnpService = new MockUpnpService();

        LocalDevice deviceOne = SampleData.createLocalDevice();
        upnpService.getRegistry().addDevice(deviceOne);

        upnpService.getRegistry().getResourceModel(
                LocalDevice.class,
                Resource.Type.DEVICE_DESCRIPTOR,
                URI.create("http://host/invalid/absolute/URI")
        );
    }

    @Test(expectedExceptions = RegistrationException.class)
    public void registerDuplicateDevices() throws Exception {
        MockUpnpService upnpService = new MockUpnpService();


        LocalDevice deviceOne = SampleData.createLocalDevice();
        upnpService.getRegistry().addDevice(deviceOne);

        LocalDevice deviceTwo = SampleData.createLocalDevice();
        upnpService.getRegistry().addDevice(deviceTwo);
    }

    @Test
    public void cleanupRemoteDevice() {
        MockUpnpService upnpService = new MockUpnpService();
        RemoteDevice rd = SampleData.createRemoteDevice();

        upnpService.getRegistry().addDevice(rd);
        upnpService.getRegistry().hydrate(rd);

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

        Resource resource = upnpService.getRegistry().getResource(
                URI.create("/upnp/device/MY-DEVICE-123/service/upnp-org/MY-SERVICE-123/events/callback.xml")
        );
        assert resource != null;

        upnpService.getRegistry().removeDevice(rd);

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

        resource = upnpService.getRegistry().getResource(
                URI.create("/upnp/device/MY-DEVICE-123/service/upnp-org/MY-SERVICE-123/events/callback.xml")
        );
        assert resource == null;


    }


}
