Skip to content

Commit

Permalink
Merge pull request Azure#1 from jcookems/assetCrudQA
Browse files Browse the repository at this point in the history
Cleanup Asset CRUD tests
Associated with workitem Azure#274
  • Loading branch information
jcookems committed Oct 5, 2012
2 parents 10cbd93 + bbf23b8 commit 5d19359
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.util.EnumSet;
import java.util.List;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
Expand All @@ -39,6 +37,9 @@ public class AccessPolicyIntegrationTest extends IntegrationTestBase {

private static final String testPrefix = "testPolicy";

@Rule
public ExpectedException expected = ExpectedException.none();

@BeforeClass
public static void setup() throws Exception {
service = MediaService.create(createConfig());
Expand Down Expand Up @@ -114,9 +115,6 @@ public void canRetrieveListOfAccessPolicies() throws Exception {
EnumSet.of(AccessPolicyPermission.WRITE, AccessPolicyPermission.LIST)));
}

@Rule
public ExpectedException expected = ExpectedException.none();

@Test
public void getWithBadIdThrowsServiceException() throws Exception {
expected.expect(ServiceException.class);
Expand All @@ -125,26 +123,7 @@ public void getWithBadIdThrowsServiceException() throws Exception {

@Test
public void getWithValidButNonExistentPolicyIdThrows404ServiceException() throws Exception {
expected.expect(new BaseMatcher<ServiceException>() {

@Override
public boolean matches(Object item) {
if (item.getClass() != ServiceException.class) {
return false;
}

if (((ServiceException) item).getHttpStatusCode() != 404) {
return false;
}

return true;
}

@Override
public void describeTo(Description description) {
description.appendText("Must be ServiceException with a 404 status code");
}
});
expected.expect(new ServiceExceptionMatcher(404));
service.getAccessPolicy("nb:pid:UUID:bce3863e-830b-49f5-9199-7cfaff52935f");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,49 @@
import java.util.List;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.microsoft.windowsazure.services.core.Configuration;
import com.microsoft.windowsazure.services.core.ServiceException;
import com.microsoft.windowsazure.services.media.models.AssetInfo;
import com.microsoft.windowsazure.services.media.models.UpdateAssetOptions;

public class MediaServiceIntegrationTest extends IntegrationTestBase {
private static MediaContract service;
private MediaContract service;
private static String testAssetPrefix = "testAsset";
private static String fakeAssetId = "nb:cid:UUID:00000000-0000-4a00-0000-000000000000";

@Rule
public ExpectedException thrown = ExpectedException.none();

@BeforeClass
public static void setup() throws Exception {
// Create all test containers and their content
config = createConfig();
service = MediaService.create(config);
List<AssetInfo> listAssetsResult = service.listAssets(null);
for (AssetInfo assetInfo : listAssetsResult) {
try {
service.deleteAsset(assetInfo.getId());
}
catch (Exception e) {
e.printStackTrace();
}
}
cleanupEnvironment();
}

@AfterClass
public static void cleanup() throws Exception {
// Configuration config = createConfiguration();
// BlobContract service = BlobService.create(config);
cleanupEnvironment();
}

// deleteContainers(service, testContainersPrefix, testContainers);
// deleteContainers(service, createableContainersPrefix, creatableContainers);
private static void cleanupEnvironment() {
config = createConfig();
MediaContract service = MediaService.create(config);
try {
List<AssetInfo> listAssetsResult = service.listAssets();
for (AssetInfo assetInfo : listAssetsResult) {
if (assetInfo.getName().startsWith(testAssetPrefix)) {
service.deleteAsset(assetInfo.getId());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}

private static Configuration createConfig() {
Expand All @@ -66,16 +75,21 @@ private static Configuration createConfig() {
return config;
}

@Before
public void setupInstance() throws Exception {
service = MediaService.create(config);
}

@Test
public void createAssetSuccess() throws Exception {
// Arrange
AssetInfo expectedAsset = new AssetInfo().setName("testAssetName");
String testName = testAssetPrefix + "Name";

// Act
AssetInfo actualAsset = service.createAsset("testAssetName");
AssetInfo actualAsset = service.createAsset(testName);

// Assert
assertEquals(expectedAsset.getName(), actualAsset.getName());
assertEquals("testName and actualAsset name", testName, actualAsset.getName());
}

@Test
Expand All @@ -86,78 +100,71 @@ public void createAssetNullNameSuccess() throws ServiceException {
AssetInfo actualAsset = service.createAsset(null);

// Assert
assertNotNull(actualAsset);
assertNotNull("actualAsset", actualAsset);
assertEquals("actualAsset.getName() should be the service default value, the empty string", "",
actualAsset.getName());
}

@Test
public void getAssetSuccess() throws Exception {
// Arrange
AssetInfo expectedAsset = new AssetInfo().setName("testGetAssetSuccess");
AssetInfo assetInfo = service.createAsset("testGetAssetSuccess");
String testName = testAssetPrefix + "GetAssetSuccess";
AssetInfo assetInfo = service.createAsset(testName);

// Act
AssetInfo actualAsset = service.getAsset(assetInfo.getId());

// Assert
assertEquals(expectedAsset.getName(), actualAsset.getName());
assertEquals(testName, actualAsset.getName());
}

@Test(expected = ServiceException.class)
@Test
public void getAssetFailedWithInvalidId() throws ServiceException {
// Arrange
AssetInfo expectedAsset = new AssetInfo().setId("IncorrectAssetId");

// Act
AssetInfo actualAsset = service.getAsset(expectedAsset.getId());

// Assert
assertTrue(false);

thrown.expect(ServiceException.class);
thrown.expect(new ServiceExceptionMatcher(404));
service.getAsset(fakeAssetId);
}

@Test
public void listAssetSuccess() throws ServiceException {
// Arrange
Collection<AssetInfo> listAssetResultBaseLine = service.listAssets();
AssetInfo assetA = new AssetInfo();
AssetInfo assetB = new AssetInfo();
service.createAsset("assetA");
service.createAsset("assetB");
service.createAsset(testAssetPrefix + "assetA");
service.createAsset(testAssetPrefix + "assetB");

// Act
Collection<AssetInfo> listAssetResult = service.listAssets();
// Assert

// Assert
assertEquals(listAssetResultBaseLine.size() + 2, listAssetResult.size());
}

@Test
public void updateAssetSuccess() throws Exception {
// Arrange
String originalTestName = testAssetPrefix + "UpdateAssetSuccess";
String updatedTestName = testAssetPrefix + "UpdateAssetSuccess";

AssetInfo updatedAsset = service.createAsset("updateAssetSuccess");
UpdateAssetOptions updateAssetOptions = new UpdateAssetOptions().setName("updateAssetSuccessResult");
updatedAsset.setName("updateAssetSuccessResult");
AssetInfo originalAsset = service.createAsset(originalTestName);
UpdateAssetOptions updateAssetOptions = new UpdateAssetOptions().setName(updatedTestName);

// Act
service.updateAsset(updatedAsset.getId(), updateAssetOptions);
AssetInfo actualAsset = service.getAsset(updatedAsset.getId());
service.updateAsset(originalAsset.getId(), updateAssetOptions);
AssetInfo updatedAsset = service.getAsset(originalAsset.getId());

// Assert
assertEquals(updatedAsset.getName(), actualAsset.getName());

assertEquals(updatedTestName, updatedAsset.getName());
}

@Test(expected = ServiceException.class)
@Test
public void updateAssetFailedWithInvalidId() throws ServiceException {
// Arrange
UpdateAssetOptions updateAssetOptions = new UpdateAssetOptions();

// Act
service.updateAsset("updateAssetFailedWithInvalidId", updateAssetOptions);

// Assert
assertTrue(false);
thrown.expect(ServiceException.class);
thrown.expect(new ServiceExceptionMatcher(404));
service.updateAsset(fakeAssetId, updateAssetOptions);
}

@Test
Expand All @@ -176,15 +183,10 @@ public void deleteAssetSuccess() throws Exception {
assertEquals(assetCountBaseline - 1, listAssetsResult.size());
}

@Test(expected = ServiceException.class)
@Test
public void deleteAssetFailedWithInvalidId() throws ServiceException {
// Arrange

// Act
service.deleteAsset("invalidAssetId");

// Assert
assertTrue(false);
thrown.expect(ServiceException.class);
thrown.expect(new ServiceExceptionMatcher(404));
service.deleteAsset(fakeAssetId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.microsoft.windowsazure.services.media;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;

import com.microsoft.windowsazure.services.core.ServiceException;

public class ServiceExceptionMatcher extends BaseMatcher<ServiceException> {
private final int httpStatusCode;

public ServiceExceptionMatcher(int httpStatusCode) {
this.httpStatusCode = httpStatusCode;
}

@Override
public boolean matches(Object item) {
if (item == null || item.getClass() != ServiceException.class) {
return false;
}

if (((ServiceException) item).getHttpStatusCode() != this.httpStatusCode) {
return false;
}

return true;
}

@Override
public void describeTo(Description description) {
description.appendText("Must be ServiceException with status code" + this.httpStatusCode);
}
}

0 comments on commit 5d19359

Please sign in to comment.