Search in sources :

Example 6 with CreateDialogNodeOptions

use of com.ibm.watson.assistant.v1.model.CreateDialogNodeOptions in project java-sdk by watson-developer-cloud.

the class AssistantServiceIT method testListDialogNodes.

/**
 * Test listDialogNodes.
 */
@Test
public void testListDialogNodes() {
    String dialogNodeName = "Test" + UUID.randomUUID().toString();
    try {
        ListDialogNodesOptions listOptions = new ListDialogNodesOptions.Builder(workspaceId).build();
        DialogNodeCollection response = service.listDialogNodes(listOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getDialogNodes());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        // nextUrl may be null
        // Now add a dialog node and make sure we get it back
        String dialogNodeDescription = "Description of " + dialogNodeName;
        Date start = new Date();
        CreateDialogNodeOptions createOptions = new CreateDialogNodeOptions.Builder(workspaceId, dialogNodeName).description(dialogNodeDescription).build();
        service.createDialogNode(createOptions).execute();
        long count = response.getDialogNodes().size();
        ListDialogNodesOptions listOptions2 = new ListDialogNodesOptions.Builder(workspaceId).pageLimit(count + 1).includeAudit(true).build();
        DialogNodeCollection response2 = service.listDialogNodes(listOptions2).execute();
        assertNotNull(response2);
        assertNotNull(response2.getDialogNodes());
        List<DialogNode> dialogNodes = response2.getDialogNodes();
        assertTrue(dialogNodes.size() > count);
        DialogNode dialogResponse = null;
        for (DialogNode node : dialogNodes) {
            if (node.getDialogNodeId().equals(dialogNodeName)) {
                dialogResponse = node;
                break;
            }
        }
        assertNotNull(dialogResponse);
        assertNotNull(dialogResponse.getDescription());
        assertEquals(dialogResponse.getDescription(), dialogNodeDescription);
        Date now = new Date();
        assertTrue(fuzzyBefore(dialogResponse.getCreated(), now));
        assertTrue(fuzzyAfter(dialogResponse.getCreated(), start));
        assertTrue(fuzzyBefore(dialogResponse.getUpdated(), now));
        assertTrue(fuzzyAfter(dialogResponse.getUpdated(), start));
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteDialogNodeOptions deleteOptions = new DeleteDialogNodeOptions.Builder(workspaceId, dialogNodeName).build();
        service.deleteDialogNode(deleteOptions).execute();
    }
}
Also used : CreateDialogNodeOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateDialogNodeOptions) ListDialogNodesOptions(com.ibm.watson.developer_cloud.assistant.v1.model.ListDialogNodesOptions) DialogNodeCollection(com.ibm.watson.developer_cloud.assistant.v1.model.DialogNodeCollection) DialogNode(com.ibm.watson.developer_cloud.assistant.v1.model.DialogNode) Date(java.util.Date) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) DeleteDialogNodeOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteDialogNodeOptions) Test(org.junit.Test)

Example 7 with CreateDialogNodeOptions

use of com.ibm.watson.assistant.v1.model.CreateDialogNodeOptions in project java-sdk by watson-developer-cloud.

the class ConversationServiceIT method testListDialogNodesWithPaging.

/**
 * Test listDialogNodes with pagination.
 */
@Test
public void testListDialogNodesWithPaging() {
    String dialogNodeName1 = "First" + UUID.randomUUID().toString();
    String dialogNodeName2 = "Second" + UUID.randomUUID().toString();
    CreateDialogNodeOptions createOptions = new CreateDialogNodeOptions.Builder(workspaceId, dialogNodeName1).build();
    service.createDialogNode(createOptions).execute();
    service.createDialogNode(createOptions.newBuilder().dialogNode(dialogNodeName2).build()).execute();
    try {
        ListDialogNodesOptions listOptions = new ListDialogNodesOptions.Builder().workspaceId(workspaceId).pageLimit(1L).sort("dialog_node").build();
        DialogNodeCollection response = service.listDialogNodes(listOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getDialogNodes());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        assertNotNull(response.getPagination().getNextUrl());
        assertNotNull(response.getPagination().getCursor());
        boolean found1 = false, found2 = false;
        while (true) {
            assertNotNull(response.getDialogNodes());
            assertTrue(response.getDialogNodes().size() == 1);
            found1 |= response.getDialogNodes().get(0).getDialogNodeId().equals(dialogNodeName1);
            found2 |= response.getDialogNodes().get(0).getDialogNodeId().equals(dialogNodeName2);
            // verify sort
            assertTrue(found1 || !found2);
            if (response.getPagination().getCursor() == null) {
                break;
            }
            String cursor = response.getPagination().getCursor();
            response = service.listDialogNodes(listOptions.newBuilder().cursor(cursor).build()).execute();
        }
        assertTrue(found1 && found2);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteDialogNodeOptions deleteOptions = new DeleteDialogNodeOptions.Builder(workspaceId, dialogNodeName1).build();
        service.deleteDialogNode(deleteOptions).execute();
        service.deleteDialogNode(deleteOptions.newBuilder().dialogNode(dialogNodeName2).build()).execute();
    }
}
Also used : DeleteDialogNodeOptions(com.ibm.watson.developer_cloud.conversation.v1.model.DeleteDialogNodeOptions) CreateDialogNodeOptions(com.ibm.watson.developer_cloud.conversation.v1.model.CreateDialogNodeOptions) ListDialogNodesOptions(com.ibm.watson.developer_cloud.conversation.v1.model.ListDialogNodesOptions) DialogNodeCollection(com.ibm.watson.developer_cloud.conversation.v1.model.DialogNodeCollection) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Test(org.junit.Test)

Example 8 with CreateDialogNodeOptions

use of com.ibm.watson.assistant.v1.model.CreateDialogNodeOptions in project java-sdk by watson-developer-cloud.

the class Assistant method createDialogNode.

/**
 * Create dialog node.
 *
 * <p>Create a new dialog node.
 *
 * <p>If you want to create multiple dialog nodes with a single API call, consider using the
 * **[Update workspace](#update-workspace)** method instead.
 *
 * @param createDialogNodeOptions the {@link CreateDialogNodeOptions} containing the options for
 *     the call
 * @return a {@link ServiceCall} with a result of type {@link DialogNode}
 */
public ServiceCall<DialogNode> createDialogNode(CreateDialogNodeOptions createDialogNodeOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(createDialogNodeOptions, "createDialogNodeOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", createDialogNodeOptions.workspaceId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/dialog_nodes", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "createDialogNode");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    if (createDialogNodeOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(createDialogNodeOptions.includeAudit()));
    }
    final JsonObject contentJson = new JsonObject();
    contentJson.addProperty("dialog_node", createDialogNodeOptions.dialogNode());
    if (createDialogNodeOptions.description() != null) {
        contentJson.addProperty("description", createDialogNodeOptions.description());
    }
    if (createDialogNodeOptions.conditions() != null) {
        contentJson.addProperty("conditions", createDialogNodeOptions.conditions());
    }
    if (createDialogNodeOptions.parent() != null) {
        contentJson.addProperty("parent", createDialogNodeOptions.parent());
    }
    if (createDialogNodeOptions.previousSibling() != null) {
        contentJson.addProperty("previous_sibling", createDialogNodeOptions.previousSibling());
    }
    if (createDialogNodeOptions.output() != null) {
        contentJson.add("output", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(createDialogNodeOptions.output()));
    }
    if (createDialogNodeOptions.context() != null) {
        contentJson.add("context", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(createDialogNodeOptions.context()));
    }
    if (createDialogNodeOptions.metadata() != null) {
        contentJson.add("metadata", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(createDialogNodeOptions.metadata()));
    }
    if (createDialogNodeOptions.nextStep() != null) {
        contentJson.add("next_step", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(createDialogNodeOptions.nextStep()));
    }
    if (createDialogNodeOptions.title() != null) {
        contentJson.addProperty("title", createDialogNodeOptions.title());
    }
    if (createDialogNodeOptions.type() != null) {
        contentJson.addProperty("type", createDialogNodeOptions.type());
    }
    if (createDialogNodeOptions.eventName() != null) {
        contentJson.addProperty("event_name", createDialogNodeOptions.eventName());
    }
    if (createDialogNodeOptions.variable() != null) {
        contentJson.addProperty("variable", createDialogNodeOptions.variable());
    }
    if (createDialogNodeOptions.actions() != null) {
        contentJson.add("actions", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(createDialogNodeOptions.actions()));
    }
    if (createDialogNodeOptions.digressIn() != null) {
        contentJson.addProperty("digress_in", createDialogNodeOptions.digressIn());
    }
    if (createDialogNodeOptions.digressOut() != null) {
        contentJson.addProperty("digress_out", createDialogNodeOptions.digressOut());
    }
    if (createDialogNodeOptions.digressOutSlots() != null) {
        contentJson.addProperty("digress_out_slots", createDialogNodeOptions.digressOutSlots());
    }
    if (createDialogNodeOptions.userLabel() != null) {
        contentJson.addProperty("user_label", createDialogNodeOptions.userLabel());
    }
    if (createDialogNodeOptions.disambiguationOptOut() != null) {
        contentJson.addProperty("disambiguation_opt_out", createDialogNodeOptions.disambiguationOptOut());
    }
    builder.bodyJson(contentJson);
    ResponseConverter<DialogNode> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DialogNode>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) DialogNode(com.ibm.watson.assistant.v1.model.DialogNode)

Example 9 with CreateDialogNodeOptions

use of com.ibm.watson.assistant.v1.model.CreateDialogNodeOptions in project java-sdk by watson-developer-cloud.

the class AssistantServiceIT method testListDialogNodesWithPaging.

/**
 * Test listDialogNodes with pagination.
 */
@Test
public void testListDialogNodesWithPaging() {
    String dialogNodeName1 = "First" + UUID.randomUUID().toString();
    String dialogNodeName2 = "Second" + UUID.randomUUID().toString();
    CreateDialogNodeOptions createOptions = new CreateDialogNodeOptions.Builder(workspaceId, dialogNodeName1).build();
    service.createDialogNode(createOptions).execute();
    service.createDialogNode(createOptions.newBuilder().dialogNode(dialogNodeName2).build()).execute();
    try {
        ListDialogNodesOptions listOptions = new ListDialogNodesOptions.Builder().workspaceId(workspaceId).pageLimit(1L).sort("dialog_node").build();
        DialogNodeCollection response = service.listDialogNodes(listOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getDialogNodes());
        assertNotNull(response.getPagination());
        assertNotNull(response.getPagination().getRefreshUrl());
        assertNotNull(response.getPagination().getNextUrl());
        assertNotNull(response.getPagination().getCursor());
        boolean found1 = false, found2 = false;
        while (true) {
            assertNotNull(response.getDialogNodes());
            assertTrue(response.getDialogNodes().size() == 1);
            found1 |= response.getDialogNodes().get(0).getDialogNodeId().equals(dialogNodeName1);
            found2 |= response.getDialogNodes().get(0).getDialogNodeId().equals(dialogNodeName2);
            // verify sort
            assertTrue(found1 || !found2);
            if (response.getPagination().getCursor() == null) {
                break;
            }
            String cursor = response.getPagination().getCursor();
            response = service.listDialogNodes(listOptions.newBuilder().cursor(cursor).build()).execute();
        }
        assertTrue(found1 && found2);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteDialogNodeOptions deleteOptions = new DeleteDialogNodeOptions.Builder(workspaceId, dialogNodeName1).build();
        service.deleteDialogNode(deleteOptions).execute();
        service.deleteDialogNode(deleteOptions.newBuilder().dialogNode(dialogNodeName2).build()).execute();
    }
}
Also used : DeleteDialogNodeOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteDialogNodeOptions) CreateDialogNodeOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateDialogNodeOptions) ListDialogNodesOptions(com.ibm.watson.developer_cloud.assistant.v1.model.ListDialogNodesOptions) DialogNodeCollection(com.ibm.watson.developer_cloud.assistant.v1.model.DialogNodeCollection) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Test(org.junit.Test)

Example 10 with CreateDialogNodeOptions

use of com.ibm.watson.assistant.v1.model.CreateDialogNodeOptions in project java-sdk by watson-developer-cloud.

the class AssistantServiceIT method testUpdateDialogNode.

/**
 * Test updateDialogNode.
 */
@Test
public void testUpdateDialogNode() {
    String dialogNodeName = "Test" + UUID.randomUUID().toString();
    String dialogNodeDescription = "Description of " + dialogNodeName;
    CreateDialogNodeOptions createOptions = new CreateDialogNodeOptions.Builder(workspaceId, dialogNodeName).description(dialogNodeDescription).build();
    service.createDialogNode(createOptions).execute();
    String dialogNodeName2 = "Test2" + UUID.randomUUID().toString();
    try {
        String dialogNodeDescription2 = "Updated description of " + dialogNodeName;
        UpdateDialogNodeOptions updateOptions = new UpdateDialogNodeOptions.Builder().workspaceId(workspaceId).dialogNode(dialogNodeName).newDialogNode(dialogNodeName2).newDescription(dialogNodeDescription2).build();
        DialogNode response = service.updateDialogNode(updateOptions).execute();
        assertNotNull(response);
        assertNotNull(response.getDialogNodeId());
        assertEquals(response.getDialogNodeId(), dialogNodeName2);
        assertNotNull(response.getDescription());
        assertEquals(response.getDescription(), dialogNodeDescription2);
    } catch (Exception ex) {
        fail(ex.getMessage());
    } finally {
        // Clean up
        DeleteDialogNodeOptions deleteOptions = new DeleteDialogNodeOptions.Builder(workspaceId, dialogNodeName2).build();
        service.deleteDialogNode(deleteOptions).execute();
    }
}
Also used : DeleteDialogNodeOptions(com.ibm.watson.developer_cloud.assistant.v1.model.DeleteDialogNodeOptions) CreateDialogNodeOptions(com.ibm.watson.developer_cloud.assistant.v1.model.CreateDialogNodeOptions) DialogNode(com.ibm.watson.developer_cloud.assistant.v1.model.DialogNode) UpdateDialogNodeOptions(com.ibm.watson.developer_cloud.assistant.v1.model.UpdateDialogNodeOptions) UnauthorizedException(com.ibm.watson.developer_cloud.service.exception.UnauthorizedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)12 UnauthorizedException (com.ibm.watson.developer_cloud.service.exception.UnauthorizedException)12 CreateDialogNodeOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateDialogNodeOptions)7 CreateDialogNodeOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateDialogNodeOptions)7 DeleteDialogNodeOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteDialogNodeOptions)6 DeleteDialogNodeOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteDialogNodeOptions)6 DialogNode (com.ibm.watson.developer_cloud.assistant.v1.model.DialogNode)4 DialogNode (com.ibm.watson.developer_cloud.conversation.v1.model.DialogNode)4 Date (java.util.Date)4 DialogNode (com.ibm.watson.assistant.v1.model.DialogNode)2 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 DialogNodeCollection (com.ibm.watson.developer_cloud.assistant.v1.model.DialogNodeCollection)2 GetDialogNodeOptions (com.ibm.watson.developer_cloud.assistant.v1.model.GetDialogNodeOptions)2 ListDialogNodesOptions (com.ibm.watson.developer_cloud.assistant.v1.model.ListDialogNodesOptions)2 DialogNodeCollection (com.ibm.watson.developer_cloud.conversation.v1.model.DialogNodeCollection)2 GetDialogNodeOptions (com.ibm.watson.developer_cloud.conversation.v1.model.GetDialogNodeOptions)2 ListDialogNodesOptions (com.ibm.watson.developer_cloud.conversation.v1.model.ListDialogNodesOptions)2 HashMap (java.util.HashMap)2 JsonObject (com.google.gson.JsonObject)1