Skip to content

Commit

Permalink
Refactor BaseMatcher<ServiceException> into reusable class
Browse files Browse the repository at this point in the history
Refactor tests that had try...catch to use the ServiceExceptionMatcher.
  • Loading branch information
jcookems committed Oct 5, 2012
1 parent 3fa1e27 commit bbf23b8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 66 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 @@ -22,7 +22,9 @@
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;
Expand All @@ -34,6 +36,9 @@ public class MediaServiceIntegrationTest extends IntegrationTestBase {
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 {
cleanupEnvironment();
Expand Down Expand Up @@ -115,21 +120,9 @@ public void getAssetSuccess() throws Exception {

@Test
public void getAssetFailedWithInvalidId() throws ServiceException {
// Arrange

// Act
try {
service.getAsset(fakeAssetId);
// Should not get here

// Assert
fail();
}
catch (ServiceException ex) {
// Assert
assertEquals("Error code should be not found, because asset does not exist to update", 404,
ex.getHttpStatusCode());
}
thrown.expect(ServiceException.class);
thrown.expect(new ServiceExceptionMatcher(404));
service.getAsset(fakeAssetId);
}

@Test
Expand Down Expand Up @@ -169,18 +162,9 @@ public void updateAssetFailedWithInvalidId() throws ServiceException {
UpdateAssetOptions updateAssetOptions = new UpdateAssetOptions();

// Act

try {
service.updateAsset(fakeAssetId, updateAssetOptions);

// Assert
fail();
}
catch (ServiceException ex) {
// Assert
assertEquals("Error code should be not found, because asset does not exist to update", 404,
ex.getHttpStatusCode());
}
thrown.expect(ServiceException.class);
thrown.expect(new ServiceExceptionMatcher(404));
service.updateAsset(fakeAssetId, updateAssetOptions);
}

@Test
Expand All @@ -201,19 +185,8 @@ public void deleteAssetSuccess() throws Exception {

@Test
public void deleteAssetFailedWithInvalidId() throws ServiceException {
// Arrange

// Act
try {
service.deleteAsset(fakeAssetId);

// Assert
fail();
}
catch (ServiceException ex) {
// Assert
assertEquals("Error code should be not found, because asset does not exist to update", 404,
ex.getHttpStatusCode());
}
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 bbf23b8

Please sign in to comment.