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 faa189c212..9dd1df4ea4 100644 --- a/apis/chef/src/main/java/org/jclouds/chef/ChefApi.java +++ b/apis/chef/src/main/java/org/jclouds/chef/ChefApi.java @@ -923,4 +923,19 @@ public interface ChefApi extends Closeable { @Path("/environments/{environmentname}/cookbooks/{cookbookname}?num_versions={numversions}") CookbookDefinition getEnvironmentCookbook(@PathParam("environmentname") String environmentname, @PathParam("cookbookname") String cookbookname, @PathParam("numversions") String numversions); + + /** + * @return List of environment recipes. + * @throws AuthorizationException + *

+ * "401 Unauthorized" if you are not a recognized user. + *

+ * "403 Forbidden" if you do not have rights to list environment recipes. + */ + @SinceApiVersion("0.10.0") + @Named("environment:recipelist") + @GET + @Path("/environments/{environmentname}/recipes") + @Fallback(EmptySetOnNotFoundOr404.class) + Set listEnvironmentRecipes(@PathParam("environmentname") String environmentname); } diff --git a/apis/chef/src/main/java/org/jclouds/chef/test/TransientChefApi.java b/apis/chef/src/main/java/org/jclouds/chef/test/TransientChefApi.java index 908ae2fcd5..90919b713d 100644 --- a/apis/chef/src/main/java/org/jclouds/chef/test/TransientChefApi.java +++ b/apis/chef/src/main/java/org/jclouds/chef/test/TransientChefApi.java @@ -372,6 +372,11 @@ public class TransientChefApi implements ChefApi { throw new UnsupportedOperationException(); } + @Override + public Set listEnvironmentRecipes(String environmentname) { + throw new UnsupportedOperationException(); + } + @Override public void close() throws IOException { closer.close(); diff --git a/apis/chef/src/test/java/org/jclouds/chef/ChefApiExpectTest.java b/apis/chef/src/test/java/org/jclouds/chef/ChefApiExpectTest.java new file mode 100644 index 0000000000..fd34e2cdea --- /dev/null +++ b/apis/chef/src/test/java/org/jclouds/chef/ChefApiExpectTest.java @@ -0,0 +1,95 @@ +/* + * 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 org.jclouds.chef; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +import java.util.Set; + +import javax.ws.rs.core.MediaType; + +import org.jclouds.chef.BaseChefApiExpectTest; +import org.jclouds.chef.ChefApi; +import org.jclouds.date.TimeStamp; +import org.jclouds.chef.config.ChefHttpApiModule; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.jclouds.rest.ConfiguresRestClient; +import org.testng.annotations.Test; + +import com.google.common.base.Supplier; +import com.google.inject.Module; + +/** + * Expect tests for the {@link ChefApi} class. + * + * @author Noorul Islam K M + */ +@Test(groups = "unit", testName = "ChefApiExpectTest") +public class ChefApiExpectTest extends BaseChefApiExpectTest { + public ChefApiExpectTest() { + provider = "chef"; + } + + public void testListEnvironmentRecipesReturns2xx() { + ChefApi api = requestSendsResponse( + signed(HttpRequest.builder() // + .method("GET") // + .endpoint("http://localhost:4000/environments/dev/recipes") // + .addHeader("X-Chef-Version", ChefApi.VERSION) // + .addHeader("Accept", MediaType.APPLICATION_JSON).build()), // + HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/environment_recipes.json", MediaType.APPLICATION_JSON)) // + .build()); + Set recipes = api.listEnvironmentRecipes("dev"); + assertEquals(recipes.size(), 3); + assertTrue(recipes.contains("apache2")); + } + + public void testListEnvironmentRecipesReturns404() { + ChefApi api = requestSendsResponse( + signed(HttpRequest.builder() // + .method("GET") // + .endpoint("http://localhost:4000/environments/dev/recipes") // + .addHeader("X-Chef-Version", ChefApi.VERSION) // + .addHeader("Accept", MediaType.APPLICATION_JSON).build()), // + HttpResponse.builder().statusCode(404) + .build()); + + assertTrue(api.listEnvironmentRecipes("dev").isEmpty()); + } + + @Override + protected Module createModule() { + return new TestChefRestClientModule(); + } + + @ConfiguresRestClient + static class TestChefRestClientModule extends ChefHttpApiModule { + @Override + protected String provideTimeStamp(@TimeStamp Supplier cache) { + return "timestamp"; + } + } + + @Override + protected ChefApiMetadata createApiMetadata() { + return new ChefApiMetadata(); + } + +} diff --git a/apis/chef/src/test/java/org/jclouds/chef/internal/BaseChefApiLiveTest.java b/apis/chef/src/test/java/org/jclouds/chef/internal/BaseChefApiLiveTest.java index 529df6843b..72563231db 100644 --- a/apis/chef/src/test/java/org/jclouds/chef/internal/BaseChefApiLiveTest.java +++ b/apis/chef/src/test/java/org/jclouds/chef/internal/BaseChefApiLiveTest.java @@ -464,6 +464,12 @@ public abstract class BaseChefApiLiveTest extends BaseChefLiv assertTrue(waitForIndex.apply(options)); } + @Test(dependsOnMethods = "testCreateEnvironment") + public void testListEnvironmentRecipes() { + Set recipeList = api.listEnvironmentRecipes(PREFIX); + assertTrue(!recipeList.isEmpty()); + } + @AfterClass(groups = { "live", "integration" }) @Override public void tearDown() { diff --git a/apis/chef/src/test/resources/environment_recipes.json b/apis/chef/src/test/resources/environment_recipes.json new file mode 100644 index 0000000000..cca3a117b7 --- /dev/null +++ b/apis/chef/src/test/resources/environment_recipes.json @@ -0,0 +1,6 @@ +[ + "ant", + "apache2", + "apache2::mod_auth_openid" +] +