Skip to content

Commit fd7d74c

Browse files
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>
1 parent 5f69721 commit fd7d74c

File tree

4 files changed

+300
-0
lines changed

4 files changed

+300
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dialogflow.cx;
18+
19+
// [START dialogflow_cx_create_page]
20+
import com.google.cloud.dialogflow.cx.v3.CreatePageRequest;
21+
import com.google.cloud.dialogflow.cx.v3.Page;
22+
import com.google.cloud.dialogflow.cx.v3.PagesClient;
23+
import java.io.IOException;
24+
25+
public class CreateSimplePage {
26+
27+
public static void main(String[] args) throws IOException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String projectId = "my-project-id";
30+
String agentId = "my-agent-id";
31+
String flowId = "my-flow-id";
32+
String location = "my-location";
33+
String displayName = "my-display-name";
34+
35+
createPage(projectId, agentId, flowId, location, displayName);
36+
}
37+
38+
// DialogFlow API Create Page Sample.
39+
// Creates a page from the provided parameters
40+
public static Page createPage(
41+
String projectId, String agentId, String flowId, String location, String displayName)
42+
throws IOException {
43+
Page response;
44+
CreatePageRequest.Builder createRequestBuilder = CreatePageRequest.newBuilder();
45+
Page.Builder pageBuilder = Page.newBuilder();
46+
47+
pageBuilder.setDisplayName(displayName);
48+
49+
createRequestBuilder
50+
.setParent(
51+
"projects/"
52+
+ projectId
53+
+ "/locations/"
54+
+ location
55+
+ "/agents/"
56+
+ agentId
57+
+ "/flows/"
58+
+ flowId)
59+
.setPage(pageBuilder);
60+
61+
// Make API request to create page
62+
try (PagesClient client = PagesClient.create()) {
63+
response = client.createPage(createRequestBuilder.build());
64+
System.out.println("Successfully created page!");
65+
return response;
66+
}
67+
}
68+
// [END dialogflow_cx_create_page]
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dialogflow.cx;
18+
19+
// [START dialogflow_cx_delete_page]
20+
import com.google.cloud.dialogflow.cx.v3.DeletePageRequest;
21+
import com.google.cloud.dialogflow.cx.v3.DeletePageRequest.Builder;
22+
import com.google.cloud.dialogflow.cx.v3.PagesClient;
23+
import java.io.IOException;
24+
25+
public class DeletePage {
26+
27+
public static void main(String[] args) throws IOException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String projectId = "my-project-id";
30+
String agentId = "my-agent-id";
31+
String flowId = "my-flow-id";
32+
String pageId = "my-page-id";
33+
String location = "my-location";
34+
35+
deletePage(projectId, agentId, flowId, pageId, location);
36+
}
37+
38+
// DialogFlow API Delete Page Sample.
39+
// Deletes a page from the provided parameters
40+
public static void deletePage(
41+
String projectId, String agentId, String flowId, String pageId, String location)
42+
throws IOException {
43+
try (PagesClient client = PagesClient.create()) {
44+
Builder deleteRequestBuilder = DeletePageRequest.newBuilder();
45+
46+
deleteRequestBuilder.setName(
47+
"projects/"
48+
+ projectId
49+
+ "/locations/"
50+
+ location
51+
+ "/agents/"
52+
+ agentId
53+
+ "/flows/"
54+
+ flowId
55+
+ "/pages/"
56+
+ pageId);
57+
58+
// Make API request to delete page
59+
client.deletePage(deleteRequestBuilder.build());
60+
System.out.println("Successfully deleted page!");
61+
}
62+
}
63+
// [END dialogflow_cx_delete_page]
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dialogflow.cx;
18+
19+
// [START dialogflow_cx_list_pages]
20+
import com.google.cloud.dialogflow.cx.v3.ListPagesRequest;
21+
import com.google.cloud.dialogflow.cx.v3.ListPagesRequest.Builder;
22+
import com.google.cloud.dialogflow.cx.v3.Page;
23+
import com.google.cloud.dialogflow.cx.v3.PagesClient;
24+
import java.io.IOException;
25+
26+
public class ListPages {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "my-project-id";
31+
String agentId = "my-agent-id";
32+
String flowId = "my-flow-id";
33+
String location = "my-location";
34+
35+
listPages(projectId, agentId, flowId, location);
36+
}
37+
38+
// DialogFlow API List Pages Sample.
39+
// Lists all pages from the provided parameters
40+
public static void listPages(String projectId, String agentId, String flowId, String location)
41+
throws IOException {
42+
PagesClient client = PagesClient.create();
43+
Builder listRequestBuilder = ListPagesRequest.newBuilder();
44+
45+
listRequestBuilder.setParent(
46+
"projects/"
47+
+ projectId
48+
+ "/locations/"
49+
+ location
50+
+ "/agents/"
51+
+ agentId
52+
+ "/flows/"
53+
+ flowId);
54+
listRequestBuilder.setLanguageCode("en");
55+
56+
// Make API request to list all pages in the project
57+
for (Page element : client.listPages(listRequestBuilder.build()).iterateAll()) {
58+
System.out.println(element);
59+
}
60+
}
61+
// [END dialogflow_cx_list_pages]
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dialogflow.cx;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.dialogflow.cx.v3.Agent;
22+
import com.google.cloud.dialogflow.cx.v3.Agent.Builder;
23+
import com.google.cloud.dialogflow.cx.v3.AgentsClient;
24+
import com.google.cloud.dialogflow.cx.v3.AgentsSettings;
25+
import com.google.cloud.dialogflow.cx.v3.Page;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.IOException;
28+
import java.io.PrintStream;
29+
import java.util.UUID;
30+
import org.junit.AfterClass;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
34+
/** Integration (system) tests for {@link PageManagment}. */
35+
public class PageManagementIT {
36+
37+
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT");
38+
private static String flowID = "00000000-0000-0000-0000-000000000000";
39+
private static String location = "global";
40+
private static String displayName = "temp_page_" + UUID.randomUUID().toString();
41+
private static String parent;
42+
private static String agentID;
43+
private static String pageID;
44+
private static ByteArrayOutputStream stdOut;
45+
46+
@BeforeClass
47+
public static void setUp() throws IOException {
48+
stdOut = new ByteArrayOutputStream();
49+
System.setOut(new PrintStream(stdOut));
50+
Builder build = Agent.newBuilder();
51+
build.setDefaultLanguageCode("en");
52+
build.setDisplayName("temp_agent_" + UUID.randomUUID().toString());
53+
build.setTimeZone("America/Los_Angeles");
54+
55+
Agent agent = build.build();
56+
57+
String apiEndpoint = "global-dialogflow.googleapis.com:443";
58+
String parentPath = "projects/" + PROJECT_ID + "/locations/global";
59+
60+
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
61+
AgentsClient client = AgentsClient.create(agentsSettings);
62+
63+
parent = client.createAgent(parentPath, agent).getName();
64+
agentID = parent.split("/")[5];
65+
}
66+
67+
@AfterClass
68+
public static void tearDown() throws IOException {
69+
String apiEndpoint = "global-dialogflow.googleapis.com:443";
70+
String parentPath = "projects/" + PROJECT_ID + "/locations/global";
71+
72+
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
73+
AgentsClient client = AgentsClient.create(agentsSettings);
74+
75+
client.deleteAgent(parent);
76+
}
77+
78+
@Test
79+
public void testCreatePage() throws IOException {
80+
Page p = CreateSimplePage.createPage(PROJECT_ID, agentID, flowID, location, displayName);
81+
pageID = p.getName().split("/")[9];
82+
83+
assertThat(p.getDisplayName()).isEqualTo(displayName);
84+
}
85+
86+
@Test
87+
public void testListPages() throws IOException {
88+
String name = "temp_page_" + UUID.randomUUID().toString();
89+
90+
Page p = CreateSimplePage.createPage(PROJECT_ID, agentID, flowID, location, name);
91+
92+
ListPages.listPages(PROJECT_ID, agentID, flowID, location);
93+
assertThat(stdOut.toString()).contains(name);
94+
}
95+
96+
@Test
97+
public void testDeletePage() throws IOException {
98+
try {
99+
DeletePage.deletePage(PROJECT_ID, agentID, flowID, pageID, location);
100+
assertThat(1).isEqualTo(1);
101+
} catch (Exception e) {
102+
assertThat(e).isEqualTo("");
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)