Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PvlSport committed Feb 6, 2023
1 parent 5724f0e commit 53954cd
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import net.jqwik.api.Property;
import net.jqwik.api.Provide;
import net.jqwik.api.Example;
import net.jqwik.api.Disabled;
import net.jqwik.api.ForAll;
import net.jqwik.api.constraints.AlphaChars;
import net.jqwik.api.constraints.Size;
Expand Down Expand Up @@ -95,7 +94,7 @@ public Arbitrary<List<TestBucket>> provideBuckets(@ForAll

assertThat(deleteResponse.getLength(), equalTo(mockDeleteResponse.getLength()));
assertThat(deleteResponse.getStatus(), equalTo(mockDeleteResponse.getStatus()));

for (int i = 0; i < allBuckets.size(); i++) {
final TestBucket testBucket = new TestBucket();

Expand All @@ -109,7 +108,6 @@ public Arbitrary<List<TestBucket>> provideBuckets(@ForAll
return Arbitraries.shuffle(testBuckets);
}

@Disabled
@Property(tries = 5)
@SneakyThrows
public void shouldBe1(@ForAll("provideBuckets") List<TestBucket> testBuckets) {
Expand All @@ -119,7 +117,6 @@ public void shouldBe1(@ForAll("provideBuckets") List<TestBucket> testBuckets) {
}

@Example
@Disabled
@SneakyThrows
public void shouldBe2() {
RestClient client = new RestClient(server.getRest().getHostName(), server.getRest().getPort(), true);
Expand All @@ -134,7 +131,6 @@ public void shouldBe2() {
}

@Example
@Disabled
@SneakyThrows
public void shouldBe3() {
RestClient client = new RestClient(server.getRest().getHostName(), server.getRest().getPort(), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,22 @@
import jakarta.ws.rs.core.Response;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import net.jqwik.api.Disabled;
import lombok.Data;
import lombok.NoArgsConstructor;
import net.jqwik.api.Property;
import net.jqwik.api.lifecycle.AfterProperty;
import net.jqwik.api.lifecycle.BeforeProperty;
import net.jqwik.api.Provide;
import net.jqwik.api.Example;
import net.jqwik.api.lifecycle.AfterTry;
import net.jqwik.api.lifecycle.BeforeTry;
import net.jqwik.api.ForAll;
import net.jqwik.api.Arbitrary;
import net.jqwik.api.Arbitraries;
import net.jqwik.api.constraints.AlphaChars;
import net.jqwik.api.constraints.StringLength;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import org.unigrid.hedgehog.client.ResponseOddityException;
Expand All @@ -38,61 +49,111 @@
public class StorageObjectTest extends BaseRestClientTest {
S3Mock api;
RestClient client;
RestClient clientMock;

String bucket = "testBucket";
String key = "testObject";
String copy = "copied";
String copySuffix = "_copied";
final int MAX_KEYS = 10000;

@BeforeTry
@BeforeProperty
public void beforeTry() {
api = new S3Mock.Builder().withPort(8001).withInMemoryBackend().build();
api.start();
client = new RestClient(server.getRest().getHostName(), server.getRest().getPort(), true);
clientMock = new RestClient(server.getRest().getHostName(), 8001, false);
}

@AfterTry
@AfterProperty
public void after() {
api.shutdown();
client.close();
clientMock.close();
}

@Example
@Disabled
@SneakyThrows
public void shouldBe1() {
final RestClient clientMock = new RestClient(server.getRest().getHostName(), 8001, false);
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class TestObject {
private String bucket;
private String key;
}

@Provide
public Arbitrary<String> nameProvider(@ForAll @AlphaChars @StringLength(max = 20, min = 1) String key) {
return Arbitraries.of(key);
}

@Provide
public Arbitrary<CreateBucketConfiguration> bucketConfigurationProvider(@ForAll("nameProvider") String configurationName) {
return Arbitraries.of(new CreateBucketConfiguration(configurationName));
}

@Provide
public Arbitrary<String> bucketProvider(@ForAll("nameProvider") String bucketName,
@ForAll("bucketConfigurationProvider") CreateBucketConfiguration config) throws ResponseOddityException {

RestClient client2 = new RestClient(server.getRest().getHostName(), server.getRest().getPort(), true);
RestClient clientMock2 = new RestClient(server.getRest().getHostName(), 8001, false);

client2.putXml("/bucket/" + bucketName, config);
clientMock2.putXml("/" + bucketName, config);

client2.close();
clientMock2.close();

return Arbitraries.of(bucketName);
}

@Provide
public Arbitrary<List<TestObject>> keyProvider(@ForAll("nameProvider") String content,
@ForAll("nameProvider") String keyName, @ForAll("bucketProvider") String bucket) throws ResponseOddityException {

RestClient client2 = new RestClient(server.getRest().getHostName(), server.getRest().getPort(), true);
RestClient clientMock2 = new RestClient(server.getRest().getHostName(), 8001, false);

CreateBucketConfiguration config = new CreateBucketConfiguration("TestConfig");
InputStream inputStream = new ByteArrayInputStream(content.getBytes());
InputStream inputStream2 = new ByteArrayInputStream(content.getBytes());

clientMock.putXml("/" + bucket, config);
client.putXml("/bucket/" + bucket, config);
clientMock2.postInputStream("/" + bucket + "/" + keyName, inputStream);
client2.postInputStream("/storage-object/" + bucket + "/" + keyName, inputStream2);

InputStream inputStream = new ByteArrayInputStream("Hello, World!".getBytes());
InputStream inputStream2 = new ByteArrayInputStream("Hello, World!".getBytes());
final List<TestObject> testObjects = new ArrayList<>();
testObjects.add(new TestObject(bucket, keyName));

client2.close();
clientMock2.close();

return Arbitraries.shuffle(testObjects);
}

@Property(tries = 10)
public void shouldBeAbleToCreateObject(@ForAll("nameProvider") String content, @ForAll("nameProvider") String key,
@ForAll("bucketProvider") String bucket) throws ResponseOddityException {

InputStream inputStream = new ByteArrayInputStream(content.getBytes());
InputStream inputStream2 = new ByteArrayInputStream(content.getBytes());

Response mockResponse = clientMock.postInputStream("/" + bucket + "/" + key, inputStream);
Response response = client.postInputStream("/storage-object/" + bucket + "/" + key, inputStream2);

assertThat(response.getStatus(), equalTo(mockResponse.getStatus()));

clientMock.close();
}

@Example
@Property(tries = 10)
@SneakyThrows
public void shouldBe2() {
public void shouldContainInputStream(@ForAll("bucketConfigurationProvider") CreateBucketConfiguration config,
@ForAll("nameProvider") String key, @ForAll("nameProvider") String bucketName) {

try {
client.post("/storage-object/" + bucket + "/" + key, "");
client.putXml("/bucket/" + bucketName, config);
client.post("/storage-object/" + bucketName + "/" + key, "");
} catch (Exception e) {
assertThat(e, isA(ResponseOddityException.class));
}
}

@Example
@Disabled
@Property(tries = 10)
@SneakyThrows
public void shouldBe3() {
public void shouldListAllObjects(@ForAll("bucketProvider") String bucket) {
ListBucketResult response = client.getEntity("/storage-object/list/" + bucket, ListBucketResult.class);

assertThat(response.getName(), equalTo(bucket));
Expand All @@ -101,65 +162,64 @@ public void shouldBe3() {
assertThat(response.getMaxKeys(), equalTo(MAX_KEYS));
}

@Example
@Disabled
@Property(tries = 10)
@SneakyThrows
public void shouldBe4() {
public void shouldCopyObject(@ForAll("keyProvider") List<TestObject> testObject) {
String bucket = testObject.get(0).getBucket();
String key = testObject.get(0).getKey();

MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
headers.add("x-amz-copy-source", "/" + bucket + "/" + key);

Response response = client.putWithHeaders("/storage-object/" + bucket + "/" + copy, headers);
Response response = client.putWithHeaders("/storage-object/" + bucket + "/" + key + copySuffix, headers);
CopyObjectResult result = response.readEntity(CopyObjectResult.class);

assertThat(response.getStatus(), equalTo(200));
assertThat(result.getETag(), is(notNullValue()));
assertThat(result.getLastModified().toString(), is(notNullValue()));
}

@Example
@Property(tries = 10)
@SneakyThrows
public void shouldBe5() {
public void shouldContainHeader(@ForAll("nameProvider") String bucketName, @ForAll("nameProvider") String key) {
MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();

try {
client.putWithHeaders("/storage-object/testBukcet/testObjecyt", headers);
client.putWithHeaders("/storage-object/" + bucketName + "/" + key, headers);
} catch (Exception e) {
assertThat(e.getMessage(), containsString("400 Bad Request"));
}
}

@Example
@Disabled
@Property(tries = 10)
@SneakyThrows
public void shouldBe6() {
Response response = client.get("/storage-object/" + bucket + "/" + key);
public void shouldGetAnObject(@ForAll("keyProvider") List<TestObject> testObject) {
Response response = client.get("/storage-object/" + testObject.get(0).getBucket() + "/"
+ testObject.get(0).getKey());

assertThat(response.getStatus(), equalTo(200));
// file should not be empty
assertThat(response.getLength(), greaterThan(0));
}

@Example
@Disabled
@Property(tries = 10)
@SneakyThrows
public void shouldBe7() {
public void shouldObjectExist(@ForAll("nameProvider") String bucketName) {
try {
client.get("/storage-object/" + bucket + "/testtest");
client.get("/storage-object/" + bucketName + "/testtest");
} catch (Exception e) {
assertThat(e, isA(ResponseOddityException.class));
assertThat(e.getMessage(), containsString("404 Not Found"));
}
}

@Example
@Disabled
@SneakyThrows
public void shouldBe8() {
Response response = client.delete("/storage-object/" + bucket + "/" + copy);
public void shouldDeleteAnObject(@ForAll("keyProvider") List<TestObject> testObject) {
Response response = client.delete("/storage-object/" + testObject.get(0).getBucket() + "/"
+ testObject.get(0).getKey());

assertThat(response.getLength(), equalTo(-1));
assertThat(response.getStatus(), equalTo(204));

client.delete("/bucket/" + bucket);
client.delete("/bucket/" + testObject.get(0).getBucket());
}
}

0 comments on commit 53954cd

Please sign in to comment.