From c5c6e7c59a9ff0525f095ae3ccf5efcaae1e2d55 Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Mon, 23 Aug 2021 09:57:55 -0700 Subject: [PATCH] samples: add create agent code sample (#279) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * samples: add create agent code sample * Lint fix * Fix failing test * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Rebased * Revised Code per comments * lint fix * Added Comment for timezone * Fixed lint * Used String.format * Added stdOut Var * removed parent variable * Update Tests * lint fix * removed parentPath * removed unreachable statemt * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Revised Code Per Comments * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot --- .../main/java/dialogflow/cx/CreateAgent.java | 62 ++++++++++++++++++ .../java/dialogflow/cx/CreateAgentIT.java | 64 +++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateAgent.java create mode 100644 dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateAgentIT.java diff --git a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateAgent.java b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateAgent.java new file mode 100644 index 00000000000..2ab019c1e9c --- /dev/null +++ b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateAgent.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_create_agent] + +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 java.io.IOException; + +public class CreateAgent { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String displayName = "my-display-name"; + + createAgent(projectId, displayName); + } + + public static Agent createAgent(String parent, String displayName) throws IOException { + + String apiEndpoint = "global-dialogflow.googleapis.com:443"; + + AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build(); + try (AgentsClient client = AgentsClient.create(agentsSettings)) { + // Set the details of the Agent to create + Builder build = Agent.newBuilder(); + + build.setDefaultLanguageCode("en"); + build.setDisplayName(displayName); + // Correct format for timezone is location/city + // For example America/Los_Angeles, Europe/Madrid, Asia/Tokyo + build.setTimeZone("America/Los_Angeles"); + + Agent agent = build.build(); + String parentPath = String.format("projects/%s/locations/%s", parent, "global"); + + // Calls the create agent api and returns the created Agent + Agent response = client.createAgent(parentPath, agent); + System.out.println(response); + return response; + } + } +} +// [END dialogflow_cx_create_agent] diff --git a/dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateAgentIT.java b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateAgentIT.java new file mode 100644 index 00000000000..b479c0eb876 --- /dev/null +++ b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateAgentIT.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; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.cloud.dialogflow.cx.v3.AgentsClient; +import com.google.cloud.dialogflow.cx.v3.AgentsSettings; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class CreateAgentIT { + + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + private static String agentPath = ""; + private ByteArrayOutputStream stdOut; + private static PrintStream originalOut; + + @Before + public void setUp() throws IOException { + originalOut = System.out; + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + } + + @After + public void tearDown() throws IOException { + System.setOut(originalOut); + String apiEndpoint = "global-dialogflow.googleapis.com:443"; + + AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build(); + AgentsClient client = AgentsClient.create(agentsSettings); + + client.deleteAgent(CreateAgentIT.agentPath); + } + + @Test + public void testCreateAgent() throws IOException { + String fakeAgent = String.format("fake_agent_%s", UUID.randomUUID().toString()); + + CreateAgentIT.agentPath = CreateAgent.createAgent(PROJECT_ID, fakeAgent).getName(); + + assertThat(stdOut.toString()).contains(fakeAgent); + } +}