Skip to content

Commit

Permalink
fix: Stop sending not specified optional fields when creating a user (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsocha2 authored Sep 6, 2022
1 parent 30e2fcb commit b7d894d
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 267 deletions.
129 changes: 69 additions & 60 deletions src/intTest/java/com/box/sdk/BoxRetentionPolicyAssignmentIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
import static com.box.sdk.UniqueTestFolder.uploadTwoFileVersionsToSpecifiedFolder;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.junit.AfterClass;
Expand All @@ -30,7 +28,7 @@ public class BoxRetentionPolicyAssignmentIT {
private static BoxRetentionPolicy policy;

@BeforeClass
public static void beforeClass() throws Exception {
public static void beforeClass() {
BoxAPIConnection api = jwtApiForServiceAccount();
policy = RetentionPolicyUtils.getOneDayRetentionPolicy(api);
setupUniqeFolder();
Expand All @@ -42,82 +40,93 @@ public static void tearDown() {
}

@Test
public void attachPolicyToFileAndGetFilesUnderRetentionAndDeleteAttachment() {
public void attachPolicyToFileAndGetFilesUnderRetentionAndDeleteAttachment() throws InterruptedException {
//given
BoxAPIConnection api = jwtApiForServiceAccount();
BoxFolder.Info folder = getUniqueFolder(api)
.createFolder(randomizeName("attachPolicyToFileAndGetFilesUnderRetention"));
BoxRetentionPolicyAssignment.Info assignmentInfo = createAssignmentToFolder(
try {
BoxRetentionPolicyAssignment.Info assignmentInfo = createAssignmentToFolder(
api, policy.getID(), folder.getID()
);
BoxFile boxFile = uploadFileWithSomeContent("file_with_retention.txt", folder.getResource());
);
BoxFile boxFile = uploadFileWithSomeContent("file_with_retention.txt", folder.getResource());

//when
BoxRetentionPolicyAssignment assignment = new BoxRetentionPolicyAssignment(api, assignmentInfo.getID());
Iterable<BoxFile.Info> filesUnderRetention =
new BoxRetentionPolicyAssignment(api, assignmentInfo.getID()).getFilesUnderRetention(5);
//when
BoxRetentionPolicyAssignment assignment = new BoxRetentionPolicyAssignment(api, assignmentInfo.getID());
Iterable<BoxFile.Info> filesUnderRetention =
new BoxRetentionPolicyAssignment(api, assignmentInfo.getID()).getFilesUnderRetention(5);

//then
Optional<BoxFile.Info> matchingFileWithRetention1 =
StreamSupport.stream(filesUnderRetention.spliterator(), false)
.filter(f -> f.getID().equals(boxFile.getID()))
.findFirst();
assertTrue(matchingFileWithRetention1.isPresent());

//when
assignment.delete();

//then
Optional<BoxFile.Info> matchingFileWithRetention2 =
//then
List<BoxFile.Info> matchingFileWithRetention1 =
StreamSupport.stream(filesUnderRetention.spliterator(), false)
.filter(f -> f.getID().equals(boxFile.getID()))
.collect(Collectors.toList());
assertThat(matchingFileWithRetention1, hasSize(1));

//when
assignment.delete();
Retry.retry(() -> {
Iterable<BoxFile.Info> filesUnderRetention2 =
new BoxRetentionPolicyAssignment(api, assignmentInfo.getID()).getFilesUnderRetention(5);

//then
List<BoxFile.Info> matchingFileWithRetention2 =
StreamSupport.stream(filesUnderRetention2.spliterator(), false)
.filter(f -> f.getID().equals(boxFile.getID()))
.findFirst();
assertFalse(matchingFileWithRetention2.isPresent());

//cleanup
deleteFolder(folder.getResource());
.collect(Collectors.toList());
assertTrue(matchingFileWithRetention2.isEmpty());
}, 3, 1000);
} finally {
//cleanup
deleteFolder(folder.getResource());
}
}

@Test
public void attachPolicyToFileAndGetFileVersionsUnderRetentionAndDeleteAttachment() {
public void attachPolicyToFileAndGetFileVersionsUnderRetentionAndDeleteAttachment() throws InterruptedException {
//given
BoxAPIConnection api = jwtApiForServiceAccount();
BoxFolder folder = getUniqueFolder(api)
.createFolder(randomizeName("attachPolicyToFileAndGetFileVersionsUnderRetention"))
.getResource();
BoxRetentionPolicyAssignment.Info assignmentInfo = createAssignmentToFolder(
try {
BoxRetentionPolicyAssignment.Info assignmentInfo = createAssignmentToFolder(
api, policy.getID(), folder.getID()
);
BoxFile boxFile = uploadTwoFileVersionsToSpecifiedFolder(
"file_with_retention.txt",
"v1",
"v2",
folder,
mock(ProgressListener.class)
);

//when
BoxRetentionPolicyAssignment assignment = new BoxRetentionPolicyAssignment(api, assignmentInfo.getID());
Iterable<BoxFile.Info> filesVersionsUnderRetention = assignment.getFileVersionsUnderRetention(5);

//then
List<BoxFile.Info> matchingFileWithRetention1 =
StreamSupport.stream(filesVersionsUnderRetention.spliterator(), false)
.filter(f -> f.getID().equals(boxFile.getID()))
.collect(Collectors.toList());
assertThat(matchingFileWithRetention1, hasSize(1));

//when
assignment.delete();

//then
List<BoxFile.Info> matchingFileWithRetention2 =
StreamSupport.stream(filesVersionsUnderRetention.spliterator(), false)
);
BoxFile boxFile = uploadTwoFileVersionsToSpecifiedFolder(
"file_with_retention.txt",
"v1",
"v2",
folder,
mock(ProgressListener.class)
);

//when
BoxRetentionPolicyAssignment assignment = new BoxRetentionPolicyAssignment(api, assignmentInfo.getID());
Iterable<BoxFile.Info> filesVersionsUnderRetention1 = assignment.getFileVersionsUnderRetention(5);

//then
List<BoxFile.Info> matchingFileWithRetention1 =
StreamSupport.stream(filesVersionsUnderRetention1.spliterator(), false)
.filter(f -> f.getID().equals(boxFile.getID()))
.collect(Collectors.toList());
assertThat(matchingFileWithRetention1, hasSize(1));

//when
assignment.delete();
Retry.retry(() -> {
Iterable<BoxFile.Info> filesVersionsUnderRetention2 = assignment.getFileVersionsUnderRetention(5);

//then
List<BoxFile.Info> matchingFileWithRetention2 =
StreamSupport.stream(filesVersionsUnderRetention2.spliterator(), false)
.filter(f -> f.getID().equals(boxFile.getID()))
.collect(Collectors.toList());
assertTrue(matchingFileWithRetention2.isEmpty());

//cleanup
deleteFolder(folder);
assertTrue(matchingFileWithRetention2.isEmpty());
}, 3, 1000);
} finally {
//cleanup
deleteFolder(folder);
}
}
}
67 changes: 38 additions & 29 deletions src/intTest/java/com/box/sdk/MetadataIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
public class MetadataIT {

@BeforeClass
public static void beforeClass() throws Exception {
public static void beforeClass() {
setupUniqeFolder();
}

@AfterClass
public static void afterClass() throws Exception {
public static void afterClass() {
removeUniqueFolder();
}

Expand All @@ -37,6 +37,7 @@ public void testMetadataPrecisionFloat() {
BoxAPIConnection api = jwtApiForServiceAccount();

long timestamp = Calendar.getInstance().getTimeInMillis();
String scope = "enterprise";
String templateKey = "precision" + timestamp;
String fieldKey = "testPrecision";

Expand All @@ -47,28 +48,33 @@ public void testMetadataPrecisionFloat() {
valueField.setDisplayName("Value Field");
fields.add(valueField);

MetadataTemplate template = MetadataTemplate.createMetadataTemplate(api, "enterprise",
templateKey, "Precision " + timestamp, false, fields);

assertEquals("float", template.getFields().get(0).getType());
MetadataTemplate template = MetadataTemplate.createMetadataTemplate(
api, scope, templateKey, "Precision " + timestamp, false, fields
);
try {
assertEquals("float", template.getFields().get(0).getType());

// Add template to item
Metadata mdValues = new Metadata();
mdValues.add("/" + fieldKey, expectedValueFloat);
BoxFolder rootFolder = getUniqueFolder(api);
BoxFolder.Info folder = rootFolder.createFolder("Metadata Precision Test " + timestamp);
Metadata actualMD = folder.getResource().createMetadata(templateKey, mdValues);
// Add template to item
Metadata mdValues = new Metadata();
mdValues.add("/" + fieldKey, expectedValueFloat);
BoxFolder rootFolder = getUniqueFolder(api);
BoxFolder.Info folder = rootFolder.createFolder("Metadata Precision Test " + timestamp);
Metadata actualMD = folder.getResource().createMetadata(templateKey, mdValues);

assertEquals(templateKey, actualMD.getTemplateName());
assertEquals(templateKey, actualMD.getTemplateName());

final double actualValueDouble = actualMD.getDouble("/" + fieldKey);
final double actualValueDouble = actualMD.getDouble("/" + fieldKey);

// Instead of "hard-coding" the delta to 4.0, let's calculate it and then validate it
final double delta = actualValueDouble - (double) expectedValueFloat;
assertEquals(4.0, delta, 0);
// Instead of "hard-coding" the delta to 4.0, let's calculate it and then validate it
final double delta = actualValueDouble - (double) expectedValueFloat;
assertEquals(4.0, delta, 0);

// Now that we know delta is 4.0, when can use it for this validation
assertEquals(expectedValueFloat, actualValueDouble, delta);
// Now that we know delta is 4.0, when can use it for this validation
assertEquals(expectedValueFloat, actualValueDouble, delta);
} finally {
this.deleteMetadata(api, template);
}
}

@Test
Expand All @@ -89,18 +95,21 @@ public void testMetadataPrecisionDouble() {

MetadataTemplate template = MetadataTemplate.createMetadataTemplate(api, "enterprise",
templateKey, "Precision " + timestamp, false, fields);
try {
assertEquals("float", template.getFields().get(0).getType());

assertEquals("float", template.getFields().get(0).getType());

// Add template to item
Metadata mdValues = new Metadata();
mdValues.add("/" + fieldKey, valueDouble);
BoxFolder rootFolder = getUniqueFolder(api);
BoxFolder.Info folder = rootFolder.createFolder("Metadata Precision Test " + timestamp);
Metadata actualMD = folder.getResource().createMetadata(templateKey, mdValues);
// Add template to item
Metadata mdValues = new Metadata();
mdValues.add("/" + fieldKey, valueDouble);
BoxFolder rootFolder = getUniqueFolder(api);
BoxFolder.Info folder = rootFolder.createFolder("Metadata Precision Test " + timestamp);
Metadata actualMD = folder.getResource().createMetadata(templateKey, mdValues);

assertEquals(templateKey, actualMD.getTemplateName());
assertEquals(valueDouble, actualMD.getDouble("/" + fieldKey), 0);
assertEquals(templateKey, actualMD.getTemplateName());
assertEquals(valueDouble, actualMD.getDouble("/" + fieldKey), 0);
} finally {
this.deleteMetadata(api, template);
}
}

@Test
Expand Down Expand Up @@ -255,7 +264,7 @@ public void testMultiSelectMetadataCRUD() {

private void deleteMetadata(BoxAPIConnection api, MetadataTemplate template) {
if (template != null) {
MetadataTemplate.deleteMetadataTemplate(api, "enterprise", template.getTemplateKey());
MetadataTemplate.deleteMetadataTemplate(api, template.getScope(), template.getTemplateKey());
}
}

Expand Down
Loading

0 comments on commit b7d894d

Please sign in to comment.