/*
 * 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.message.UpnpMessage;
import org.teleal.cling.model.message.header.DeviceTypeHeader;
import org.teleal.cling.model.message.header.HostHeader;
import org.teleal.cling.model.message.header.MANHeader;
import org.teleal.cling.model.message.header.MXHeader;
import org.teleal.cling.model.message.header.RootDeviceHeader;
import org.teleal.cling.model.message.header.STAllHeader;
import org.teleal.cling.model.message.header.ServiceTypeHeader;
import org.teleal.cling.model.message.header.UDADeviceTypeHeader;
import org.teleal.cling.model.message.header.UDAServiceTypeHeader;
import org.teleal.cling.model.message.header.UDNHeader;
import org.teleal.cling.model.message.header.UpnpHeader;
import org.teleal.cling.model.types.DeviceType;
import org.teleal.cling.model.types.NotificationSubtype;
import org.teleal.cling.model.types.ServiceType;
import org.teleal.cling.model.types.UDADeviceType;
import org.teleal.cling.model.types.UDAServiceType;
import org.teleal.cling.model.types.UDN;
import org.teleal.cling.protocol.async.SendingSearch;
import org.teleal.cling.test.MockUpnpService;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.UUID;


public class SearchExecuteTest {

    @DataProvider(name = "searchTargets")
    public Object[][] getData() {
        return new UpnpHeader[][]{
                {new STAllHeader()},
                {new RootDeviceHeader()},
                {new UDADeviceTypeHeader(new UDADeviceType("MyDeviceType", 1))},
                {new UDAServiceTypeHeader(new UDAServiceType("MyServiceType", 1))},
                {new DeviceTypeHeader(new DeviceType("org-teleal-upnp", "MyDeviceType", 1))},
                {new ServiceTypeHeader(new ServiceType("org-teleal-upnp", "MyServiceType", 1))},
                {new UDNHeader(new UDN(UUID.randomUUID()))}
        };
    }

    @Test(dataProvider = "searchTargets")
    public void search(UpnpHeader searchTarget) throws Exception {

        MockUpnpService upnpService = new MockUpnpService();

        SendingSearch prot = new SendingSearch(upnpService, searchTarget);
        prot.run();

        Assert.assertEquals(upnpService.getOutgoingDatagramMessages().size(), 2);

        for (UpnpMessage msg : upnpService.getOutgoingDatagramMessages()) {
            assertSearchMessage(msg, searchTarget);
        }
    }

    @Test
    public void searchDefaults() {
        SendingSearch search = new SendingSearch(new MockUpnpService());
        Assert.assertEquals(search.getSearchTarget().getString(), new STAllHeader().getString());
    }

    @Test(expectedExceptions = java.lang.IllegalArgumentException.class)
    public void searchInvalidST() {
        SendingSearch search = new SendingSearch(new MockUpnpService(), new MXHeader());
    }

    protected void assertSearchMessage(UpnpMessage msg, UpnpHeader searchTarget) {
        Assert.assertEquals(msg.getHeaders().getFirstHeader(UpnpHeader.Type.MAN).getString(), new MANHeader(NotificationSubtype.DISCOVER.getHeaderString()).getString());
        Assert.assertEquals(msg.getHeaders().getFirstHeader(UpnpHeader.Type.MX).getString(), new MXHeader().getString());
        Assert.assertEquals(msg.getHeaders().getFirstHeader(UpnpHeader.Type.ST).getString(), searchTarget.getString());
        Assert.assertEquals(msg.getHeaders().getFirstHeader(UpnpHeader.Type.HOST).getString(), new HostHeader().getString());
    }
}
