Skip to content

Commit d88015d

Browse files
committed
Updated Redact samples and tests.
1 parent f4b54b2 commit d88015d

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.privacy.dlp.v2.InfoType;
2323
import com.google.privacy.dlp.v2.InspectConfig;
2424
import com.google.privacy.dlp.v2.Likelihood;
25+
import com.google.privacy.dlp.v2.ProjectName;
2526
import com.google.privacy.dlp.v2.RedactImageRequest;
2627
import com.google.privacy.dlp.v2.RedactImageResponse;
2728
import com.google.protobuf.ByteString;
@@ -110,7 +111,7 @@ private static void redactImage(
110111

111112
RedactImageRequest redactImageRequest =
112113
RedactImageRequest.newBuilder()
113-
.setParent(projectId)
114+
.setParent(ProjectName.of(projectId).toString())
114115
.addAllImageRedactionConfigs(imageRedactionConfigs)
115116
.setByteItem(byteContentItem)
116117
.setInspectConfig(inspectConfig)
@@ -142,11 +143,12 @@ public static void main(String[] args) throws Exception {
142143
commandLineOptions.addOption(infoTypesOption);
143144

144145
Option inputFilePathOption =
145-
Option.builder("o").hasArg(true).longOpt("inputFilePath").required(false).build();
146+
Option.builder("f").hasArg(true).longOpt("inputFilePath").required(false).build();
146147
commandLineOptions.addOption(inputFilePathOption);
147148

148149
Option outputFilePathOption =
149150
Option.builder("o").hasArg(true).longOpt("outputFilePath").required(false).build();
151+
150152
commandLineOptions.addOption(outputFilePathOption);
151153

152154
Option projectIdOption = Option.builder("projectId").hasArg(true).required(false).build();

dlp/src/test/java/com/example/dlp/RedactIT.java

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
package com.example.dlp;
1818

1919
import static junit.framework.TestCase.assertFalse;
20+
import static org.hamcrest.CoreMatchers.containsString;
21+
import static org.hamcrest.CoreMatchers.not;
2022
import static org.junit.Assert.assertNotNull;
23+
import static org.junit.Assert.assertThat;
2124
import static org.junit.Assert.assertTrue;
2225

2326
import java.io.ByteArrayOutputStream;
@@ -47,36 +50,27 @@ public void setUp() {
4750
}
4851

4952
@Test
50-
public void testInfoTypesInStringAreReplaced() throws Exception {
51-
String text =
52-
"\"My phone number is (234) 456-7890 and my email address is [email protected]\"";
53-
Redact.main(new String[] {"-s", text, "-r", "_REDACTED_"});
54-
String output = bout.toString();
55-
assertTrue(output.contains("My phone number is _REDACTED_ and my email address is _REDACTED_"));
56-
}
57-
58-
@Ignore // TODO: b/69461298
59-
@Test
60-
public void testInfoTypesInImageAreReplaced() throws Exception {
61-
ClassLoader classLoader = getClass().getClassLoader();
62-
// confirm that current data contains info types
63-
File file = new File(classLoader.getResource("test.png").getFile());
64-
Inspect.main(new String[] {"-f", file.getAbsolutePath()});
65-
String output = bout.toString();
66-
assertTrue(output.contains("PHONE_NUMBER"));
67-
assertTrue(output.contains("EMAIL_ADDRESS"));
68-
bout.reset();
69-
70-
String outputFilePath = "output.png";
53+
public void testRedactImage() throws Exception {
54+
// InspectIT Tests verify original has PII present
55+
String outputFilePath = "src/test/resources/output.png";
7156

57+
// Restrict phone number, but not email
7258
Redact.main(
7359
new String[] {
74-
"-f", file.getAbsolutePath(), "-infoTypes", "PHONE_NUMBER", "-o", outputFilePath
60+
"-f", "src/test/resources/test.png",
61+
"-infoTypes", "PHONE_NUMBER",
62+
"-o", outputFilePath
7563
});
76-
Inspect.main(new String[] {"-f", outputFilePath});
77-
output = bout.toString();
78-
assertFalse(output.contains("PHONE_NUMBER"));
79-
assertTrue(output.contains("EMAIL_ADDRESS"));
64+
bout.reset();
65+
66+
// Verify that phone_number is missing but email is present
67+
Inspect.main(new String[] {
68+
"-f", outputFilePath,
69+
"-infoTypes", "PHONE_NUMBER", "EMAIL_ADDRESS"
70+
});
71+
String output = bout.toString();
72+
assertThat(output, not(containsString("PHONE_NUMBER")));
73+
assertThat(output, containsString("EMAIL_ADDRESS"));
8074
}
8175

8276
@After

0 commit comments

Comments
 (0)