Skip to content
Permalink
Browse files
JCLOUDS-691: Remove 404 fallbacks of PUT and POST calls from OpenStac…
…k APIs
  • Loading branch information
devcsrj committed Feb 6, 2016
1 parent 3b49016 commit 167312fb41997b9add33e409f542fc4a4dcd22cd
Showing 30 changed files with 20 additions and 3,246 deletions.
@@ -18,8 +18,6 @@

import static org.jclouds.openstack.glance.v1_0.options.CreateImageOptions.Builder.copyFrom;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

import javax.ws.rs.core.MediaType;
@@ -35,8 +33,6 @@
import org.jclouds.openstack.glance.v1_0.parse.ParseImageDetailsTest;
import org.jclouds.openstack.glance.v1_0.parse.ParseImagesInDetailTest;
import org.jclouds.openstack.glance.v1_0.parse.ParseImagesTest;
import org.jclouds.rest.AuthorizationException;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.util.Strings2;
import org.testng.annotations.Test;

@@ -65,21 +61,6 @@ public void testListWhenResponseIs2xx() throws Exception {
new ParseImagesTest().expected().toString());
}

public void testListWhenReponseIs404IsEmpty() throws Exception {
HttpRequest list = HttpRequest.builder().method("GET")
.endpoint("https://glance.jclouds.org:9292/v1.0/images")
.addHeader("Accept", "application/json")
.addHeader("X-Auth-Token", authToken).build();

HttpResponse listResponse = HttpResponse.builder().statusCode(404).build();

GlanceApi apiWhenNoExist = requestsSendResponses(keystoneAuthWithUsernameAndPassword,
responseWithKeystoneAccess, versionNegotiationRequest, versionNegotiationResponse,
list, listResponse);

assertTrue(apiWhenNoExist.getImageApi("az-1.region-a.geo-1").list().concat().isEmpty());
}

public void testListInDetailWhenResponseIs2xx() throws Exception {
HttpRequest listInDetail = HttpRequest.builder().method("GET")
.endpoint("https://glance.jclouds.org:9292/v1.0/images/detail")
@@ -99,21 +80,6 @@ public void testListInDetailWhenResponseIs2xx() throws Exception {
new ParseImagesInDetailTest().expected().toString());
}

public void testListInDetailWhenReponseIs404IsEmpty() throws Exception {
HttpRequest listInDetail = HttpRequest.builder().method("GET")
.endpoint("https://glance.jclouds.org:9292/v1.0/images/detail")
.addHeader("Accept", "application/json")
.addHeader("X-Auth-Token", authToken).build();

HttpResponse listInDetailResponse = HttpResponse.builder().statusCode(404).build();

GlanceApi apiWhenNoExistInDetail = requestsSendResponses(keystoneAuthWithUsernameAndPassword,
responseWithKeystoneAccess, versionNegotiationRequest, versionNegotiationResponse,
listInDetail, listInDetailResponse);

assertTrue(apiWhenNoExistInDetail.getImageApi("az-1.region-a.geo-1").listInDetail().concat().isEmpty());
}

public void testShowWhenResponseIs2xx() throws Exception {
HttpRequest show = HttpRequest.builder().method("HEAD")
.endpoint("https://glance.jclouds.org:9292/v1.0/images/fcc451d0-f6e4-4824-ad8f-70ec12326d07")
@@ -132,23 +98,6 @@ public void testShowWhenResponseIs2xx() throws Exception {
new ParseImageDetailsFromHeadersTest().expected().toString());
}

public void testShowWhenReponseIs404IsNull() throws Exception {
HttpRequest show = HttpRequest.builder().method("HEAD")
.endpoint("https://glance.jclouds.org:9292/v1.0/images/fcc451d0-f6e4-4824-ad8f-70ec12326d07")
.addHeader("Accept", MediaType.APPLICATION_JSON)
.addHeader("X-Auth-Token", authToken).build();


HttpResponse showResponse = HttpResponse.builder().statusCode(404).build();

GlanceApi apiWhenNoExist = requestsSendResponses(keystoneAuthWithUsernameAndPassword,
responseWithKeystoneAccess, versionNegotiationRequest, versionNegotiationResponse,
show, showResponse);

assertNull(apiWhenNoExist.getImageApi("az-1.region-a.geo-1").get("fcc451d0-f6e4-4824-ad8f-70ec12326d07"));
}


public void testGetAsStreamWhenResponseIs2xx() throws Exception {
HttpRequest get = HttpRequest.builder().method("GET")
.endpoint("https://glance.jclouds.org:9292/v1.0/images/fcc451d0-f6e4-4824-ad8f-70ec12326d07")
@@ -167,21 +116,6 @@ public void testGetAsStreamWhenResponseIs2xx() throws Exception {
"foo");
}

public void testGetAsStreamWhenReponseIs404IsNull() throws Exception {
HttpRequest get = HttpRequest.builder().method("GET")
.endpoint("https://glance.jclouds.org:9292/v1.0/images/fcc451d0-f6e4-4824-ad8f-70ec12326d07")
.addHeader("Accept", MediaType.APPLICATION_JSON)
.addHeader("X-Auth-Token", authToken).build();

HttpResponse getResponse = HttpResponse.builder().statusCode(404).build();

GlanceApi apiWhenNoExist = requestsSendResponses(keystoneAuthWithUsernameAndPassword,
responseWithKeystoneAccess, versionNegotiationRequest, versionNegotiationResponse,
get, getResponse);

assertNull(apiWhenNoExist.getImageApi("az-1.region-a.geo-1").getAsStream("fcc451d0-f6e4-4824-ad8f-70ec12326d07"));
}

public void testCreateWhenResponseIs2xx() throws Exception {
HttpRequest get = HttpRequest.builder().method("POST")
.endpoint("https://glance.jclouds.org:9292/v1.0/images")
@@ -203,27 +137,6 @@ public void testCreateWhenResponseIs2xx() throws Exception {
new ParseImageDetailsTest().expected());
}

@Test(expectedExceptions = AuthorizationException.class)
public void testCreateWhenResponseIs4xx() throws Exception {
HttpRequest get = HttpRequest.builder().method("POST")
.endpoint("https://glance.jclouds.org:9292/v1.0/images")
.addHeader("x-image-meta-name", "test")
.addHeader("Accept", MediaType.APPLICATION_JSON)
.addHeader("X-Auth-Token", authToken)
.payload(payloadFromStringWithContentType("somedata", MediaType.APPLICATION_OCTET_STREAM)).build();

HttpResponse createResponse = HttpResponse.builder().statusCode(403)
.payload(payloadFromResource("/image.json")).build();

GlanceApi apiWhenExist = requestsSendResponses(keystoneAuthWithUsernameAndPassword,
responseWithKeystoneAccess, versionNegotiationRequest, versionNegotiationResponse,
get, createResponse);

assertEquals(apiWhenExist.getConfiguredRegions(), ImmutableSet.of("az-1.region-a.geo-1"));

apiWhenExist.getImageApi("az-1.region-a.geo-1").create("test", new StringPayload("somedata"));
}

public void testImageCreateCopyFrom() throws Exception {
HttpRequest get = HttpRequest.builder().method("POST")
.endpoint("https://glance.jclouds.org:9292/v1.0/images")
@@ -265,26 +178,6 @@ public void testReserveWhenResponseIs2xx() throws Exception {
assertEquals(apiWhenExist.getImageApi("az-1.region-a.geo-1").reserve("test"), new ParseImageDetailsTest().expected());
}

@Test(expectedExceptions = AuthorizationException.class)
public void testReserveWhenResponseIs4xx() throws Exception {
HttpRequest get = HttpRequest.builder().method("POST")
.endpoint("https://glance.jclouds.org:9292/v1.0/images")
.addHeader("x-image-meta-name", "test")
.addHeader("Accept", MediaType.APPLICATION_JSON)
.addHeader("X-Auth-Token", authToken).build();

HttpResponse createResponse = HttpResponse.builder().statusCode(403)
.payload(payloadFromResource("/image.json")).build();

GlanceApi apiWhenExist = requestsSendResponses(keystoneAuthWithUsernameAndPassword,
responseWithKeystoneAccess, versionNegotiationRequest, versionNegotiationResponse,
get, createResponse);

assertEquals(apiWhenExist.getConfiguredRegions(), ImmutableSet.of("az-1.region-a.geo-1"));

apiWhenExist.getImageApi("az-1.region-a.geo-1").reserve("test");
}

public void testUpdateMetadataWhenResponseIs2xx() throws Exception {
HttpRequest get = HttpRequest.builder().method("PUT")
.endpoint("https://glance.jclouds.org:9292/v1.0/images/fcc451d0-f6e4-4824-ad8f-70ec12326d07")
@@ -322,32 +215,6 @@ public void testUpdateMetadataWhenResponseIs2xx() throws Exception {
new ParseImageDetailsTest().expected());
}

@Test(expectedExceptions = ResourceNotFoundException.class)
public void testUpdateMetadataWhenResponseIs4xx() throws Exception {
HttpRequest get = HttpRequest.builder().method("PUT")
.endpoint("https://glance.jclouds.org:9292/v1.0/images/fcc451d0-f6e4-4824-ad8f-70ec12326d07")
.headers(
ImmutableMultimap.<String, String>builder()
.put("Accept", MediaType.APPLICATION_JSON)
.put("X-Image-Meta-Name", "newname")
.put("X-Image-Meta-Is_public", "true")
.put("X-Auth-Token", authToken).build())
.build();

HttpResponse updateResponse = HttpResponse.builder().statusCode(404).build();

GlanceApi apiWhenExist = requestsSendResponses(keystoneAuthWithUsernameAndPassword,
responseWithKeystoneAccess, versionNegotiationRequest, versionNegotiationResponse,
get, updateResponse);

assertEquals(apiWhenExist.getConfiguredRegions(), ImmutableSet.of("az-1.region-a.geo-1"));

apiWhenExist.getImageApi("az-1.region-a.geo-1")
.update("fcc451d0-f6e4-4824-ad8f-70ec12326d07",
UpdateImageOptions.Builder.name("newname"),
UpdateImageOptions.Builder.isPublic(true));
}

public void testUpdateImageWhenResponseIs2xx() throws Exception {
HttpRequest get = HttpRequest.builder().method("PUT")
.endpoint("https://glance.jclouds.org:9292/v1.0/images/fcc451d0-f6e4-4824-ad8f-70ec12326d07")
@@ -395,30 +262,6 @@ public void testUpdateNameAndImageWhenResponseIs2xx() throws Exception {
new StringPayload("somenewdata"), UpdateImageOptions.Builder.name("anothernewname")), new ParseImageDetailsTest().expected());
}

@Test(expectedExceptions = AuthorizationException.class)
public void testUpdateNameAndImageWhenResponseIs4xx() throws Exception {
HttpRequest get = HttpRequest.builder().method("PUT")
.endpoint("https://glance.jclouds.org:9292/v1.0/images/fcc451d0-f6e4-4824-ad8f-70ec12326d07")
.headers(
ImmutableMultimap.<String, String>builder()
.put("Accept", MediaType.APPLICATION_JSON)
.put("X-Image-Meta-Name", "anothernewname")
.put("X-Auth-Token", authToken).build())
.payload(payloadFromStringWithContentType("somenewdata", MediaType.APPLICATION_OCTET_STREAM))
.build();

HttpResponse updateResponse = HttpResponse.builder().statusCode(403).build();

GlanceApi apiWhenExist = requestsSendResponses(keystoneAuthWithUsernameAndPassword,
responseWithKeystoneAccess, versionNegotiationRequest, versionNegotiationResponse,
get, updateResponse);

assertEquals(apiWhenExist.getConfiguredRegions(), ImmutableSet.of("az-1.region-a.geo-1"));

apiWhenExist.getImageApi("az-1.region-a.geo-1").upload("fcc451d0-f6e4-4824-ad8f-70ec12326d07",
new StringPayload("somenewdata"), UpdateImageOptions.Builder.name("anothernewname"));
}

public void testDeleteWhenResponseIs2xx() throws Exception {
HttpRequest get = HttpRequest.builder().method("DELETE")
.endpoint("https://glance.jclouds.org:9292/v1.0/images/fcc451d0-f6e4-4824-ad8f-70ec12326d07")
@@ -435,22 +278,4 @@ public void testDeleteWhenResponseIs2xx() throws Exception {

assertTrue(apiWhenExist.getImageApi("az-1.region-a.geo-1").delete("fcc451d0-f6e4-4824-ad8f-70ec12326d07"));
}

public void testDeleteWhenResponseIs4xx() throws Exception {
HttpRequest get = HttpRequest.builder().method("DELETE")
.endpoint("https://glance.jclouds.org:9292/v1.0/images/fcc451d0-f6e4-4824-ad8f-70ec12326d07")
.addHeader("Accept", MediaType.APPLICATION_JSON)
.addHeader("X-Auth-Token", authToken).build();


HttpResponse getResponse = HttpResponse.builder().statusCode(404).build();

GlanceApi apiWhenExist = requestsSendResponses(keystoneAuthWithUsernameAndPassword,
responseWithKeystoneAccess, versionNegotiationRequest, versionNegotiationResponse,
get, getResponse);

assertEquals(apiWhenExist.getConfiguredRegions(), ImmutableSet.of("az-1.region-a.geo-1"));

assertFalse(apiWhenExist.getImageApi("az-1.region-a.geo-1").delete("fcc451d0-f6e4-4824-ad8f-70ec12326d07"));
}
}
@@ -63,30 +63,4 @@ public void testListTypes() throws Exception {
}
}

public void testListTypesIsEmpty() throws Exception {
MockWebServer server = mockOpenStackServer();
server.enqueue(addCommonHeaders(new MockResponse().setBody(stringFromResource("/access.json"))));
server.enqueue(addCommonHeaders(new MockResponse().setResponseCode(404)));

try {
HeatApi heatApi = api(server.getUrl("/").toString(), "openstack-heat", overrides);
ResourceApi api = heatApi.getResourceApi("RegionOne");

List<String> resourceTypes = api.listTypes();

/*
* Check request
*/
assertThat(server.getRequestCount()).isEqualTo(2);
assertAuthentication(server);
assertRequest(server.takeRequest(), "GET", BASE_URI + "/resource_types");

/*
* Check response
*/
assertThat(resourceTypes).isEmpty();
} finally {
server.shutdown();
}
}
}

0 comments on commit 167312f

Please sign in to comment.