-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: add page management code samples (#300)
* 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 <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
eac0c4b
commit 52f3092
Showing
4 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateSimplePage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
} |
64 changes: 64 additions & 0 deletions
64
dialogflow-cx/snippets/src/main/java/dialogflow/cx/DeletePage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
} |
62 changes: 62 additions & 0 deletions
62
dialogflow-cx/snippets/src/main/java/dialogflow/cx/ListPages.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
} |
105 changes: 105 additions & 0 deletions
105
dialogflow-cx/snippets/src/test/java/dialogflow/cx/PageManagementIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(""); | ||
} | ||
} | ||
} |