From dda4a46e83716ec0958ef82898686b0d0e17a0bb Mon Sep 17 00:00:00 2001 From: Adam Lowe Date: Fri, 27 Apr 2012 16:41:49 +0100 Subject: [PATCH] Adding ApiMetadata domain object for Keystone 2.0 --- .../keystone/v2_0/domain/ApiMetadata.java | 170 ++++++++++++++++++ .../keystone/v2_0/domain/MediaType.java | 111 ++++++++++++ .../v2_0/parse/ParseApiMetadataTest.java | 70 ++++++++ .../test/resources/apiMetadataResponse.json | 1 + 4 files changed, 352 insertions(+) create mode 100644 common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/domain/ApiMetadata.java create mode 100644 common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/domain/MediaType.java create mode 100644 common/openstack/src/test/java/org/jclouds/openstack/keystone/v2_0/parse/ParseApiMetadataTest.java create mode 100644 common/openstack/src/test/resources/apiMetadataResponse.json diff --git a/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/domain/ApiMetadata.java b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/domain/ApiMetadata.java new file mode 100644 index 0000000000..61057d76da --- /dev/null +++ b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/domain/ApiMetadata.java @@ -0,0 +1,170 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds 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.openstack.keystone.v2_0.domain; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Collections; +import java.util.Date; +import java.util.Set; + +import org.jclouds.javax.annotation.Nullable; +import org.jclouds.openstack.domain.Link; +import org.jclouds.openstack.domain.Resource; + +import com.google.common.base.Objects; +import com.google.common.base.Objects.ToStringHelper; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; +import com.google.gson.annotations.SerializedName; + +/** + * Class ApiMetadata + */ +public class ApiMetadata extends Resource { + + public static Builder builder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder().fromApiMetadata(this); + } + + public static class Builder extends Resource.Builder { + private String status; + private Date updated; + private Set mediaTypes = Sets.newLinkedHashSet(); + + public Builder status(String status) { + this.status = status; + return this; + } + + public Builder updated(Date updated) { + this.updated = updated; + return this; + } + + public Builder mediaTypes(Set mediaTypes) { + this.mediaTypes = mediaTypes; + return this; + } + + public ApiMetadata build() { + return new ApiMetadata(id, name, links, updated, status, mediaTypes); + } + + public Builder fromApiMetadata(ApiMetadata in) { + return fromResource(in) + .status(in.getStatus()) + .updated(in.getUpdated()) + .mediaTypes(in.getMediaTypes()); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder id(String id) { + return Builder.class.cast(super.id(id)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder name(String name) { + return Builder.class.cast(super.name(name)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder links(Set links) { + return Builder.class.cast(super.links(links)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder fromResource(Resource in) { + return Builder.class.cast(super.fromResource(in)); + } + } + + private final String status; + private final Date updated; + @SerializedName("media-types") + private final Set mediaTypes; + + protected ApiMetadata(String id, String name, Set links, Date updated, String status, Set mediaTypes) { + super(id, name, links); + this.status = status; + this.updated = updated; + this.mediaTypes = ImmutableSet.copyOf(checkNotNull(mediaTypes, "mediaTypes")); + } + + /** + */ + @Nullable + public String getStatus() { + return this.status; + } + + /** + */ + @Nullable + public Date getUpdated() { + return this.updated; + } + + /** + */ + @Nullable + public Set getMediaTypes() { + return Collections.unmodifiableSet(this.mediaTypes); + } + + @Override + public int hashCode() { + return Objects.hashCode(status, updated, mediaTypes); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + ApiMetadata that = ApiMetadata.class.cast(obj); + return Objects.equal(this.status, that.status) + && Objects.equal(this.updated, that.updated) + && Objects.equal(this.mediaTypes, that.mediaTypes) + ; + } + + protected ToStringHelper string() { + return super.string() + .add("status", status) + .add("updated", updated) + .add("mediaTypes", mediaTypes); + } + +} \ No newline at end of file diff --git a/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/domain/MediaType.java b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/domain/MediaType.java new file mode 100644 index 0000000000..cb3cf6d071 --- /dev/null +++ b/common/openstack/src/main/java/org/jclouds/openstack/keystone/v2_0/domain/MediaType.java @@ -0,0 +1,111 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds 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.openstack.keystone.v2_0.domain; + +import org.jclouds.javax.annotation.Nullable; + +import com.google.common.base.Objects; +import com.google.common.base.Objects.ToStringHelper; + +/** + * Class MediaType + */ +public class MediaType { + + public static Builder builder() { + return new Builder(); + } + + public Builder toBuilder() { + return builder().fromMediaType(this); + } + + public static class Builder { + + private String base; + private String type; + + public Builder base(String base) { + this.base = base; + return this; + } + + public Builder type(String type) { + this.type = type; + return this; + } + + public MediaType build() { + return new MediaType(this); + } + + public Builder fromMediaType(MediaType in) { + return this.base(in.getBase()).type(in.getType()); + } + } + + private final String base; + private final String type; + + protected MediaType(Builder builder) { + this.base = builder.base; + this.type = builder.type; + } + + /** + */ + @Nullable + public String getBase() { + return this.base; + } + + /** + */ + @Nullable + public String getType() { + return this.type; + } + + @Override + public int hashCode() { + return Objects.hashCode(base, type); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + MediaType that = MediaType.class.cast(obj); + return Objects.equal(this.base, that.base) + && Objects.equal(this.type, that.type) + ; + } + + protected ToStringHelper string() { + return Objects.toStringHelper("") + .add("base", base) + .add("type", type); + } + + @Override + public String toString() { + return string().toString(); + } + +} \ No newline at end of file diff --git a/common/openstack/src/test/java/org/jclouds/openstack/keystone/v2_0/parse/ParseApiMetadataTest.java b/common/openstack/src/test/java/org/jclouds/openstack/keystone/v2_0/parse/ParseApiMetadataTest.java new file mode 100644 index 0000000000..83718ee799 --- /dev/null +++ b/common/openstack/src/test/java/org/jclouds/openstack/keystone/v2_0/parse/ParseApiMetadataTest.java @@ -0,0 +1,70 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds 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.openstack.keystone.v2_0.parse; + +import java.net.URI; + +import javax.ws.rs.Consumes; +import javax.ws.rs.core.MediaType; + +import org.jclouds.date.internal.SimpleDateFormatDateService; +import org.jclouds.json.BaseItemParserTest; +import org.jclouds.openstack.domain.Link; +import org.jclouds.openstack.keystone.v2_0.domain.Access; +import org.jclouds.openstack.keystone.v2_0.domain.ApiMetadata; +import org.jclouds.openstack.keystone.v2_0.domain.Endpoint; +import org.jclouds.openstack.keystone.v2_0.domain.Role; +import org.jclouds.openstack.keystone.v2_0.domain.Service; +import org.jclouds.openstack.keystone.v2_0.domain.Tenant; +import org.jclouds.openstack.keystone.v2_0.domain.Token; +import org.jclouds.openstack.keystone.v2_0.domain.User; +import org.jclouds.rest.annotations.SelectJson; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSet; + +/** + * @author Adam Lowe + */ +@Test(groups = "unit", testName = "ParseApiMetadataTest") +public class ParseApiMetadataTest extends BaseItemParserTest { + + @Override + public String resource() { + return "/apiMetadataResponse.json"; + } + + @Override + @SelectJson("version") + @Consumes(MediaType.APPLICATION_JSON) + public ApiMetadata expected() { + return ApiMetadata.builder().id("v2.0") + .links(ImmutableSet.of(Link.builder().relation(Link.Relation.SELF).href(URI.create("http://172.16.89.140:5000/v2.0/")).build(), + Link.builder().relation(Link.Relation.DESCRIBEDBY).type("text/html").href(URI.create("http://docs.openstack.org/api/openstack-identity-service/2.0/content/")).build(), + Link.builder().relation(Link.Relation.DESCRIBEDBY).type("application/pdf").href(URI.create("http://docs.openstack.org/api/openstack-identity-service/2.0/identity-dev-guide-2.0.pdf")).build() + )) + .status("beta") + .updated(new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-11-19T00:00:00Z")) + .mediaTypes(ImmutableSet.of( + org.jclouds.openstack.keystone.v2_0.domain.MediaType.builder().base("application/json").type("application/vnd.openstack.identity-v2.0+json").build(), + org.jclouds.openstack.keystone.v2_0.domain.MediaType.builder().base("application/xml").type("application/vnd.openstack.identity-v2.0+xml").build() + )) + .build(); + } +} diff --git a/common/openstack/src/test/resources/apiMetadataResponse.json b/common/openstack/src/test/resources/apiMetadataResponse.json new file mode 100644 index 0000000000..09934299c9 --- /dev/null +++ b/common/openstack/src/test/resources/apiMetadataResponse.json @@ -0,0 +1 @@ +{"version": {"status": "beta", "updated": "2011-11-19T00:00:00Z", "media-types": [{"base": "application/json", "type": "application/vnd.openstack.identity-v2.0+json"}, {"base": "application/xml", "type": "application/vnd.openstack.identity-v2.0+xml"}], "id": "v2.0", "links": [{"href": "http://172.16.89.140:5000/v2.0/", "rel": "self"}, {"href": "http://docs.openstack.org/api/openstack-identity-service/2.0/content/", "type": "text/html", "rel": "describedby"}, {"href": "http://docs.openstack.org/api/openstack-identity-service/2.0/identity-dev-guide-2.0.pdf", "type": "application/pdf", "rel": "describedby"}]}} \ No newline at end of file