fix issue #1040: tenant can be null

This commit is contained in:
Adrian Cole 2012-12-06 11:15:01 -08:00
parent 0b7e67e8e3
commit 6d6b53a5e0
4 changed files with 72 additions and 10 deletions

View File

@ -23,8 +23,11 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.beans.ConstructorProperties; import java.beans.ConstructorProperties;
import java.util.Date; import java.util.Date;
import org.jclouds.javax.annotation.Nullable;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper; 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 * 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<Token> {
protected String id; protected String id;
protected Date expires; protected Date expires;
protected Tenant tenant; protected Optional<Tenant> tenant = Optional.absent();
/** /**
* @see Token#getId() * @see Token#getId()
@ -76,7 +79,7 @@ public class Token implements Comparable<Token> {
* @see Token#getTenant() * @see Token#getTenant()
*/ */
public T tenant(Tenant tenant) { public T tenant(Tenant tenant) {
this.tenant = tenant; this.tenant = Optional.fromNullable(tenant);
return self(); return self();
} }
@ -88,7 +91,7 @@ public class Token implements Comparable<Token> {
return this return this
.id(in.getId()) .id(in.getId())
.expires(in.getExpires()) .expires(in.getExpires())
.tenant(in.getTenant()); .tenant(in.getTenant().orNull());
} }
} }
@ -101,15 +104,15 @@ public class Token implements Comparable<Token> {
private final String id; private final String id;
private final Date expires; private final Date expires;
private final Tenant tenant; private final Optional<Tenant> tenant;
@ConstructorProperties({ @ConstructorProperties({
"id", "expires", "tenant" "id", "expires", "tenant"
}) })
protected Token(String id, Date expires, Tenant tenant) { protected Token(String id, Date expires, @Nullable Optional<Tenant> tenant) {
this.id = checkNotNull(id, "id"); this.id = checkNotNull(id, "id");
this.expires = checkNotNull(expires, "expires"); this.expires = checkNotNull(expires, "expires");
this.tenant = checkNotNull(tenant, "tenant"); this.tenant = tenant == null ? Optional.<Tenant> absent() : tenant;
} }
/** /**
@ -131,7 +134,7 @@ public class Token implements Comparable<Token> {
/** /**
* @return the tenant assigned to the token * @return the tenant assigned to the token
*/ */
public Tenant getTenant() { public Optional<Tenant> getTenant() {
return this.tenant; return this.tenant;
} }
@ -151,8 +154,8 @@ public class Token implements Comparable<Token> {
} }
protected ToStringHelper string() { protected ToStringHelper string() {
return Objects.toStringHelper(this) return Objects.toStringHelper(this).omitNullValues()
.add("id", id).add("expires", expires).add("tenant", tenant); .add("id", id).add("expires", expires).add("tenant", tenant.orNull());
} }
@Override @Override

View File

@ -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<Access> {
@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();
}
}

View File

@ -0,0 +1 @@
{"access": {"token": {"expires": "2012-12-02T01:44:54Z", "id": "5afc3adea6654e758b4a9cf01bafe507"}, "serviceCatalog": [], "user": {"username": "nova", "roles_links": [], "id": "bf45fd7586c2410c980c651b918aa850", "roles": [], "name": "nova"}}}

View File

@ -105,7 +105,7 @@ public class HPCloudObjectStorageBlobRequestSigner implements BlobRequestSigner
@PostConstruct @PostConstruct
public void populateTenantId() { public void populateTenantId() {
// Defer call from constructor since access.get issues an RPC. // 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 @Override