From 1440ef2b3a0d2feee355161a9c9cae5585808d06 Mon Sep 17 00:00:00 2001 From: Daniel Broudy Date: Mon, 10 Nov 2014 18:35:58 -0800 Subject: [PATCH] Added Image.deprecate, ImageApiMockTest completed, removed ImageApiExpectTest --- .../GoogleComputeEngineImageToImage.java | 11 +- .../domain/Deprecated.java | 11 +- .../features/ImageApi.java | 16 ++ .../options/DeprecateOptions.java | 11 +- .../GoogleComputeEngineImageToImageTest.java | 42 +++- .../features/ImageApiExpectTest.java | 184 ------------------ .../features/ImageApiLiveTest.java | 30 +++ .../features/ImageApiMockTest.java | 135 +++++++++++++ .../BaseGoogleComputeEngineApiMockTest.java | 4 + .../parse/ParseImageListTest.java | 9 +- .../parse/ParseImageTest.java | 12 +- .../parse/ParseOperationTest.java | 13 +- .../src/test/resources/image_deprecate.json | 7 + 13 files changed, 275 insertions(+), 210 deletions(-) delete mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ImageApiExpectTest.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ImageApiMockTest.java create mode 100644 providers/google-compute-engine/src/test/resources/image_deprecate.json diff --git a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/functions/GoogleComputeEngineImageToImage.java b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/functions/GoogleComputeEngineImageToImage.java index 24fab1802d..1700e3d8fd 100644 --- a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/functions/GoogleComputeEngineImageToImage.java +++ b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/compute/functions/GoogleComputeEngineImageToImage.java @@ -27,6 +27,7 @@ import java.util.List; import org.jclouds.compute.domain.ImageBuilder; import org.jclouds.compute.domain.OperatingSystem; import org.jclouds.compute.domain.OsFamily; +import org.jclouds.googlecomputeengine.domain.Deprecated.State; import org.jclouds.googlecomputeengine.domain.Image; import com.google.common.base.Function; @@ -44,6 +45,13 @@ public final class GoogleComputeEngineImageToImage implements Function splits = Lists.newArrayList(image.name().split("-")); OperatingSystem.Builder osBuilder = defaultOperatingSystem(image); if (splits == null || splits.size() == 0 || splits.size() < 3) { @@ -58,9 +66,6 @@ public final class GoogleComputeEngineImageToImage implements Function { - HttpRequest get = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/global/images/centos-6-2-v20120326") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public void get() throws Exception { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/image_get.json")).build(); - - ImageApi imageApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).images(); - - assertEquals(imageApi.get(get.getEndpoint()), new ParseImageTest().expected()); - } - - public void getResponseIs4xx() throws Exception { - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - ImageApi imageApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).images(); - - assertNull(imageApi.get(get.getEndpoint())); - } - - public void getByName() throws Exception { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/image_get.json")).build(); - - ImageApi imageApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).images(); - - assertEquals(imageApi.get("centos-6-2-v20120326"), new ParseImageTest().expected()); - } - - public void getByNameResponseIs4xx() throws Exception { - HttpResponse response = HttpResponse.builder().statusCode(404).build(); - - ImageApi imageApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, get, response).images(); - - assertNull(imageApi.get("centos-6-2-v20120326")); - } - - public void testDeleteImageResponseIs2xx() { - HttpRequest delete = HttpRequest - .builder() - .method("DELETE") - .endpoint(BASE_URL + "/party/global/images/centos-6-2-v20120326") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse deleteResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/operation.json")).build(); - - ImageApi imageApi = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, delete, deleteResponse).images(); - - assertEquals(imageApi.delete("centos-6-2-v20120326"), new ParseOperationTest().expected()); - } - - public void testDeleteImageResponseIs4xx() { - HttpRequest delete = HttpRequest - .builder() - .method("DELETE") - .endpoint(BASE_URL + "/party/global/images/centos-6-2-v20120326") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - HttpResponse deleteResponse = HttpResponse.builder().statusCode(404).build(); - - ImageApi imageApi = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, delete, deleteResponse).images(); - - assertNull(imageApi.delete("centos-6-2-v20120326")); - } - - public static final HttpRequest LIST_PROJECT_IMAGES_REQUEST = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/party/global/images") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public static final HttpResponse LIST_PROJECT_IMAGES_RESPONSE = HttpResponse.builder().statusCode(200) - .payload(staticPayloadFromResource("/image_list.json")).build(); - - public void list() { - - ImageApi imageApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, LIST_PROJECT_IMAGES_REQUEST, LIST_PROJECT_IMAGES_RESPONSE).images(); - - assertEquals(imageApi.list().next(), new ParseImageListTest().expected()); - } - - public void listEmpty() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/list_empty.json")).build(); - - ImageApi imageApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, LIST_PROJECT_IMAGES_REQUEST, response).images(); - - assertFalse(imageApi.list().hasNext()); - } - - public static final HttpRequest LIST_CENTOS_IMAGES_REQUEST = HttpRequest - .builder() - .method("GET") - .endpoint(BASE_URL + "/centos-cloud/global/images") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN).build(); - - public void listInProject() { - - ImageApi imageApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, LIST_CENTOS_IMAGES_REQUEST, LIST_PROJECT_IMAGES_RESPONSE).images(); - - assertEquals(imageApi.listInProject("centos-cloud").next(), new ParseImageListTest().expected()); - } - - public void listInProjectEmpty() { - HttpResponse response = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/list_empty.json")).build(); - - ImageApi imageApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), - TOKEN_RESPONSE, LIST_CENTOS_IMAGES_REQUEST, response).images(); - - assertFalse(imageApi.listInProject("centos-cloud").hasNext()); - } - - public void testCreateImageFromPdResponseIs2xx(){ - HttpRequest createImage = HttpRequest - .builder() - .method("POST") - .endpoint(BASE_URL + "/party/global/images") - .addHeader("Accept", "application/json") - .addHeader("Authorization", "Bearer " + TOKEN) - .payload(payloadFromResource("/image_insert_from_pd.json")) - .build(); - - HttpResponse createImageResponse = HttpResponse.builder().statusCode(200) - .payload(payloadFromResource("/operation.json")).build(); - - ImageApi imageApi = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), - TOKEN_RESPONSE, createImage, createImageResponse).images(); - - assertEquals(imageApi.createFromDisk("my-image", BASE_URL + "/party/zones/us-central1-a/disks/mydisk"), - new ParseOperationTest().expected()); - } -} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ImageApiLiveTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ImageApiLiveTest.java index 5699edbe85..4d1afc7ef8 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ImageApiLiveTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ImageApiLiveTest.java @@ -18,15 +18,19 @@ package org.jclouds.googlecomputeengine.features; import static org.jclouds.googlecomputeengine.options.ListOptions.Builder.maxResults; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; import java.net.URI; import java.util.Iterator; import java.util.List; +import org.jclouds.date.internal.SimpleDateFormatDateService; import org.jclouds.googlecloud.domain.ListPage; import org.jclouds.googlecomputeengine.domain.Disk; import org.jclouds.googlecomputeengine.domain.Image; +import org.jclouds.googlecomputeengine.domain.Deprecated.State; import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiLiveTest; +import org.jclouds.googlecomputeengine.options.DeprecateOptions; import org.testng.annotations.Test; public class ImageApiLiveTest extends BaseGoogleComputeEngineApiLiveTest { @@ -81,6 +85,32 @@ public class ImageApiLiveTest extends BaseGoogleComputeEngineApiLiveTest { } @Test(groups = "live", dependsOnMethods = "testGetCreatedImage") + public void testDeprecateImage(){ + Image image = api().get(IMAGE_NAME); + assertNull(image.deprecated()); + String deprecated = "2015-07-16T22:16:13.468Z"; + String obsolete = "2016-10-16T22:16:13.468Z"; + String deleted = "2017-01-16T22:16:13.468Z"; + + URI replacement = URI.create("https://www.googleapis.com/compute/v1/projects/centos-cloud/global/images/centos-6-2-v20120326test"); + + DeprecateOptions deprecateOptions = new DeprecateOptions().state(State.DEPRECATED) + .replacement(replacement) + .deprecated(new SimpleDateFormatDateService().iso8601DateParse(deprecated)) + .obsolete(new SimpleDateFormatDateService().iso8601DateParse(obsolete)) + .deleted(new SimpleDateFormatDateService().iso8601DateParse(deleted)); + + assertOperationDoneSuccessfully(api().deprecate(IMAGE_NAME, deprecateOptions)); + + image = api().get(IMAGE_NAME); + assertEquals(image.deprecated().state(), State.DEPRECATED); + assertEquals(image.deprecated().replacement(), replacement); + assertEquals(image.deprecated().deprecated(), deprecated); + assertEquals(image.deprecated().obsolete(), obsolete); + assertEquals(image.deprecated().deleted(), deleted); + } + + @Test(groups = "live", dependsOnMethods = "testDeprecateImage", alwaysRun = true) public void testCleanup(){ assertOperationDoneSuccessfully(api().delete(IMAGE_NAME)); assertOperationDoneSuccessfully(diskApi().delete(DISK_NAME)); diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ImageApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ImageApiMockTest.java new file mode 100644 index 0000000000..7fd62c50c7 --- /dev/null +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ImageApiMockTest.java @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.googlecomputeengine.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNull; + +import java.net.URI; + +import org.jclouds.date.internal.SimpleDateFormatDateService; +import org.jclouds.googlecomputeengine.domain.Deprecated.State; +import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiMockTest; +import org.jclouds.googlecomputeengine.options.DeprecateOptions; +import org.jclouds.googlecomputeengine.parse.ParseImageListTest; +import org.jclouds.googlecomputeengine.parse.ParseImageTest; +import org.jclouds.googlecomputeengine.parse.ParseOperationTest; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "ImageApiMockTest", singleThreaded = true) +public class ImageApiMockTest extends BaseGoogleComputeEngineApiMockTest { + + public void get() throws Exception { + server.enqueue(jsonResponse("/image_get.json")); + + assertEquals(imageApi().get(URI.create(url("/projects/party/global/images/centos-6-2-v20120326"))), + new ParseImageTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/global/images/centos-6-2-v20120326"); + } + + public void get_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(imageApi().get(URI.create(url("/projects/party/global/images/centos-6-2-v20120326")))); + assertSent(server, "GET", "/projects/party/global/images/centos-6-2-v20120326"); + } + + public void getByName() throws Exception { + server.enqueue(jsonResponse("/image_get.json")); + + assertEquals(imageApi().get("centos-6-2-v20120326"), new ParseImageTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/global/images/centos-6-2-v20120326"); + } + + public void getByName_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(imageApi().get("centos-6-2-v20120326")); + assertSent(server, "GET", "/projects/party/global/images/centos-6-2-v20120326"); + } + + public void deleteImage_2xx() throws Exception { + server.enqueue(jsonResponse("/operation.json")); + + assertEquals(imageApi().delete("centos-6-2-v20120326"), + new ParseOperationTest().expected(url("/projects"))); + assertSent(server, "DELETE", "/projects/party/global/images/centos-6-2-v20120326"); + } + + public void deleteImage_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(imageApi().delete("centos-6-2-v20120326")); + assertSent(server, "DELETE", "/projects/party/global/images/centos-6-2-v20120326"); + } + + public void list() throws InterruptedException { + server.enqueue(jsonResponse("/image_list.json")); + + assertEquals(imageApi().list().next(), new ParseImageListTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/party/global/images"); + } + + public void list_empty() throws InterruptedException { + server.enqueue(jsonResponse("/list_empty.json")); + + assertFalse(imageApi().list().hasNext()); + assertSent(server, "GET", "/projects/party/global/images"); + } + + public void listInProject() throws InterruptedException { + server.enqueue(jsonResponse("/image_list.json")); + + assertEquals(imageApi().listInProject("centos-cloud").next(), new ParseImageListTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/centos-cloud/global/images"); + } + + public void listInProject_empty() throws InterruptedException { + server.enqueue(jsonResponse("/list_empty.json")); + + assertFalse(imageApi().listInProject("centos-cloud").hasNext()); + assertSent(server, "GET", "/projects/centos-cloud/global/images"); + } + + public void createImageFromPd_2xx() throws InterruptedException{ + server.enqueue(jsonResponse("/operation.json")); + + assertEquals(imageApi().createFromDisk("my-image", url("/projects/party/zones/us-central1-a/disks/mydisk")), + new ParseOperationTest().expected(url("/projects"))); + assertSent(server, "POST", "/projects/party/global/images", stringFromResource("/image_insert_from_pd.json")); + } + + public void deprecateImage_2xx() throws InterruptedException{ + String imageName = "test-image"; + server.enqueue(jsonResponse("/operation.json")); + + DeprecateOptions options = new DeprecateOptions().state(State.DEPRECATED) + .replacement(URI.create(url("/projects/centos-cloud/global/images/centos-6-2-v20120326test"))) + .deprecated(new SimpleDateFormatDateService().iso8601DateParse("2014-07-16T22:16:13.468Z")) + .obsolete(new SimpleDateFormatDateService().iso8601DateParse("2014-10-16T22:16:13.468Z")) + .deleted(new SimpleDateFormatDateService().iso8601DateParse("2015-01-16T22:16:13.468Z")); + + assertEquals(imageApi().deprecate(imageName, options), + new ParseOperationTest().expected(url("/projects"))); + assertSent(server, "POST", "/projects/party/global/images/" + imageName + "/deprecate", stringFromResource("/image_deprecate.json")); + } + + ImageApi imageApi(){ + return api().images(); + } +} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/internal/BaseGoogleComputeEngineApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/internal/BaseGoogleComputeEngineApiMockTest.java index d0c8dee237..1b06ad20e0 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/internal/BaseGoogleComputeEngineApiMockTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/internal/BaseGoogleComputeEngineApiMockTest.java @@ -104,6 +104,10 @@ public class BaseGoogleComputeEngineApiMockTest { return new MockResponse().addHeader("Content-Type", "application/json").setBody(stringFromResource(resource)); } + protected MockResponse response404(){ + return new MockResponse().setStatus("HTTP/1.1 404 Not Found"); + } + protected String stringFromResource(String resourceName) { try { return toStringAndClose(getClass().getResourceAsStream(resourceName)) diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseImageListTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseImageListTest.java index c1288750b1..16c66e5544 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseImageListTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseImageListTest.java @@ -37,8 +37,13 @@ public class ParseImageListTest extends BaseGoogleComputeEngineParseTest expected() { - return ForwardingListPage.create( // - ImmutableList.of(new ParseImageTest().expected()), // items + return expected(BASE_URL); + } + + @Consumes(MediaType.APPLICATION_JSON) + public ListPage expected(String baseUrl) { + return ForwardingListPage.create( // + ImmutableList.of(new ParseImageTest().expected(baseUrl)), // items null // nextPageToken ); } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseImageTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseImageTest.java index 27bb940921..7a14a37f35 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseImageTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseImageTest.java @@ -24,6 +24,7 @@ import java.net.URI; import javax.ws.rs.Consumes; import org.jclouds.googlecomputeengine.domain.Deprecated; +import org.jclouds.googlecomputeengine.domain.Deprecated.State; import org.jclouds.googlecomputeengine.domain.Image; import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineParseTest; import org.testng.annotations.Test; @@ -38,16 +39,21 @@ public class ParseImageTest extends BaseGoogleComputeEngineParseTest { @Override @Consumes(APPLICATION_JSON) public Image expected() { + return expected(BASE_URL); + } + + @Consumes(APPLICATION_JSON) + public Image expected(String baseUrl) { return Image.create( // "12941197498378735318", // id - URI.create(BASE_URL + "/centos-cloud/global/images/centos-6-2-v20120326"), // selfLink + URI.create(baseUrl + "/centos-cloud/global/images/centos-6-2-v20120326"), // selfLink "centos-6-2-v20120326", // name "DEPRECATED. CentOS 6.2 image; Created Mon, 26 Mar 2012 21:19:09 +0000", // description "RAW", // sourceType RawDisk.create(URI.create(""), "TAR", null), // rawDisk Deprecated.create( // deprecated - "DEPRECATED", // state - URI.create(BASE_URL + "/centos-cloud/global/images/centos-6-v20130104"), // replacement + State.DEPRECATED, // state + URI.create(baseUrl + "/centos-cloud/global/images/centos-6-v20130104"), // replacement null, // deprecated null, // obsolete null // deleted diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseOperationTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseOperationTest.java index 9023dd9416..a4e9055adb 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseOperationTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseOperationTest.java @@ -36,12 +36,17 @@ public class ParseOperationTest extends BaseGoogleComputeEngineParseTest