/*
 * 
 * 
 * Copyright (C) 2007 SIPfoundry Inc.
 * Licensed by SIPfoundry under the LGPL license.
 * 
 * Copyright (C) 2007 Pingtel Corp.
 * Licensed to SIPfoundry under a Contributor Agreement.
 * 
 * $
 */
package org.sipfoundry.sipxconfig.service;

import junit.framework.TestCase;

import org.easymock.classextension.EasyMock;
import org.easymock.classextension.IMocksControl;
import org.sipfoundry.sipxconfig.device.ModelSource;

public class AddServiceTaskTest extends TestCase {

    public void testAddService() {
        ServiceDescriptor turkeyDesctriptor = new ServiceDescriptor("bird", "turkey");
        IMocksControl serviceSourceControl = EasyMock.createControl();
        ModelSource<ServiceDescriptor> serviceSource = serviceSourceControl.createMock(ModelSource.class);
        serviceSource.getModel("turkey");
        serviceSourceControl.andReturn(turkeyDesctriptor).anyTimes();
        serviceSourceControl.replay();
        
        ConfiguredService turkey = new UnmanagedService();
        turkey.setDescriptor(turkeyDesctriptor);
        
        IMocksControl serviceManagerControl = EasyMock.createStrictControl();
        ServiceManager serviceManager = serviceManagerControl.createMock(ServiceManager.class);

        
        // already a service
        serviceManager.getServiceByName("Thanksgiving");
        serviceManagerControl.andReturn(turkey);
        serviceManagerControl.replay();
        AddServiceTask task = new AddServiceTask();
        task.setServiceManager(serviceManager);
        task.setServicesSource(serviceSource);
        try {
            task.addService("Thanksgiving", "chicken", "1.1.1.1");
            fail();
        } catch (IllegalArgumentException expected) {            
        }        
        serviceManagerControl.verify();
        

        // update service
        serviceManagerControl.reset();
        serviceManager.getServiceByName("Thanksgiving");
        serviceManagerControl.andReturn(turkey);
        serviceManager.saveService(turkey);
        serviceManagerControl.replay();        
        turkey.setName("Thanksgiving");
        task.addService("Thanksgiving", "turkey", "1.1.1.1");
        serviceManagerControl.verify();
        
        // add a service
        serviceManagerControl.reset();
        serviceManager.getServiceByName("Thanksgiving");
        serviceManagerControl.andReturn(null);
        serviceManager.newService(turkeyDesctriptor);
        serviceManagerControl.andReturn(turkey);
        serviceManager.saveService(turkey);        
        serviceManagerControl.replay();            
        turkey.setName("Thanksgiving");
        task.addService("Thanksgiving", "turkey", "1.1.1.1");        
        serviceManagerControl.verify();
    }

    public void testAssertArgument() {
        AddServiceTask.assertArgument(new String[] {"x"}, 0, "x");
        try {
            AddServiceTask.assertArgument(new String[0], 0, "x");
            fail();
        } catch (IllegalArgumentException expected) {
            assertTrue(true);
        }
    }
}

Add forgotten unit test for adding a new service task
Add forgotten unit test for adding a new service task

git-svn-id: f26ccc5efe72c2bd8e1c40f599fe313f2692e4de@9231 a612230a-c5fa-0310-af8b-88eea846685b
