mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-11-04 00:02:37 +01:00 
			
		
		
		
	Setup unittests for the nicira nvp plugin
Changed the creation of the NiciraNvpApi to a factory method that can be overridden by a mock object. Setup two tests to test the configure function of the NiciraNvpResource to test this factory method.
This commit is contained in:
		
							parent
							
								
									7857b1f62a
								
							
						
					
					
						commit
						751e74708e
					
				@ -26,4 +26,12 @@
 | 
			
		||||
    <version>4.1.0-SNAPSHOT</version>
 | 
			
		||||
    <relativePath>../../pom.xml</relativePath>
 | 
			
		||||
  </parent>
 | 
			
		||||
  <dependencies>
 | 
			
		||||
  	<dependency>
 | 
			
		||||
  		<groupId>org.mockito</groupId>
 | 
			
		||||
  		<artifactId>mockito-all</artifactId>
 | 
			
		||||
  		<version>1.9.5</version>
 | 
			
		||||
  		<scope>test</scope>
 | 
			
		||||
  	</dependency>
 | 
			
		||||
  </dependencies>
 | 
			
		||||
</project>
 | 
			
		||||
 | 
			
		||||
@ -72,15 +72,7 @@ public class NiciraNvpApi {
 | 
			
		||||
    
 | 
			
		||||
    private HttpClient _client;
 | 
			
		||||
 | 
			
		||||
    public NiciraNvpApi(String host, String adminuser, String adminpass) throws NiciraNvpApiException {
 | 
			
		||||
        this._host = host;
 | 
			
		||||
        this._adminpass = adminpass;
 | 
			
		||||
        this._adminuser = adminuser;
 | 
			
		||||
        
 | 
			
		||||
        if (_host == null || _adminpass == null || _adminuser == null) {
 | 
			
		||||
            throw new NiciraNvpApiException("host, adminuser and adminpass may not be null");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    public NiciraNvpApi() {
 | 
			
		||||
        _client = new HttpClient(s_httpClientManager);
 | 
			
		||||
        _client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
 | 
			
		||||
        
 | 
			
		||||
@ -93,6 +85,15 @@ public class NiciraNvpApi {
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public void setControllerAddress(String address) {
 | 
			
		||||
    	this._host = address;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public void setAdminCredentials(String username, String password) {
 | 
			
		||||
    	this._adminuser = username;
 | 
			
		||||
    	this._adminpass = password;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Logs into the Nicira API. The cookie is stored in the <code>_authcookie<code> variable.
 | 
			
		||||
     * <p>
 | 
			
		||||
 | 
			
		||||
@ -88,39 +88,25 @@ public class NiciraNvpResource implements ServerResource {
 | 
			
		||||
    private static final Logger s_logger = Logger.getLogger(NiciraNvpResource.class);
 | 
			
		||||
    
 | 
			
		||||
    private String _name;
 | 
			
		||||
    private String _ip;
 | 
			
		||||
    private String _adminuser;
 | 
			
		||||
    private String _adminpass;
 | 
			
		||||
    private String _guid;
 | 
			
		||||
    private String _zoneId;
 | 
			
		||||
    private int _numRetries;
 | 
			
		||||
    
 | 
			
		||||
    private NiciraNvpApi _niciraNvpApi;
 | 
			
		||||
    
 | 
			
		||||
    protected NiciraNvpApi createNiciraNvpApi() {
 | 
			
		||||
    	return new NiciraNvpApi();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean configure(String name, Map<String, Object> params)
 | 
			
		||||
            throws ConfigurationException {
 | 
			
		||||
        
 | 
			
		||||
    	
 | 
			
		||||
        _name = (String) params.get("name");
 | 
			
		||||
        if (_name == null) {
 | 
			
		||||
            throw new ConfigurationException("Unable to find name");
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        _ip = (String) params.get("ip");
 | 
			
		||||
        if (_ip == null) {
 | 
			
		||||
            throw new ConfigurationException("Unable to find IP");
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        _adminuser = (String) params.get("adminuser");
 | 
			
		||||
        if (_adminuser == null) {
 | 
			
		||||
            throw new ConfigurationException("Unable to find admin username");
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        _adminpass = (String) params.get("adminpass");
 | 
			
		||||
        if (_adminpass == null) {
 | 
			
		||||
            throw new ConfigurationException("Unable to find admin password");
 | 
			
		||||
        }               
 | 
			
		||||
        
 | 
			
		||||
        _guid = (String)params.get("guid");
 | 
			
		||||
        if (_guid == null) {
 | 
			
		||||
            throw new ConfigurationException("Unable to find the guid");
 | 
			
		||||
@ -133,11 +119,24 @@ public class NiciraNvpResource implements ServerResource {
 | 
			
		||||
        
 | 
			
		||||
        _numRetries = 2;
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            _niciraNvpApi = new NiciraNvpApi(_ip, _adminuser, _adminpass);
 | 
			
		||||
        } catch (NiciraNvpApiException e) {
 | 
			
		||||
            throw new ConfigurationException(e.getMessage());
 | 
			
		||||
        String ip = (String) params.get("ip");
 | 
			
		||||
        if (ip == null) {
 | 
			
		||||
            throw new ConfigurationException("Unable to find IP");
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        String adminuser = (String) params.get("adminuser");
 | 
			
		||||
        if (adminuser == null) {
 | 
			
		||||
            throw new ConfigurationException("Unable to find admin username");
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        String adminpass = (String) params.get("adminpass");
 | 
			
		||||
        if (adminpass == null) {
 | 
			
		||||
            throw new ConfigurationException("Unable to find admin password");
 | 
			
		||||
        }               
 | 
			
		||||
        
 | 
			
		||||
        _niciraNvpApi = createNiciraNvpApi();
 | 
			
		||||
        _niciraNvpApi.setControllerAddress(ip);
 | 
			
		||||
        _niciraNvpApi.setAdminCredentials(adminuser,adminpass);
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,49 @@
 | 
			
		||||
package com.cloud.network.resource;
 | 
			
		||||
 | 
			
		||||
import static org.mockito.Mockito.*;
 | 
			
		||||
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
import javax.naming.ConfigurationException;
 | 
			
		||||
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import com.cloud.network.nicira.NiciraNvpApi;
 | 
			
		||||
 | 
			
		||||
public class NiciraNvpResourceTest {
 | 
			
		||||
	NiciraNvpApi _nvpApi = mock(NiciraNvpApi.class);
 | 
			
		||||
	NiciraNvpResource _resource;
 | 
			
		||||
	
 | 
			
		||||
	@Before
 | 
			
		||||
	public void setUp() {
 | 
			
		||||
		_resource = new NiciraNvpResource() {
 | 
			
		||||
			protected NiciraNvpApi createNiciraNvpApi() {
 | 
			
		||||
				return _nvpApi;
 | 
			
		||||
			}
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	@Test (expected=ConfigurationException.class)
 | 
			
		||||
	public void resourceConfigureFailure() throws ConfigurationException {
 | 
			
		||||
		_resource.configure("NiciraNvpResource", Collections.<String,Object>emptyMap());
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	@Test 
 | 
			
		||||
	public void resourceConfigure() throws ConfigurationException {
 | 
			
		||||
		Map<String,Object> parameters = new HashMap<String,Object>();
 | 
			
		||||
		parameters.put("name","nvptestdevice");
 | 
			
		||||
		parameters.put("ip","127.0.0.1");
 | 
			
		||||
		parameters.put("adminuser","adminuser");
 | 
			
		||||
		parameters.put("guid", "aaaaa-bbbbb-ccccc");
 | 
			
		||||
		parameters.put("zoneId", "blublub");
 | 
			
		||||
		parameters.put("adminpass","adminpass");
 | 
			
		||||
		_resource.configure("NiciraNvpResource", parameters);
 | 
			
		||||
		
 | 
			
		||||
		verify(_nvpApi).setAdminCredentials("adminuser", "adminpass");
 | 
			
		||||
		verify(_nvpApi).setControllerAddress("127.0.0.1");
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user