Skip to content

Commit 3dc8bad

Browse files
committed
Update trigger tests.
1 parent 268796c commit 3dc8bad

File tree

2 files changed

+142
-21
lines changed

2 files changed

+142
-21
lines changed

dlp/src/main/java/com/example/dlp/Triggers.java

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.example.dlp;
1818

19+
import com.google.cloud.ServiceOptions;
1920
import com.google.cloud.dlp.v2.DlpServiceClient;
2021
import com.google.privacy.dlp.v2.CloudStorageOptions;
2122
import com.google.privacy.dlp.v2.CreateJobTriggerRequest;
@@ -26,6 +27,8 @@
2627
import com.google.privacy.dlp.v2.JobTrigger;
2728
import com.google.privacy.dlp.v2.Likelihood;
2829
import com.google.privacy.dlp.v2.ListJobTriggersRequest;
30+
import com.google.privacy.dlp.v2.ProjectJobTriggerName;
31+
import com.google.privacy.dlp.v2.ProjectName;
2932
import com.google.privacy.dlp.v2.Schedule;
3033
import com.google.privacy.dlp.v2.StorageConfig;
3134
import com.google.protobuf.Duration;
@@ -50,7 +53,6 @@ public class Triggers {
5053
* @param triggerId (Optional) name of the trigger to be created
5154
* @param displayName (Optional) display name for the trigger to be created
5255
* @param description (Optional) description for the trigger to be created
53-
* @param gcsUrl URL path to GCS bucket, eg. gs://my-bucket-name
5456
* @param scanPeriod How often to wait between scans, in days (minimum = 1 day)
5557
* @param infoTypes infoTypes of information to match eg. InfoType.PHONE_NUMBER,
5658
* InfoType.EMAIL_ADDRESS
@@ -62,21 +64,24 @@ private static void createTrigger(
6264
String triggerId,
6365
String displayName,
6466
String description,
65-
String gcsUrl,
67+
String bucketName,
68+
String fileName,
6669
int scanPeriod,
6770
List<InfoType> infoTypes,
6871
Likelihood minLikelihood,
6972
int maxFindings,
70-
String projectId) {
73+
String projectId) throws Exception {
7174

7275
// instantiate a client
73-
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
76+
DlpServiceClient dlpServiceClient = DlpServiceClient.create();
77+
try {
7478

75-
CloudStorageOptions.FileSet fileSet =
76-
CloudStorageOptions.FileSet.newBuilder().setUrl(gcsUrl).build();
7779
CloudStorageOptions cloudStorageOptions =
78-
CloudStorageOptions.newBuilder().setFileSet(fileSet).build();
79-
80+
CloudStorageOptions.newBuilder()
81+
.setFileSet(
82+
CloudStorageOptions.FileSet.newBuilder()
83+
.setUrl("gs://" + bucketName + "/" + fileName))
84+
.build();
8085
StorageConfig storageConfig =
8186
StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).build();
8287

@@ -110,16 +115,17 @@ private static void createTrigger(
110115
.addTriggers(trigger)
111116
.build();
112117

118+
System.out.println("Pause");
113119
// Create scan request
114120
CreateJobTriggerRequest createJobTriggerRequest =
115121
CreateJobTriggerRequest.newBuilder()
116-
.setParent(projectId)
122+
.setParent(ProjectName.of(projectId).toString())
117123
.setJobTrigger(jobTrigger)
118124
.build();
119125

120126
JobTrigger createdJobTrigger = dlpServiceClient.createJobTrigger(createJobTriggerRequest);
121127

122-
System.out.println("Created Trigger: " + createdJobTrigger.getDisplayName());
128+
System.out.println("Created Trigger: " + createdJobTrigger.getName());
123129
} catch (Exception e) {
124130
System.out.println("Error creating trigger :" + e.getMessage());
125131
}
@@ -135,7 +141,8 @@ private static void listTriggers(String projectId) {
135141
// Instantiates a client
136142
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
137143
ListJobTriggersRequest listJobTriggersRequest =
138-
ListJobTriggersRequest.newBuilder().setParent(projectId).build();
144+
ListJobTriggersRequest.newBuilder()
145+
.setParent(ProjectName.of(projectId).toString()).build();
139146
DlpServiceClient.ListJobTriggersPagedResponse response =
140147
dlpServiceClient.listJobTriggers(listJobTriggersRequest);
141148
response
@@ -172,11 +179,14 @@ private static void listTriggers(String projectId) {
172179
private static void deleteTrigger(String projectId, String triggerId) {
173180
// Instantiates a client
174181
// triggerName to provided as projects/project-id/jobTriggers/triggerId
175-
String triggerName = String.format("projects/%s/jobTriggers/%s", projectId, triggerId);
182+
183+
ProjectJobTriggerName triggerName = ProjectJobTriggerName.of(projectId, triggerId);
176184
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
177185
DeleteJobTriggerRequest deleteJobTriggerRequest =
178-
DeleteJobTriggerRequest.newBuilder().setName(triggerName).build();
186+
DeleteJobTriggerRequest.newBuilder().setName(triggerName.toString()).build();
179187
dlpServiceClient.deleteJobTrigger(deleteJobTriggerRequest);
188+
189+
System.out.println("Trigger deleted: " + triggerName.toString());
180190
} catch (Exception e) {
181191
System.out.println("Error deleting trigger :" + e.getMessage());
182192
}
@@ -191,20 +201,23 @@ public static void main(String[] args) throws Exception {
191201
optionsGroup.setRequired(true);
192202

193203
Option createTriggerOption =
194-
new Option("c", "create", true, "Create trigger to scan a GCS bucket");
204+
new Option("c", "create", false, "Create trigger to scan a GCS bucket");
195205
optionsGroup.addOption(createTriggerOption);
196206

197-
Option listTriggersOption = new Option("l", "list", true, "List triggers");
207+
Option listTriggersOption = new Option("l", "list", false, "List triggers");
198208
optionsGroup.addOption(listTriggersOption);
199209

200-
Option deleteTriggerOption = new Option("d", "delete", true, "Delete trigger");
210+
Option deleteTriggerOption = new Option("d", "delete", false, "Delete trigger");
201211
optionsGroup.addOption(deleteTriggerOption);
202212

203213
Options commandLineOptions = new Options();
204214
commandLineOptions.addOptionGroup(optionsGroup);
205215

206-
Option gcsUrlOption = Option.builder("gcsUrl").hasArg(true).required(false).build();
207-
commandLineOptions.addOption(gcsUrlOption);
216+
Option bucketNameOption = Option.builder("bucketName").hasArg(true).required(false).build();
217+
commandLineOptions.addOption(bucketNameOption);
218+
219+
Option gcsFileNameOption = Option.builder("fileName").hasArg(true).required(false).build();
220+
commandLineOptions.addOption(gcsFileNameOption);
208221

209222
Option minLikelihoodOption =
210223
Option.builder("minLikelihood").hasArg(true).required(false).build();
@@ -223,10 +236,14 @@ public static void main(String[] args) throws Exception {
223236
commandLineOptions.addOption(projectIdOption);
224237

225238
Option triggerIdOption = Option.builder("triggerId").hasArg(true).required(false).build();
239+
commandLineOptions.addOption(triggerIdOption);
226240
Option displayNameOption = Option.builder("displayName").hasArg(true).required(false).build();
241+
commandLineOptions.addOption(displayNameOption);
227242
Option descriptionOption = Option.builder("description").hasArg(true).required(false).build();
243+
commandLineOptions.addOption(descriptionOption);
228244

229245
Option scanPeriodOption = Option.builder("scanPeriod").hasArg(true).required(false).build();
246+
commandLineOptions.addOption(scanPeriodOption);
230247

231248
CommandLineParser parser = new DefaultParser();
232249
HelpFormatter formatter = new HelpFormatter();
@@ -241,7 +258,8 @@ public static void main(String[] args) throws Exception {
241258
return;
242259
}
243260

244-
String projectId = cmd.getOptionValue(projectIdOption.getOpt());
261+
String projectId =
262+
cmd.getOptionValue(projectIdOption.getOpt(), ServiceOptions.getDefaultProjectId());
245263
if (cmd.hasOption("c")) {
246264
Likelihood minLikelihood =
247265
Likelihood.valueOf(
@@ -251,7 +269,8 @@ public static void main(String[] args) throws Exception {
251269
String triggerId = cmd.getOptionValue(triggerIdOption.getOpt());
252270
String displayName = cmd.getOptionValue(displayNameOption.getOpt(), "");
253271
String description = cmd.getOptionValue(descriptionOption.getOpt(), "");
254-
String gcsUrl = cmd.getOptionValue(gcsUrlOption.getOpt());
272+
String bucketName = cmd.getOptionValue(bucketNameOption.getOpt());
273+
String fileName = cmd.getOptionValue(gcsFileNameOption.getOpt());
255274
int scanPeriod = Integer.valueOf(cmd.getOptionValue(scanPeriodOption.getOpt()));
256275
List<InfoType> infoTypesList = new ArrayList<>();
257276
if (cmd.hasOption(infoTypesOption.getOpt())) {
@@ -265,7 +284,8 @@ public static void main(String[] args) throws Exception {
265284
triggerId,
266285
displayName,
267286
description,
268-
gcsUrl,
287+
bucketName,
288+
fileName,
269289
scanPeriod,
270290
infoTypesList,
271291
minLikelihood,
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2018 Google Inc.
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 com.example.dlp;
18+
19+
import static org.hamcrest.CoreMatchers.containsString;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertThat;
22+
import static org.junit.Assert.assertTrue;
23+
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.PrintStream;
26+
import java.util.Date;
27+
import java.util.regex.Matcher;
28+
import java.util.regex.Pattern;
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
35+
@RunWith(JUnit4.class)
36+
// CHECKSTYLE OFF: AbbreviationAsWordInName
37+
public class TriggersIT {
38+
// CHECKSTYLE ON: AbbreviationAsWordInName
39+
40+
private ByteArrayOutputStream bout;
41+
private PrintStream out;
42+
43+
private String bucketName = System.getenv("GOOGLE_CLOUD_PROJECT") + "/dlp";
44+
private String topicId = "dlp-tests";
45+
private String subscriptionId = "dlp-test";
46+
47+
@Before
48+
public void setUp() {
49+
bout = new ByteArrayOutputStream();
50+
out = new PrintStream(bout);
51+
System.setOut(out);
52+
assertNotNull(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
53+
}
54+
55+
@After
56+
public void tearDown() {
57+
System.setOut(null);
58+
bout.reset();
59+
}
60+
61+
@Test
62+
public void testCreateTrigger() throws Exception {
63+
Triggers.main(new String[] {
64+
"-c",
65+
"-displayName", String.format("trigger-name-%s", new Date()),
66+
"-triggerId", String.format("trigger%s", System.currentTimeMillis()),
67+
"-description", String.format("description-%s", new Date()),
68+
"-bucketName", bucketName,
69+
"-fileName", "test.txt",
70+
"-scanPeriod", "1"
71+
});
72+
String output = bout.toString();
73+
assertThat(output, containsString("Created Trigger:"));
74+
}
75+
76+
@Test
77+
public void testListTrigger() throws Exception {
78+
Triggers.main(new String[] {
79+
"-l"
80+
});
81+
String output = bout.toString();
82+
assertThat(output, containsString("Trigger:"));
83+
}
84+
85+
@Test
86+
public void testDeleteTrigger() throws Exception {
87+
Triggers.main(new String[] { "-l" });
88+
String output = bout.toString();
89+
Matcher templateIds = Pattern.compile("(?<=jobTriggers/)[0-9]+").matcher(output);
90+
assertTrue(templateIds.find());
91+
String triggerId = templateIds.group(0);
92+
bout.reset();
93+
Triggers.main(new String[] {
94+
"-d",
95+
"-triggerId", triggerId,
96+
});
97+
output = bout.toString();
98+
assertThat(output, containsString("Trigger deleted:"));
99+
}
100+
101+
}

0 commit comments

Comments
 (0)