JCLOUDS-272: Migrate search role tests from ChefApiTest to ChefApiExpectTest.

This commit is contained in:
Noorul Islam K M 2013-09-18 13:05:37 +05:30 committed by Ignasi Barrera
parent 223540a48c
commit eee53e38db
4 changed files with 89 additions and 37 deletions

View File

@ -25,6 +25,9 @@ import javax.ws.rs.core.MediaType;
import org.jclouds.chef.config.ChefHttpApiModule;
import org.jclouds.chef.domain.CookbookDefinition;
import org.jclouds.chef.domain.Role;
import org.jclouds.chef.domain.SearchResult;
import org.jclouds.chef.options.SearchOptions;
import org.jclouds.date.TimeStamp;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
@ -36,7 +39,7 @@ import com.google.inject.Module;
/**
* Expect tests for the {@link ChefApi} class.
*
*
* @author Noorul Islam K M
*/
@Test(groups = "unit", testName = "ChefApiExpectTest")
@ -91,7 +94,7 @@ public class ChefApiExpectTest extends BaseChefApiExpectTest<ChefApi> {
Set<String> nodes = api.listNodes();
assertTrue(nodes.isEmpty(), String.format("Expected nodes to be empty but was: %s", nodes));
}
public void testListRecipesInEnvironmentReturnsValidSet() {
ChefApi api = requestSendsResponse(
signed(getHttpRequestBuilder("GET", "/environments/dev/recipes").build()),
@ -156,6 +159,50 @@ public class ChefApiExpectTest extends BaseChefApiExpectTest<ChefApi> {
assertTrue(cookbooks.isEmpty(), String.format("Expected cookbooks to be empty but was: %s", cookbooks));
}
public void testSearchRolesReturnsValidResult() {
ChefApi api = requestSendsResponse(
signed(getHttpRequestBuilder("GET", "/search/role").build()),
HttpResponse.builder().statusCode(200)
.payload(payloadFromResourceWithContentType("/search_role.json", MediaType.APPLICATION_JSON)) //
.build());
SearchResult<? extends Role> result = api.searchRoles();
assertEquals(result.size(), 1);
assertEquals(result.iterator().next().getName(), "webserver");
}
public void testSearchRolesReturnsEmptyResult() {
ChefApi api = requestSendsResponse(
signed(getHttpRequestBuilder("GET", "/search/role").build()),
HttpResponse.builder().statusCode(200)
.payload(payloadFromResourceWithContentType("/search_role_empty.json", MediaType.APPLICATION_JSON)) //
.build());
SearchResult<? extends Role> result = api.searchRoles();
assertTrue(result.isEmpty(), String.format("Expected search result to be empty but was: %s", result));
}
public void testSearchRolesWithOptionsReturnsValidResult() {
ChefApi api = requestSendsResponse(
signed(getHttpRequestBuilder("GET", "/search/role").addQueryParam("q", "name:webserver").build()),
HttpResponse.builder().statusCode(200)
.payload(payloadFromResourceWithContentType("/search_role.json", MediaType.APPLICATION_JSON)) //
.build());
SearchOptions options = SearchOptions.Builder.query("name:webserver");
SearchResult<? extends Role> result = api.searchRoles(options);
assertEquals(result.size(), 1);
assertEquals(result.iterator().next().getName(), "webserver");
}
public void testSearchRolesWithOptionsReturnsEmptyResult() {
ChefApi api = requestSendsResponse(
signed(getHttpRequestBuilder("GET", "/search/role").addQueryParam("q", "name:dummy").build()),
HttpResponse.builder().statusCode(200)
.payload(payloadFromResourceWithContentType("/search_role_empty.json", MediaType.APPLICATION_JSON)) //
.build());
SearchOptions options = SearchOptions.Builder.query("name:dummy");
SearchResult<? extends Role> result = api.searchRoles(options);
assertTrue(result.isEmpty(), String.format("Expected search result to be empty but was: %s", result));
}
@Override
protected Module createModule() {
return new TestChefRestClientModule();
@ -173,5 +220,5 @@ public class ChefApiExpectTest extends BaseChefApiExpectTest<ChefApi> {
protected ChefApiMetadata createApiMetadata() {
return new ChefApiMetadata();
}
}

View File

@ -635,40 +635,6 @@ public class ChefApiTest extends BaseAsyncApiTest<ChefApi> {
}
public void testSearchRoles() throws SecurityException, NoSuchMethodException, IOException {
Invokable<?, ?> method = method(ChefApi.class, "searchRoles");
GeneratedHttpRequest httpRequest = processor.apply(Invocation.create(method, ImmutableList.of()));
assertRequestLineEquals(httpRequest, "GET http://localhost:4000/search/role HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\nX-Chef-Version: " + ChefApiMetadata.DEFAULT_API_VERSION
+ "-test\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseSearchRolesFromJson.class);
assertSaxResponseParserClassEquals(method, null);
assertFallbackClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
public void testSearchRolesWithOptions() throws SecurityException, NoSuchMethodException, IOException {
Invokable<?, ?> method = method(ChefApi.class, "searchRoles", SearchOptions.class);
GeneratedHttpRequest httpRequest = processor.apply(Invocation.create(method,
ImmutableList.<Object> of(SearchOptions.Builder.query("text"))));
assertRequestLineEquals(httpRequest, "GET http://localhost:4000/search/role?q=text HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\nX-Chef-Version: " + ChefApiMetadata.DEFAULT_API_VERSION
+ "-test\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseSearchRolesFromJson.class);
assertSaxResponseParserClassEquals(method, null);
assertFallbackClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
public void testSearchClients() throws SecurityException, NoSuchMethodException, IOException {
Invokable<?, ?> method = method(ChefApi.class, "searchClients");

View File

@ -0,0 +1,34 @@
{
"total": 1,
"start": 0,
"rows": [
{
"name": "webserver",
"description": "The base role for systems that serve HTTP traffic",
"json_class": "Chef::Role",
"default_attributes": {
"apache2": {
"listen_ports": [
"80",
"443"
]
}
},
"override_attributes": {
"apache2": {
"max_children": "50"
}
},
"chef_type": "role",
"run_list": [],
"env_run_lists": {
"prod": [
"recipe[apache2]"
],
"staging": [
"recipe[apache2::staging]"
]
}
}
]
}

View File

@ -0,0 +1,5 @@
{
"total": 0,
"start": 0,
"rows": []
}