Skip to content

Commit

Permalink
Merge pull request #539 from 1010sachin/add_bucketsHelperITClass_branch
Browse files Browse the repository at this point in the history
Add a new IntegrationTestHelper class for buckets.
  • Loading branch information
1010sachin authored Jun 25, 2019
2 parents 464aa05 + 7e28fff commit 9e2f780
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,23 @@ public MantaObjectResponse head(final String rawPath) throws IOException {
return new MantaObjectResponse(path, headers);
}

/**
* Get the options associated with a Manta object.
*
* @param rawPath The fully qualified path of the object. i.e. /user/stor/foo/bar/baz
* @return The {@link MantaObjectResponse}.
* @throws IOException If an IO exception has occurred.
* @throws MantaClientHttpResponseException If a http status code {@literal > 300} is returned.
*/
public MantaObjectResponse options(final String rawPath) throws IOException {
Validate.notBlank(rawPath, "Path must not be empty nor null");

String path = formatPath(rawPath);
final HttpResponse response = httpHelper.httpOptions(path);
final MantaHttpHeaders headers = new MantaHttpHeaders(response.getAllHeaders());
return new MantaObjectResponse(path, headers);
}

/**
* Return a stream of the contents of a directory in Manta as an {@link Iterator}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public interface HttpHelper extends AutoCloseable, HttpConnectionAware {
*/
HttpResponse httpHead(String path) throws IOException;

/**
* Executes a HTTP OPTIONS against the remote Manta API.
*
* @param path The fully qualified path of the object. i.e. /user/stor/foo/bar/baz
* @return Apache HTTP Client response object
* @throws IOException when there is a problem getting the object over the network
*/
HttpResponse httpOptions(String path) throws IOException;

/**
* Executes a HTTP GET against the remote Manta API.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.message.BasicHeader;

Expand Down Expand Up @@ -97,6 +98,17 @@ public HttpDelete delete(final String path) {
return request;
}

/**
* Convenience method used for building OPTIONS operations.
* @param path path to resource
* @return instance of configured {@link org.apache.http.client.methods.HttpRequestBase} object.
*/
public HttpOptions options(final String path) {
final HttpOptions request = new HttpOptions(uriForPath(path));
prepare(request);
return request;
}

/**
* Convenience method used for building DELETE operations.
* @param path path to resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@
import org.apache.http.NameValuePair;
import org.apache.http.ProtocolException;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
Expand Down Expand Up @@ -215,6 +216,16 @@ public HttpResponse httpHead(final String path) throws IOException {
return executeAndCloseRequest(head, "HEAD {} response [{}] {} ");
}

@Override
public HttpResponse httpOptions(final String path) throws IOException {
Validate.notNull(path, "Path must not be null");

LOGGER.debug("OPTIONS {}", path);

final HttpOptions options = requestFactory.options(path);
return executeAndCloseRequest(options, "HEAD {} response [{}] {} ");
}

@Override
public HttpResponse httpGet(final String path) throws IOException {
Validate.notNull(path, "Path must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package com.joyent.manta.client;

import com.joyent.manta.client.helper.IntegrationTestHelper;
import com.joyent.manta.config.ConfigContext;
import com.joyent.manta.config.IntegrationTestConfigContext;
import com.joyent.manta.exception.MantaClientException;
Expand Down Expand Up @@ -62,20 +63,24 @@ public class MantaClientIT {
private String testPathPrefix;

@BeforeClass
@Parameters({"usingEncryption"})
public void beforeClass(@Optional Boolean usingEncryption) throws IOException {
@Parameters({"usingEncryption", "testType"})
public void beforeClass(@Optional Boolean usingEncryption,
@Optional String testType) throws IOException {

// Let TestNG configuration take precedence over environment variables
ConfigContext config = new IntegrationTestConfigContext(usingEncryption);
String testName = this.getClass().getSimpleName();

mantaClient = new MantaClient(config);
testPathPrefix = IntegrationTestConfigContext.generateBasePath(config, this.getClass().getSimpleName());
mantaClient.putDirectory(testPathPrefix, true);
testPathPrefix = IntegrationTestHelper.setupTestPath(config, mantaClient,
testName, testType);

IntegrationTestHelper.createTestBucketOrDirectory(mantaClient, testPathPrefix, testType);
}

@AfterClass
public void afterClass() throws IOException {
IntegrationTestConfigContext.cleanupTestDirectory(mantaClient, testPathPrefix);
IntegrationTestHelper.cleanupTestBucketOrDirectory(mantaClient, testPathPrefix);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.joyent.manta.client.helper;

import com.joyent.manta.client.MantaClient;
import com.joyent.manta.config.ConfigContext;
import com.joyent.manta.config.IntegrationTestConfigContext;
import com.joyent.manta.exception.MantaClientHttpResponseException;
import com.joyent.manta.exception.MantaErrorCode;
import org.apache.commons.lang3.BooleanUtils;
import org.testng.SkipException;

import java.io.IOException;
import java.util.UUID;

/**
* A helper class that contains methods to assist in the Integration testing of
* manta buckets functionality.
*
* @author <a href="https://github.com/1010sachin">Sachin Gupta</a>
*/

public class IntegrationTestHelper {

private static String setupBucketsPath(final ConfigContext config, final String testName) {
String bucketPathPrefix = String.format("%s%sbuckets%s%s", config.getMantaHomeDirectory(),
MantaClient.SEPARATOR, MantaClient.SEPARATOR,
testName.toLowerCase() + UUID.randomUUID());
return bucketPathPrefix + MantaClient.SEPARATOR + "objects" + MantaClient.SEPARATOR;
}

private static boolean verifyBucketsSupport(final ConfigContext config,
final MantaClient client) throws IOException {
try {
String path = String.format("%s%sbuckets", config.getMantaHomeDirectory(),
MantaClient.SEPARATOR);
client.options(path);
} catch (MantaClientHttpResponseException me) {
if (MantaErrorCode.RESOURCE_NOT_FOUND_ERROR.equals(me.getServerCode())) {
return false;
}
}
return true;
}

public static void cleanupTestBucketOrDirectory(final MantaClient client,
final String testPath) throws IOException {

String bucketObjectsSubstring = String.format("%sobjects%s", MantaClient.SEPARATOR,
MantaClient.SEPARATOR);
if (testPath.contains(bucketObjectsSubstring)) {
String bucketPath = testPath.substring(0, testPath.lastIndexOf(bucketObjectsSubstring));
//Delete bucket here
} else {
IntegrationTestConfigContext.cleanupTestDirectory(client, testPath);
}
}

public static String setupTestPath(final ConfigContext config,
final MantaClient client,
final String testName,
final String testType) throws IOException {
if ("buckets".equals(testType)) {
if (BooleanUtils.isTrue(verifyBucketsSupport(config, client))) {
return setupBucketsPath(config, testName);
} else {
throw new SkipException("Bucket operations not supported by the server");
}
}

return IntegrationTestConfigContext.generateBasePath(config, testName);
}

public static void createTestBucketOrDirectory(final MantaClient client,
final String testPath,
final String testType) throws IOException {
if ("buckets".equals(testType)) {
//Create bucket here
} else {
client.putDirectory(testPath, true);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ public static String generateSuiteBasePath(final ConfigContext config) {
public static String generateBasePath(final ConfigContext config, final String testBaseName) {
return generateSuiteBasePath(config) + testBaseName + SEPARATOR;
}

public static String generateBucketsBasePath(final ConfigContext config, final String testBaseName) {
return generateSuiteBasePath(config) + testBaseName + SEPARATOR + "buckets" + SEPARATOR;
}

public static String generateBasePathWithoutSeparator(final ConfigContext config, final String testBaseName) {
return generateSuiteBasePath(config) + testBaseName;
Expand Down

0 comments on commit 9e2f780

Please sign in to comment.