JCLOUDS-285: Add name field in CookbookDefinition domain

This commit is contained in:
Noorul Islam K M 2013-09-17 23:03:50 +05:30 committed by Ignasi Barrera
parent e780bf2404
commit 65b64b0f28
8 changed files with 104 additions and 11 deletions

View File

@ -38,9 +38,15 @@ public class CookbookDefinition {
}
public static class Builder {
private String name;
private URI url;
private ImmutableSet.Builder<Version> versions = ImmutableSet.builder();
public Builder name(String name) {
this.name = checkNotNull(name, "name");
return this;
}
public Builder url(URI url) {
this.url = checkNotNull(url, "url");
return this;
@ -56,20 +62,33 @@ public class CookbookDefinition {
return this;
}
public Builder from(CookbookDefinition def) {
this.url = checkNotNull(def.getUrl(), "url");
this.versions.addAll(checkNotNull(def.getVersions(), "versions"));
this.name = def.getName();
return this;
}
public CookbookDefinition build() {
return new CookbookDefinition(url, versions.build());
return new CookbookDefinition(name, url, versions.build());
}
}
private final String name;
private final URI url;
private final Set<Version> versions;
@ConstructorProperties({ "url", "versions" })
protected CookbookDefinition(URI url, @Nullable Set<Version> versions) {
@ConstructorProperties({"name", "url", "versions" })
protected CookbookDefinition(String name, URI url, @Nullable Set<Version> versions) {
this.name = name;
this.url = url;
this.versions = copyOfOrEmpty(versions);
}
public String getName() {
return name;
}
public URI getUrl() {
return url;
}
@ -82,6 +101,7 @@ public class CookbookDefinition {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((url == null) ? 0 : url.hashCode());
result = prime * result + ((versions == null) ? 0 : versions.hashCode());
return result;
@ -96,6 +116,11 @@ public class CookbookDefinition {
if (getClass() != obj.getClass())
return false;
CookbookDefinition other = (CookbookDefinition) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (url == null) {
if (other.url != null)
return false;
@ -111,7 +136,7 @@ public class CookbookDefinition {
@Override
public String toString() {
return "CookbookDefinition [url=" + url + ", versions=" + versions + "]";
return "CookbookDefinition [name=" + name + ", url=" + url + ", versions=" + versions + "]";
}
public static class Version {

View File

@ -43,6 +43,12 @@ public class ParseCookbookDefinitionFromJsonv10 implements Function<HttpResponse
@Override
public CookbookDefinition apply(HttpResponse response) {
return parser.apply(response).values().iterator().next();
Map<String, CookbookDefinition> result = parser.apply(response);
String cookbookName = result.keySet().iterator().next();
CookbookDefinition def = result.values().iterator().next();
return CookbookDefinition.builder() //
.from(def) //
.name(cookbookName) //
.build();
}
}

View File

@ -17,7 +17,6 @@
package org.jclouds.chef.functions;
import com.google.common.base.Function;
import com.google.common.collect.Sets;
import org.jclouds.chef.domain.CookbookDefinition;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.ParseJson;
@ -27,6 +26,9 @@ import javax.inject.Singleton;
import java.util.Map;
import java.util.Set;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Sets.newLinkedHashSet;
/**
* Parses the cookbook versions in a Chef Server >= 0.10.8.
*
@ -47,6 +49,17 @@ public class ParseCookbookDefinitionListFromJsonv10 implements Function<HttpResp
@Override
public Set<CookbookDefinition> apply(HttpResponse response) {
return Sets.newLinkedHashSet(parser.apply(response).values());
Set<Map.Entry<String, CookbookDefinition>> result = parser.apply(response).entrySet();
return newLinkedHashSet(transform(result, new Function<Map.Entry<String, CookbookDefinition>, CookbookDefinition>() {
@Override
public CookbookDefinition apply(Map.Entry<String, CookbookDefinition> input) {
String cookbookName = input.getKey();
CookbookDefinition def = input.getValue();
return CookbookDefinition.builder() //
.from(def) //
.name(cookbookName) //
.build();
}
}));
}
}

View File

@ -24,6 +24,7 @@ import java.util.Set;
import javax.ws.rs.core.MediaType;
import org.jclouds.chef.config.ChefHttpApiModule;
import org.jclouds.chef.domain.CookbookDefinition;
import org.jclouds.date.TimeStamp;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
@ -156,6 +157,20 @@ public class ChefApiExpectTest extends BaseChefApiExpectTest<ChefApi> {
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()), //
HttpResponse.builder().statusCode(200)
.payload(payloadFromResourceWithContentType("/env_cookbooks.json", MediaType.APPLICATION_JSON)) //
.build());
Set<CookbookDefinition> cookbooks = api.listCookbooksInEnvironment("dev");
assertEquals(cookbooks.size(), 2);
}
@Override
protected Module createModule() {
return new TestChefRestClientModule();

View File

@ -58,7 +58,7 @@ public class ParseCookbookDefinitionFromJsonv10Test {
CookbookDefinition.Version v420 = CookbookDefinition.Version.builder()
.url(new URI("http://localhost:4000/cookbooks/apache2/4.2.0")).version("4.2.0").build();
CookbookDefinition definition = CookbookDefinition.builder()
.url(new URI("http://localhost:4000/cookbooks/apache2")).version(v510).version(v420).build();
.name("apache2").url(new URI("http://localhost:4000/cookbooks/apache2")).version(v510).version(v420).build();
assertEquals(handler.apply(HttpResponse
.builder()

View File

@ -59,14 +59,14 @@ public class ParseCookbookDefinitionListFromJsonv10Test {
CookbookDefinition.Version v420 = CookbookDefinition.Version.builder()
.url(new URI("http://localhost:4000/cookbooks/apache2/4.2.0")).version("4.2.0").build();
CookbookDefinition apache2 = CookbookDefinition.builder()
.url(new URI("http://localhost:4000/cookbooks/apache2")).version(v510).version(v420).build();
.name("apache2").url(new URI("http://localhost:4000/cookbooks/apache2")).version(v510).version(v420).build();
CookbookDefinition.Version v100 = CookbookDefinition.Version.builder()
.url(new URI("http://localhost:4000/cookbooks/nginx/1.0.0")).version("1.0.0").build();
CookbookDefinition.Version v130 = CookbookDefinition.Version.builder()
CookbookDefinition.Version v030 = CookbookDefinition.Version.builder()
.url(new URI("http://localhost:4000/cookbooks/nginx/0.3.0")).version("0.3.0").build();
CookbookDefinition nginx = CookbookDefinition.builder()
.url(new URI("http://localhost:4000/cookbooks/nginx")).version(v100).version(v130).build();
.name("nginx").url(new URI("http://localhost:4000/cookbooks/nginx")).version(v100).version(v030).build();
assertEquals(handler.apply(HttpResponse
.builder()

View File

@ -39,6 +39,7 @@ import java.util.Set;
import org.jclouds.chef.ChefApi;
import org.jclouds.chef.domain.ChecksumStatus;
import org.jclouds.chef.domain.Client;
import org.jclouds.chef.domain.CookbookDefinition;
import org.jclouds.chef.domain.CookbookVersion;
import org.jclouds.chef.domain.DatabagItem;
import org.jclouds.chef.domain.Environment;
@ -64,6 +65,9 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.io.Closeables;
import com.google.common.primitives.Bytes;
import static com.google.common.collect.Iterables.any;
import static com.google.common.collect.Iterables.all;
/**
* Tests behavior of {@code ChefApi}
*
@ -483,6 +487,16 @@ public abstract class BaseChefApiLiveTest<A extends ChefApi> extends BaseChefLiv
assertTrue(!nodeList.isEmpty());
}
@Test(dependsOnMethods = "testCreateNewCookbook")
public void testListCookbooksInEnvironment() throws Exception {
Set<CookbookDefinition> cookbooks = api.listCookbooksInEnvironment("_default");
assertTrue(any(cookbooks, new Predicate<CookbookDefinition>() {
@Override
public boolean apply(CookbookDefinition input) {
return PREFIX.equals(input.getName());
}}), String.format("Cookbook %s not in %s", PREFIX, cookbooks));
}
@AfterClass(groups = { "live", "integration" })
@Override
public void tearDown() {

View File

@ -0,0 +1,20 @@
{
"apache2" => {
"url" => "http://localhost:4000/cookbooks/apache2",
"versions" => [
{"url" => "http://localhost:4000/cookbooks/apache2/5.1.0",
"version" => "5.1.0"},
{"url" => "http://localhost:4000/cookbooks/apache2/4.2.0",
"version" => "4.2.0"}
]
},
"nginx" => {
"url" => "http://localhost:4000/cookbooks/nginx",
"versions" => [
{"url" => "http://localhost:4000/cookbooks/nginx/1.0.0",
"version" => "1.0.0"},
{"url" => "http://localhost:4000/cookbooks/nginx/0.3.0",
"version" => "0.3.0"}
]
}
}