From c8f3e2ffd2efdd3bbd313b97afe21a35c62c6242 Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Thu, 12 Aug 2021 13:26:19 -0700 Subject: [PATCH] samples: add update intent code sample (#275) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add update intent samples * Fixed lint * lint fix * lint fix * lint fix * lint fix * lint fix * lint fix * lint fix * fixed failing test * lint fix * failing test fix * Fix failing test * fixed fialing test * Added Agent Builder * removed unused builder * lint fix * lint fix * Fixed comments * Updated main * Lint fix * removed return type * Fix build error * added exception * fixed failing test * added missing include * Rebased * 🦉 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/UpdateIntent.java | 74 ++++++++++++++ .../java/dialogflow/cx/UpdateIntentTest.java | 99 +++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 dialogflow-cx/snippets/src/main/java/dialogflow/cx/UpdateIntent.java create mode 100644 dialogflow-cx/snippets/src/test/java/dialogflow/cx/UpdateIntentTest.java diff --git a/dialogflow-cx/snippets/src/main/java/dialogflow/cx/UpdateIntent.java b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/UpdateIntent.java new file mode 100644 index 00000000000..73a87889880 --- /dev/null +++ b/dialogflow-cx/snippets/src/main/java/dialogflow/cx/UpdateIntent.java @@ -0,0 +1,74 @@ +/* + * 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_update_intent] + +import com.google.cloud.dialogflow.cx.v3.Intent; +import com.google.cloud.dialogflow.cx.v3.Intent.Builder; +import com.google.cloud.dialogflow.cx.v3.IntentsClient; +import com.google.cloud.dialogflow.cx.v3.UpdateIntentRequest; +import com.google.protobuf.FieldMask; +import java.io.IOException; + +public class UpdateIntent { + + 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 intentId = "my-intent-id"; + String location = "my-location"; + String displayName = "my-display-name"; + updateIntent(projectId, agentId, intentId, location, displayName); + } + + // DialogFlow API Update Intent sample. + public static void updateIntent( + String projectId, String agentId, String intentId, String location, String displayName) + throws IOException { + try (IntentsClient client = IntentsClient.create()) { + String intentPath = + "projects/" + + projectId + + "/locations/" + + location + + "/agents/" + + agentId + + "/intents/" + + intentId; + + Builder intentBuilder = client.getIntent(intentPath).toBuilder(); + + intentBuilder.setDisplayName(displayName); + FieldMask fieldMask = FieldMask.newBuilder().addPaths("display_name").build(); + + Intent intent = intentBuilder.build(); + UpdateIntentRequest request = + UpdateIntentRequest.newBuilder() + .setIntent(intent) + .setLanguageCode("en") + .setUpdateMask(fieldMask) + .build(); + + // Make API request to update intent using fieldmask + Intent response = client.updateIntent(request); + System.out.println(response); + } + } +} +// [END dialogflow_cx_update_intent] diff --git a/dialogflow-cx/snippets/src/test/java/dialogflow/cx/UpdateIntentTest.java b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/UpdateIntentTest.java new file mode 100644 index 00000000000..bb210fa2dc4 --- /dev/null +++ b/dialogflow-cx/snippets/src/test/java/dialogflow/cx/UpdateIntentTest.java @@ -0,0 +1,99 @@ +/* + * 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.Intent; +import com.google.cloud.dialogflow.cx.v3.IntentsClient; +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 UpdateIntentTest { + + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + private static String parent = ""; + private static String intentID = ""; + private static String intentPath = ""; + private static String agentID = ""; + + private ByteArrayOutputStream stdOut; + + @Before + public 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(); + UpdateIntentTest.agentID = parent.split("/")[5]; + + try (IntentsClient intentsClient = IntentsClient.create()) { + com.google.cloud.dialogflow.cx.v3.Intent.Builder intent = Intent.newBuilder(); + intent.setDisplayName("temp_intent_" + UUID.randomUUID().toString()); + + UpdateIntentTest.intentPath = intentsClient.createIntent(parent, intent.build()).getName(); + UpdateIntentTest.intentID = UpdateIntentTest.intentPath.split("/")[7]; + } + } + + @After + public void tearDown() throws IOException { + stdOut = null; + System.setOut(null); + 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 testUpdateIntent() throws IOException { + + String fakeIntent = "fake_intent_" + UUID.randomUUID().toString(); + + UpdateIntent.updateIntent( + PROJECT_ID, UpdateIntentTest.agentID, UpdateIntentTest.intentID, "global", fakeIntent); + + assertThat(stdOut.toString()).contains(fakeIntent); + } +}