Repository: jclouds-labs Updated Branches: refs/heads/arm-fixes [created] 283762369
http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/features/ResourceGroupApiLiveTest.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/features/ResourceGroupApiLiveTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/features/ResourceGroupApiLiveTest.java deleted file mode 100644 index 1ad8bf3..0000000 --- a/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/features/ResourceGroupApiLiveTest.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * 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.azurecompute.arm.features; - -import com.google.common.base.Predicate; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; - -import java.net.URI; -import java.util.List; - -import org.jclouds.azurecompute.arm.domain.ResourceGroup; -import org.jclouds.azurecompute.arm.functions.ParseJobStatus.JobStatus; -import org.jclouds.azurecompute.arm.internal.BaseAzureComputeApiLiveTest; - -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.assertEquals; -import org.jclouds.util.Predicates2; - - -@Test(groups = "live", testName = "ResourceGroupApiLiveTest") -public class ResourceGroupApiLiveTest extends BaseAzureComputeApiLiveTest { - private String resourcegroup; - - @BeforeClass - @Override - public void setup(){ - super.setup(); - resourcegroup = getResourceGroupName(); - } - - private ResourceGroupApi api() { - return api.getResourceGroupApi(); - } - - @Test(dependsOnMethods = "testCreate") - public void testList() { - final List<ResourceGroup> resourceGroups = api().list(); - - assertTrue(resourceGroups.size() > 0); - - assertTrue(Iterables.any(resourceGroups, new Predicate<ResourceGroup>() { - - @Override - public boolean apply(final ResourceGroup group) { - return resourcegroup.equals(group.name()); - } - })); - } - - @Test(dependsOnMethods = "testCreate") - public void testRead() { - final ResourceGroup group = api().get(resourcegroup); - assertNotNull(group); - assertEquals(group.name(), resourcegroup); - assertEquals(group.location(), LOCATION); - } - - public void testCreate() { - - final ResourceGroup resourceGroup = api().create("jcloudstest", LOCATION, null); - assertEquals(resourceGroup.name(), "jcloudstest"); - assertEquals(resourceGroup.location(), LOCATION); - assertEquals(resourceGroup.tags().size(), 0); - assertTrue(resourceGroup.id().contains("jcloudstest")); - assertEquals(resourceGroup.properties().provisioningState(), "Succeeded"); - } - - @Test(dependsOnMethods = "testCreate") - public void testUpdateWithEmptyTag() { - ImmutableMap<String, String> tags = ImmutableMap.<String, String>builder().build(); - - final ResourceGroup resourceGroup = api().update("jcloudstest", tags); - - assertEquals(resourceGroup.tags().size(), 0); - assertEquals(resourceGroup.properties().provisioningState(), "Succeeded"); - } - - @Test(dependsOnMethods = "testCreate") - public void testUpdateWithTag() { - ImmutableMap<String, String> tags = ImmutableMap.<String, String>builder().put("test1", "value1").build(); - - final ResourceGroup resourceGroup = api().update("jcloudstest", tags); - - assertEquals(resourceGroup.tags().size(), 1); - assertEquals(resourceGroup.properties().provisioningState(), "Succeeded"); - } - - @AfterClass(alwaysRun = true) - public void testDelete() throws Exception { - URI uri = api().delete(resourcegroup); - - if (uri != null){ - assertTrue(uri.toString().contains("api-version")); - assertTrue(uri.toString().contains("operationresults")); - - boolean jobDone = Predicates2.retry(new Predicate<URI>() { - @Override public boolean apply(URI uri) { - return JobStatus.DONE == api.getJobApi().jobStatus(uri); - } - }, 60 * 1 * 1000 /* 1 minute timeout */).apply(uri); - assertTrue(jobDone, "delete operation did not complete in the configured timeout"); - } - - uri = api().delete("jcloudstest"); - if (uri != null){ - assertTrue(uri.toString().contains("api-version")); - assertTrue(uri.toString().contains("operationresults")); - - boolean jobDone = Predicates2.retry(new Predicate<URI>() { - @Override public boolean apply(URI uri) { - return JobStatus.DONE == api.getJobApi().jobStatus(uri); - } - }, 60 * 1 * 1000 /* 1 minute timeout */).apply(uri); - assertTrue(jobDone, "delete operation did not complete in the configured timeout"); - } - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/features/ResourceGroupApiMockTest.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/features/ResourceGroupApiMockTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/features/ResourceGroupApiMockTest.java deleted file mode 100644 index 91cb2b1..0000000 --- a/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/features/ResourceGroupApiMockTest.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * 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.azurecompute.arm.features; - -import static com.google.common.collect.Iterables.isEmpty; -import static com.google.common.collect.Iterables.size; -import static org.testng.Assert.assertNotNull; - -import java.net.URI; -import java.util.List; -import com.google.common.collect.ImmutableMap; - -import org.jclouds.azurecompute.arm.domain.ResourceGroup; -import org.jclouds.azurecompute.arm.internal.BaseAzureComputeApiMockTest; -import org.testng.annotations.Test; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertEquals; - -@Test(groups = "unit", testName = "ResourceGroupApiMockTest", singleThreaded = true) -public class ResourceGroupApiMockTest extends BaseAzureComputeApiMockTest { - - final String subscriptionid = "12345678-1234-1234-1234-123456789012"; - final String requestUrl = "/subscriptions/" + subscriptionid + "/resourcegroups"; - final String version = "?api-version=2015-01-01"; - - public void testListResourceGroups() throws InterruptedException { - server.enqueue(jsonResponse("/resourcegroups.json")); - - List<ResourceGroup> resourceGroups = api.getResourceGroupApi().list(); - - assertEquals(size(resourceGroups), 2); - - assertSent(server, "GET", requestUrl + version); - } - - public void testListResourceGroupsReturns404() throws InterruptedException { - server.enqueue(response404()); - - List<ResourceGroup> resourceGroups = api.getResourceGroupApi().list(); - - assertTrue(isEmpty(resourceGroups)); - - assertEquals(server.getRequestCount(), 1); - assertSent(server, "GET", requestUrl + version); - } - - public void testCreateResourceGroup() throws InterruptedException { - server.enqueue(jsonResponse("/resourcegroup.json").setStatus("HTTP/1.1 201 Created")); - - ImmutableMap<String, String> tags = ImmutableMap.<String, String>builder().put("tagname1", "tagvalue1").build(); - - ResourceGroup resourceGroup = api.getResourceGroupApi().create("jcloudstest", "West US", tags); - - assertEquals(resourceGroup.name(), "jcloudstest"); - assertEquals(resourceGroup.location(), "westus"); - assertEquals(resourceGroup.tags().size(), 1); - assertTrue(resourceGroup.id().contains("jcloudstest")); - - assertEquals(server.getRequestCount(), 1); - assertSent(server, "PUT", requestUrl + "/jcloudstest" + version, String.format("{\"location\":\"%s\", \"tags\":{\"tagname1\":\"tagvalue1\"}}", "West US")); - } - - public void testCreateResourceGroupWithNoTag() throws InterruptedException { - server.enqueue(jsonResponse("/resourcegroup.json").setStatus("HTTP/1.1 201 Created")); - - ResourceGroup resourceGroup = api.getResourceGroupApi().create("jcloudstest", "West US", null); - - assertEquals(resourceGroup.name(), "jcloudstest"); - assertEquals(resourceGroup.location(), "westus"); - assertTrue(resourceGroup.id().contains("jcloudstest")); - - assertEquals(server.getRequestCount(), 1); - assertSent(server, "PUT", requestUrl + "/jcloudstest" + version, String.format("{\"location\":\"%s\"}", "West US")); - } - - public void testGetResourceGroup() throws InterruptedException { - server.enqueue(jsonResponse("/resourcegroup.json")); - - ResourceGroup resourceGroup = api.getResourceGroupApi().get("jcloudstest"); - - assertEquals(resourceGroup.name(), "jcloudstest"); - - assertEquals(server.getRequestCount(), 1); - assertSent(server, "GET", requestUrl + "/jcloudstest" + version); - } - - public void testGetResourceGroupReturns404() throws InterruptedException { - server.enqueue(response404()); - - ResourceGroup resourceGroup = api.getResourceGroupApi().get("jcloudstest"); - - assertNull(resourceGroup); - - assertEquals(server.getRequestCount(), 1); - assertSent(server, "GET", requestUrl + "/jcloudstest" + version); - } - - public void testUpdateResourceGroupTags() throws InterruptedException { - server.enqueue(jsonResponse("/resourcegroupupdated.json")); - - ImmutableMap<String, String> tags = ImmutableMap.<String, String>builder().build(); - - ResourceGroup resourceGroup = api.getResourceGroupApi().update("jcloudstest", tags); - - - assertEquals(resourceGroup.tags().size(), 0); - assertEquals(resourceGroup.properties().provisioningState(), "Succeeded"); - - assertEquals(server.getRequestCount(), 1); - assertSent(server, "PATCH", requestUrl + "/jcloudstest" + version, "{\"tags\":{}}"); - } - - public void testDeleteResourceGroup() throws InterruptedException { - server.enqueue(response202WithHeader()); - - URI uri = api.getResourceGroupApi().delete("jcloudstest"); - - assertEquals(server.getRequestCount(), 1); - assertSent(server, "DELETE", requestUrl + "/jcloudstest" + version); - assertNotNull(uri); - - assertTrue(uri.toString().contains("api-version")); - assertTrue(uri.toString().contains("operationresults")); - } - - public void testDeleteResourceGroupReturns404() throws InterruptedException { - server.enqueue(response404()); - - URI uri = api.getResourceGroupApi().delete("jcloudstest"); - assertNull(uri); - assertEquals(server.getRequestCount(), 1); - assertSent(server, "DELETE", requestUrl + "/jcloudstest" + version); - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/functions/URIParserTest.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/functions/URIParserTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/functions/URIParserTest.java deleted file mode 100644 index f7b234d..0000000 --- a/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/functions/URIParserTest.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.azurecompute.arm.functions; - -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertNotNull; - -import java.net.URI; - -import org.jclouds.http.HttpResponse; -import org.testng.annotations.Test; -import com.google.common.collect.Multimap; -import com.google.common.collect.LinkedHashMultimap; - -@Test(groups = "unit", testName = "URIParserTest") -public class URIParserTest { - - public void testApply() { - URIParser parser = new URIParser(); - Multimap<String, String> headers = LinkedHashMultimap.<String, String> create(); - - URI uri = parser.apply(HttpResponse.builder().statusCode(200).build()); - assertNull(uri); - - try { - uri = parser.apply(HttpResponse.builder().statusCode(202).build()); - } catch (IllegalStateException ex){ - assertNotNull(ex); - } - - headers.put("Location", "https://someuri"); - - uri = parser.apply(HttpResponse.builder().statusCode(202).headers(headers).build()); - assertNotNull(uri); - - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/AbstractAzureComputeApiLiveTest.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/AbstractAzureComputeApiLiveTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/AbstractAzureComputeApiLiveTest.java deleted file mode 100644 index aa0663a..0000000 --- a/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/AbstractAzureComputeApiLiveTest.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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.azurecompute.arm.internal; - -import static com.google.common.base.Preconditions.checkNotNull; -import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.OPERATION_POLL_INITIAL_PERIOD; -import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.OPERATION_POLL_MAX_PERIOD; -import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.OPERATION_TIMEOUT; -import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.TCP_RULE_FORMAT; -import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.TCP_RULE_REGEXP; -import java.util.Properties; -import java.util.Random; - -import org.jclouds.apis.BaseApiLiveTest; -import org.jclouds.azurecompute.arm.AzureComputeApi; -import org.jclouds.azurecompute.arm.AzureComputeProviderMetadata; -import org.jclouds.compute.config.ComputeServiceProperties; -import org.jclouds.providers.ProviderMetadata; - -public abstract class AbstractAzureComputeApiLiveTest extends BaseApiLiveTest<AzureComputeApi> { - - protected static final int RAND = new Random().nextInt(999); - - public AbstractAzureComputeApiLiveTest() { - provider = "azurecompute-arm"; - } - - @Override protected Properties setupProperties() { - Properties properties = super.setupProperties(); - properties.put(ComputeServiceProperties.POLL_INITIAL_PERIOD, 1000); - properties.put(ComputeServiceProperties.POLL_MAX_PERIOD, 10000); - properties.setProperty(OPERATION_TIMEOUT, "60000"); - properties.setProperty(OPERATION_POLL_INITIAL_PERIOD, "5"); - properties.setProperty(OPERATION_POLL_MAX_PERIOD, "15"); - properties.setProperty(TCP_RULE_FORMAT, "tcp_%s-%s"); - properties.setProperty(TCP_RULE_REGEXP, "tcp_\\d{1,5}-\\d{1,5}"); - - // for oauth - AzureLiveTestUtils.defaultProperties(properties); - checkNotNull(setIfTestSystemPropertyPresent(properties, "jclouds.oauth.resource"), "test.jclouds.oauth.resource"); - checkNotNull(setIfTestSystemPropertyPresent(properties, "oauth.endpoint"), "test.oauth.endpoint"); - return properties; - } - - @Override - protected ProviderMetadata createProviderMetadata() { - AzureComputeProviderMetadata pm = AzureComputeProviderMetadata.builder().build(); - String endpoint = null; - if (System.getProperty("test.azurecompute-arm.endpoint") != null){ - endpoint = System.getProperty("test.azurecompute-arm.endpoint"); - pm.toBuilder().endpoint(endpoint); - } - return pm; - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/AzureLiveTestUtils.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/AzureLiveTestUtils.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/AzureLiveTestUtils.java deleted file mode 100644 index c578e84..0000000 --- a/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/AzureLiveTestUtils.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.azurecompute.arm.internal; - -import java.util.Properties; -import static org.jclouds.oauth.v2.config.OAuthProperties.CREDENTIAL_TYPE; -import static org.jclouds.oauth.v2.config.CredentialType.CLIENT_CREDENTIALS_SECRET; - -public class AzureLiveTestUtils { - - public static Properties defaultProperties(Properties properties) { - properties = properties == null ? new Properties() : properties; - properties.put("oauth.identity", "foo"); - properties.put("oauth.credential", "password"); - properties.put("oauth.endpoint", "https://login.microsoftonline.com/oauth2/token"); - properties.put(CREDENTIAL_TYPE, CLIENT_CREDENTIALS_SECRET.toString()); - return properties; - } -} - http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/BaseAzureComputeApiLiveTest.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/BaseAzureComputeApiLiveTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/BaseAzureComputeApiLiveTest.java deleted file mode 100644 index fb2e42a..0000000 --- a/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/BaseAzureComputeApiLiveTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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.azurecompute.arm.internal; -import static org.testng.Assert.assertNotNull; - -import com.google.common.collect.ImmutableMap; -import org.jclouds.azurecompute.arm.domain.ResourceGroup; - -import org.testng.annotations.AfterClass; - -public class BaseAzureComputeApiLiveTest extends AbstractAzureComputeApiLiveTest { - public static final String LOCATION = "westeurope"; - private String resourceGroupName = null; - - protected String getEndpoint() { - String endpoint = null; - if (System.getProperty("test.azurecompute-arm.endpoint") != null) { - endpoint = System.getProperty("test.azurecompute-arm.endpoint"); - } - assertNotNull(endpoint); - return endpoint; - } - - protected String getResourceGroupName() { - if (resourceGroupName == null) { - resourceGroupName = String.format("%3.24s", - System.getProperty("user.name") + RAND + "groupjclouds"); - createResourceGroup(resourceGroupName); - } - return resourceGroupName; - } - - private void createResourceGroup(String name) { - ImmutableMap<String, String> tags = ImmutableMap.<String, String>builder().build(); - - final ResourceGroup resourceGroup = api.getResourceGroupApi().create( - name, LOCATION, tags); - } - - private void deleteResourceGroup(String name) { - api.getResourceGroupApi().delete(name); - } - - - @AfterClass(alwaysRun = true) - @Override - protected void tearDown() { - super.tearDown(); - deleteResourceGroup(getResourceGroupName()); - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/BaseAzureComputeApiMockTest.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/BaseAzureComputeApiMockTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/BaseAzureComputeApiMockTest.java deleted file mode 100644 index fdbdf70..0000000 --- a/azurecompute-arm/src/test/java/org/jclouds/azurecompute.arm/internal/BaseAzureComputeApiMockTest.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * 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.azurecompute.arm.internal; -import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor; -import static org.testng.Assert.assertEquals; -import static org.jclouds.oauth.v2.config.CredentialType.BEARER_TOKEN_CREDENTIALS; -import static org.jclouds.oauth.v2.config.OAuthProperties.CREDENTIAL_TYPE; - -import java.io.IOException; -import java.util.Properties; -import java.util.Set; - -import org.jclouds.ContextBuilder; -import org.jclouds.concurrent.config.ExecutorServiceModule; -import org.jclouds.azurecompute.arm.AzureComputeApi; -import org.jclouds.azurecompute.arm.AzureComputeProviderMetadata; -import org.jclouds.json.Json; -import org.jclouds.rest.ApiContext; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; - -import com.google.common.base.Charsets; -import com.google.common.base.Throwables; -import com.google.common.collect.ImmutableSet; -import com.google.common.io.Resources; -import com.google.gson.JsonParser; -import com.google.inject.Module; -import com.squareup.okhttp.mockwebserver.MockResponse; -import com.squareup.okhttp.mockwebserver.MockWebServer; -import com.squareup.okhttp.mockwebserver.RecordedRequest; - -public class BaseAzureComputeApiMockTest { - - private static final String MOCK_BEARER_TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik1uQ19WWmNBVGZNNXBPWWlKSE1iYTlnb0VLWSIsImtpZCI6Ik1uQ19WWmNBVGZNNXBPWWlKSE1iYTlnb0VLWSJ9"; - private static final String DEFAULT_ENDPOINT = new AzureComputeProviderMetadata().getEndpoint(); - - private final Set<Module> modules = ImmutableSet.<Module> of(new ExecutorServiceModule(sameThreadExecutor())); - - protected MockWebServer server; - protected AzureComputeApi api; - private Json json; - - // So that we can ignore formatting. - private final JsonParser parser = new JsonParser(); - - @BeforeMethod - public void start() throws IOException { - server = new MockWebServer(); - server.play(); - Properties properties = new Properties(); - properties.put(CREDENTIAL_TYPE, BEARER_TOKEN_CREDENTIALS.toString()); - AzureComputeProviderMetadata pm = AzureComputeProviderMetadata.builder().build(); - ApiContext<AzureComputeApi> ctx = ContextBuilder.newBuilder(pm) - .credentials("", MOCK_BEARER_TOKEN) - .endpoint(server.getUrl("/").toString() + "subscriptions/12345678-1234-1234-1234-123456789012") - .modules(modules) - .overrides(properties) - .build(); - json = ctx.utils().injector().getInstance(Json.class); - api = ctx.getApi(); - } - - @AfterMethod(alwaysRun = true) - public void stop() throws IOException { - server.shutdown(); - api.close(); - } - - protected String url(String path) { - return server.getUrl(path).toString(); - } - - protected MockResponse jsonResponse(String resource) { - 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 MockResponse response200() { - return new MockResponse().setStatus("HTTP/1.1 200 OK"); - } - - protected MockResponse response202() { - return new MockResponse().setStatus("HTTP/1.1 202 Accepted"); - } - - protected MockResponse response202WithHeader() { - return new MockResponse() - .setStatus("HTTP/1.1 202 Accepted") - .addHeader("Location", "https://management.azure.com/subscriptions/12345678-1234-1234-1234-123456789012/operationresults/eyJqb2JJZCI6IlJFU09VUkNFR1JPVVBERUxFVElPTkpPQi1SVEVTVC1DRU5UUkFMVVMiLCJqb2JMb2NhdGlvbiI6ImNlbnRyYWx1cyJ9?api-version=2014-04-01"); - } - - protected String stringFromResource(String resourceName) { - try { - return Resources.toString(getClass().getResource(resourceName), Charsets.UTF_8) - .replace(DEFAULT_ENDPOINT, url("")); - } catch (IOException e) { - throw Throwables.propagate(e); - } - } - - protected RecordedRequest assertSent(MockWebServer server, String method, String path) throws InterruptedException { - RecordedRequest request = server.takeRequest(); - assertEquals(request.getMethod(), method); - assertEquals(request.getPath(), path); - assertEquals(request.getHeader("Accept"), "application/json"); - assertEquals(request.getHeader("Authorization"), "Bearer " + MOCK_BEARER_TOKEN); - return request; - } - - protected RecordedRequest assertSent(MockWebServer server, String method, String path, String json) - throws InterruptedException { - RecordedRequest request = assertSent(server, method, path); - assertEquals(request.getHeader("Content-Type"), "application/json"); - assertEquals(parser.parse(new String(request.getBody(), Charsets.UTF_8)), parser.parse(json)); - return request; - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/AzureComputeProviderMetadataTest.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/AzureComputeProviderMetadataTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/AzureComputeProviderMetadataTest.java new file mode 100644 index 0000000..2003f9a --- /dev/null +++ b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/AzureComputeProviderMetadataTest.java @@ -0,0 +1,28 @@ +/* + * 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.azurecompute.arm; + +import org.jclouds.providers.internal.BaseProviderMetadataTest; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "AzureManagementProviderMetadataTest") +public class AzureComputeProviderMetadataTest extends BaseProviderMetadataTest { + + public AzureComputeProviderMetadataTest() { + super(new AzureComputeProviderMetadata(), new AzureManagementApiMetadata()); + } +} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/JobApiMockTest.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/JobApiMockTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/JobApiMockTest.java new file mode 100644 index 0000000..ba87ca5 --- /dev/null +++ b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/JobApiMockTest.java @@ -0,0 +1,62 @@ +/* + * 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.azurecompute.arm.features; + +import java.io.IOException; +import java.net.URI; +import org.jclouds.azurecompute.arm.functions.ParseJobStatus.JobStatus; +import org.jclouds.azurecompute.arm.internal.BaseAzureComputeApiMockTest; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; + +@Test(groups = "unit", testName = "JobApiMockTest", singleThreaded = true) +public class JobApiMockTest extends BaseAzureComputeApiMockTest { + + final String requestUrl = "/operationresults/eyJqb2JJZCI6IlJFU09VUkNFR1JPVVBERUxFVElPTkpPQi1SVEVTVC1DRU5UUkFMVVMiLCJqb2JMb2NhdGlvbiI6ImNlbnRyYWx1cyJ9?api-version=2014-04-01"; + + public void testGetJobStatus() throws IOException, InterruptedException { + server.enqueue(response200()); + + JobStatus status = api.getJobApi().jobStatus(URI.create(requestUrl)); + + assertEquals(status, JobStatus.DONE); + + assertSent(server, "GET", requestUrl); + } + + public void testGetJobStatusInProgress() throws InterruptedException { + server.enqueue(response202WithHeader()); + + JobStatus status = api.getJobApi().jobStatus(URI.create(requestUrl)); + + assertEquals(status, JobStatus.IN_PROGRESS); + + assertSent(server, "GET", requestUrl); + } + + public void testGetJobStatusFailed() throws InterruptedException { + server.enqueue(jsonResponse("/resourcegroup.json").setStatus("HTTP/1.1 204 No Content")); + + JobStatus status = api.getJobApi().jobStatus(URI.create(requestUrl)); + + assertEquals(status, JobStatus.FAILED); + + assertSent(server, "GET", requestUrl); + } + +} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiLiveTest.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiLiveTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiLiveTest.java new file mode 100644 index 0000000..1ad8bf3 --- /dev/null +++ b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiLiveTest.java @@ -0,0 +1,137 @@ +/* + * 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.azurecompute.arm.features; + +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; + +import java.net.URI; +import java.util.List; + +import org.jclouds.azurecompute.arm.domain.ResourceGroup; +import org.jclouds.azurecompute.arm.functions.ParseJobStatus.JobStatus; +import org.jclouds.azurecompute.arm.internal.BaseAzureComputeApiLiveTest; + +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.assertEquals; +import org.jclouds.util.Predicates2; + + +@Test(groups = "live", testName = "ResourceGroupApiLiveTest") +public class ResourceGroupApiLiveTest extends BaseAzureComputeApiLiveTest { + private String resourcegroup; + + @BeforeClass + @Override + public void setup(){ + super.setup(); + resourcegroup = getResourceGroupName(); + } + + private ResourceGroupApi api() { + return api.getResourceGroupApi(); + } + + @Test(dependsOnMethods = "testCreate") + public void testList() { + final List<ResourceGroup> resourceGroups = api().list(); + + assertTrue(resourceGroups.size() > 0); + + assertTrue(Iterables.any(resourceGroups, new Predicate<ResourceGroup>() { + + @Override + public boolean apply(final ResourceGroup group) { + return resourcegroup.equals(group.name()); + } + })); + } + + @Test(dependsOnMethods = "testCreate") + public void testRead() { + final ResourceGroup group = api().get(resourcegroup); + assertNotNull(group); + assertEquals(group.name(), resourcegroup); + assertEquals(group.location(), LOCATION); + } + + public void testCreate() { + + final ResourceGroup resourceGroup = api().create("jcloudstest", LOCATION, null); + assertEquals(resourceGroup.name(), "jcloudstest"); + assertEquals(resourceGroup.location(), LOCATION); + assertEquals(resourceGroup.tags().size(), 0); + assertTrue(resourceGroup.id().contains("jcloudstest")); + assertEquals(resourceGroup.properties().provisioningState(), "Succeeded"); + } + + @Test(dependsOnMethods = "testCreate") + public void testUpdateWithEmptyTag() { + ImmutableMap<String, String> tags = ImmutableMap.<String, String>builder().build(); + + final ResourceGroup resourceGroup = api().update("jcloudstest", tags); + + assertEquals(resourceGroup.tags().size(), 0); + assertEquals(resourceGroup.properties().provisioningState(), "Succeeded"); + } + + @Test(dependsOnMethods = "testCreate") + public void testUpdateWithTag() { + ImmutableMap<String, String> tags = ImmutableMap.<String, String>builder().put("test1", "value1").build(); + + final ResourceGroup resourceGroup = api().update("jcloudstest", tags); + + assertEquals(resourceGroup.tags().size(), 1); + assertEquals(resourceGroup.properties().provisioningState(), "Succeeded"); + } + + @AfterClass(alwaysRun = true) + public void testDelete() throws Exception { + URI uri = api().delete(resourcegroup); + + if (uri != null){ + assertTrue(uri.toString().contains("api-version")); + assertTrue(uri.toString().contains("operationresults")); + + boolean jobDone = Predicates2.retry(new Predicate<URI>() { + @Override public boolean apply(URI uri) { + return JobStatus.DONE == api.getJobApi().jobStatus(uri); + } + }, 60 * 1 * 1000 /* 1 minute timeout */).apply(uri); + assertTrue(jobDone, "delete operation did not complete in the configured timeout"); + } + + uri = api().delete("jcloudstest"); + if (uri != null){ + assertTrue(uri.toString().contains("api-version")); + assertTrue(uri.toString().contains("operationresults")); + + boolean jobDone = Predicates2.retry(new Predicate<URI>() { + @Override public boolean apply(URI uri) { + return JobStatus.DONE == api.getJobApi().jobStatus(uri); + } + }, 60 * 1 * 1000 /* 1 minute timeout */).apply(uri); + assertTrue(jobDone, "delete operation did not complete in the configured timeout"); + } + } +} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiMockTest.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiMockTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiMockTest.java new file mode 100644 index 0000000..91cb2b1 --- /dev/null +++ b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/features/ResourceGroupApiMockTest.java @@ -0,0 +1,150 @@ +/* + * 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.azurecompute.arm.features; + +import static com.google.common.collect.Iterables.isEmpty; +import static com.google.common.collect.Iterables.size; +import static org.testng.Assert.assertNotNull; + +import java.net.URI; +import java.util.List; +import com.google.common.collect.ImmutableMap; + +import org.jclouds.azurecompute.arm.domain.ResourceGroup; +import org.jclouds.azurecompute.arm.internal.BaseAzureComputeApiMockTest; +import org.testng.annotations.Test; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertEquals; + +@Test(groups = "unit", testName = "ResourceGroupApiMockTest", singleThreaded = true) +public class ResourceGroupApiMockTest extends BaseAzureComputeApiMockTest { + + final String subscriptionid = "12345678-1234-1234-1234-123456789012"; + final String requestUrl = "/subscriptions/" + subscriptionid + "/resourcegroups"; + final String version = "?api-version=2015-01-01"; + + public void testListResourceGroups() throws InterruptedException { + server.enqueue(jsonResponse("/resourcegroups.json")); + + List<ResourceGroup> resourceGroups = api.getResourceGroupApi().list(); + + assertEquals(size(resourceGroups), 2); + + assertSent(server, "GET", requestUrl + version); + } + + public void testListResourceGroupsReturns404() throws InterruptedException { + server.enqueue(response404()); + + List<ResourceGroup> resourceGroups = api.getResourceGroupApi().list(); + + assertTrue(isEmpty(resourceGroups)); + + assertEquals(server.getRequestCount(), 1); + assertSent(server, "GET", requestUrl + version); + } + + public void testCreateResourceGroup() throws InterruptedException { + server.enqueue(jsonResponse("/resourcegroup.json").setStatus("HTTP/1.1 201 Created")); + + ImmutableMap<String, String> tags = ImmutableMap.<String, String>builder().put("tagname1", "tagvalue1").build(); + + ResourceGroup resourceGroup = api.getResourceGroupApi().create("jcloudstest", "West US", tags); + + assertEquals(resourceGroup.name(), "jcloudstest"); + assertEquals(resourceGroup.location(), "westus"); + assertEquals(resourceGroup.tags().size(), 1); + assertTrue(resourceGroup.id().contains("jcloudstest")); + + assertEquals(server.getRequestCount(), 1); + assertSent(server, "PUT", requestUrl + "/jcloudstest" + version, String.format("{\"location\":\"%s\", \"tags\":{\"tagname1\":\"tagvalue1\"}}", "West US")); + } + + public void testCreateResourceGroupWithNoTag() throws InterruptedException { + server.enqueue(jsonResponse("/resourcegroup.json").setStatus("HTTP/1.1 201 Created")); + + ResourceGroup resourceGroup = api.getResourceGroupApi().create("jcloudstest", "West US", null); + + assertEquals(resourceGroup.name(), "jcloudstest"); + assertEquals(resourceGroup.location(), "westus"); + assertTrue(resourceGroup.id().contains("jcloudstest")); + + assertEquals(server.getRequestCount(), 1); + assertSent(server, "PUT", requestUrl + "/jcloudstest" + version, String.format("{\"location\":\"%s\"}", "West US")); + } + + public void testGetResourceGroup() throws InterruptedException { + server.enqueue(jsonResponse("/resourcegroup.json")); + + ResourceGroup resourceGroup = api.getResourceGroupApi().get("jcloudstest"); + + assertEquals(resourceGroup.name(), "jcloudstest"); + + assertEquals(server.getRequestCount(), 1); + assertSent(server, "GET", requestUrl + "/jcloudstest" + version); + } + + public void testGetResourceGroupReturns404() throws InterruptedException { + server.enqueue(response404()); + + ResourceGroup resourceGroup = api.getResourceGroupApi().get("jcloudstest"); + + assertNull(resourceGroup); + + assertEquals(server.getRequestCount(), 1); + assertSent(server, "GET", requestUrl + "/jcloudstest" + version); + } + + public void testUpdateResourceGroupTags() throws InterruptedException { + server.enqueue(jsonResponse("/resourcegroupupdated.json")); + + ImmutableMap<String, String> tags = ImmutableMap.<String, String>builder().build(); + + ResourceGroup resourceGroup = api.getResourceGroupApi().update("jcloudstest", tags); + + + assertEquals(resourceGroup.tags().size(), 0); + assertEquals(resourceGroup.properties().provisioningState(), "Succeeded"); + + assertEquals(server.getRequestCount(), 1); + assertSent(server, "PATCH", requestUrl + "/jcloudstest" + version, "{\"tags\":{}}"); + } + + public void testDeleteResourceGroup() throws InterruptedException { + server.enqueue(response202WithHeader()); + + URI uri = api.getResourceGroupApi().delete("jcloudstest"); + + assertEquals(server.getRequestCount(), 1); + assertSent(server, "DELETE", requestUrl + "/jcloudstest" + version); + assertNotNull(uri); + + assertTrue(uri.toString().contains("api-version")); + assertTrue(uri.toString().contains("operationresults")); + } + + public void testDeleteResourceGroupReturns404() throws InterruptedException { + server.enqueue(response404()); + + URI uri = api.getResourceGroupApi().delete("jcloudstest"); + assertNull(uri); + assertEquals(server.getRequestCount(), 1); + assertSent(server, "DELETE", requestUrl + "/jcloudstest" + version); + } + +} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/functions/URIParserTest.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/functions/URIParserTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/functions/URIParserTest.java new file mode 100644 index 0000000..f7b234d --- /dev/null +++ b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/functions/URIParserTest.java @@ -0,0 +1,51 @@ +/* + * 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.azurecompute.arm.functions; + +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertNotNull; + +import java.net.URI; + +import org.jclouds.http.HttpResponse; +import org.testng.annotations.Test; +import com.google.common.collect.Multimap; +import com.google.common.collect.LinkedHashMultimap; + +@Test(groups = "unit", testName = "URIParserTest") +public class URIParserTest { + + public void testApply() { + URIParser parser = new URIParser(); + Multimap<String, String> headers = LinkedHashMultimap.<String, String> create(); + + URI uri = parser.apply(HttpResponse.builder().statusCode(200).build()); + assertNull(uri); + + try { + uri = parser.apply(HttpResponse.builder().statusCode(202).build()); + } catch (IllegalStateException ex){ + assertNotNull(ex); + } + + headers.put("Location", "https://someuri"); + + uri = parser.apply(HttpResponse.builder().statusCode(202).headers(headers).build()); + assertNotNull(uri); + + } +} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/AbstractAzureComputeApiLiveTest.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/AbstractAzureComputeApiLiveTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/AbstractAzureComputeApiLiveTest.java new file mode 100644 index 0000000..aa0663a --- /dev/null +++ b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/AbstractAzureComputeApiLiveTest.java @@ -0,0 +1,69 @@ +/* + * 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.azurecompute.arm.internal; + +import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.OPERATION_POLL_INITIAL_PERIOD; +import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.OPERATION_POLL_MAX_PERIOD; +import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.OPERATION_TIMEOUT; +import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.TCP_RULE_FORMAT; +import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.TCP_RULE_REGEXP; +import java.util.Properties; +import java.util.Random; + +import org.jclouds.apis.BaseApiLiveTest; +import org.jclouds.azurecompute.arm.AzureComputeApi; +import org.jclouds.azurecompute.arm.AzureComputeProviderMetadata; +import org.jclouds.compute.config.ComputeServiceProperties; +import org.jclouds.providers.ProviderMetadata; + +public abstract class AbstractAzureComputeApiLiveTest extends BaseApiLiveTest<AzureComputeApi> { + + protected static final int RAND = new Random().nextInt(999); + + public AbstractAzureComputeApiLiveTest() { + provider = "azurecompute-arm"; + } + + @Override protected Properties setupProperties() { + Properties properties = super.setupProperties(); + properties.put(ComputeServiceProperties.POLL_INITIAL_PERIOD, 1000); + properties.put(ComputeServiceProperties.POLL_MAX_PERIOD, 10000); + properties.setProperty(OPERATION_TIMEOUT, "60000"); + properties.setProperty(OPERATION_POLL_INITIAL_PERIOD, "5"); + properties.setProperty(OPERATION_POLL_MAX_PERIOD, "15"); + properties.setProperty(TCP_RULE_FORMAT, "tcp_%s-%s"); + properties.setProperty(TCP_RULE_REGEXP, "tcp_\\d{1,5}-\\d{1,5}"); + + // for oauth + AzureLiveTestUtils.defaultProperties(properties); + checkNotNull(setIfTestSystemPropertyPresent(properties, "jclouds.oauth.resource"), "test.jclouds.oauth.resource"); + checkNotNull(setIfTestSystemPropertyPresent(properties, "oauth.endpoint"), "test.oauth.endpoint"); + return properties; + } + + @Override + protected ProviderMetadata createProviderMetadata() { + AzureComputeProviderMetadata pm = AzureComputeProviderMetadata.builder().build(); + String endpoint = null; + if (System.getProperty("test.azurecompute-arm.endpoint") != null){ + endpoint = System.getProperty("test.azurecompute-arm.endpoint"); + pm.toBuilder().endpoint(endpoint); + } + return pm; + } +} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/AzureLiveTestUtils.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/AzureLiveTestUtils.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/AzureLiveTestUtils.java new file mode 100644 index 0000000..c578e84 --- /dev/null +++ b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/AzureLiveTestUtils.java @@ -0,0 +1,34 @@ +/* + * 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.azurecompute.arm.internal; + +import java.util.Properties; +import static org.jclouds.oauth.v2.config.OAuthProperties.CREDENTIAL_TYPE; +import static org.jclouds.oauth.v2.config.CredentialType.CLIENT_CREDENTIALS_SECRET; + +public class AzureLiveTestUtils { + + public static Properties defaultProperties(Properties properties) { + properties = properties == null ? new Properties() : properties; + properties.put("oauth.identity", "foo"); + properties.put("oauth.credential", "password"); + properties.put("oauth.endpoint", "https://login.microsoftonline.com/oauth2/token"); + properties.put(CREDENTIAL_TYPE, CLIENT_CREDENTIALS_SECRET.toString()); + return properties; + } +} + http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/BaseAzureComputeApiLiveTest.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/BaseAzureComputeApiLiveTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/BaseAzureComputeApiLiveTest.java new file mode 100644 index 0000000..fb2e42a --- /dev/null +++ b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/BaseAzureComputeApiLiveTest.java @@ -0,0 +1,65 @@ +/* + * 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.azurecompute.arm.internal; +import static org.testng.Assert.assertNotNull; + +import com.google.common.collect.ImmutableMap; +import org.jclouds.azurecompute.arm.domain.ResourceGroup; + +import org.testng.annotations.AfterClass; + +public class BaseAzureComputeApiLiveTest extends AbstractAzureComputeApiLiveTest { + public static final String LOCATION = "westeurope"; + private String resourceGroupName = null; + + protected String getEndpoint() { + String endpoint = null; + if (System.getProperty("test.azurecompute-arm.endpoint") != null) { + endpoint = System.getProperty("test.azurecompute-arm.endpoint"); + } + assertNotNull(endpoint); + return endpoint; + } + + protected String getResourceGroupName() { + if (resourceGroupName == null) { + resourceGroupName = String.format("%3.24s", + System.getProperty("user.name") + RAND + "groupjclouds"); + createResourceGroup(resourceGroupName); + } + return resourceGroupName; + } + + private void createResourceGroup(String name) { + ImmutableMap<String, String> tags = ImmutableMap.<String, String>builder().build(); + + final ResourceGroup resourceGroup = api.getResourceGroupApi().create( + name, LOCATION, tags); + } + + private void deleteResourceGroup(String name) { + api.getResourceGroupApi().delete(name); + } + + + @AfterClass(alwaysRun = true) + @Override + protected void tearDown() { + super.tearDown(); + deleteResourceGroup(getResourceGroupName()); + } +} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/9170aa97/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/BaseAzureComputeApiMockTest.java ---------------------------------------------------------------------- diff --git a/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/BaseAzureComputeApiMockTest.java b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/BaseAzureComputeApiMockTest.java new file mode 100644 index 0000000..ab33ab1 --- /dev/null +++ b/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/internal/BaseAzureComputeApiMockTest.java @@ -0,0 +1,140 @@ +/* + * 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.azurecompute.arm.internal; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor; +import static org.jclouds.oauth.v2.config.CredentialType.BEARER_TOKEN_CREDENTIALS; +import static org.jclouds.oauth.v2.config.OAuthProperties.CREDENTIAL_TYPE; +import static org.testng.Assert.assertEquals; + +import java.io.IOException; +import java.util.Properties; +import java.util.Set; + +import org.jclouds.ContextBuilder; +import org.jclouds.azurecompute.arm.AzureComputeApi; +import org.jclouds.azurecompute.arm.AzureComputeProviderMetadata; +import org.jclouds.concurrent.config.ExecutorServiceModule; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; + +import com.google.common.base.Charsets; +import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableSet; +import com.google.common.io.Resources; +import com.google.gson.JsonParser; +import com.google.inject.Module; +import com.squareup.okhttp.mockwebserver.MockResponse; +import com.squareup.okhttp.mockwebserver.MockWebServer; +import com.squareup.okhttp.mockwebserver.RecordedRequest; + +public class BaseAzureComputeApiMockTest { + + private static final String MOCK_BEARER_TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik1uQ19WWmNBVGZNNXBPWWlKSE1iYTlnb0VLWSIsImtpZCI6Ik1uQ19WWmNBVGZNNXBPWWlKSE1iYTlnb0VLWSJ9"; + private static final String DEFAULT_ENDPOINT = new AzureComputeProviderMetadata().getEndpoint(); + + private final Set<Module> modules = ImmutableSet.<Module> of(new ExecutorServiceModule(sameThreadExecutor())); + + protected MockWebServer server; + protected AzureComputeApi api; + + // So that we can ignore formatting. + private final JsonParser parser = new JsonParser(); + + @BeforeMethod + public void start() throws IOException { + server = new MockWebServer(); + server.play(); + Properties properties = new Properties(); + checkNotNull(setIfTestSystemPropertyPresent(properties, "oauth.endpoint"), "test.oauth.endpoint"); + properties.put(CREDENTIAL_TYPE, BEARER_TOKEN_CREDENTIALS.toString()); + AzureComputeProviderMetadata pm = AzureComputeProviderMetadata.builder().build(); + api = ContextBuilder.newBuilder(pm) + .credentials("", MOCK_BEARER_TOKEN) + .endpoint(server.getUrl("/").toString() + "subscriptions/12345678-1234-1234-1234-123456789012") + .modules(modules) + .overrides(properties) + .buildApi(AzureComputeApi.class); + } + + @AfterMethod(alwaysRun = true) + public void stop() throws IOException { + server.shutdown(); + api.close(); + } + + protected String url(String path) { + return server.getUrl(path).toString(); + } + + protected MockResponse jsonResponse(String resource) { + 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 MockResponse response200() { + return new MockResponse().setStatus("HTTP/1.1 200 OK"); + } + + protected MockResponse response202() { + return new MockResponse().setStatus("HTTP/1.1 202 Accepted"); + } + + protected MockResponse response202WithHeader() { + return new MockResponse() + .setStatus("HTTP/1.1 202 Accepted") + .addHeader("Location", "https://management.azure.com/subscriptions/12345678-1234-1234-1234-123456789012/operationresults/eyJqb2JJZCI6IlJFU09VUkNFR1JPVVBERUxFVElPTkpPQi1SVEVTVC1DRU5UUkFMVVMiLCJqb2JMb2NhdGlvbiI6ImNlbnRyYWx1cyJ9?api-version=2014-04-01"); + } + + protected String stringFromResource(String resourceName) { + try { + return Resources.toString(getClass().getResource(resourceName), Charsets.UTF_8) + .replace(DEFAULT_ENDPOINT, url("")); + } catch (IOException e) { + throw Throwables.propagate(e); + } + } + + protected RecordedRequest assertSent(MockWebServer server, String method, String path) throws InterruptedException { + RecordedRequest request = server.takeRequest(); + assertEquals(request.getMethod(), method); + assertEquals(request.getPath(), path); + assertEquals(request.getHeader("Accept"), "application/json"); + assertEquals(request.getHeader("Authorization"), "Bearer " + MOCK_BEARER_TOKEN); + return request; + } + + protected RecordedRequest assertSent(MockWebServer server, String method, String path, String json) + throws InterruptedException { + RecordedRequest request = assertSent(server, method, path); + assertEquals(request.getHeader("Content-Type"), "application/json"); + assertEquals(parser.parse(new String(request.getBody(), Charsets.UTF_8)), parser.parse(json)); + return request; + } + + private String setIfTestSystemPropertyPresent(Properties overrides, String key) { + if (System.getProperties().containsKey("test." + key)) { + String val = System.getProperty("test." + key); + overrides.setProperty(key, val); + return val; + } + return null; + } +}
