|
15 | 15 | import static org.junit.Assert.assertEquals; |
16 | 16 | import static org.junit.Assert.assertNotEquals; |
17 | 17 | import static org.junit.Assert.assertNotNull; |
| 18 | +import static org.junit.Assert.assertNull; |
18 | 19 | import static org.junit.Assert.assertTrue; |
19 | 20 | import static org.junit.Assert.fail; |
20 | 21 |
|
| 22 | +import java.text.DateFormat; |
| 23 | +import java.text.SimpleDateFormat; |
21 | 24 | import java.util.ArrayList; |
22 | 25 | import java.util.Date; |
23 | 26 | import java.util.HashMap; |
|
36 | 39 | import com.ibm.watson.developer_cloud.conversation.v1.model.IntentCollectionResponse; |
37 | 40 | import com.ibm.watson.developer_cloud.conversation.v1.model.IntentExportResponse; |
38 | 41 | import com.ibm.watson.developer_cloud.conversation.v1.model.IntentResponse; |
| 42 | +import com.ibm.watson.developer_cloud.conversation.v1.model.ListLogsOptions; |
| 43 | +import com.ibm.watson.developer_cloud.conversation.v1.model.LogCollectionResponse; |
| 44 | +import com.ibm.watson.developer_cloud.conversation.v1.model.LogExportResponse; |
39 | 45 | import com.ibm.watson.developer_cloud.conversation.v1.model.UpdateWorkspace; |
40 | 46 | import com.ibm.watson.developer_cloud.conversation.v1.model.WorkspaceCollectionResponse; |
41 | 47 | import com.ibm.watson.developer_cloud.conversation.v1.model.WorkspaceExportResponse; |
@@ -63,6 +69,8 @@ public class ConversationServiceIT extends WatsonServiceTest { |
63 | 69 | protected String workspaceId; |
64 | 70 | private String exampleIntent; |
65 | 71 |
|
| 72 | + DateFormat isoDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX"); |
| 73 | + |
66 | 74 | /* |
67 | 75 | * (non-Javadoc) |
68 | 76 | * |
@@ -301,6 +309,52 @@ public void testListCounterexamples() { |
301 | 309 | } |
302 | 310 | } |
303 | 311 |
|
| 312 | + /** |
| 313 | + * Test listCounterexamples with paging. |
| 314 | + */ |
| 315 | + @Test |
| 316 | + public void testListCounterexamplesWithPaging() { |
| 317 | + |
| 318 | + String counterExampleText1 = "alpha" + UUID.randomUUID().toString(); // gotta be unique |
| 319 | + String counterExampleText2 = "zeta" + UUID.randomUUID().toString(); // gotta be unique |
| 320 | + |
| 321 | + // Add two counterexamples |
| 322 | + service.createCounterexample(workspaceId, counterExampleText1).execute(); |
| 323 | + service.createCounterexample(workspaceId, counterExampleText2).execute(); |
| 324 | + |
| 325 | + try { |
| 326 | + CounterexampleCollectionResponse response = |
| 327 | + service.listCounterexamples(workspaceId, 1L, null, "text", null).execute(); |
| 328 | + assertNotNull(response); |
| 329 | + assertNotNull(response.getPagination()); |
| 330 | + assertNotNull(response.getPagination().getRefreshUrl()); |
| 331 | + assertNotNull(response.getPagination().getNextUrl()); |
| 332 | + assertNotNull(response.getPagination().getCursor()); |
| 333 | + |
| 334 | + boolean found1 = false, found2 = false; |
| 335 | + while (true) { |
| 336 | + assertNotNull(response.getCounterexamples()); |
| 337 | + assertTrue(response.getCounterexamples().size() == 1); |
| 338 | + found1 |= response.getCounterexamples().get(0).getText().equals(counterExampleText1); |
| 339 | + found2 |= response.getCounterexamples().get(0).getText().equals(counterExampleText2); |
| 340 | + assertTrue(found1 || !found2); // verify sort |
| 341 | + if (response.getPagination().getCursor() == null) { |
| 342 | + break; |
| 343 | + } |
| 344 | + String cursor = response.getPagination().getCursor(); |
| 345 | + response = service.listCounterexamples(workspaceId, 1L, null, "text", cursor).execute(); |
| 346 | + } |
| 347 | + |
| 348 | + assertTrue(found1 && found2); |
| 349 | + } catch (Exception ex) { |
| 350 | + fail(ex.getMessage()); |
| 351 | + } finally { |
| 352 | + // Clean up |
| 353 | + service.deleteCounterexample(workspaceId, counterExampleText1).execute(); |
| 354 | + service.deleteCounterexample(workspaceId, counterExampleText2).execute(); |
| 355 | + } |
| 356 | + } |
| 357 | + |
304 | 358 | /** |
305 | 359 | * Test updateCounterexample. |
306 | 360 | */ |
@@ -491,6 +545,54 @@ public void testListExamples() { |
491 | 545 |
|
492 | 546 | } |
493 | 547 |
|
| 548 | + /** |
| 549 | + * Test listExamples with paging. |
| 550 | + */ |
| 551 | + @Test |
| 552 | + public void testListExamplesWithPaging() { |
| 553 | + |
| 554 | + createExampleIntent(); |
| 555 | + |
| 556 | + String exampleText1 = "Alpha " + UUID.randomUUID().toString(); // gotta be unique |
| 557 | + String exampleText2 = "Zeta " + UUID.randomUUID().toString(); // gotta be unique |
| 558 | + service.createExample(workspaceId, exampleIntent, exampleText1).execute(); |
| 559 | + service.createExample(workspaceId, exampleIntent, exampleText2).execute(); |
| 560 | + |
| 561 | + try { |
| 562 | + ExampleCollectionResponse response = |
| 563 | + service.listExamples(workspaceId, exampleIntent, 1L, null, "-text", null).execute(); |
| 564 | + assertNotNull(response); |
| 565 | + assertNotNull(response.getExamples()); |
| 566 | + assertNotNull(response.getPagination()); |
| 567 | + assertNotNull(response.getPagination().getRefreshUrl()); |
| 568 | + assertNotNull(response.getPagination().getNextUrl()); |
| 569 | + assertNotNull(response.getPagination().getCursor()); |
| 570 | + |
| 571 | + boolean found1 = false, found2 = false; |
| 572 | + while (true) { |
| 573 | + assertNotNull(response.getExamples()); |
| 574 | + assertTrue(response.getExamples().size() == 1); |
| 575 | + found1 |= response.getExamples().get(0).getText().equals(exampleText1); |
| 576 | + found2 |= response.getExamples().get(0).getText().equals(exampleText2); |
| 577 | + assertTrue(found2 || !found1); // verify sort |
| 578 | + if (response.getPagination().getCursor() == null) { |
| 579 | + break; |
| 580 | + } |
| 581 | + String cursor = response.getPagination().getCursor(); |
| 582 | + response = service.listExamples(workspaceId, exampleIntent, 1L, null, "-text", cursor).execute(); |
| 583 | + } |
| 584 | + assertTrue(found1 && found2); |
| 585 | + |
| 586 | + } catch (Exception ex) { |
| 587 | + fail(ex.getMessage()); |
| 588 | + } finally { |
| 589 | + // Clean up |
| 590 | + service.deleteExample(workspaceId, exampleIntent, exampleText1).execute(); |
| 591 | + service.deleteExample(workspaceId, exampleIntent, exampleText2).execute(); |
| 592 | + } |
| 593 | + |
| 594 | + } |
| 595 | + |
494 | 596 | /** |
495 | 597 | * Test updateExample. |
496 | 598 | */ |
@@ -585,14 +687,13 @@ public void testCreateIntent() { |
585 | 687 | public void testDeleteIntent() { |
586 | 688 |
|
587 | 689 | String intentName = "Hello" + UUID.randomUUID().toString(); // gotta be unique |
588 | | - String intentDescription = "Description of " + intentName; |
589 | 690 |
|
590 | | - service.createIntent(workspaceId, intentName, intentDescription, null).execute(); |
| 691 | + service.createIntent(workspaceId, intentName, null, null).execute(); |
591 | 692 |
|
592 | 693 | service.deleteIntent(workspaceId, intentName).execute(); |
593 | 694 |
|
594 | 695 | try { |
595 | | - service.getIntent(workspaceId, intentName, false).execute(); |
| 696 | + service.getIntent(workspaceId, intentName, null).execute(); |
596 | 697 | fail("deleteIntent failed"); |
597 | 698 | } catch (Exception ex) { |
598 | 699 | // Expected result |
@@ -658,7 +759,7 @@ public void testListIntents() { |
658 | 759 | String intentName = "Hello" + UUID.randomUUID().toString(); // gotta be unique |
659 | 760 |
|
660 | 761 | try { |
661 | | - IntentCollectionResponse response = service.listIntents(workspaceId, false, null, null, null, null).execute(); |
| 762 | + IntentCollectionResponse response = service.listIntents(workspaceId, null, null, null, null, null).execute(); |
662 | 763 | assertNotNull(response); |
663 | 764 | assertNotNull(response.getIntents()); |
664 | 765 | assertNotNull(response.getPagination()); |
@@ -713,6 +814,54 @@ public void testListIntents() { |
713 | 814 | } |
714 | 815 | } |
715 | 816 |
|
| 817 | + /** |
| 818 | + * Test listIntents with paging. |
| 819 | + */ |
| 820 | + @Test |
| 821 | + public void testListIntentsWithPaging() { |
| 822 | + |
| 823 | + String intentName1 = "First" + UUID.randomUUID().toString(); // gotta be unique |
| 824 | + String intentName2 = "Second" + UUID.randomUUID().toString(); // gotta be unique |
| 825 | + |
| 826 | + service.createIntent(workspaceId, intentName1, null, null).execute(); |
| 827 | + service.createIntent(workspaceId, intentName2, null, null).execute(); |
| 828 | + |
| 829 | + try { |
| 830 | + IntentCollectionResponse response = |
| 831 | + service.listIntents(workspaceId, true, 1L, null, "modified", null).execute(); |
| 832 | + assertNotNull(response); |
| 833 | + assertNotNull(response.getIntents()); |
| 834 | + assertNotNull(response.getPagination()); |
| 835 | + assertNotNull(response.getPagination().getRefreshUrl()); |
| 836 | + assertNotNull(response.getPagination().getNextUrl()); |
| 837 | + assertNotNull(response.getPagination().getCursor()); |
| 838 | + |
| 839 | + boolean found1 = false, found2 = false; |
| 840 | + while (true) { |
| 841 | + assertNotNull(response.getIntents()); |
| 842 | + assertTrue(response.getIntents().size() == 1); |
| 843 | + found1 |= response.getIntents().get(0).getIntent().equals(intentName1); |
| 844 | + found2 |= response.getIntents().get(0).getIntent().equals(intentName2); |
| 845 | + assertTrue(found1 || !found2); // verify sort |
| 846 | + if (response.getPagination().getCursor() == null) { |
| 847 | + break; |
| 848 | + } |
| 849 | + String cursor = response.getPagination().getCursor(); |
| 850 | + response = service.listIntents(workspaceId, true, 1L, null, "modified", cursor).execute(); |
| 851 | + |
| 852 | + } |
| 853 | + assertTrue(found1 && found2); |
| 854 | + |
| 855 | + |
| 856 | + } catch (Exception ex) { |
| 857 | + fail(ex.getMessage()); |
| 858 | + } finally { |
| 859 | + // Clean up |
| 860 | + service.deleteIntent(workspaceId, intentName1).execute(); |
| 861 | + service.deleteIntent(workspaceId, intentName2).execute(); |
| 862 | + } |
| 863 | + } |
| 864 | + |
716 | 865 | /** |
717 | 866 | * Test updateIntent. |
718 | 867 | */ |
@@ -1003,6 +1152,36 @@ public void testListWorkspaces() { |
1003 | 1152 | assertNotNull(wResponse.getDescription()); |
1004 | 1153 | } |
1005 | 1154 |
|
| 1155 | + /** |
| 1156 | + * Test listWorkspaces with paging. |
| 1157 | + */ |
| 1158 | + @Test |
| 1159 | + public void testListWorkspacesWithPaging() { |
| 1160 | + |
| 1161 | + WorkspaceCollectionResponse response = |
| 1162 | + service.listWorkspaces(1L, null, "-modified", null).execute(); |
| 1163 | + |
| 1164 | + assertNotNull(response); |
| 1165 | + assertNotNull(response.getPagination()); |
| 1166 | + assertNotNull(response.getPagination().getRefreshUrl()); |
| 1167 | + assertNotNull(response.getPagination().getNextUrl()); |
| 1168 | + assertNotNull(response.getPagination().getCursor()); |
| 1169 | + |
| 1170 | + boolean found = false; |
| 1171 | + while (true) { |
| 1172 | + assertNotNull(response.getWorkspaces()); |
| 1173 | + assertTrue(response.getWorkspaces().size() == 1); |
| 1174 | + found |= response.getWorkspaces().get(0).getWorkspaceId().equals(workspaceId); |
| 1175 | + if (response.getPagination().getCursor() == null) { |
| 1176 | + break; |
| 1177 | + } |
| 1178 | + String cursor = response.getPagination().getCursor(); |
| 1179 | + response = service.listWorkspaces(1L, null, "-modified", cursor).execute(); |
| 1180 | + } |
| 1181 | + |
| 1182 | + assertTrue(found); |
| 1183 | + } |
| 1184 | + |
1006 | 1185 | /** |
1007 | 1186 | * Test updateWorkspace. |
1008 | 1187 | */ |
@@ -1039,4 +1218,73 @@ public void testUpdateWorkspace() { |
1039 | 1218 | service.deleteCounterexample(workspaceId, counterExampleText).execute(); |
1040 | 1219 | } |
1041 | 1220 | } |
| 1221 | + |
| 1222 | + /** |
| 1223 | + * Test listLogs. |
| 1224 | + */ |
| 1225 | + @Test |
| 1226 | + public void testListLogs() { |
| 1227 | + |
| 1228 | + try { |
| 1229 | + ListLogsOptions listOptions = new ListLogsOptions.Builder() |
| 1230 | + .workspaceId(workspaceId) |
| 1231 | + .build(); |
| 1232 | + LogCollectionResponse response = service.listLogs(listOptions).execute(); |
| 1233 | + assertNotNull(response); |
| 1234 | + assertNotNull(response.getLogs()); |
| 1235 | + assertNotNull(response.getPagination()); |
| 1236 | + // Empirically -- no refresh_url in pagination of listLogs |
| 1237 | + // assertNotNull(response.getPagination().getRefreshUrl()); |
| 1238 | + // nextUrl may be null |
| 1239 | + if (response.getPagination().getNextUrl() == null) { |
| 1240 | + assertNull(response.getPagination().getCursor()); |
| 1241 | + } else { |
| 1242 | + assertNotNull(response.getPagination().getCursor()); |
| 1243 | + } |
| 1244 | + } catch (Exception ex) { |
| 1245 | + fail(ex.getMessage()); |
| 1246 | + } |
| 1247 | + } |
| 1248 | + |
| 1249 | + /** |
| 1250 | + * Test listLogs with pagination. |
| 1251 | + */ |
| 1252 | + @Test |
| 1253 | + public void testListLogsWithPaging() { |
| 1254 | + |
| 1255 | + try { |
| 1256 | + ListLogsOptions.Builder listOptionsBuilder = new ListLogsOptions.Builder(workspaceId); |
| 1257 | + listOptionsBuilder.sort("-request_timestamp"); |
| 1258 | + listOptionsBuilder.filter("request.input.text:wipers"); |
| 1259 | + listOptionsBuilder.pageLimit(1L); |
| 1260 | + |
| 1261 | + LogCollectionResponse response = service.listLogs(listOptionsBuilder.build()).execute(); |
| 1262 | + assertNotNull(response); |
| 1263 | + assertNotNull(response.getLogs()); |
| 1264 | + assertNotNull(response.getPagination()); |
| 1265 | + // Empirically -- no refresh_url in pagination of listLogs |
| 1266 | + // assertNotNull(response.getPagination().getRefreshUrl()); |
| 1267 | + assertNotNull(response.getPagination().getNextUrl()); |
| 1268 | + assertNotNull(response.getPagination().getCursor()); |
| 1269 | + |
| 1270 | + assertTrue(response.getLogs().size() == 1); |
| 1271 | + LogExportResponse logEntry1 = response.getLogs().get(0); |
| 1272 | + |
| 1273 | + String cursor = response.getPagination().getCursor(); |
| 1274 | + response = service.listLogs(listOptionsBuilder.cursor(cursor).build()).execute(); |
| 1275 | + |
| 1276 | + assertNotNull(response.getLogs()); |
| 1277 | + assertTrue(response.getLogs().size() == 1); |
| 1278 | + |
| 1279 | + LogExportResponse logEntry2 = response.getLogs().get(0); |
| 1280 | + |
| 1281 | + Date requestDate1 = isoDateFormat.parse(logEntry1.getRequestTimestamp()); |
| 1282 | + Date requestDate2 = isoDateFormat.parse(logEntry2.getRequestTimestamp()); |
| 1283 | + |
| 1284 | + assertTrue(requestDate2.before(requestDate1)); |
| 1285 | + |
| 1286 | + } catch (Exception ex) { |
| 1287 | + fail(ex.getMessage()); |
| 1288 | + } |
| 1289 | + } |
1042 | 1290 | } |