Fix and add some Rest unit and IT tests. Fix by adding a missing type of RoutingConfig.

Signed-off-by: Hugo Trippaers <htrippaers@schubergphilis.com>
This commit is contained in:
Antonio Fornie 2014-02-18 17:02:12 +01:00 committed by Hugo Trippaers
parent b70af44ecf
commit 808c6df6d7
4 changed files with 263 additions and 210 deletions

View File

@ -634,6 +634,9 @@ public class NiciraNvpApi {
public static class RoutingConfigAdapter implements JsonDeserializer<RoutingConfig> {
private static final String ROUTING_TABLE_ROUTING_CONFIG = "RoutingTableRoutingConfig";
private static final String SINGLE_DEFAULT_ROUTE_IMPLICIT_ROUTING_CONFIG = "SingleDefaultRouteImplicitRoutingConfig";
@Override
public RoutingConfig deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) throws JsonParseException {
final JsonObject jsonObject = jsonElement.getAsJsonObject();
@ -643,8 +646,10 @@ public class NiciraNvpApi {
}
final String routingConfigType = jsonObject.get("type").getAsString();
if ("SingleDefaultRouteImplicitRoutingConfig".equals(routingConfigType)) {
if (SINGLE_DEFAULT_ROUTE_IMPLICIT_ROUTING_CONFIG.equals(routingConfigType)) {
return context.deserialize(jsonElement, SingleDefaultRouteImplicitRoutingConfig.class);
} else if (ROUTING_TABLE_ROUTING_CONFIG.equals(routingConfigType)) {
return context.deserialize(jsonElement, RoutingTableRoutingConfig.class);
}
throw new JsonParseException("Failed to deserialize type \"" + routingConfigType + "\"");

View File

@ -0,0 +1,30 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package com.cloud.network.nicira;
/**
*
*/
public class RoutingTableRoutingConfig extends RoutingConfig {
public final String type = "RoutingTableRoutingConfig";
public RoutingTableRoutingConfig() {
}
}

View File

@ -20,199 +20,214 @@
package com.cloud.network.nicira;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.junit.Before;
import org.junit.Test;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.cloud.utils.rest.RESTServiceConnector;
import com.cloud.utils.rest.RESTValidationStrategy;
public class NiciraNvpApiTest {
// protected static final String UUID = "aaaa";
// protected static final String UUID2 = "bbbb";
// protected static final String UUID_SEC_PROFILE_URI = NiciraNvpApi.SEC_PROFILE_URI_PREFIX + "/aaaa";
// protected static final String SCHEMA = "myTestSchema";
// protected static final String SCHEMA2 = "myTestSchema2";
// protected static final String HREF = "myTestHref";
// protected static final String HREF2 = "myTestHref2";
// protected static final String DISPLAY_NAME = "myTestName";
// protected static final String UUID_JSON_RESPONSE = "{\"uuid\" : \"aaaa\"}";
// protected static final String SEC_PROFILE_JSON_RESPONSE =
// "{\"uuid\" : \"aaaa\","
// + "\"display_name\" : \"myTestName\","
// + "\"href\" : \"myTestHref\","
// + "\"schema\" : \"myTestSchema\"}";
//
// protected static final String SEC_PROFILE_LIST_JSON_RESPONSE = "{\"results\" : [{\"uuid\" : \"aaaa\","
// + "\"display_name\" : \"myTestName\","
// + "\"href\" : \"myTestHref\","
// + "\"schema\" : \"myTestSchema\"},"
// + "{ \"uuid\" : \"bbbb\","
// + "\"display_name\" : \"myTestName2\","
// + "\"href\" : \"myTestHref2\","
// + "\"schema\" : \"myTestSchema2\"}],"
// + "\"result_count\": 2}";
//
// NiciraNvpApi api;
// HttpClient client = mock(HttpClient.class);
// HttpMethod method;
// String type;
// String uri;
//
// @Before
// public void setUp() {
// final HttpClientParams hmp = mock(HttpClientParams.class);
// when(client.getParams()).thenReturn(hmp);
// api = new NiciraNvpApi();
//
// api.restConnector = new RESTServiceConnector() {
// @Override
// public HttpClient createHttpClient() {
// return client;
// }
//
// @Override
// public HttpMethod createMethod(final String newType, final String newUri) {
// type = newType;
// uri = newUri;
// return method;
// }
// };
//
// api.setAdminCredentials("admin", "adminpass");
// api.setControllerAddress("localhost");
// }
//
// @Test
// public void testFindSecurityProfile() throws NiciraNvpApiException, IOException {
// // Prepare
// method = mock(GetMethod.class);
// when(method.getStatusCode()).thenReturn(HttpStatus.SC_OK);
// when(method.getResponseBodyAsString()).thenReturn(SEC_PROFILE_LIST_JSON_RESPONSE);
// final NameValuePair[] queryString = new NameValuePair[]{
// new NameValuePair("fields","*")};
//
// // Execute
// final NiciraNvpList<SecurityProfile> actualProfiles = api.findSecurityProfile();
//
// // Assert
// verify(method, times(1)).releaseConnection();
// verify(method, times(1)).setQueryString(queryString);
// assertEquals("Wrong Uuid in the newly created SecurityProfile",
// UUID, actualProfiles.getResults().get(0).getUuid());
// assertEquals("Wrong Uuid in the newly created SecurityProfile",
// HREF, actualProfiles.getResults().get(0).getHref());
// assertEquals("Wrong Schema in the newly created SecurityProfile",
// SCHEMA, actualProfiles.getResults().get(0).getSchema());
// assertEquals("Wrong Uuid in the newly created SecurityProfile",
// UUID2, actualProfiles.getResults().get(1).getUuid());
// assertEquals("Wrong Uuid in the newly created SecurityProfile",
// HREF2, actualProfiles.getResults().get(1).getHref());
// assertEquals("Wrong Schema in the newly created SecurityProfile",
// SCHEMA2, actualProfiles.getResults().get(1).getSchema());
// assertEquals("Wrong Schema in the newly created SecurityProfile",
// 2, actualProfiles.getResultCount());
// assertEquals("Wrong URI for SecurityProfile creation REST service",
// NiciraNvpApi.SEC_PROFILE_URI_PREFIX, uri);
// assertEquals("Wrong URI for SecurityProfile creation REST service",
// NiciraNvpApi.GET_METHOD_TYPE, type);
// }
//
// @Test
// public void testFindSecurityProfileByUuid() throws NiciraNvpApiException, IOException {
// // Prepare
// method = mock(GetMethod.class);
// when(method.getStatusCode()).thenReturn(HttpStatus.SC_OK);
// when(method.getResponseBodyAsString()).thenReturn(SEC_PROFILE_LIST_JSON_RESPONSE);
// final NameValuePair[] queryString = new NameValuePair[]{
// new NameValuePair("uuid", UUID),
// new NameValuePair("fields","*")
// };
//
// // Execute
// final NiciraNvpList<SecurityProfile> actualProfiles = api.findSecurityProfile(UUID);
//
// // Assert
// verify(method, times(1)).releaseConnection();
// verify(method, times(1)).setQueryString(queryString);
// assertEquals("Wrong Uuid in the newly created SecurityProfile",
// UUID, actualProfiles.getResults().get(0).getUuid());
// assertEquals("Wrong Uuid in the newly created SecurityProfile",
// HREF, actualProfiles.getResults().get(0).getHref());
// assertEquals("Wrong Schema in the newly created SecurityProfile",
// SCHEMA, actualProfiles.getResults().get(0).getSchema());
// assertEquals("Wrong Uuid in the newly created SecurityProfile",
// UUID2, actualProfiles.getResults().get(1).getUuid());
// assertEquals("Wrong Uuid in the newly created SecurityProfile",
// HREF2, actualProfiles.getResults().get(1).getHref());
// assertEquals("Wrong Schema in the newly created SecurityProfile",
// SCHEMA2, actualProfiles.getResults().get(1).getSchema());
// assertEquals("Wrong Schema in the newly created SecurityProfile",
// 2, actualProfiles.getResultCount());
// assertEquals("Wrong URI for SecurityProfile creation REST service",
// NiciraNvpApi.SEC_PROFILE_URI_PREFIX, uri);
// assertEquals("Wrong HTTP method for SecurityProfile creation REST service",
// NiciraNvpApi.GET_METHOD_TYPE, type);
// }
//
// @Test
// public void testCreateSecurityProfile() throws NiciraNvpApiException, IOException {
// // Prepare
// final SecurityProfile inputSecProfile = new SecurityProfile();
// method = mock(PostMethod.class);
// when(method.getStatusCode()).thenReturn(HttpStatus.SC_CREATED);
// when(method.getResponseBodyAsString()).thenReturn(SEC_PROFILE_JSON_RESPONSE);
//
// // Execute
// final SecurityProfile actualSecProfile = api.createSecurityProfile(inputSecProfile);
//
// // Assert
// verify(method, times(1)).releaseConnection();
// assertEquals("Wrong Uuid in the newly created SecurityProfile",
// UUID, actualSecProfile.getUuid());
// assertEquals("Wrong Uuid in the newly created SecurityProfile",
// HREF, actualSecProfile.getHref());
// assertEquals("Wrong Schema in the newly created SecurityProfile",
// SCHEMA, actualSecProfile.getSchema());
// assertEquals("Wrong URI for SecurityProfile creation REST service",
// NiciraNvpApi.SEC_PROFILE_URI_PREFIX, uri);
// assertEquals("Wrong HTTP method for SecurityProfile creation REST service",
// NiciraNvpApi.POST_METHOD_TYPE, type);
// }
//
// @Test
// public void testUpdateSecurityProfile() throws NiciraNvpApiException, IOException {
// // Prepare
// final SecurityProfile inputSecProfile = new SecurityProfile();
// method = mock(PutMethod.class);
// when(method.getStatusCode()).thenReturn(HttpStatus.SC_OK);
//
// // Execute
// api.updateSecurityProfile(inputSecProfile, UUID);
//
// // Assert
// verify(method, times(1)).releaseConnection();
// assertEquals("Wrong URI for SecurityProfile creation REST service",
// UUID_SEC_PROFILE_URI, uri);
// assertEquals("Wrong HTTP method for SecurityProfile creation REST service",
// NiciraNvpApi.PUT_METHOD_TYPE, type);
// }
//
// @Test
// public void testDeleteSecurityProfile() throws NiciraNvpApiException, IOException {
// // Prepare
// method = mock(DeleteMethod.class);
// when(method.getStatusCode()).thenReturn(HttpStatus.SC_NO_CONTENT);
//
// // Execute
// api.deleteSecurityProfile(UUID);
//
// // Assert
// verify(method, times(1)).releaseConnection();
// assertEquals("Wrong URI for SecurityProfile deletion REST service", UUID_SEC_PROFILE_URI, uri);
// assertEquals("Wrong HTTP method for SecurityProfile deletion REST service", NiciraNvpApi.DELETE_METHOD_TYPE, type);
// }
protected static final String UUID = "aaaa";
protected static final String UUID2 = "bbbb";
protected static final String UUID_SEC_PROFILE_URI = NiciraNvpApi.SEC_PROFILE_URI_PREFIX + "/aaaa";
protected static final String SCHEMA = "myTestSchema";
protected static final String SCHEMA2 = "myTestSchema2";
protected static final String HREF = "myTestHref";
protected static final String HREF2 = "myTestHref2";
protected static final String SEC_PROFILE_JSON_RESPONSE =
"{\"uuid\" : \"aaaa\","
+ "\"display_name\" : \"myTestName\","
+ "\"href\" : \"myTestHref\","
+ "\"schema\" : \"myTestSchema\"}";
protected static final String SEC_PROFILE_LIST_JSON_RESPONSE = "{\"results\" : [{\"uuid\" : \"aaaa\","
+ "\"display_name\" : \"myTestName\","
+ "\"href\" : \"myTestHref\","
+ "\"schema\" : \"myTestSchema\"},"
+ "{ \"uuid\" : \"bbbb\","
+ "\"display_name\" : \"myTestName2\","
+ "\"href\" : \"myTestHref2\","
+ "\"schema\" : \"myTestSchema2\"}],"
+ "\"result_count\": 2}";
NiciraNvpApi api;
HttpClient client = mock(HttpClient.class);
HttpMethod method;
String type;
String uri;
@Before
public void setUp() {
final HttpClientParams hmp = mock(HttpClientParams.class);
when(client.getParams()).thenReturn(hmp);
api = new NiciraNvpApi();
api.restConnector = new RESTServiceConnector(new RESTValidationStrategy()) {
@Override
public HttpClient createHttpClient() {
return client;
}
@Override
public HttpMethod createMethod(final String newType, final String newUri) {
type = newType;
uri = newUri;
return method;
}
};
api.setAdminCredentials("admin", "adminpass");
api.setControllerAddress("localhost");
}
@Test
public void testFindSecurityProfile() throws NiciraNvpApiException, IOException {
// Prepare
method = mock(GetMethod.class);
when(method.getStatusCode()).thenReturn(HttpStatus.SC_OK);
when(method.getResponseBodyAsString()).thenReturn(SEC_PROFILE_LIST_JSON_RESPONSE);
final NameValuePair[] queryString = new NameValuePair[]{
new NameValuePair("fields","*")};
// Execute
final NiciraNvpList<SecurityProfile> actualProfiles = api.findSecurityProfile();
// Assert
verify(method, times(1)).releaseConnection();
verify(method, times(1)).setQueryString(queryString);
assertEquals("Wrong Uuid in the newly created SecurityProfile",
UUID, actualProfiles.getResults().get(0).getUuid());
assertEquals("Wrong Uuid in the newly created SecurityProfile",
HREF, actualProfiles.getResults().get(0).getHref());
assertEquals("Wrong Schema in the newly created SecurityProfile",
SCHEMA, actualProfiles.getResults().get(0).getSchema());
assertEquals("Wrong Uuid in the newly created SecurityProfile",
UUID2, actualProfiles.getResults().get(1).getUuid());
assertEquals("Wrong Uuid in the newly created SecurityProfile",
HREF2, actualProfiles.getResults().get(1).getHref());
assertEquals("Wrong Schema in the newly created SecurityProfile",
SCHEMA2, actualProfiles.getResults().get(1).getSchema());
assertEquals("Wrong Schema in the newly created SecurityProfile",
2, actualProfiles.getResultCount());
assertEquals("Wrong URI for SecurityProfile creation REST service",
NiciraNvpApi.SEC_PROFILE_URI_PREFIX, uri);
assertEquals("Wrong URI for SecurityProfile creation REST service",
NiciraNvpApi.GET_METHOD_TYPE, type);
}
@Test
public void testFindSecurityProfileByUuid() throws NiciraNvpApiException, IOException {
// Prepare
method = mock(GetMethod.class);
when(method.getStatusCode()).thenReturn(HttpStatus.SC_OK);
when(method.getResponseBodyAsString()).thenReturn(SEC_PROFILE_LIST_JSON_RESPONSE);
final NameValuePair[] queryString = new NameValuePair[]{
new NameValuePair("uuid", UUID),
new NameValuePair("fields","*")
};
// Execute
final NiciraNvpList<SecurityProfile> actualProfiles = api.findSecurityProfile(UUID);
// Assert
verify(method, times(1)).releaseConnection();
verify(method, times(1)).setQueryString(queryString);
assertEquals("Wrong Uuid in the newly created SecurityProfile",
UUID, actualProfiles.getResults().get(0).getUuid());
assertEquals("Wrong Uuid in the newly created SecurityProfile",
HREF, actualProfiles.getResults().get(0).getHref());
assertEquals("Wrong Schema in the newly created SecurityProfile",
SCHEMA, actualProfiles.getResults().get(0).getSchema());
assertEquals("Wrong Uuid in the newly created SecurityProfile",
UUID2, actualProfiles.getResults().get(1).getUuid());
assertEquals("Wrong Uuid in the newly created SecurityProfile",
HREF2, actualProfiles.getResults().get(1).getHref());
assertEquals("Wrong Schema in the newly created SecurityProfile",
SCHEMA2, actualProfiles.getResults().get(1).getSchema());
assertEquals("Wrong Schema in the newly created SecurityProfile",
2, actualProfiles.getResultCount());
assertEquals("Wrong URI for SecurityProfile creation REST service",
NiciraNvpApi.SEC_PROFILE_URI_PREFIX, uri);
assertEquals("Wrong HTTP method for SecurityProfile creation REST service",
NiciraNvpApi.GET_METHOD_TYPE, type);
}
@Test
public void testCreateSecurityProfile() throws NiciraNvpApiException, IOException {
// Prepare
final SecurityProfile inputSecProfile = new SecurityProfile();
method = mock(PostMethod.class);
when(method.getStatusCode()).thenReturn(HttpStatus.SC_CREATED);
when(method.getResponseBodyAsString()).thenReturn(SEC_PROFILE_JSON_RESPONSE);
// Execute
final SecurityProfile actualSecProfile = api.createSecurityProfile(inputSecProfile);
// Assert
verify(method, times(1)).releaseConnection();
assertEquals("Wrong Uuid in the newly created SecurityProfile",
UUID, actualSecProfile.getUuid());
assertEquals("Wrong Uuid in the newly created SecurityProfile",
HREF, actualSecProfile.getHref());
assertEquals("Wrong Schema in the newly created SecurityProfile",
SCHEMA, actualSecProfile.getSchema());
assertEquals("Wrong URI for SecurityProfile creation REST service",
NiciraNvpApi.SEC_PROFILE_URI_PREFIX, uri);
assertEquals("Wrong HTTP method for SecurityProfile creation REST service",
NiciraNvpApi.POST_METHOD_TYPE, type);
}
@Test
public void testUpdateSecurityProfile() throws NiciraNvpApiException, IOException {
// Prepare
final SecurityProfile inputSecProfile = new SecurityProfile();
method = mock(PutMethod.class);
when(method.getStatusCode()).thenReturn(HttpStatus.SC_OK);
// Execute
api.updateSecurityProfile(inputSecProfile, UUID);
// Assert
verify(method, times(1)).releaseConnection();
assertEquals("Wrong URI for SecurityProfile creation REST service",
UUID_SEC_PROFILE_URI, uri);
assertEquals("Wrong HTTP method for SecurityProfile creation REST service",
NiciraNvpApi.PUT_METHOD_TYPE, type);
}
@Test
public void testDeleteSecurityProfile() throws NiciraNvpApiException, IOException {
// Prepare
method = mock(DeleteMethod.class);
when(method.getStatusCode()).thenReturn(HttpStatus.SC_NO_CONTENT);
// Execute
api.deleteSecurityProfile(UUID);
// Assert
verify(method, times(1)).releaseConnection();
assertEquals("Wrong URI for SecurityProfile deletion REST service", UUID_SEC_PROFILE_URI, uri);
assertEquals("Wrong HTTP method for SecurityProfile deletion REST service", NiciraNvpApi.DELETE_METHOD_TYPE, type);
}
@Test(expected = JsonParseException.class)
public void testRoutingConfigAdapterNoType() throws NiciraNvpApiException, IOException {

View File

@ -28,7 +28,6 @@ import static org.mockito.Mockito.when;
import java.io.IOException;
import java.util.Collections;
//import java.util.List;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
@ -45,28 +44,7 @@ import org.junit.Test;
public class RESTServiceConnectorTest {
protected static final String UUID = "aaaa";
protected static final String UUID2 = "bbbb";
protected static final String SCHEMA = "myTestSchema";
protected static final String SCHEMA2 = "myTestSchema2";
protected static final String HREF = "myTestHref";
protected static final String HREF2 = "myTestHref2";
protected static final String DISPLAY_NAME = "myTestName";
protected static final String UUID_JSON_RESPONSE = "{\"uuid\" : \"aaaa\"}";
protected static final String SEC_PROFILE_JSON_RESPONSE =
"{\"uuid\" : \"aaaa\","
+ "\"display_name\" : \"myTestName\","
+ "\"href\" : \"myTestHref\","
+ "\"schema\" : \"myTestSchema\"}";
protected static final String SEC_PROFILE_LIST_JSON_RESPONSE = "{\"results\" : [{\"uuid\" : \"aaaa\","
+ "\"display_name\" : \"myTestName\","
+ "\"href\" : \"myTestHref\","
+ "\"schema\" : \"myTestSchema\"},"
+ "{ \"uuid\" : \"bbbb\","
+ "\"display_name\" : \"myTestName2\","
+ "\"href\" : \"myTestHref2\","
+ "\"schema\" : \"myTestSchema2\"}],"
+ "\"result_count\": 2}";
RESTServiceConnector connector;
HttpClient client = mock(HttpClient.class);
@ -175,9 +153,6 @@ public class RESTServiceConnectorTest {
verify(gm, times(1)).getStatusCode();
}
/* Bit of a roundabout way to ensure that login is called after an un authorized result
* It not possible to properly mock login()
*/
@Test(expected = CloudstackRESTException.class)
public void testExecuteMethodWithLogin() throws CloudstackRESTException, HttpException, IOException {
final GetMethod gm = mock(GetMethod.class);
@ -187,6 +162,34 @@ public class RESTServiceConnectorTest {
verify(gm, times(1)).getStatusCode();
}
/* Bit of a roundabout way to ensure that login is called after an un authorized result
* It not possible to properly mock login()
*/
public void testExecuteMethodWithLoginSucced2ndAttempt() throws CloudstackRESTException, HttpException, IOException {
// Prepare
final GetMethod gm = mock(GetMethod.class);
when(gm.getStatusCode()).thenReturn(HttpStatus.SC_UNAUTHORIZED).thenReturn(HttpStatus.SC_UNAUTHORIZED);
final RESTValidationStrategy previousValidationStrategy = connector.validation;
connector.validation = new RESTValidationStrategy(){
@Override
protected void login(final String protocol, final HttpClient client)
throws CloudstackRESTException {
// Do nothing
}
};
connector.setAdminCredentials("admin", "adminpass");
connector.setControllerAddress("localhost");
// Execute
connector.executeMethod(gm);
// Leave mock object as is was
connector.validation = previousValidationStrategy;
// Assert/verify
verify(gm, times(2)).getStatusCode();
}
@Test
public void testExecuteCreateObject() throws CloudstackRESTException, IOException {
JsonEntity ls = new JsonEntity();