From 52f3092807cc9fd0eab7d118f35f911750fda94b Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Tue, 14 Sep 2021 16:38:07 -0700 Subject: [PATCH] samples: add page management code samples (#300) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * samples: add page management code samples * Lint Fix * Lint Fix * lint fix * lint fix * failing test fix * Failing Test Fix * change private to public for agentID * lint fix * lint fix * lint fix * Failing Test Fix * failing test fix * fix lint * called getName instead of name for page * Failing test fix * added parameters to variables * Added return type to create page * lint fix * Fixed failing test * Fix java failing tests * fix test * Test Fix * test fix * changed to afterall beforeall * Changed before and after all * Lint fix * test fail fix * lint fix * chagned stdout to static * failing test fix * added stdout setup * removed loop * updated list tests * lint fix * moved stdout * Added return type to listpage * lint fix * changed list test * update test * lint fix * lint fix * Fix testListPage * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Revised code per comments * Lint fix * CreateSimplePage builder import fix * CreateSimplePage test fix * moved region tags * Revised code per commments * lint fix * Returned response * changed testListPage * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * removed results string Co-authored-by: Owl Bot --- .../java/dialogflow/cx/CreateSimplePage.java | 69 ++++++++++++ .../main/java/dialogflow/cx/DeletePage.java | 64 +++++++++++ .../main/java/dialogflow/cx/ListPages.java | 62 +++++++++++ .../java/dialogflow/cx/PageManagementIT.java | 105 ++++++++++++++++++ 4 files changed, 300 insertions(+) create mode 100644 dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateSimplePage.java create mode 100644 dialogflow-cx/snippets/src/main/java/dialogflow/cx/DeletePage.java create mode 100644 dialogflow-cx/snippets/src/main/java/dialogflow/cx/ListPages.java create mode 100644 dialogflow-cx/snippets/src/test/java/dialogflow/cx/PageManagementIT.java diff --git a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateSimplePage.java b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateSimplePage.java new file mode 100644 index 00000000000..e18b60eeb98 --- /dev/null +++ b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateSimplePage.java @@ -0,0 +1,69 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dialogflow.cx; + +// [START dialogflow_cx_create_page] +import com.google.cloud.dialogflow.cx.v3.CreatePageRequest; +import com.google.cloud.dialogflow.cx.v3.Page; +import com.google.cloud.dialogflow.cx.v3.PagesClient; +import java.io.IOException; + +public class CreateSimplePage { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String agentId = "my-agent-id"; + String flowId = "my-flow-id"; + String location = "my-location"; + String displayName = "my-display-name"; + + createPage(projectId, agentId, flowId, location, displayName); + } + + // DialogFlow API Create Page Sample. + // Creates a page from the provided parameters + public static Page createPage( + String projectId, String agentId, String flowId, String location, String displayName) + throws IOException { + Page response; + CreatePageRequest.Builder createRequestBuilder = CreatePageRequest.newBuilder(); + Page.Builder pageBuilder = Page.newBuilder(); + + pageBuilder.setDisplayName(displayName); + + createRequestBuilder + .setParent( + "projects/" + + projectId + + "/locations/" + + location + + "/agents/" + + agentId + + "/flows/" + + flowId) + .setPage(pageBuilder); + + // Make API request to create page + try (PagesClient client = PagesClient.create()) { + response = client.createPage(createRequestBuilder.build()); + System.out.println("Successfully created page!"); + return response; + } + } + // [END dialogflow_cx_create_page] +} diff --git a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/DeletePage.java b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/DeletePage.java new file mode 100644 index 00000000000..98bd5e1fdea --- /dev/null +++ b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/DeletePage.java @@ -0,0 +1,64 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dialogflow.cx; + +// [START dialogflow_cx_delete_page] +import com.google.cloud.dialogflow.cx.v3.DeletePageRequest; +import com.google.cloud.dialogflow.cx.v3.DeletePageRequest.Builder; +import com.google.cloud.dialogflow.cx.v3.PagesClient; +import java.io.IOException; + +public class DeletePage { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String agentId = "my-agent-id"; + String flowId = "my-flow-id"; + String pageId = "my-page-id"; + String location = "my-location"; + + deletePage(projectId, agentId, flowId, pageId, location); + } + + // DialogFlow API Delete Page Sample. + // Deletes a page from the provided parameters + public static void deletePage( + String projectId, String agentId, String flowId, String pageId, String location) + throws IOException { + try (PagesClient client = PagesClient.create()) { + Builder deleteRequestBuilder = DeletePageRequest.newBuilder(); + + deleteRequestBuilder.setName( + "projects/" + + projectId + + "/locations/" + + location + + "/agents/" + + agentId + + "/flows/" + + flowId + + "/pages/" + + pageId); + + // Make API request to delete page + client.deletePage(deleteRequestBuilder.build()); + System.out.println("Successfully deleted page!"); + } + } + // [END dialogflow_cx_delete_page] +} diff --git a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/ListPages.java b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/ListPages.java new file mode 100644 index 00000000000..f935b638911 --- /dev/null +++ b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/ListPages.java @@ -0,0 +1,62 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dialogflow.cx; + +// [START dialogflow_cx_list_pages] +import com.google.cloud.dialogflow.cx.v3.ListPagesRequest; +import com.google.cloud.dialogflow.cx.v3.ListPagesRequest.Builder; +import com.google.cloud.dialogflow.cx.v3.Page; +import com.google.cloud.dialogflow.cx.v3.PagesClient; +import java.io.IOException; + +public class ListPages { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String agentId = "my-agent-id"; + String flowId = "my-flow-id"; + String location = "my-location"; + + listPages(projectId, agentId, flowId, location); + } + + // DialogFlow API List Pages Sample. + // Lists all pages from the provided parameters + public static void listPages(String projectId, String agentId, String flowId, String location) + throws IOException { + PagesClient client = PagesClient.create(); + Builder listRequestBuilder = ListPagesRequest.newBuilder(); + + listRequestBuilder.setParent( + "projects/" + + projectId + + "/locations/" + + location + + "/agents/" + + agentId + + "/flows/" + + flowId); + listRequestBuilder.setLanguageCode("en"); + + // Make API request to list all pages in the project + for (Page element : client.listPages(listRequestBuilder.build()).iterateAll()) { + System.out.println(element); + } + } + // [END dialogflow_cx_list_pages] +} diff --git a/dialogflow-cx/snippets/src/test/java/dialogflow/cx/PageManagementIT.java b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/PageManagementIT.java new file mode 100644 index 00000000000..d0e8fac930a --- /dev/null +++ b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/PageManagementIT.java @@ -0,0 +1,105 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dialogflow.cx; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.cloud.dialogflow.cx.v3.Agent; +import com.google.cloud.dialogflow.cx.v3.Agent.Builder; +import com.google.cloud.dialogflow.cx.v3.AgentsClient; +import com.google.cloud.dialogflow.cx.v3.AgentsSettings; +import com.google.cloud.dialogflow.cx.v3.Page; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +/** Integration (system) tests for {@link PageManagment}. */ +public class PageManagementIT { + + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + private static String flowID = "00000000-0000-0000-0000-000000000000"; + private static String location = "global"; + private static String displayName = "temp_page_" + UUID.randomUUID().toString(); + private static String parent; + private static String agentID; + private static String pageID; + private static ByteArrayOutputStream stdOut; + + @BeforeClass + public static void setUp() throws IOException { + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + Builder build = Agent.newBuilder(); + build.setDefaultLanguageCode("en"); + build.setDisplayName("temp_agent_" + UUID.randomUUID().toString()); + build.setTimeZone("America/Los_Angeles"); + + Agent agent = build.build(); + + String apiEndpoint = "global-dialogflow.googleapis.com:443"; + String parentPath = "projects/" + PROJECT_ID + "/locations/global"; + + AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build(); + AgentsClient client = AgentsClient.create(agentsSettings); + + parent = client.createAgent(parentPath, agent).getName(); + agentID = parent.split("/")[5]; + } + + @AfterClass + public static void tearDown() throws IOException { + String apiEndpoint = "global-dialogflow.googleapis.com:443"; + String parentPath = "projects/" + PROJECT_ID + "/locations/global"; + + AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build(); + AgentsClient client = AgentsClient.create(agentsSettings); + + client.deleteAgent(parent); + } + + @Test + public void testCreatePage() throws IOException { + Page p = CreateSimplePage.createPage(PROJECT_ID, agentID, flowID, location, displayName); + pageID = p.getName().split("/")[9]; + + assertThat(p.getDisplayName()).isEqualTo(displayName); + } + + @Test + public void testListPages() throws IOException { + String name = "temp_page_" + UUID.randomUUID().toString(); + + Page p = CreateSimplePage.createPage(PROJECT_ID, agentID, flowID, location, name); + + ListPages.listPages(PROJECT_ID, agentID, flowID, location); + assertThat(stdOut.toString()).contains(name); + } + + @Test + public void testDeletePage() throws IOException { + try { + DeletePage.deletePage(PROJECT_ID, agentID, flowID, pageID, location); + assertThat(1).isEqualTo(1); + } catch (Exception e) { + assertThat(e).isEqualTo(""); + } + } +}