Skip to content

Commit 226cb71

Browse files
test(assistantv1): add new tests for new methods
1 parent 64a2622 commit 226cb71

File tree

1 file changed

+223
-0
lines changed

1 file changed

+223
-0
lines changed

assistant/src/test/java/com/ibm/watson/assistant/v1/AssistantServiceIT.java

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.util.List;
3939
import java.util.Map;
4040
import java.util.UUID;
41+
import java.util.concurrent.CountDownLatch;
42+
import java.util.concurrent.TimeUnit;
4143
import org.junit.Before;
4244
import org.junit.Ignore;
4345
import org.junit.Test;
@@ -52,6 +54,7 @@ public class AssistantServiceIT extends AssistantServiceTest {
5254
private String workspaceId;
5355

5456
private DateFormat isoDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
57+
private CountDownLatch lock = new CountDownLatch(1);
5558

5659
/**
5760
* Sets up the tests.
@@ -1464,6 +1467,226 @@ public void testUpdateWorkspace() {
14641467
}
14651468
}
14661469

1470+
/** Test createWorkspaceAsync, exportWorkspaceAsync, and updateWorkspaceAsync */
1471+
@Test
1472+
public void testWorkspaceAsyncSuite() {
1473+
1474+
String workspaceName = "API Test " + UUID.randomUUID().toString(); // gotta be unique
1475+
String workspaceDescription = "Description of " + workspaceName;
1476+
String workspaceLanguage = "en";
1477+
1478+
// metadata
1479+
Map<String, Object> workspaceMetadata = new HashMap<String, Object>();
1480+
String metadataValue = "value for " + workspaceName;
1481+
workspaceMetadata.put("key", metadataValue);
1482+
1483+
// intents
1484+
List<CreateIntent> workspaceIntents = new ArrayList<CreateIntent>();
1485+
String intentName = "Hello" + UUID.randomUUID().toString(); // gotta be unique
1486+
String intentDescription = "Description of " + intentName;
1487+
String intentExample = "Example of " + intentName;
1488+
List<Example> intentExamples = new ArrayList<>();
1489+
intentExamples.add(new Example.Builder().text(intentExample).build());
1490+
workspaceIntents.add(
1491+
new CreateIntent.Builder()
1492+
.intent(intentName)
1493+
.description(intentDescription)
1494+
.examples(intentExamples)
1495+
.build());
1496+
1497+
// entities
1498+
List<CreateEntity> workspaceEntities = new ArrayList<CreateEntity>();
1499+
String entityName = "Hello" + UUID.randomUUID().toString(); // gotta be unique
1500+
String entityDescription = "Description of " + entityName;
1501+
String entityValue = "Value of " + entityName;
1502+
String entityValueSynonym = "Synonym for Value of " + entityName;
1503+
List<CreateValue> entityValues = new ArrayList<CreateValue>();
1504+
entityValues.add(
1505+
new CreateValue.Builder().value(entityValue).addSynonym(entityValueSynonym).build());
1506+
workspaceEntities.add(
1507+
new CreateEntity.Builder()
1508+
.entity(entityName)
1509+
.description(entityDescription)
1510+
.values(entityValues)
1511+
.build());
1512+
1513+
// counterexamples
1514+
List<Counterexample> workspaceCounterExamples = new ArrayList<>();
1515+
String counterExampleText = "Counterexample for " + workspaceName;
1516+
workspaceCounterExamples.add(new Counterexample.Builder().text(counterExampleText).build());
1517+
1518+
// systemSettings
1519+
WorkspaceSystemSettingsDisambiguation disambiguation =
1520+
new WorkspaceSystemSettingsDisambiguation.Builder()
1521+
.enabled(true)
1522+
.noneOfTheAbovePrompt("none of the above")
1523+
.prompt("prompt")
1524+
.sensitivity(WorkspaceSystemSettingsDisambiguation.Sensitivity.HIGH)
1525+
.build();
1526+
WorkspaceSystemSettingsTooling tooling =
1527+
new WorkspaceSystemSettingsTooling.Builder().storeGenericResponses(true).build();
1528+
WorkspaceSystemSettings systemSettings =
1529+
new WorkspaceSystemSettings.Builder()
1530+
.disambiguation(disambiguation)
1531+
.tooling(tooling)
1532+
.build();
1533+
1534+
// webhooks
1535+
String webhookHeaderName = "Webhook-Header";
1536+
String webhookHeaderValue = "webhook_header_value";
1537+
String webhookName = "java-sdk-test-webhook";
1538+
String webhookUrl = "https://github.com/watson-developer-cloud/java-sdk";
1539+
WebhookHeader webhookHeader =
1540+
new WebhookHeader.Builder().name(webhookHeaderName).value(webhookHeaderValue).build();
1541+
Webhook webhook =
1542+
new Webhook.Builder().name(webhookName).url(webhookUrl).addHeaders(webhookHeader).build();
1543+
1544+
CreateWorkspaceAsyncOptions createAsyncOptions =
1545+
new CreateWorkspaceAsyncOptions.Builder()
1546+
.name(workspaceName)
1547+
.description(workspaceDescription)
1548+
.language(workspaceLanguage)
1549+
.metadata(workspaceMetadata)
1550+
.intents(workspaceIntents)
1551+
.entities(workspaceEntities)
1552+
.counterexamples(workspaceCounterExamples)
1553+
.systemSettings(systemSettings)
1554+
.addWebhooks(webhook)
1555+
.build();
1556+
1557+
String workspaceId = null;
1558+
try {
1559+
Workspace response = service.createWorkspaceAsync(createAsyncOptions).execute().getResult();
1560+
assertNotNull(response);
1561+
assertNotNull(response.getWorkspaceId());
1562+
ExportWorkspaceAsyncOptions exportWorkspaceAsyncOptions =
1563+
new ExportWorkspaceAsyncOptions.Builder().workspaceId(response.getWorkspaceId()).build();
1564+
Workspace exportResponse =
1565+
service.exportWorkspaceAsync(exportWorkspaceAsyncOptions).execute().getResult();
1566+
1567+
for (int seconds = 0;
1568+
exportResponse.getStatus().equals("Processing") && seconds <= 60;
1569+
seconds += 5) {
1570+
lock.await(5, TimeUnit.SECONDS);
1571+
exportResponse =
1572+
service.exportWorkspaceAsync(exportWorkspaceAsyncOptions).execute().getResult();
1573+
}
1574+
1575+
workspaceId = exportResponse.getWorkspaceId();
1576+
assertNotNull(exportResponse.getName());
1577+
assertEquals(exportResponse.getName(), workspaceName);
1578+
assertNotNull(exportResponse.getDescription());
1579+
assertEquals(exportResponse.getDescription(), workspaceDescription);
1580+
assertNotNull(exportResponse.getLanguage());
1581+
assertEquals(exportResponse.getLanguage(), workspaceLanguage);
1582+
1583+
// metadata
1584+
assertNotNull(exportResponse.getMetadata());
1585+
assertNotNull(exportResponse.getMetadata().get("key"));
1586+
assertEquals(exportResponse.getMetadata().get("key"), metadataValue);
1587+
1588+
GetWorkspaceOptions getOptions =
1589+
new GetWorkspaceOptions.Builder(workspaceId).export(true).build();
1590+
Workspace exResponse = service.getWorkspace(getOptions).execute().getResult();
1591+
assertNotNull(exResponse);
1592+
1593+
// intents
1594+
assertNotNull(exResponse.getIntents());
1595+
assertTrue(exResponse.getIntents().size() == 1);
1596+
assertNotNull(exResponse.getIntents().get(0).getIntent());
1597+
assertEquals(exResponse.getIntents().get(0).getIntent(), intentName);
1598+
assertNotNull(exResponse.getIntents().get(0).getDescription());
1599+
assertEquals(exResponse.getIntents().get(0).getDescription(), intentDescription);
1600+
assertNotNull(exResponse.getIntents().get(0).getExamples());
1601+
assertTrue(exResponse.getIntents().get(0).getExamples().size() == 1);
1602+
assertNotNull(exResponse.getIntents().get(0).getExamples().get(0));
1603+
assertNotNull(exResponse.getIntents().get(0).getExamples().get(0).text());
1604+
assertEquals(exResponse.getIntents().get(0).getExamples().get(0).text(), intentExample);
1605+
1606+
// entities
1607+
assertNotNull(exResponse.getEntities());
1608+
assertTrue(exResponse.getEntities().size() == 1);
1609+
assertNotNull(exResponse.getEntities().get(0).getEntity());
1610+
assertEquals(exResponse.getEntities().get(0).getEntity(), entityName);
1611+
assertNotNull(exResponse.getEntities().get(0).getDescription());
1612+
assertEquals(exResponse.getEntities().get(0).getDescription(), entityDescription);
1613+
assertNotNull(exResponse.getEntities().get(0).getValues());
1614+
assertTrue(exResponse.getEntities().get(0).getValues().size() == 1);
1615+
assertNotNull(exResponse.getEntities().get(0).getValues().get(0).value());
1616+
assertEquals(exResponse.getEntities().get(0).getValues().get(0).value(), entityValue);
1617+
assertNotNull(exResponse.getEntities().get(0).getValues().get(0).synonyms());
1618+
assertTrue(exResponse.getEntities().get(0).getValues().get(0).synonyms().size() == 1);
1619+
assertEquals(
1620+
exResponse.getEntities().get(0).getValues().get(0).synonyms().get(0), entityValueSynonym);
1621+
1622+
// counterexamples
1623+
assertNotNull(exResponse.getCounterexamples());
1624+
assertTrue(exResponse.getCounterexamples().size() == 1);
1625+
assertNotNull(exResponse.getCounterexamples().get(0).text());
1626+
assertEquals(exResponse.getCounterexamples().get(0).text(), counterExampleText);
1627+
1628+
// systemSettings
1629+
assertNotNull(exResponse.getSystemSettings());
1630+
assertEquals(
1631+
exResponse.getSystemSettings().getDisambiguation().noneOfTheAbovePrompt(),
1632+
disambiguation.noneOfTheAbovePrompt());
1633+
assertEquals(
1634+
exResponse.getSystemSettings().getDisambiguation().sensitivity(),
1635+
disambiguation.sensitivity());
1636+
assertEquals(
1637+
exResponse.getSystemSettings().getDisambiguation().prompt(), disambiguation.prompt());
1638+
assertEquals(
1639+
exResponse.getSystemSettings().getDisambiguation().enabled(), disambiguation.enabled());
1640+
assertEquals(
1641+
exResponse.getSystemSettings().getTooling().storeGenericResponses(),
1642+
tooling.storeGenericResponses());
1643+
1644+
// webhooks
1645+
assertNotNull(exResponse.getWebhooks());
1646+
assertEquals(webhookName, exResponse.getWebhooks().get(0).name());
1647+
assertEquals(webhookUrl, exResponse.getWebhooks().get(0).url());
1648+
assertEquals(webhookHeaderName, exResponse.getWebhooks().get(0).headers().get(0).name());
1649+
assertEquals(webhookHeaderValue, exResponse.getWebhooks().get(0).headers().get(0).value());
1650+
1651+
String updatedDescription = "Angelo's First Java SDK IT";
1652+
UpdateWorkspaceAsyncOptions updateAsyncOptions =
1653+
new UpdateWorkspaceAsyncOptions.Builder()
1654+
.workspaceId(workspaceId)
1655+
.description(updatedDescription)
1656+
.build();
1657+
1658+
Workspace updateResponse =
1659+
service.updateWorkspaceAsync(updateAsyncOptions).execute().getResult();
1660+
1661+
assertNotNull(updateResponse);
1662+
assertNotNull(updateResponse.getWorkspaceId());
1663+
ExportWorkspaceAsyncOptions newExportWorkspaceAsyncOptions =
1664+
new ExportWorkspaceAsyncOptions.Builder()
1665+
.workspaceId(updateResponse.getWorkspaceId())
1666+
.build();
1667+
1668+
for (int seconds = 0;
1669+
updateResponse.getStatus().equals("Processing") && seconds <= 60;
1670+
seconds += 5) {
1671+
lock.await(5, TimeUnit.SECONDS);
1672+
updateResponse =
1673+
service.exportWorkspaceAsync(newExportWorkspaceAsyncOptions).execute().getResult();
1674+
}
1675+
1676+
assertEquals(updatedDescription, updateResponse.getDescription());
1677+
1678+
} catch (Exception ex) {
1679+
fail(ex.getMessage());
1680+
} finally {
1681+
// Clean up
1682+
if (workspaceId != null) {
1683+
DeleteWorkspaceOptions deleteOptions =
1684+
new DeleteWorkspaceOptions.Builder(workspaceId).build();
1685+
service.deleteWorkspace(deleteOptions).execute();
1686+
}
1687+
}
1688+
}
1689+
14671690
/** Test listLogs. */
14681691
@Test
14691692
@Ignore

0 commit comments

Comments
 (0)