Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.mockito.Mockito;

import java.net.HttpURLConnection;
Expand All @@ -23,34 +26,43 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Arrays;

@RunWith(Enclosed.class)
public class TestAuthenticatedURL {

@RunWith(Parameterized.class)
public static class TheParameterizedPart {
@Parameterized.Parameter(value = 0)
public String tokenStr;

@Parameterized.Parameters
public static Collection<Object> testData() {
Object[] data = new Object[] {"foo",
"RanDOMstrING",
// "", // invalid input ; fails
"123@456#"};
return Arrays.asList(data);
}

// PUTs #3
@Test
public void testToken() throws Exception {
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
Assert.assertFalse(token.isSet());
token = new AuthenticatedURL.Token("foo");
token = new AuthenticatedURL.Token(tokenStr);
Assert.assertTrue(token.isSet());
Assert.assertEquals("foo", token.toString());
}

@Test
public void testInjectToken() throws Exception {
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
token.set("foo");
AuthenticatedURL.injectToken(conn, token);
Mockito.verify(conn).addRequestProperty(Mockito.eq("Cookie"), Mockito.anyString());
Assert.assertEquals(tokenStr, token.toString());
}

// PUTs #4
@Test
public void testExtractTokenOK() throws Exception {
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);

Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);

String tokenStr = "foo";
Map<String, List<String>> headers = new HashMap<String, List<String>>();
List<String> cookies = new ArrayList<String>();
cookies.add(AuthenticatedURL.AUTH_COOKIE + "=" + tokenStr);
Expand All @@ -63,6 +75,26 @@ public void testExtractTokenOK() throws Exception {
Assert.assertEquals(tokenStr, token.toString());
}

}

public static class NotParameterizedPart {

// CUTs #1
@Test(expected = IllegalArgumentException.class)
public void testEmptyTokenFailure() {
new AuthenticatedURL.Token(null);
}

@Test
public void testInjectToken() throws Exception {
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
token.set("foo");
AuthenticatedURL.injectToken(conn, token);
Mockito.verify(conn).addRequestProperty(Mockito.eq("Cookie"), Mockito.anyString());
}


@Test
public void testExtractTokenFail() throws Exception {
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
Expand Down Expand Up @@ -113,6 +145,7 @@ public void testGetAuthenticator() throws Exception {

AuthenticatedURL aURL = new AuthenticatedURL(authenticator);
Assert.assertEquals(authenticator, aURL.getAuthenticator());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import org.apache.hadoop.security.ProviderUtils;
import org.apache.hadoop.test.GenericTestUtils;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.IOException;
import java.net.URI;
Expand All @@ -35,27 +38,48 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.fail;

@RunWith(Enclosed.class)
public class TestKeyProvider {

private static final String CIPHER = "AES";

@RunWith(Parameterized.class)
public static class TheParameterizedPart {
@Parameterized.Parameter(value = 0)
public String name;
@Parameterized.Parameter(value = 1)
public Integer version;

@Parameterized.Parameters
public static Collection buildVersionName() {
return Arrays.asList(new Object[][] {
{ "/a/b", 3 },
{ "/aaa", 12 },
{ "", 0 },
{ "@#!&^^&$@", -1 },
{ "@@", -99999 }
});
}

// PUTs #15
@Test
public void testBuildVersionName() throws Exception {
assertEquals("/a/b@3", KeyProvider.buildVersionName("/a/b", 3));
assertEquals("/aaa@12", KeyProvider.buildVersionName("/aaa", 12));
assertEquals(name + "@" + version, KeyProvider.buildVersionName(name, version));
}

// PUTs #16
@Test
public void testParseVersionName() throws Exception {
assertEquals("/a/b", KeyProvider.getBaseName("/a/b@3"));
assertEquals("/aaa", KeyProvider.getBaseName("/aaa@112"));
assertEquals(name, KeyProvider.getBaseName(name + "@" + version));
try {
KeyProvider.getBaseName("no-slashes");
assertTrue("should have thrown", false);
Expand All @@ -64,6 +88,10 @@ public void testParseVersionName() throws Exception {
}
}

}

public static class NotParameterizedPart {

@Test
public void testKeyMaterial() throws Exception {
byte[] key1 = new byte[]{1,2,3,4};
Expand Down Expand Up @@ -284,6 +312,7 @@ public void testConfiguration() throws Exception {
conf.set("a", "A");
MyKeyProvider kp = new MyKeyProvider(conf);
Assert.assertEquals("A", kp.getConf().get("a"));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,43 @@
import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

public class TestQuotaUsage {
import java.util.Arrays;
import java.util.Collection;

// check the empty constructor correctly initialises the object
@Test
public void testConstructorEmpty() {
QuotaUsage quotaUsage = new QuotaUsage.Builder().build();
assertEquals("getQuota", -1, quotaUsage.getQuota());
assertEquals("getSpaceConsumed", 0, quotaUsage.getSpaceConsumed());
assertEquals("getSpaceQuota", -1, quotaUsage.getSpaceQuota());
}
@RunWith(Enclosed.class)
public class TestQuotaUsage {

@RunWith(Parameterized.class)
public static class TheParameterizedPart {
@Parameterized.Parameter(value = 0)
public long fileAndDirCount;
@Parameterized.Parameter(value = 1)
public long quota;
@Parameterized.Parameter(value = 2)
public long spaceConsumed;
@Parameterized.Parameter(value = 3)
public long spaceQuota;
@Parameterized.Parameter(value = 4)
public long SSDQuota;

@Parameterized.Parameters
public static Collection<Object> testData() {
Object[][] data = new Object[][] { {22222, 44444, 55555, 66666, 300000},
{22222, 3, 11111, 7, 444444},
{-1, 1, -1, 1, -1},
{222255555, 222256578, 1073741825, 1, 5}
};
return Arrays.asList(data);
}

// PUTs #19
// check the full constructor with quota information
@Test
public void testConstructorWithQuota() {
long fileAndDirCount = 22222;
long quota = 44444;
long spaceConsumed = 55555;
long spaceQuota = 66666;

QuotaUsage quotaUsage = new QuotaUsage.Builder().
fileAndDirectoryCount(fileAndDirCount).quota(quota).
spaceConsumed(spaceConsumed).spaceQuota(spaceQuota).build();
Expand All @@ -51,11 +68,10 @@ public void testConstructorWithQuota() {
assertEquals("getSpaceQuota", spaceQuota, quotaUsage.getSpaceQuota());
}

// PUTs #20
// check the constructor with quota information
@Test
public void testConstructorNoQuota() {
long spaceConsumed = 11111;
long fileAndDirCount = 22222;
QuotaUsage quotaUsage = new QuotaUsage.Builder().
fileAndDirectoryCount(fileAndDirCount).
spaceConsumed(spaceConsumed).build();
Expand All @@ -67,66 +83,33 @@ public void testConstructorNoQuota() {
assertEquals("getSpaceQuota", -1, quotaUsage.getSpaceQuota());
}

// check the header
@Test
public void testGetHeader() {
String header = " QUOTA REM_QUOTA SPACE_QUOTA "
+ "REM_SPACE_QUOTA ";
assertEquals(header, QuotaUsage.getHeader());
}

// PUTs #21
// check the toString method with quotas
@Test
public void testToStringWithQuota() {
long fileAndDirCount = 55555;
long quota = 44444;
long spaceConsumed = 55555;
long spaceQuota = 66665;

QuotaUsage quotaUsage = new QuotaUsage.Builder().
fileAndDirectoryCount(fileAndDirCount).quota(quota).
spaceConsumed(spaceConsumed).spaceQuota(spaceQuota).build();
String expected =" 44444 -11111 66665" +
" 11110 ";
String expected = String.format(QuotaUsage.QUOTA_STRING_FORMAT + QuotaUsage.SPACE_QUOTA_STRING_FORMAT,
quota, (quota-fileAndDirCount), spaceQuota, (spaceQuota - spaceConsumed));
assertEquals(expected, quotaUsage.toString());
}

// PUTs #22
// check the toString method with quotas
@Test
public void testToStringNoQuota() {
QuotaUsage quotaUsage = new QuotaUsage.Builder().
fileAndDirectoryCount(1234).build();
fileAndDirectoryCount(fileAndDirCount).build();
String expected = " none inf none"
+ " inf ";
assertEquals(expected, quotaUsage.toString());
}

// check the toString method with quotas
@Test
public void testToStringHumanWithQuota() {
long fileAndDirCount = 222255555;
long quota = 222256578;
long spaceConsumed = 1073741825;
long spaceQuota = 1;

QuotaUsage quotaUsage = new QuotaUsage.Builder().
fileAndDirectoryCount(fileAndDirCount).quota(quota).
spaceConsumed(spaceConsumed).spaceQuota(spaceQuota).build();
String expected = " 212.0 M 1023 1 "
+ " -1 G ";
assertEquals(expected, quotaUsage.toString(true));
}

// PUTs #23
// check the equality
@Test
public void testCompareQuotaUsage() {
long fileAndDirCount = 222255555;
long quota = 222256578;
long spaceConsumed = 1073741825;
long spaceQuota = 1;
long SSDspaceConsumed = 100000;
long SSDQuota = 300000;

QuotaUsage quotaUsage1 = new QuotaUsage.Builder().
fileAndDirectoryCount(fileAndDirCount).quota(quota).
spaceConsumed(spaceConsumed).spaceQuota(spaceQuota).
Expand All @@ -143,4 +126,42 @@ public void testCompareQuotaUsage() {

assertEquals(quotaUsage1, quotaUsage2);
}
}


public static class NotParameterizedPart {
// check the empty constructor correctly initialises the object
@Test
public void testConstructorEmpty() {
QuotaUsage quotaUsage = new QuotaUsage.Builder().build();
assertEquals("getQuota", -1, quotaUsage.getQuota());
assertEquals("getSpaceConsumed", 0, quotaUsage.getSpaceConsumed());
assertEquals("getSpaceQuota", -1, quotaUsage.getSpaceQuota());
}

// check the header
@Test
public void testGetHeader() {
String header = " QUOTA REM_QUOTA SPACE_QUOTA "
+ "REM_SPACE_QUOTA ";
assertEquals(header, QuotaUsage.getHeader());
}

// check the toString method with quotas
// Can be removed #1, covered in PUTs #21
@Test
public void testToStringHumanWithQuota() {
long fileAndDirCount = 222255555;
long quota = 222256578;
long spaceConsumed = 1073741825;
long spaceQuota = 1;

QuotaUsage quotaUsage = new QuotaUsage.Builder().
fileAndDirectoryCount(fileAndDirCount).quota(quota).
spaceConsumed(spaceConsumed).spaceQuota(spaceQuota).build();
String expected = " 212.0 M 1023 1 "
+ " -1 G ";
assertEquals(expected, quotaUsage.toString(true));
}
}
}
Loading