fixed parsing on smartcloud

This commit is contained in:
Adrian Cole 2011-05-14 01:25:52 -07:00
parent 5b8ee87a4d
commit 1435f9b00b
11 changed files with 175 additions and 7 deletions

View File

@ -0,0 +1,51 @@
/**
*
* Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.http.functions;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.http.HttpResponse;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
/**
* @author Adrian Cole
*/
@Singleton
public class UnwrapOnlyJsonValueInSet<T> implements Function<HttpResponse, T> {
private final UnwrapOnlyJsonValue<Set<T>> json;
@Inject
UnwrapOnlyJsonValueInSet(UnwrapOnlyJsonValue<Set<T>> json) {
this.json = json;
}
@Override
public T apply(HttpResponse arg0) {
Set<T> set = json.apply(arg0);
if (set == null || set.size() == 0)
return null;
return Iterables.getOnlyElement(set);
}
}

View File

@ -82,6 +82,7 @@ import org.jclouds.http.functions.ReturnInputStream;
import org.jclouds.http.functions.ReturnStringIf2xx;
import org.jclouds.http.functions.ReturnTrueIf2xx;
import org.jclouds.http.functions.UnwrapOnlyJsonValue;
import org.jclouds.http.functions.UnwrapOnlyJsonValueInSet;
import org.jclouds.http.functions.UnwrapOnlyNestedJsonValue;
import org.jclouds.http.functions.UnwrapOnlyNestedJsonValueInSet;
import org.jclouds.http.functions.ParseSax.HandlerWithResult;
@ -787,6 +788,8 @@ public class RestAnnotationProcessor<T> {
parserType = Types.newParameterizedType(UnwrapOnlyJsonValue.class, returnVal);
else if (depth == 2 && edgeCollection == Map.class)
parserType = Types.newParameterizedType(UnwrapOnlyNestedJsonValue.class, returnVal);
else if (depth == 2 && edgeCollection == Set.class)
parserType = Types.newParameterizedType(UnwrapOnlyJsonValueInSet.class, returnVal);
else if (depth == 3 && edgeCollection == Set.class)
parserType = Types.newParameterizedType(UnwrapOnlyNestedJsonValueInSet.class, returnVal);
else

View File

@ -97,6 +97,7 @@ import org.jclouds.http.functions.ReturnInputStream;
import org.jclouds.http.functions.ReturnStringIf2xx;
import org.jclouds.http.functions.ReturnTrueIf2xx;
import org.jclouds.http.functions.UnwrapOnlyJsonValue;
import org.jclouds.http.functions.UnwrapOnlyJsonValueInSet;
import org.jclouds.http.functions.UnwrapOnlyNestedJsonValue;
import org.jclouds.http.functions.UnwrapOnlyNestedJsonValueInSet;
import org.jclouds.http.internal.PayloadEnclosingImpl;
@ -833,7 +834,13 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@Unwrap(depth = 2)
@Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<Long> testUnwrapDepth2Long();
@GET
@Path("/")
@Unwrap(depth = 2, edgeCollection = Set.class)
@Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<String> testUnwrapDepth2Set();
@GET
@Path("/")
@Unwrap(depth = 3, edgeCollection = Set.class)
@ -1033,6 +1040,23 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
.<String> of());
}
@SuppressWarnings("unchecked")
public void testUnwrapDepth2Set() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestPut.class.getMethod("testUnwrapDepth2Set");
HttpRequest request = factory(TestPut.class).createRequest(method);
assertResponseParserClassEquals(method, request, UnwrapOnlyJsonValueInSet.class);
// now test that it works!
Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request);
assertEquals(parser.apply(new HttpResponse(200, "ok",
newStringPayload("{\"runit\":[\"0.7.0\"]}"))), "0.7.0");
assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{\"runit\":[]}"))), null);
}
@SuppressWarnings("unchecked")
public void testUnwrapDepth2Long() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestPut.class.getMethod("testUnwrapDepth2Long");

View File

@ -181,6 +181,7 @@ public interface IBMSmartCloudAsyncClient {
@POST
@Path(IBMSmartCloudAsyncClient.VERSION + "/instances")
@Consumes(MediaType.APPLICATION_JSON)
@Unwrap(depth = 2, edgeCollection = Set.class)
ListenableFuture<Instance> createInstanceInLocation(@FormParam("location") String location,
@FormParam("name") String name, @FormParam("imageID") String imageID,
@FormParam("instanceType") String instanceType, CreateInstanceOptions... options);

View File

@ -21,6 +21,7 @@ package org.jclouds.ibm.smartcloud;
import static org.jclouds.Constants.PROPERTY_API_VERSION;
import static org.jclouds.Constants.PROPERTY_ENDPOINT;
import static org.jclouds.Constants.PROPERTY_ISO3166_CODES;
import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL;
import static org.jclouds.compute.reference.ComputeServiceConstants.PROPERTY_TIMEOUT_NODE_RUNNING;
import static org.jclouds.location.reference.LocationConstants.ISO3166_CODES;
import static org.jclouds.location.reference.LocationConstants.PROPERTY_ZONE;
@ -50,6 +51,7 @@ public class IBMSmartCloudPropertiesBuilder extends PropertiesBuilder {
properties.setProperty(PROPERTY_API_VERSION, IBMSmartCloudAsyncClient.VERSION);
properties.setProperty(PROPERTY_ENDPOINT, "https://www-147.ibm.com/computecloud/enterprise/api/rest");
properties.setProperty(PROPERTY_TIMEOUT_NODE_RUNNING, (15 * 60 * 1000) + "");
properties.setProperty(PROPERTY_SESSION_INTERVAL, 60 * 5 + "");
return properties;
}

View File

@ -82,7 +82,7 @@ public class Address implements Comparable<Address> {
}
public String getIP() {
return "".equals(ip.trim()) ? null : ip;
return "".equals(ip.trim()) ? null : ip.trim();
}
public String getId() {

View File

@ -74,11 +74,11 @@ public class IP {
// TODO custom parser to do this once
public String getHostname() {
return "".equals(hostname.trim()) ? null : hostname;
return "".equals(hostname.trim()) ? null : hostname.trim();
}
public String getIP() {
return "".equals(ip.trim()) ? null : ip;
return "".equals(ip.trim()) ? null : ip.trim();
}
public int getType() {

View File

@ -31,6 +31,7 @@ import org.jclouds.http.functions.ParseJson;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.http.functions.ReleasePayloadAndReturn;
import org.jclouds.http.functions.UnwrapOnlyJsonValue;
import org.jclouds.http.functions.UnwrapOnlyJsonValueInSet;
import org.jclouds.ibm.smartcloud.domain.Image;
import org.jclouds.ibm.smartcloud.options.CreateInstanceOptions;
import org.jclouds.ibm.smartcloud.options.RestartInstanceOptions;
@ -470,7 +471,7 @@ public class IBMSmartCloudAsyncClientTest extends RestClientTest<IBMSmartCloudAs
assertPayloadEquals(httpRequest, "location=1&imageID=22&name=name&instanceType=instanceType",
"application/x-www-form-urlencoded", false);
assertResponseParserClassEquals(method, httpRequest, ParseJson.class);
assertResponseParserClassEquals(method, httpRequest, UnwrapOnlyJsonValueInSet.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
@ -495,7 +496,7 @@ public class IBMSmartCloudAsyncClientTest extends RestClientTest<IBMSmartCloudAs
"location=location&imageID=22&name=name&instanceType=instanceType&ip=1&publicKey=MOO&volumeID=2&oss.storage.id.0.mnt=%2Fmnt&insight_admin_password=myPassword1&db2_admin_password=myPassword2&report_user_password=myPassword3",
"application/x-www-form-urlencoded", false);
assertResponseParserClassEquals(method, httpRequest, ParseJson.class);
assertResponseParserClassEquals(method, httpRequest, UnwrapOnlyJsonValueInSet.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);

View File

@ -38,7 +38,7 @@ import com.google.common.collect.Iterables;
*
* @author Adrian Cole
*/
@Test(groups = "live", testName = "IBMSmartCloudClientLiveTest")
@Test(groups = "live", testName = "ReadOnlyIBMSmartCloudClientLiveTest")
public class ReadOnlyIBMSmartCloudClientLiveTest extends BaseIBMSmartCloudClientLiveTest {
@Test

View File

@ -0,0 +1,62 @@
/**
*
* Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.ibm.smartcloud.parse;
import java.util.Date;
import java.util.Set;
import org.jclouds.ibm.smartcloud.config.IBMSmartCloudParserModule;
import org.jclouds.ibm.smartcloud.domain.Instance;
import org.jclouds.ibm.smartcloud.domain.Instance.Software;
import org.jclouds.ibm.smartcloud.domain.Instance.Status;
import org.jclouds.json.BaseItemParserTest;
import org.jclouds.json.config.GsonModule;
import org.jclouds.rest.annotations.Unwrap;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "GetInstanceTest")
public class NewInstanceTest extends BaseItemParserTest<Instance> {
protected Injector injector() {
return Guice.createInjector(new GsonModule(), new IBMSmartCloudParserModule());
}
@Override
public String resource() {
return "/new_instance.json";
}
@Override
@Unwrap(depth = 2, edgeCollection = Set.class)
public Instance expected() {
return Instance.builder().launchTime(new Date(1305359078796l)).software(
ImmutableSet.of(new Software("SUSE Linux Enterprise Server", "OS", "11 SP1"))).requestId("80904")
.keyName("adriancole").name("adriancole").instanceType("COP32.1/2048/60").status(Status.NEW).owner(
"adrian@cloudconscious.com").location("101").imageId("20015393").requestName("adriancole").id(
"80604").expirationTime(new Date(1368431087355l)).build();
}
}

View File

@ -0,0 +1,24 @@
{
"instances": [{
"launchTime": 1305359078796,
"software": [{
"version": "11 SP1",
"type": "OS",
"name": "SUSE Linux Enterprise Server"
}],
"requestId": "80904",
"keyName": "adriancole",
"name": "adriancole",
"instanceType": "COP32.1/2048/60",
"status": 0,
"owner": "adrian@cloudconscious.com",
"location": "101",
"imageId": "20015393",
"productCodes": [],
"volumes": [],
"requestName": "adriancole",
"id": "80604",
"secondaryIP": [],
"expirationTime": 1368431087355
}]
}