diff --git a/apis/chef/src/main/java/org/jclouds/chef/ChefApi.java b/apis/chef/src/main/java/org/jclouds/chef/ChefApi.java index b8b451380a..560102259c 100644 --- a/apis/chef/src/main/java/org/jclouds/chef/ChefApi.java +++ b/apis/chef/src/main/java/org/jclouds/chef/ChefApi.java @@ -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 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 listCookbooksInEnvironment(@PathParam("environmentname") String environmentName, @PathParam("numversions") String numVersions); diff --git a/apis/chef/src/test/java/org/jclouds/chef/ChefApiExpectTest.java b/apis/chef/src/test/java/org/jclouds/chef/ChefApiExpectTest.java index 65344efb4e..634c5da009 100644 --- a/apis/chef/src/test/java/org/jclouds/chef/ChefApiExpectTest.java +++ b/apis/chef/src/test/java/org/jclouds/chef/ChefApiExpectTest.java @@ -45,16 +45,20 @@ public class ChefApiExpectTest extends BaseChefApiExpectTest { 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 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 { 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 clients = api.listClients(); @@ -75,11 +75,7 @@ public class ChefApiExpectTest extends BaseChefApiExpectTest { 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 { 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 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 { 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 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 { 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 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 { assertEquals(cookbooks.size(), 2); } + public void testListCookbooksInEnvironmentReturnsEmptySetOn404() { + ChefApi api = requestSendsResponse( + signed(getHttpRequestBuilder("GET", "/environments/dev/cookbooks").build()), + HttpResponse.builder().statusCode(404).build()); + Set 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 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();