Skip to content

Commit f9ce2e8

Browse files
committed
test(Discovery v1): Refactor integration tests to use unique filenames
1 parent 6708f7c commit f9ce2e8

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

discovery/src/test/java/com/ibm/watson/discovery/v1/DiscoveryServiceIT.java

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@
131131
import com.ibm.watson.discovery.v1.model.UpdateDocumentOptions;
132132
import com.ibm.watson.discovery.v1.model.UpdateEnvironmentOptions;
133133
import com.ibm.watson.discovery.v1.model.UpdateTrainingExampleOptions;
134-
import com.ibm.watson.discovery.v1.query.AggregationType;
135-
import com.ibm.watson.discovery.v1.query.Operator;
134+
import com.ibm.watson.discovery.query.AggregationType;
135+
import com.ibm.watson.discovery.query.Operator;
136136
import org.junit.After;
137137
import org.junit.AfterClass;
138138
import org.junit.Assume;
@@ -858,10 +858,9 @@ public void addDocumentIsSuccessful() {
858858

859859
AddDocumentOptions.Builder builder = new AddDocumentOptions.Builder();
860860
builder.environmentId(environmentId);
861-
//builder.collectionId(collection.getCollectionId());
862861
builder.collectionId(collectionId);
863862
builder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
864-
builder.filename("test_file");
863+
builder.filename(UUID.randomUUID().toString());
865864
DocumentAccepted createResponse = discovery.addDocument(builder.build()).execute().getResult();
866865

867866
assertFalse(createResponse.getDocumentId().isEmpty());
@@ -879,7 +878,7 @@ public void addDocumentWithConfigurationIsSuccessful() {
879878
builder.environmentId(environmentId);
880879
builder.collectionId(collectionId);
881880
builder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
882-
builder.filename("test_file");
881+
builder.filename(UUID.randomUUID().toString());
883882
DocumentAccepted createResponse = discovery.addDocument(builder.build()).execute().getResult();
884883

885884
assertFalse(createResponse.getDocumentId().isEmpty());
@@ -898,7 +897,7 @@ public void addDocumentWithMetadataIsSuccessful() {
898897

899898
AddDocumentOptions.Builder builder = new AddDocumentOptions.Builder(environmentId, collectionId);
900899
builder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
901-
builder.filename("test_file");
900+
builder.filename(UUID.randomUUID().toString());
902901
builder.metadata(myMetadata.toString());
903902

904903
DocumentAccepted createResponse = discovery.addDocument(builder.build()).execute().getResult();
@@ -913,19 +912,24 @@ public void addDocumentWithMetadataIsSuccessful() {
913912
assertTrue(queryResponse.getResults().get(0).getMetadata() != null);
914913
}
915914

915+
@Ignore
916916
@Test
917917
public void deleteDocumentIsSuccessful() {
918-
DocumentAccepted documentAccepted = createTestDocument("test_document", collectionId);
918+
DocumentAccepted createResponse = createTestDocument(collectionId);
919+
String documentId = createResponse.getDocumentId();
919920

920-
DeleteDocumentOptions deleteOptions = new DeleteDocumentOptions.Builder(environmentId, collectionId,
921-
documentAccepted.getDocumentId()).build();
921+
WaitFor.Condition documentAccepted = new WaitForDocumentAccepted(environmentId, collectionId, documentId);
922+
WaitFor.waitFor(documentAccepted, 5, TimeUnit.SECONDS, 500);
923+
924+
DeleteDocumentOptions deleteOptions
925+
= new DeleteDocumentOptions.Builder(environmentId, collectionId, documentId).build();
922926
discovery.deleteDocument(deleteOptions).execute();
923927
}
924928

925929
@Ignore
926930
@Test
927931
public void getDocumentIsSuccessful() {
928-
DocumentAccepted documentAccepted = createTestDocument("test_document", collectionId);
932+
DocumentAccepted documentAccepted = createTestDocument(collectionId);
929933

930934
GetDocumentStatusOptions getOptions = new GetDocumentStatusOptions.Builder(environmentId, collectionId,
931935
documentAccepted.getDocumentId()).build();
@@ -936,7 +940,7 @@ public void getDocumentIsSuccessful() {
936940

937941
@Test
938942
public void updateDocumentIsSuccessful() {
939-
DocumentAccepted documentAccepted = createTestDocument("test_document", collectionId);
943+
DocumentAccepted documentAccepted = createTestDocument(collectionId);
940944

941945
uniqueName = UUID.randomUUID().toString();
942946
String myDocumentJson = "{\"field\":\"value2\"}";
@@ -945,7 +949,7 @@ public void updateDocumentIsSuccessful() {
945949
UpdateDocumentOptions.Builder updateBuilder = new UpdateDocumentOptions.Builder(environmentId, collectionId,
946950
documentAccepted.getDocumentId());
947951
updateBuilder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
948-
updateBuilder.filename("test_file");
952+
updateBuilder.filename(UUID.randomUUID().toString());
949953
DocumentAccepted updateResponse = discovery.updateDocument(updateBuilder.build()).execute().getResult();
950954

951955
GetDocumentStatusOptions getOptions = new GetDocumentStatusOptions.Builder(environmentId, collectionId,
@@ -970,7 +974,7 @@ public void updateAnotherDocumentIsSuccessful() {
970974
UpdateDocumentOptions.Builder updateBuilder = new UpdateDocumentOptions.Builder(environmentId, collectionId,
971975
documentAccepted.getDocumentId());
972976
updateBuilder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
973-
updateBuilder.filename("test_file");
977+
updateBuilder.filename(UUID.randomUUID().toString());
974978
DocumentAccepted updateResponse = discovery.updateDocument(updateBuilder.build()).execute().getResult();
975979

976980
GetDocumentStatusOptions getOptions = new GetDocumentStatusOptions.Builder(environmentId, collectionId,
@@ -985,7 +989,7 @@ public void updateAnotherDocumentIsSuccessful() {
985989
public void updateDocumentWithMetadataIsSuccessful() {
986990
Collection collection = createTestCollection();
987991
String collectionId = collection.getCollectionId();
988-
DocumentAccepted documentAccepted = createTestDocument("test_document", collectionId);
992+
DocumentAccepted documentAccepted = createTestDocument(collectionId);
989993

990994
String myDocumentJson = "{\"field\":\"value2\"}";
991995
InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes());
@@ -1188,9 +1192,12 @@ public void queryWithAggregationFilterIsSuccessful() {
11881192

11891193
@Test
11901194
public void queryWithAggregationNestedIsSuccessful() throws InterruptedException {
1191-
DocumentAccepted testDocument = createNestedTestDocument("test_document_1", collectionId);
1195+
DocumentAccepted testDocument = createNestedTestDocument(collectionId);
11921196
String documentId = testDocument.getDocumentId();
11931197

1198+
WaitFor.Condition documentAccepted = new WaitForDocumentAccepted(environmentId, collectionId, documentId);
1199+
WaitFor.waitFor(documentAccepted, 5, TimeUnit.SECONDS, 500);
1200+
11941201
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
11951202
StringBuilder sb = new StringBuilder();
11961203
sb.append(AggregationType.NESTED);
@@ -1205,17 +1212,6 @@ public void queryWithAggregationNestedIsSuccessful() throws InterruptedException
12051212
String aggregation = sb.toString();
12061213
queryBuilder.aggregation(aggregation);
12071214

1208-
GetDocumentStatusOptions getOptions = new GetDocumentStatusOptions.Builder()
1209-
.environmentId(environmentId)
1210-
.collectionId(collectionId)
1211-
.documentId(documentId)
1212-
.build();
1213-
DocumentStatus status = discovery.getDocumentStatus(getOptions).execute().getResult();
1214-
while (status.getStatus().equals(DocumentAccepted.Status.PROCESSING)) {
1215-
Thread.sleep(3000);
1216-
status = discovery.getDocumentStatus(getOptions).execute().getResult();
1217-
}
1218-
12191215
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute().getResult();
12201216
Nested nested = (Nested) queryResponse.getAggregations().get(0);
12211217
assertEquals(AggregationType.NESTED.getName(), nested.getType());
@@ -1227,10 +1223,10 @@ public void queryWithAggregationNestedIsSuccessful() throws InterruptedException
12271223
@Test
12281224
public void queryWithAggregationTimesliceIsSuccessful() throws InterruptedException {
12291225
String myDocumentJson = "{\"time\":\"1999-02-16T00:00:00.000-05:00\"}";
1230-
DocumentAccepted testDocument1 = createTestDocument(myDocumentJson, "timeslice_document_1", collectionId);
1226+
DocumentAccepted testDocument1 = createTestDocument(myDocumentJson, UUID.randomUUID().toString(), collectionId);
12311227
String documentId1 = testDocument1.getDocumentId();
12321228
myDocumentJson = "{\"time\":\"1999-04-16T00:00:00.000-05:00\"}";
1233-
DocumentAccepted testDocument2 = createTestDocument(myDocumentJson, "timeslice_document_2", collectionId);
1229+
DocumentAccepted testDocument2 = createTestDocument(myDocumentJson, UUID.randomUUID().toString(), collectionId);
12341230
String documentId2 = testDocument2.getDocumentId();
12351231

12361232
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
@@ -1305,10 +1301,10 @@ public void queryWithAggregationUniqueCountIsSuccessful() {
13051301

13061302
@Test
13071303
public void queryWithPassagesIsSuccessful() throws InterruptedException, FileNotFoundException {
1308-
createTestDocument(getStringFromInputStream(new FileInputStream(PASSAGES_TEST_FILE_1)), "test_document_1",
1309-
collectionId);
1304+
createTestDocument(getStringFromInputStream(new FileInputStream(PASSAGES_TEST_FILE_1)),
1305+
UUID.randomUUID().toString(), collectionId);
13101306
createTestDocument(getStringFromInputStream(new FileInputStream(PASSAGES_TEST_FILE_2)),
1311-
"test_document_2", collectionId);
1307+
UUID.randomUUID().toString(), collectionId);
13121308

13131309
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
13141310
queryBuilder.passages(true);
@@ -1411,7 +1407,7 @@ public void addTrainingDataIsSuccessful() {
14111407
AddTrainingDataOptions.Builder builder = new AddTrainingDataOptions.Builder(environmentId, collectionId);
14121408
String naturalLanguageQuery = "Example query" + UUID.randomUUID().toString();
14131409
builder.naturalLanguageQuery(naturalLanguageQuery);
1414-
String documentId = createTestDocument("test_document", collectionId).getDocumentId();
1410+
String documentId = createTestDocument(collectionId).getDocumentId();
14151411
int relevance = 0;
14161412
TrainingExample example = new TrainingExample.Builder()
14171413
.documentId(documentId)
@@ -1750,7 +1746,7 @@ public void credentialsOperationsAreSuccessful() {
17501746
@Test
17511747
public void createEventIsSuccessful() {
17521748
// create test document
1753-
DocumentAccepted accepted = createTestDocument("event_file", collectionId);
1749+
DocumentAccepted accepted = createTestDocument(collectionId);
17541750

17551751
// make query to get session_token
17561752
QueryOptions queryOptions = new QueryOptions.Builder()
@@ -2005,16 +2001,21 @@ private void deleteCollection(DeleteCollectionOptions deleteOptions) {
20052001
private Collection createTestCollection() {
20062002
Configuration createConfigResponse = createTestConfig();
20072003

2008-
String uniqueCollectionName = uniqueName + "-collection";
2004+
String uniqueCollectionName = "java-sdk-" + uniqueName + "-collection";
20092005
CreateCollectionOptions.Builder createCollectionBuilder = new CreateCollectionOptions.Builder(environmentId,
20102006
uniqueCollectionName)
20112007
.configurationId(createConfigResponse.configurationId());
20122008
return createCollection(createCollectionBuilder.build());
20132009
}
20142010

2015-
private DocumentAccepted createNestedTestDocument(String filename, String collectionId) {
2011+
private DocumentAccepted createNestedTestDocument(String collectionId) {
20162012
String myDocumentJson = "{\"nested_fields\":{\"field\":\"value\"}}";
2017-
return createTestDocument(myDocumentJson, filename, collectionId);
2013+
return createTestDocument(myDocumentJson, UUID.randomUUID().toString(), collectionId);
2014+
}
2015+
2016+
private DocumentAccepted createTestDocument(String collectionId) {
2017+
String myDocumentJson = "{\"field\":\"value\"}";
2018+
return createTestDocument(myDocumentJson, UUID.randomUUID().toString(), collectionId);
20182019
}
20192020

20202021
private DocumentAccepted createTestDocument(String filename, String collectionId) {

0 commit comments

Comments
 (0)