From a1f284925278d5f8582a4ca46ab2b4ec303f32de Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Thu, 6 Dec 2012 11:15:01 -0800 Subject: [PATCH] fix issue #1040: tenant can be null --- .../openstack/keystone/v2_0/domain/Token.java | 21 ++++--- .../v2_0/parse/ParseMinimalAccessTest.java | 58 +++++++++++++++++++ .../src/test/resources/access_minimal.json | 1 + ...HPCloudObjectStorageBlobRequestSigner.java | 2 +- 4 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 apis/openstack-keystone/src/test/java/org/jclouds/openstack/keystone/v2_0/parse/ParseMinimalAccessTest.java create mode 100644 apis/openstack-keystone/src/test/resources/access_minimal.json diff --git a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v2_0/domain/Token.java b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v2_0/domain/Token.java index 233426e079..55d702abaa 100644 --- a/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v2_0/domain/Token.java +++ b/apis/openstack-keystone/src/main/java/org/jclouds/openstack/keystone/v2_0/domain/Token.java @@ -23,8 +23,11 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.beans.ConstructorProperties; import java.util.Date; +import org.jclouds.javax.annotation.Nullable; + import com.google.common.base.Objects; import com.google.common.base.Objects.ToStringHelper; +import com.google.common.base.Optional; /** * A token is an arbitrary bit of text that is used to access resources. Each token has a scope @@ -54,7 +57,7 @@ public class Token implements Comparable { protected String id; protected Date expires; - protected Tenant tenant; + protected Optional tenant = Optional.absent(); /** * @see Token#getId() @@ -76,7 +79,7 @@ public class Token implements Comparable { * @see Token#getTenant() */ public T tenant(Tenant tenant) { - this.tenant = tenant; + this.tenant = Optional.fromNullable(tenant); return self(); } @@ -88,7 +91,7 @@ public class Token implements Comparable { return this .id(in.getId()) .expires(in.getExpires()) - .tenant(in.getTenant()); + .tenant(in.getTenant().orNull()); } } @@ -101,15 +104,15 @@ public class Token implements Comparable { private final String id; private final Date expires; - private final Tenant tenant; + private final Optional tenant; @ConstructorProperties({ "id", "expires", "tenant" }) - protected Token(String id, Date expires, Tenant tenant) { + protected Token(String id, Date expires, @Nullable Optional tenant) { this.id = checkNotNull(id, "id"); this.expires = checkNotNull(expires, "expires"); - this.tenant = checkNotNull(tenant, "tenant"); + this.tenant = tenant == null ? Optional. absent() : tenant; } /** @@ -131,7 +134,7 @@ public class Token implements Comparable { /** * @return the tenant assigned to the token */ - public Tenant getTenant() { + public Optional getTenant() { return this.tenant; } @@ -151,8 +154,8 @@ public class Token implements Comparable { } protected ToStringHelper string() { - return Objects.toStringHelper(this) - .add("id", id).add("expires", expires).add("tenant", tenant); + return Objects.toStringHelper(this).omitNullValues() + .add("id", id).add("expires", expires).add("tenant", tenant.orNull()); } @Override diff --git a/apis/openstack-keystone/src/test/java/org/jclouds/openstack/keystone/v2_0/parse/ParseMinimalAccessTest.java b/apis/openstack-keystone/src/test/java/org/jclouds/openstack/keystone/v2_0/parse/ParseMinimalAccessTest.java new file mode 100644 index 0000000000..2352a08015 --- /dev/null +++ b/apis/openstack-keystone/src/test/java/org/jclouds/openstack/keystone/v2_0/parse/ParseMinimalAccessTest.java @@ -0,0 +1,58 @@ +/** + * 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 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.keystone.v2_0.domain.Access; +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; + +/** + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "ParseMinimalAccessTest") +public class ParseMinimalAccessTest extends BaseItemParserTest { + + @Override + public String resource() { + return "/access_minimal.json"; + } + + @Override + @SelectJson("access") + @Consumes(MediaType.APPLICATION_JSON) + public Access expected() { + return Access.builder() + .token(Token.builder() + .expires(new SimpleDateFormatDateService().iso8601SecondsDateParse("2012-12-02T01:44:54Z")) + .id("5afc3adea6654e758b4a9cf01bafe507").build()) + .user(User.builder() + .id("bf45fd7586c2410c980c651b918aa850") + .name("nova") +// .username("nova") TODO: add optional username field! + .build()).build(); + } + +} diff --git a/apis/openstack-keystone/src/test/resources/access_minimal.json b/apis/openstack-keystone/src/test/resources/access_minimal.json new file mode 100644 index 0000000000..3a168b759d --- /dev/null +++ b/apis/openstack-keystone/src/test/resources/access_minimal.json @@ -0,0 +1 @@ +{"access": {"token": {"expires": "2012-12-02T01:44:54Z", "id": "5afc3adea6654e758b4a9cf01bafe507"}, "serviceCatalog": [], "user": {"username": "nova", "roles_links": [], "id": "bf45fd7586c2410c980c651b918aa850", "roles": [], "name": "nova"}}} \ No newline at end of file diff --git a/providers/hpcloud-objectstorage/src/main/java/org/jclouds/hpcloud/objectstorage/blobstore/HPCloudObjectStorageBlobRequestSigner.java b/providers/hpcloud-objectstorage/src/main/java/org/jclouds/hpcloud/objectstorage/blobstore/HPCloudObjectStorageBlobRequestSigner.java index a321211caa..df7396fdd7 100644 --- a/providers/hpcloud-objectstorage/src/main/java/org/jclouds/hpcloud/objectstorage/blobstore/HPCloudObjectStorageBlobRequestSigner.java +++ b/providers/hpcloud-objectstorage/src/main/java/org/jclouds/hpcloud/objectstorage/blobstore/HPCloudObjectStorageBlobRequestSigner.java @@ -106,7 +106,7 @@ public class HPCloudObjectStorageBlobRequestSigner implements BlobRequestSigner @PostConstruct public void populateTenantId() { // Defer call from constructor since access.get issues an RPC. - this.tenantId = access.get().getToken().getTenant().getId(); + this.tenantId = access.get().getToken().getTenant().get().getId(); } @Override