Make listCookbooksInEnvironment return an empty set on 404

Added tests to validate above.
Also factored out some duplicate code in test.
This commit is contained in:
Noorul Islam K M 2013-09-19 17:16:30 +05:30 committed by Ignasi Barrera
parent ce1cba6f85
commit 0e4d32b98e
2 changed files with 45 additions and 59 deletions

View File

@ -192,7 +192,7 @@ public interface ChefApi extends Closeable {
*
* @param environmentname The name of the environment to get the cookbooks
* from.
* @return The definitions of the cookbooks (URL and versions) available in
* @return The definitions of the cookbooks (name, URL and versions) available in
* the given environment.
*/
@SinceApiVersion("0.10.0")
@ -200,17 +200,18 @@ public interface ChefApi extends Closeable {
@GET
@ResponseParser(ParseCookbookDefinitionListFromJsonv10.class)
@Path("/environments/{environmentname}/cookbooks")
@Fallback(NullOnNotFoundOr404.class)
@Fallback(EmptySetOnNotFoundOr404.class)
Set<CookbookDefinition> listCookbooksInEnvironment(@PathParam("environmentname") String environmentName);
/**
* Lists the cookbooks that are available in the given environment, limiting
* the number of versions returned for each cookbook.
*
* @param environmentname The name of the environment.
* @param environmentname The name of the environment to get the cookbooks
* from.
* @param numversions The number of cookbook versions to include in the
* response, where n is the number of cookbook versions.
* @return The definitions of the cookbooks (URL and versions) available in
* @return The definitions of the cookbooks (name, URL and versions) available in
* the given environment.
*/
@SinceApiVersion("0.10.0")
@ -218,7 +219,7 @@ public interface ChefApi extends Closeable {
@GET
@ResponseParser(ParseCookbookDefinitionListFromJsonv10.class)
@Path("/environments/{environmentname}/cookbooks?num_versions={numversions}")
@Fallback(NullOnNotFoundOr404.class)
@Fallback(EmptySetOnNotFoundOr404.class)
Set<CookbookDefinition> listCookbooksInEnvironment(@PathParam("environmentname") String environmentName,
@PathParam("numversions") String numVersions);

View File

@ -45,16 +45,20 @@ public class ChefApiExpectTest extends BaseChefApiExpectTest<ChefApi> {
provider = "chef";
}
private HttpRequest.Builder getHttpRequestBuilder(String method, String endPoint) {
return HttpRequest.builder() //
.method(method) //
.endpoint("http://localhost:4000" + endPoint) //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON);
}
public void testListClientsReturns2xx() {
ChefApi api = requestSendsResponse(
signed(HttpRequest.builder() //
.method("GET") //
.endpoint("http://localhost:4000/clients") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON).build()), //
HttpResponse.builder().statusCode(200)
.payload(payloadFromResourceWithContentType("/clients_list.json", MediaType.APPLICATION_JSON)) //
.build());
signed(getHttpRequestBuilder("GET", "/clients").build()),
HttpResponse.builder().statusCode(200) //
.payload(payloadFromResourceWithContentType("/clients_list.json", MediaType.APPLICATION_JSON)) //
.build());
Set<String> nodes = api.listClients();
assertEquals(nodes.size(), 3);
assertTrue(nodes.contains("adam"), String.format("Expected nodes to contain 'adam' but was: %s", nodes));
@ -62,11 +66,7 @@ public class ChefApiExpectTest extends BaseChefApiExpectTest<ChefApi> {
public void testListClientsReturns404() {
ChefApi api = requestSendsResponse(
signed(HttpRequest.builder() //
.method("GET") //
.endpoint("http://localhost:4000/clients") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON).build()), //
signed(getHttpRequestBuilder("GET", "/clients").build()),
HttpResponse.builder().statusCode(404)
.build());
Set<String> clients = api.listClients();
@ -75,11 +75,7 @@ public class ChefApiExpectTest extends BaseChefApiExpectTest<ChefApi> {
public void testListNodesReturns2xx() {
ChefApi api = requestSendsResponse(
signed(HttpRequest.builder() //
.method("GET") //
.endpoint("http://localhost:4000/nodes") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON).build()), //
signed(getHttpRequestBuilder("GET", "/nodes").build()),
HttpResponse.builder().statusCode(200)
.payload(payloadFromResourceWithContentType("/nodes_list.json", MediaType.APPLICATION_JSON)) //
.build());
@ -90,24 +86,15 @@ public class ChefApiExpectTest extends BaseChefApiExpectTest<ChefApi> {
public void testListNodesReturns404() {
ChefApi api = requestSendsResponse(
signed(HttpRequest.builder() //
.method("GET") //
.endpoint("http://localhost:4000/nodes") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON).build()), //
HttpResponse.builder().statusCode(404)
.build());
signed(getHttpRequestBuilder("GET", "/nodes").build()),
HttpResponse.builder().statusCode(404).build());
Set<String> nodes = api.listNodes();
assertTrue(nodes.isEmpty(), String.format("Expected nodes to be empty but was: %s", nodes));
}
public void testListRecipesInEnvironmentReturns2xx() {
ChefApi api = requestSendsResponse(
signed(HttpRequest.builder() //
.method("GET") //
.endpoint("http://localhost:4000/environments/dev/recipes") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON).build()), //
signed(getHttpRequestBuilder("GET", "/environments/dev/recipes").build()),
HttpResponse.builder().statusCode(200)
.payload(payloadFromResourceWithContentType("/environment_recipes.json", MediaType.APPLICATION_JSON)) //
.build());
@ -118,24 +105,15 @@ public class ChefApiExpectTest extends BaseChefApiExpectTest<ChefApi> {
public void testListRecipesInEnvironmentReturns404() {
ChefApi api = requestSendsResponse(
signed(HttpRequest.builder() //
.method("GET") //
.endpoint("http://localhost:4000/environments/dev/recipes") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON).build()), //
HttpResponse.builder().statusCode(404)
.build());
signed(getHttpRequestBuilder("GET", "/environments/dev/recipes").build()),
HttpResponse.builder().statusCode(404).build());
Set<String> recipes = api.listRecipesInEnvironment("dev");
assertTrue(recipes.isEmpty(), String.format("Expected recipes to be empty but was: %s", recipes));
}
public void testListNodesInEnvironmentReturns2xx() {
ChefApi api = requestSendsResponse(
signed(HttpRequest.builder() //
.method("GET") //
.endpoint("http://localhost:4000/environments/dev/nodes") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON).build()), //
signed(getHttpRequestBuilder("GET", "/environments/dev/nodes").build()),
HttpResponse.builder().statusCode(200)
.payload(payloadFromResourceWithContentType("/nodes_list.json", MediaType.APPLICATION_JSON)) //
.build());
@ -146,24 +124,15 @@ public class ChefApiExpectTest extends BaseChefApiExpectTest<ChefApi> {
public void testListNodesInEnvironmentReturns404() {
ChefApi api = requestSendsResponse(
signed(HttpRequest.builder() //
.method("GET") //
.endpoint("http://localhost:4000/environments/dev/nodes") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON).build()), //
HttpResponse.builder().statusCode(404)
.build());
signed(getHttpRequestBuilder("GET", "/environments/dev/nodes").build()),
HttpResponse.builder().statusCode(404).build());
Set<String> nodes = api.listNodesInEnvironment("dev");
assertTrue(nodes.isEmpty(), String.format("Expected nodes to be empty but was: %s", nodes));
}
public void testListCookbooksInEnvironmentReturnsValidSet() {
ChefApi api = requestSendsResponse(
signed(HttpRequest.builder() //
.method("GET") //
.endpoint("http://localhost:4000/environments/dev/cookbooks") //
.addHeader("X-Chef-Version", ChefApiMetadata.DEFAULT_API_VERSION) //
.addHeader("Accept", MediaType.APPLICATION_JSON).build()), //
signed(getHttpRequestBuilder("GET", "/environments/dev/cookbooks").build()),
HttpResponse.builder().statusCode(200)
.payload(payloadFromResourceWithContentType("/env_cookbooks.json", MediaType.APPLICATION_JSON)) //
.build());
@ -171,6 +140,22 @@ public class ChefApiExpectTest extends BaseChefApiExpectTest<ChefApi> {
assertEquals(cookbooks.size(), 2);
}
public void testListCookbooksInEnvironmentReturnsEmptySetOn404() {
ChefApi api = requestSendsResponse(
signed(getHttpRequestBuilder("GET", "/environments/dev/cookbooks").build()),
HttpResponse.builder().statusCode(404).build());
Set<CookbookDefinition> cookbooks = api.listCookbooksInEnvironment("dev");
assertTrue(cookbooks.isEmpty(), String.format("Expected cookbooks to be empty but was: %s", cookbooks));
}
public void testListCookbooksInEnvironmentWithNumVersionReturnsEmptySetOn404() {
ChefApi api = requestSendsResponse(
signed(getHttpRequestBuilder("GET", "/environments/dev/cookbooks").addQueryParam("num_versions", "2").build()),
HttpResponse.builder().statusCode(404).build());
Set<CookbookDefinition> cookbooks = api.listCookbooksInEnvironment("dev", "2");
assertTrue(cookbooks.isEmpty(), String.format("Expected cookbooks to be empty but was: %s", cookbooks));
}
@Override
protected Module createModule() {
return new TestChefRestClientModule();