From ee08f059ba67e8849e7306bd086c406544a8c6f3 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 7 Mar 2012 13:11:31 -0800 Subject: [PATCH] modernized some gogrid tests and corrected iso information for datacenter 3 --- .../gogrid/GoGridPropertiesBuilder.java | 4 +- .../functions/GenericResponseContainer.java | 2 +- .../functions/ParseErrorFromJsonResponse.java | 57 --------- .../ParseServerFromJsonResponse.java | 52 -------- .../ParseServerListFromJsonResponse.java | 53 -------- .../gogrid/handlers/GoGridErrorHandler.java | 40 +++--- .../services/GridServerAsyncClient.java | 32 +++-- .../GoGridTemplateBuilderLiveTest.java | 2 +- .../ParseErrorFromJsonResponseTest.java | 59 --------- .../ParseServersFromJsonResponseTest.java | 104 --------------- .../gogrid/parse/ParseServerListTest.java | 79 ++++++++++++ .../jclouds/gogrid/parse/ParseServerTest.java | 76 +++++++++++ .../BaseGoGridRestClientExpectTest.java | 37 ++++++ .../services/GridServerAsyncClientTest.java | 89 +------------ .../services/GridServerClientExpectTest.java | 118 ++++++++++++++++++ 15 files changed, 360 insertions(+), 444 deletions(-) delete mode 100644 providers/gogrid/src/main/java/org/jclouds/gogrid/functions/ParseErrorFromJsonResponse.java delete mode 100644 providers/gogrid/src/main/java/org/jclouds/gogrid/functions/ParseServerFromJsonResponse.java delete mode 100644 providers/gogrid/src/main/java/org/jclouds/gogrid/functions/ParseServerListFromJsonResponse.java delete mode 100644 providers/gogrid/src/test/java/org/jclouds/gogrid/functions/ParseErrorFromJsonResponseTest.java delete mode 100644 providers/gogrid/src/test/java/org/jclouds/gogrid/functions/ParseServersFromJsonResponseTest.java create mode 100644 providers/gogrid/src/test/java/org/jclouds/gogrid/parse/ParseServerListTest.java create mode 100644 providers/gogrid/src/test/java/org/jclouds/gogrid/parse/ParseServerTest.java create mode 100644 providers/gogrid/src/test/java/org/jclouds/gogrid/services/BaseGoGridRestClientExpectTest.java create mode 100644 providers/gogrid/src/test/java/org/jclouds/gogrid/services/GridServerClientExpectTest.java diff --git a/providers/gogrid/src/main/java/org/jclouds/gogrid/GoGridPropertiesBuilder.java b/providers/gogrid/src/main/java/org/jclouds/gogrid/GoGridPropertiesBuilder.java index 97cccfdfcd..6bb3362ce6 100644 --- a/providers/gogrid/src/main/java/org/jclouds/gogrid/GoGridPropertiesBuilder.java +++ b/providers/gogrid/src/main/java/org/jclouds/gogrid/GoGridPropertiesBuilder.java @@ -42,10 +42,10 @@ public class GoGridPropertiesBuilder extends PropertiesBuilder { protected Properties defaultProperties() { Properties properties = super.defaultProperties(); properties.setProperty(PROPERTY_ZONES, "1,2,3"); - properties.setProperty(PROPERTY_ISO3166_CODES, "US-CA,US-VA,BR-SP"); + properties.setProperty(PROPERTY_ISO3166_CODES, "US-CA,US-VA,NL-NH"); properties.setProperty(PROPERTY_ZONE + ".1." + ISO3166_CODES, "US-CA"); properties.setProperty(PROPERTY_ZONE + ".2." + ISO3166_CODES, "US-VA"); - properties.setProperty(PROPERTY_ZONE + ".3." + ISO3166_CODES, "BR-SP"); + properties.setProperty(PROPERTY_ZONE + ".3." + ISO3166_CODES, "NL-NH"); properties.setProperty(PROPERTY_API_VERSION, GoGridAsyncClient.VERSION); properties.setProperty(PROPERTY_ENDPOINT, "https://api.gogrid.com/api"); properties.setProperty(PROPERTY_GOGRID_DEFAULT_DC, "1"); diff --git a/providers/gogrid/src/main/java/org/jclouds/gogrid/functions/GenericResponseContainer.java b/providers/gogrid/src/main/java/org/jclouds/gogrid/functions/GenericResponseContainer.java index 07532a69a5..f13711f09d 100644 --- a/providers/gogrid/src/main/java/org/jclouds/gogrid/functions/GenericResponseContainer.java +++ b/providers/gogrid/src/main/java/org/jclouds/gogrid/functions/GenericResponseContainer.java @@ -32,7 +32,7 @@ import java.util.SortedSet; * * @author Oleksiy Yarmula */ -class GenericResponseContainer { +public class GenericResponseContainer { private Summary summary; private String status; diff --git a/providers/gogrid/src/main/java/org/jclouds/gogrid/functions/ParseErrorFromJsonResponse.java b/providers/gogrid/src/main/java/org/jclouds/gogrid/functions/ParseErrorFromJsonResponse.java deleted file mode 100644 index dfe9cb94d8..0000000000 --- a/providers/gogrid/src/main/java/org/jclouds/gogrid/functions/ParseErrorFromJsonResponse.java +++ /dev/null @@ -1,57 +0,0 @@ -/** - * 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.gogrid.functions; - -import java.util.SortedSet; - -import javax.inject.Inject; - -import org.jclouds.gogrid.domain.internal.ErrorResponse; -import org.jclouds.http.HttpResponse; -import org.jclouds.http.functions.ParseJson; - -import com.google.common.base.Function; -import com.google.inject.Singleton; - -/** - * Parses {@link org.jclouds.gogrid.domain.internal.ErrorResponse error - * response} from a json string. - * - * GoGrid may return multiple error objects, if multiple errors were found. - * - * @author Oleksiy Yarmula - */ -@Singleton -public class ParseErrorFromJsonResponse implements - Function> { - - private final ParseJson> json; - - @Inject - ParseErrorFromJsonResponse( - ParseJson> json) { - this.json = json; - } - - @Override - public SortedSet apply(HttpResponse arg0) { - return json.apply(arg0).getList(); - } - -} diff --git a/providers/gogrid/src/main/java/org/jclouds/gogrid/functions/ParseServerFromJsonResponse.java b/providers/gogrid/src/main/java/org/jclouds/gogrid/functions/ParseServerFromJsonResponse.java deleted file mode 100644 index 9b8f6c45a4..0000000000 --- a/providers/gogrid/src/main/java/org/jclouds/gogrid/functions/ParseServerFromJsonResponse.java +++ /dev/null @@ -1,52 +0,0 @@ -/** - * 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.gogrid.functions; - -import javax.inject.Inject; -import javax.inject.Singleton; - -import org.jclouds.gogrid.domain.Server; -import org.jclouds.http.HttpResponse; - -import com.google.common.base.Function; -import com.google.common.collect.Iterables; - -/** - * Parses a single {@link Server} from a json string. - * - * This class delegates parsing to {@link ParseServerListFromJsonResponse}. - * - * @author Oleksiy Yarmula - */ -@Singleton -public class ParseServerFromJsonResponse implements - Function { - private final ParseServerListFromJsonResponse parser; - - @Inject - ParseServerFromJsonResponse(ParseServerListFromJsonResponse parser) { - this.parser = parser; - } - - @Override - public Server apply(HttpResponse arg0) { - return Iterables.getOnlyElement(parser.apply(arg0)); - } - -} diff --git a/providers/gogrid/src/main/java/org/jclouds/gogrid/functions/ParseServerListFromJsonResponse.java b/providers/gogrid/src/main/java/org/jclouds/gogrid/functions/ParseServerListFromJsonResponse.java deleted file mode 100644 index 8e39e5d5da..0000000000 --- a/providers/gogrid/src/main/java/org/jclouds/gogrid/functions/ParseServerListFromJsonResponse.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * 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.gogrid.functions; - -import java.util.SortedSet; - -import javax.inject.Inject; -import javax.inject.Singleton; - -import org.jclouds.gogrid.domain.Server; -import org.jclouds.http.HttpResponse; -import org.jclouds.http.functions.ParseJson; - -import com.google.common.base.Function; - -/** - * Parses {@link Server servers} from a json string. - * - * @author Adrian Cole - */ -@Singleton -public class ParseServerListFromJsonResponse implements - Function> { - - private final ParseJson> json; - - @Inject - ParseServerListFromJsonResponse(ParseJson> json) { - this.json = json; - } - - @Override - public SortedSet apply(HttpResponse arg0) { - return json.apply(arg0).getList(); - } - -} diff --git a/providers/gogrid/src/main/java/org/jclouds/gogrid/handlers/GoGridErrorHandler.java b/providers/gogrid/src/main/java/org/jclouds/gogrid/handlers/GoGridErrorHandler.java index e0acddcd20..40712920ca 100644 --- a/providers/gogrid/src/main/java/org/jclouds/gogrid/handlers/GoGridErrorHandler.java +++ b/providers/gogrid/src/main/java/org/jclouds/gogrid/handlers/GoGridErrorHandler.java @@ -18,17 +18,20 @@ */ package org.jclouds.gogrid.handlers; +import static org.jclouds.http.HttpUtils.closeClientButKeepContentStream; import static org.jclouds.http.HttpUtils.releasePayload; +import java.io.ByteArrayInputStream; import java.util.Set; import org.jclouds.gogrid.GoGridResponseException; import org.jclouds.gogrid.domain.internal.ErrorResponse; -import org.jclouds.gogrid.functions.ParseErrorFromJsonResponse; +import org.jclouds.gogrid.functions.GenericResponseContainer; import org.jclouds.http.HttpCommand; import org.jclouds.http.HttpErrorHandler; import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponseException; +import org.jclouds.http.functions.ParseJson; import org.jclouds.rest.AuthorizationException; import org.jclouds.rest.ResourceNotFoundException; @@ -40,30 +43,35 @@ import com.google.inject.Inject; */ public class GoGridErrorHandler implements HttpErrorHandler { - private final ParseErrorFromJsonResponse errorParser; + private final ParseJson> errorParser; @Inject - public GoGridErrorHandler(ParseErrorFromJsonResponse errorParser) { + public GoGridErrorHandler(ParseJson> errorParser) { this.errorParser = errorParser; } @Override public void handleError(HttpCommand command, HttpResponse response) { try { - Exception exception = new HttpResponseException(command, response); - Set errors = parseErrorsFromContentOrNull(response); + // it is important to always read fully and close streams + byte[] data = closeClientButKeepContentStream(response); + String message = data != null ? new String(data) : null; + + Exception exception = message != null ? new HttpResponseException(command, response, message) + : new HttpResponseException(command, response); + Set errors = parseErrorsFromContentOrNull(data); if (errors != null) exception = new GoGridResponseException(command, response, errors); switch (response.getStatusCode()) { - case 400: - if (Iterables.get(errors, 0).getMessage().indexOf("No object found") != -1) { - exception = new ResourceNotFoundException(Iterables.get(errors, 0).getMessage(), exception); + case 400: + if (Iterables.get(errors, 0).getMessage().indexOf("No object found") != -1) { + exception = new ResourceNotFoundException(Iterables.get(errors, 0).getMessage(), exception); + break; + } + break; + case 403: + exception = new AuthorizationException(exception.getMessage(), exception); break; - } - break; - case 403: - exception = new AuthorizationException(exception.getMessage(), exception); - break; } command.setException(exception); } finally { @@ -71,10 +79,10 @@ public class GoGridErrorHandler implements HttpErrorHandler { } } - Set parseErrorsFromContentOrNull(HttpResponse response) { - if (response.getPayload() != null) { + Set parseErrorsFromContentOrNull(byte[] response) { + if (response != null) { try { - return errorParser.apply(response); + return errorParser.apply(new ByteArrayInputStream(response)).getList(); } catch (/* Parsing */Exception e) { return null; } diff --git a/providers/gogrid/src/main/java/org/jclouds/gogrid/services/GridServerAsyncClient.java b/providers/gogrid/src/main/java/org/jclouds/gogrid/services/GridServerAsyncClient.java index d257605105..ab1745d096 100644 --- a/providers/gogrid/src/main/java/org/jclouds/gogrid/services/GridServerAsyncClient.java +++ b/providers/gogrid/src/main/java/org/jclouds/gogrid/services/GridServerAsyncClient.java @@ -44,16 +44,16 @@ import org.jclouds.gogrid.domain.Server; import org.jclouds.gogrid.filters.SharedKeyLiteAuthentication; import org.jclouds.gogrid.functions.ParseCredentialsFromJsonResponse; import org.jclouds.gogrid.functions.ParseOptionsFromJsonResponse; -import org.jclouds.gogrid.functions.ParseServerFromJsonResponse; -import org.jclouds.gogrid.functions.ParseServerListFromJsonResponse; import org.jclouds.gogrid.functions.ParseServerNameToCredentialsMapFromJsonResponse; import org.jclouds.gogrid.options.AddServerOptions; import org.jclouds.gogrid.options.GetServerListOptions; import org.jclouds.rest.annotations.BinderParam; import org.jclouds.rest.annotations.ExceptionParser; +import org.jclouds.rest.annotations.OnlyElement; import org.jclouds.rest.annotations.QueryParams; import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.ResponseParser; +import org.jclouds.rest.annotations.SelectJson; import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; @@ -76,7 +76,8 @@ public interface GridServerAsyncClient { * @see GridServerClient#getServerList(org.jclouds.gogrid.options.GetServerListOptions...) */ @GET - @ResponseParser(ParseServerListFromJsonResponse.class) + @SelectJson("list") + @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class) @Path("/grid/server/list") ListenableFuture> getServerList(GetServerListOptions... getServerListOptions); @@ -84,7 +85,7 @@ public interface GridServerAsyncClient { * @see GridServerClient#getServersByName(String...) */ @GET - @ResponseParser(ParseServerListFromJsonResponse.class) + @SelectJson("list") @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class) @Path("/grid/server/get") ListenableFuture> getServersByName( @@ -94,7 +95,7 @@ public interface GridServerAsyncClient { * @see GridServerClient#getServersById(Long...) */ @GET - @ResponseParser(ParseServerListFromJsonResponse.class) + @SelectJson("list") @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class) @Path("/grid/server/get") ListenableFuture> getServersById( @@ -121,7 +122,8 @@ public interface GridServerAsyncClient { * org.jclouds.gogrid.options.AddServerOptions...) */ @GET - @ResponseParser(ParseServerFromJsonResponse.class) + @SelectJson("list") + @OnlyElement @Path("/grid/server/add") ListenableFuture addServer(@QueryParam(NAME_KEY) String name, @QueryParam(IMAGE_KEY) String image, @QueryParam(SERVER_RAM_KEY) String ram, @@ -131,7 +133,8 @@ public interface GridServerAsyncClient { * @see GridServerClient#power(String, org.jclouds.gogrid.domain.PowerCommand) */ @GET - @ResponseParser(ParseServerFromJsonResponse.class) + @SelectJson("list") + @OnlyElement @Path("/grid/server/power") ListenableFuture power( @QueryParam(SERVER_ID_OR_NAME_KEY) String idOrName, @@ -141,7 +144,8 @@ public interface GridServerAsyncClient { * @see GridServerClient#deleteById(Long) */ @GET - @ResponseParser(ParseServerFromJsonResponse.class) + @SelectJson("list") + @OnlyElement @Path("/grid/server/delete") @ExceptionParser(ReturnNullOnNotFoundOr404.class) ListenableFuture deleteById(@QueryParam(ID_KEY) long id); @@ -150,7 +154,8 @@ public interface GridServerAsyncClient { * @see GridServerClient#deleteByName(String) */ @GET - @ResponseParser(ParseServerFromJsonResponse.class) + @SelectJson("list") + @OnlyElement @Path("/grid/server/delete") @ExceptionParser(ReturnNullOnNotFoundOr404.class) ListenableFuture deleteByName(@QueryParam(NAME_KEY) String name); @@ -187,7 +192,8 @@ public interface GridServerAsyncClient { * @see GridServerClient#editServerDescription */ @GET - @ResponseParser(ParseServerFromJsonResponse.class) + @SelectJson("list") + @OnlyElement @Path("/grid/server/edit") ListenableFuture editServerDescription(@QueryParam("id") long id, @QueryParam("description") String newDescription); @@ -196,7 +202,8 @@ public interface GridServerAsyncClient { * @see GridServerClient#editServerRam */ @GET - @ResponseParser(ParseServerFromJsonResponse.class) + @SelectJson("list") + @OnlyElement @Path("/grid/server/edit") ListenableFuture editServerRam(@QueryParam("id") long id, @QueryParam("server.ram") String ram); @@ -205,7 +212,8 @@ public interface GridServerAsyncClient { * @see GridServerClient#editServerType */ @GET - @ResponseParser(ParseServerFromJsonResponse.class) + @SelectJson("list") + @OnlyElement @Path("/grid/server/edit") ListenableFuture editServerType(@QueryParam("id") long id, @QueryParam("server.type") String newType); diff --git a/providers/gogrid/src/test/java/org/jclouds/gogrid/compute/GoGridTemplateBuilderLiveTest.java b/providers/gogrid/src/test/java/org/jclouds/gogrid/compute/GoGridTemplateBuilderLiveTest.java index dd90c51b48..9d917a2174 100644 --- a/providers/gogrid/src/test/java/org/jclouds/gogrid/compute/GoGridTemplateBuilderLiveTest.java +++ b/providers/gogrid/src/test/java/org/jclouds/gogrid/compute/GoGridTemplateBuilderLiveTest.java @@ -80,6 +80,6 @@ public class GoGridTemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest { @Override protected Set getIso3166Codes() { - return ImmutableSet. of("US-CA", "US-VA", "BR-SP"); + return ImmutableSet. of("US-CA", "US-VA", "NL-NH"); } } \ No newline at end of file diff --git a/providers/gogrid/src/test/java/org/jclouds/gogrid/functions/ParseErrorFromJsonResponseTest.java b/providers/gogrid/src/test/java/org/jclouds/gogrid/functions/ParseErrorFromJsonResponseTest.java deleted file mode 100644 index 9121038706..0000000000 --- a/providers/gogrid/src/test/java/org/jclouds/gogrid/functions/ParseErrorFromJsonResponseTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/** - * 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.gogrid.functions; - -import java.io.InputStream; -import java.net.UnknownHostException; - -import org.jclouds.gogrid.domain.internal.ErrorResponse; -import org.jclouds.http.HttpResponse; -import org.jclouds.io.Payloads; -import org.jclouds.json.config.GsonModule; -import org.testng.annotations.Test; - -import com.google.common.collect.Iterables; -import com.google.inject.Guice; -import com.google.inject.Injector; - -/** - * @author Oleksiy Yarmula - */ -//NOTE:without testName, this will not call @Before* and fail w/NPE during surefire -@Test(groups = "unit", testName = "ParseErrorFromJsonResponseTest") -public class ParseErrorFromJsonResponseTest { - - Injector i = Guice.createInjector(new GsonModule() { - @Override - protected void configure() { - bind(DateAdapter.class).to(LongDateAdapter.class); - super.configure(); - } - }); - - @Test - public void testApplyInputStreamDetails() throws UnknownHostException { - InputStream is = getClass().getResourceAsStream("/test_error_handler.json"); - - ParseErrorFromJsonResponse parser = i.getInstance(ParseErrorFromJsonResponse.class); - ErrorResponse response = Iterables.getOnlyElement(parser.apply(new HttpResponse(200, "ok", Payloads - .newInputStreamPayload(is)))); - assert "No object found that matches your input criteria.".equals(response.getMessage()); - assert "IllegalArgumentException".equals(response.getErrorCode()); - } -} diff --git a/providers/gogrid/src/test/java/org/jclouds/gogrid/functions/ParseServersFromJsonResponseTest.java b/providers/gogrid/src/test/java/org/jclouds/gogrid/functions/ParseServersFromJsonResponseTest.java deleted file mode 100644 index f2db917705..0000000000 --- a/providers/gogrid/src/test/java/org/jclouds/gogrid/functions/ParseServersFromJsonResponseTest.java +++ /dev/null @@ -1,104 +0,0 @@ -/** - * 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.gogrid.functions; - -import static org.testng.Assert.assertEquals; - -import java.io.InputStream; -import java.net.UnknownHostException; -import java.util.Date; -import java.util.Map; -import java.util.SortedSet; - -import javax.inject.Singleton; - -import org.jclouds.gogrid.config.GoGridParserModule; -import org.jclouds.gogrid.domain.BillingToken; -import org.jclouds.gogrid.domain.Customer; -import org.jclouds.gogrid.domain.Ip; -import org.jclouds.gogrid.domain.IpState; -import org.jclouds.gogrid.domain.Option; -import org.jclouds.gogrid.domain.Server; -import org.jclouds.gogrid.domain.ServerImage; -import org.jclouds.gogrid.domain.ServerImageState; -import org.jclouds.gogrid.domain.ServerImageType; -import org.jclouds.gogrid.domain.ServerState; -import org.jclouds.gogrid.functions.internal.CustomDeserializers; -import org.jclouds.http.HttpResponse; -import org.jclouds.io.Payloads; -import org.jclouds.json.config.GsonModule; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableSortedSet; -import com.google.common.collect.Iterables; -import com.google.common.collect.Maps; -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.Provides; - -/** - * Tests behavior of {@code ParseStatusesFromJsonResponse} - * - * @author Adrian Cole - */ -//NOTE:without testName, this will not call @Before* and fail w/NPE during surefire -@Test(groups = "unit", testName = "ParseServersFromJsonResponseTest") -public class ParseServersFromJsonResponseTest { - - @Test - public void testApplyInputStreamDetails() throws UnknownHostException { - InputStream is = getClass().getResourceAsStream("/test_get_server_list.json"); - - ParseServerListFromJsonResponse parser = i.getInstance(ParseServerListFromJsonResponse.class); - SortedSet response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); - - Option dc = new Option(1l, "US-West-1", "US West 1 Datacenter"); - Option centOs = new Option(13L, "CentOS 5.2 (32-bit)", "CentOS 5.2 (32-bit)"); - Option webServer = new Option(1L, "Web Server", "Web or Application Server"); - Server server = new Server(75245L, dc, false, "PowerServer", "server to test the api. created by Alex", - ServerState.ON, webServer, new Option(1L, "512MB", "Server with 512MB RAM"), centOs, new Ip(1313079L, - "204.51.240.178", "204.51.240.176/255.255.255.240", true, IpState.ASSIGNED, dc), new ServerImage( - 1946L, "GSI-f8979644-e646-4711-ad58-d98a5fa3612c", "BitNami Gallery 2.3.1-0", - "http://bitnami.org/stack/gallery", centOs, null, ServerImageType.WEB_APPLICATION_SERVER, - ServerImageState.AVAILABLE, 0.0, "24732/GSI-f8979644-e646-4711-ad58-d98a5fa3612c.img", true, true, - new Date(1261504577971L), new Date(1262649582180L), ImmutableSortedSet.of(new BillingToken(38L, - "CentOS 5.2 32bit", 0.0), new BillingToken(56L, "BitNami: Gallery", 0.0)), new Customer(24732L, - "BitRock"))); - assertEquals(Iterables.getOnlyElement(response), server); - } - - Injector i = Guice.createInjector(new GsonModule() { - @Override - protected void configure() { - install(new GoGridParserModule()); - super.configure(); - } - - @Provides - @Singleton - @SuppressWarnings( { "unused", "rawtypes" }) - public Map provideCustomAdapterBindings() { - Map bindings = Maps.newHashMap(); - bindings.put(IpState.class, new CustomDeserializers.IpStateAdapter()); - bindings.put(ServerImageType.class, new CustomDeserializers.ServerImageTypeAdapter()); - bindings.put(ServerImageState.class, new CustomDeserializers.ServerImageStateAdapter()); - return bindings; - } - }); -} diff --git a/providers/gogrid/src/test/java/org/jclouds/gogrid/parse/ParseServerListTest.java b/providers/gogrid/src/test/java/org/jclouds/gogrid/parse/ParseServerListTest.java new file mode 100644 index 0000000000..ec3802c664 --- /dev/null +++ b/providers/gogrid/src/test/java/org/jclouds/gogrid/parse/ParseServerListTest.java @@ -0,0 +1,79 @@ +/** + * 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.gogrid.parse; + +import java.util.Date; +import java.util.Set; + +import org.jclouds.gogrid.config.GoGridParserModule; +import org.jclouds.gogrid.domain.BillingToken; +import org.jclouds.gogrid.domain.Customer; +import org.jclouds.gogrid.domain.Ip; +import org.jclouds.gogrid.domain.IpState; +import org.jclouds.gogrid.domain.Option; +import org.jclouds.gogrid.domain.Server; +import org.jclouds.gogrid.domain.ServerImage; +import org.jclouds.gogrid.domain.ServerImageState; +import org.jclouds.gogrid.domain.ServerImageType; +import org.jclouds.gogrid.domain.ServerState; +import org.jclouds.json.BaseSetParserTest; +import org.jclouds.json.config.GsonModule; +import org.jclouds.rest.annotations.SelectJson; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableSortedSet; +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "ParseServerListTest") +public class ParseServerListTest extends BaseSetParserTest { + + @Override + public String resource() { + return "/test_get_server_list.json"; + } + + @Override + @SelectJson("list") + public Set expected() { + Option dc = new Option(1l, "US-West-1", "US West 1 Datacenter"); + Option centOs = new Option(13L, "CentOS 5.2 (32-bit)", "CentOS 5.2 (32-bit)"); + Option webServer = new Option(1L, "Web Server", "Web or Application Server"); + return ImmutableSet.of(new Server(75245L, dc, false, "PowerServer", "server to test the api. created by Alex", + ServerState.ON, webServer, new Option(1L, "512MB", "Server with 512MB RAM"), centOs, new Ip(1313079L, + "204.51.240.178", "204.51.240.176/255.255.255.240", true, IpState.ASSIGNED, dc), + new ServerImage(1946L, "GSI-f8979644-e646-4711-ad58-d98a5fa3612c", "BitNami Gallery 2.3.1-0", + "http://bitnami.org/stack/gallery", centOs, null, ServerImageType.WEB_APPLICATION_SERVER, + ServerImageState.AVAILABLE, 0.0, "24732/GSI-f8979644-e646-4711-ad58-d98a5fa3612c.img", true, + true, new Date(1261504577971L), new Date(1262649582180L), ImmutableSortedSet.of( + new BillingToken(38L, "CentOS 5.2 32bit", 0.0), new BillingToken(56L, + "BitNami: Gallery", 0.0)), new Customer(24732L, "BitRock")))); + + } + + protected Injector injector() { + return Guice.createInjector(new GoGridParserModule(), new GsonModule()); + } + +} diff --git a/providers/gogrid/src/test/java/org/jclouds/gogrid/parse/ParseServerTest.java b/providers/gogrid/src/test/java/org/jclouds/gogrid/parse/ParseServerTest.java new file mode 100644 index 0000000000..c78a68d276 --- /dev/null +++ b/providers/gogrid/src/test/java/org/jclouds/gogrid/parse/ParseServerTest.java @@ -0,0 +1,76 @@ +/** + * 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.gogrid.parse; + +import java.util.Date; + +import org.jclouds.gogrid.config.GoGridParserModule; +import org.jclouds.gogrid.domain.BillingToken; +import org.jclouds.gogrid.domain.Customer; +import org.jclouds.gogrid.domain.Ip; +import org.jclouds.gogrid.domain.IpState; +import org.jclouds.gogrid.domain.Option; +import org.jclouds.gogrid.domain.Server; +import org.jclouds.gogrid.domain.ServerImage; +import org.jclouds.gogrid.domain.ServerImageState; +import org.jclouds.gogrid.domain.ServerImageType; +import org.jclouds.gogrid.domain.ServerState; +import org.jclouds.json.BaseItemParserTest; +import org.jclouds.json.config.GsonModule; +import org.jclouds.rest.annotations.OnlyElement; +import org.jclouds.rest.annotations.SelectJson; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSortedSet; +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "ParseServerTest") +public class ParseServerTest extends BaseItemParserTest { + + @Override + public String resource() { + return "/test_get_server_list.json"; + } + + @Override + @SelectJson("list") + @OnlyElement + public Server expected() { + Option dc = new Option(1l, "US-West-1", "US West 1 Datacenter"); + Option centOs = new Option(13L, "CentOS 5.2 (32-bit)", "CentOS 5.2 (32-bit)"); + Option webServer = new Option(1L, "Web Server", "Web or Application Server"); + return new Server(75245L, dc, false, "PowerServer", "server to test the api. created by Alex", + ServerState.ON, webServer, new Option(1L, "512MB", "Server with 512MB RAM"), centOs, new Ip(1313079L, + "204.51.240.178", "204.51.240.176/255.255.255.240", true, IpState.ASSIGNED, dc), new ServerImage( + 1946L, "GSI-f8979644-e646-4711-ad58-d98a5fa3612c", "BitNami Gallery 2.3.1-0", + "http://bitnami.org/stack/gallery", centOs, null, ServerImageType.WEB_APPLICATION_SERVER, + ServerImageState.AVAILABLE, 0.0, "24732/GSI-f8979644-e646-4711-ad58-d98a5fa3612c.img", true, true, + new Date(1261504577971L), new Date(1262649582180L), ImmutableSortedSet.of(new BillingToken(38L, + "CentOS 5.2 32bit", 0.0), new BillingToken(56L, "BitNami: Gallery", 0.0)), new Customer(24732L, + "BitRock"))); + } + + protected Injector injector() { + return Guice.createInjector(new GoGridParserModule(), new GsonModule()); + } +} diff --git a/providers/gogrid/src/test/java/org/jclouds/gogrid/services/BaseGoGridRestClientExpectTest.java b/providers/gogrid/src/test/java/org/jclouds/gogrid/services/BaseGoGridRestClientExpectTest.java new file mode 100644 index 0000000000..3da872721a --- /dev/null +++ b/providers/gogrid/src/test/java/org/jclouds/gogrid/services/BaseGoGridRestClientExpectTest.java @@ -0,0 +1,37 @@ +package org.jclouds.gogrid.services; + +import org.jclouds.date.TimeStamp; +import org.jclouds.gogrid.GoGridClient; +import org.jclouds.gogrid.config.GoGridRestClientModule; +import org.jclouds.http.RequiresHttp; +import org.jclouds.rest.BaseRestClientExpectTest; +import org.jclouds.rest.ConfiguresRestClient; + +import com.google.common.base.Supplier; +import com.google.inject.Module; + +/** + * + * @author Adrian Cole + */ +public class BaseGoGridRestClientExpectTest extends BaseRestClientExpectTest { + + public BaseGoGridRestClientExpectTest() { + provider = "gogrid"; + } + + @RequiresHttp + @ConfiguresRestClient + protected static final class TestGoGridRestClientModule extends GoGridRestClientModule { + + @Override + protected Long provideTimeStamp(@TimeStamp Supplier cache) { + return 1267243795L; + } + } + + @Override + protected Module createModule() { + return new TestGoGridRestClientModule(); + } +} diff --git a/providers/gogrid/src/test/java/org/jclouds/gogrid/services/GridServerAsyncClientTest.java b/providers/gogrid/src/test/java/org/jclouds/gogrid/services/GridServerAsyncClientTest.java index 3702c1552a..7b5d68ae8c 100644 --- a/providers/gogrid/src/test/java/org/jclouds/gogrid/services/GridServerAsyncClientTest.java +++ b/providers/gogrid/src/test/java/org/jclouds/gogrid/services/GridServerAsyncClientTest.java @@ -24,9 +24,6 @@ import java.lang.reflect.Method; import org.jclouds.gogrid.domain.PowerCommand; import org.jclouds.gogrid.functions.ParseCredentialsFromJsonResponse; import org.jclouds.gogrid.functions.ParseOptionsFromJsonResponse; -import org.jclouds.gogrid.functions.ParseServerFromJsonResponse; -import org.jclouds.gogrid.functions.ParseServerListFromJsonResponse; -import org.jclouds.gogrid.options.AddServerOptions; import org.jclouds.gogrid.options.GetServerListOptions; import org.jclouds.http.HttpRequest; import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; @@ -40,34 +37,12 @@ import com.google.inject.TypeLiteral; /** * Tests behavior of {@code GridServerAsyncClient} * - * @author Oleksiy Yarmula + * @author Oleksiy Yarmula, Adrian Cole */ // NOTE:without testName, this will not call @Before* and fail w/NPE during surefire @Test(groups = "unit", testName = "GridServerAsyncClientTest") public class GridServerAsyncClientTest extends BaseGoGridAsyncClientTest { - @Test - public void testGetServerListNoOptions() throws NoSuchMethodException, IOException { - Method method = GridServerAsyncClient.class.getMethod("getServerList", GetServerListOptions[].class); - HttpRequest httpRequest = processor.createRequest(method); - - assertRequestLineEquals(httpRequest, "GET https://api.gogrid.com/api/grid/server/list?v=1.6 HTTP/1.1"); - assertNonPayloadHeadersEqual(httpRequest, ""); - assertPayloadEquals(httpRequest, null, null, false); - - assertResponseParserClassEquals(method, httpRequest, ParseServerListFromJsonResponse.class); - assertSaxResponseParserClassEquals(method, null); - assertExceptionParserClassEquals(method, null); - - checkFilters(httpRequest); - httpRequest = Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest); - - assertRequestLineEquals(httpRequest, "GET https://api.gogrid.com/api/grid/server/list?" - + "v=1.6&sig=3f446f171455fbb5574aecff4997b273&api_key=foo " + "HTTP/1.1"); - assertNonPayloadHeadersEqual(httpRequest, ""); - assertPayloadEquals(httpRequest, null, null, false); - } - @Test public void testGetServerListWithOptions() throws NoSuchMethodException, IOException { Method method = GridServerAsyncClient.class.getMethod("getServerList", GetServerListOptions[].class); @@ -79,9 +54,8 @@ public class GridServerAsyncClientTest extends BaseGoGridAsyncClientTest