From 773aa30c64108f5dc28a6fefb58d1ca4baa016ae Mon Sep 17 00:00:00 2001 From: Ignasi Barrera Date: Mon, 25 Jan 2016 15:39:06 +0100 Subject: [PATCH] JCLOUDS-1067: Fix cookbook grouping mapping in Chef metadata --- .../org/jclouds/chef/domain/Grouping.java | 103 ++++++++++++++++++ .../org/jclouds/chef/domain/Metadata.java | 12 +- .../ParseCookbookVersionFromJsonTest.java | 6 +- .../resources/apache-chef-demo-cookbook.json | 11 +- 4 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 apis/chef/src/main/java/org/jclouds/chef/domain/Grouping.java diff --git a/apis/chef/src/main/java/org/jclouds/chef/domain/Grouping.java b/apis/chef/src/main/java/org/jclouds/chef/domain/Grouping.java new file mode 100644 index 0000000000..c9c739e721 --- /dev/null +++ b/apis/chef/src/main/java/org/jclouds/chef/domain/Grouping.java @@ -0,0 +1,103 @@ +/* + * 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.domain; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.beans.ConstructorProperties; + +/** + * Information of a group of attributes in a namespace. + */ +public class Grouping { + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private String title; + private String description; + + public Builder title(String title) { + this.title = checkNotNull(title, "title"); + return this; + } + + public Builder description(String description) { + this.description = description; + return this; + } + + public Grouping build() { + return new Grouping(title, description); + } + } + + private final String title; + private final String description; + + @ConstructorProperties({ "title", "description" }) + protected Grouping(String title, String description) { + this.title = title; + this.description = description; + } + + public String getTitle() { + return title; + } + + public String getDescription() { + return description; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((description == null) ? 0 : description.hashCode()); + result = prime * result + ((title == null) ? 0 : title.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Grouping other = (Grouping) obj; + if (description == null) { + if (other.description != null) + return false; + } else if (!description.equals(other.description)) + return false; + if (title == null) { + if (other.title != null) + return false; + } else if (!title.equals(other.title)) + return false; + return true; + } + + @Override + public String toString() { + return "Grouping [title=" + title + ", description=" + description + "]"; + } + +} diff --git a/apis/chef/src/main/java/org/jclouds/chef/domain/Metadata.java b/apis/chef/src/main/java/org/jclouds/chef/domain/Metadata.java index 33f8dfb526..7da6c67e02 100644 --- a/apis/chef/src/main/java/org/jclouds/chef/domain/Metadata.java +++ b/apis/chef/src/main/java/org/jclouds/chef/domain/Metadata.java @@ -49,7 +49,7 @@ public class Metadata { private ImmutableMap.Builder recipes = ImmutableMap.builder(); private ImmutableMap.Builder replacing = ImmutableMap.builder(); private String name; - private ImmutableMap.Builder groupings = ImmutableMap.builder(); + private ImmutableMap.Builder groupings = ImmutableMap.builder(); private String longDescription; private ImmutableMap.Builder attributes = ImmutableMap.builder(); private ImmutableMap.Builder recommendations = ImmutableMap.builder(); @@ -154,12 +154,12 @@ public class Metadata { return this; } - public Builder grouping(String key, String value) { + public Builder grouping(String key, Grouping value) { this.groupings.put(checkNotNull(key, "key"), checkNotNull(value, "value")); return this; } - public Builder grouping(Map groupings) { + public Builder grouping(Map groupings) { this.groupings.putAll(checkNotNull(groupings, "groupings")); return this; } @@ -211,7 +211,7 @@ public class Metadata { private final Map recipes; private final Map replacing; private final String name; - private final Map groupings; + private final Map groupings; @SerializedName("long_description") private final String longDescription; private final Map attributes; @@ -224,7 +224,7 @@ public class Metadata { @Nullable Map dependencies, String maintainerEmail, @Nullable Map conflicting, String description, @Nullable Map providing, @Nullable Map platforms, String version, @Nullable Map recipes, @Nullable Map replacing, String name, - @Nullable Map groupings, String longDescription, @Nullable Map attributes, + @Nullable Map groupings, String longDescription, @Nullable Map attributes, @Nullable Map recommendations) { this.license = license; this.maintainer = maintainer; @@ -297,7 +297,7 @@ public class Metadata { return name; } - public Map getGroupings() { + public Map getGroupings() { return groupings; } diff --git a/apis/chef/src/test/java/org/jclouds/chef/functions/ParseCookbookVersionFromJsonTest.java b/apis/chef/src/test/java/org/jclouds/chef/functions/ParseCookbookVersionFromJsonTest.java index 7bc19bfb01..598fbe8a4d 100644 --- a/apis/chef/src/test/java/org/jclouds/chef/functions/ParseCookbookVersionFromJsonTest.java +++ b/apis/chef/src/test/java/org/jclouds/chef/functions/ParseCookbookVersionFromJsonTest.java @@ -25,6 +25,7 @@ import java.net.URI; import org.jclouds.chef.ChefApiMetadata; import org.jclouds.chef.config.ChefParserModule; import org.jclouds.chef.domain.CookbookVersion; +import org.jclouds.chef.domain.Grouping; import org.jclouds.chef.domain.Metadata; import org.jclouds.chef.domain.Resource; import org.jclouds.http.HttpResponse; @@ -101,8 +102,11 @@ public class ParseCookbookVersionFromJsonTest { .maintainer("Your Name") // .maintainerEmail("youremail@example.com") // .description("A fabulous new cookbook") // - .version("0.0.0").name("apache-chef-demo") // + .version("0.0.0") // + .name("apache-chef-demo") // .longDescription("") // + .grouping("one", Grouping.builder().title("One title").description("One description").build()) // + .grouping("two", Grouping.builder().title("Two title").description("Two description").build()) // .build()) .rootFile( Resource diff --git a/apis/chef/src/test/resources/apache-chef-demo-cookbook.json b/apis/chef/src/test/resources/apache-chef-demo-cookbook.json index 228a3c06d4..8df911f18d 100644 --- a/apis/chef/src/test/resources/apache-chef-demo-cookbook.json +++ b/apis/chef/src/test/resources/apache-chef-demo-cookbook.json @@ -17,7 +17,16 @@ "long_description": "", "recommendations": {}, "version": "0.0.0", - "groupings": {}, + "groupings": { + "one": { + "title": "One title", + "description": "One description" + }, + "two": { + "title": "Two title", + "description": "Two description" + } + }, "recipes": {}, "conflicting": {}, "description": "A fabulous new cookbook",