forked from GoogleCloudPlatform/java-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSamplesTests.java
More file actions
237 lines (201 loc) · 7.27 KB
/
SamplesTests.java
File metadata and controls
237 lines (201 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright 2018 Google Inc.
*
* 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 com.example.containeranalysis;
import static java.lang.Thread.sleep;
import static junit.framework.Assert.fail;
import static junit.framework.TestCase.assertEquals;
import com.google.cloud.pubsub.v1.Subscriber;
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.containeranalysis.v1alpha1.Note;
import com.google.containeranalysis.v1alpha1.Occurrence;
import com.google.containeranalysis.v1alpha1.VulnerabilityType.VulnerabilityDetails;
import com.google.pubsub.v1.ProjectSubscriptionName;
import java.util.Date;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/**
* test runner
*/
@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class SamplesTests {
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private String noteId;
private String imageUrl;
private Note noteObj;
private static final int SLEEP_TIME = 1000;
private static final int TRY_LIMIT = 10;
@Rule
public TestName name = new TestName();
@Before
public void setUp() {
System.out.println(name.getMethodName());
noteId = "note-" + (new Date()).getTime() + name.getMethodName();
imageUrl = "www." + (new Date()).getTime() + name.getMethodName() + ".com";
try {
noteObj = Samples.createNote(noteId, PROJECT_ID);
} catch (Exception e) {
// note creation fails
}
}
@After
public void tearDown() {
try {
Samples.deleteNote(noteId, PROJECT_ID);
} catch (Exception e) {
// note deletion fails
}
}
@Test
public void testCreateNote() throws Exception {
// note should have been created as part of set up. verify that it succeeded
Note n = Samples.getNote(noteId, PROJECT_ID);
assertEquals(n.getName(), noteObj.getName());
}
@Test
public void testDeleteNote() throws Exception {
Samples.deleteNote(noteId, PROJECT_ID);
try {
Samples.getNote(noteId, PROJECT_ID);
//above should throw, because note was deleted
fail("note not deleted");
} catch (Exception e) {
// test passes
}
}
@Test
public void testUpdateNote() throws Exception {
String descriptionText = "updated";
Note.Builder builder = Note.newBuilder(noteObj);
builder.setShortDescription(descriptionText);
Samples.updateNote(builder.build(), noteId, PROJECT_ID);
Note updated = Samples.getNote(noteId, PROJECT_ID);
assertEquals(descriptionText, updated.getShortDescription());
}
@Test
public void testCreateOccurrence() throws Exception {
Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
Occurrence retrieved = Samples.getOccurrence(o.getName());
assertEquals(o.getName(), retrieved.getName());
//clean up
Samples.deleteOccurrence(o.getName());
}
@Test
public void testDeleteOccurrence() throws Exception {
Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
String occName = o.getName();
Samples.deleteOccurrence(occName);
try {
Samples.getOccurrence(occName);
//getOccurrence should fail, because occurrence was deleted
fail("failed to delete occurrence");
} catch (Exception e) {
//test passes
}
}
@Test
public void testUpdateOccurrence() throws Exception {
String typeId = "newType";
Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
Occurrence.Builder b = Occurrence.newBuilder(o);
VulnerabilityDetails.Builder v = VulnerabilityDetails.newBuilder();
v.setType(typeId);
b.setVulnerabilityDetails(v.build());
Samples.updateOccurrence(o.getName(), b.build());
Occurrence o2 = Samples.getOccurrence(o.getName());
assertEquals(typeId, o2.getVulnerabilityDetails().getType());
//clean up
Samples.deleteOccurrence(o2.getName());
}
@Test
public void testOccurrencesForImage() throws Exception {
int newCount;
int tries = 0;
int origCount = Samples.getOccurrencesForImage(imageUrl, PROJECT_ID);
final Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
do {
newCount = Samples.getOccurrencesForImage(imageUrl, PROJECT_ID);
sleep(SLEEP_TIME);
} while (newCount != 1 && tries < TRY_LIMIT);
assertEquals(1, newCount);
assertEquals(0, origCount);
//clean up
Samples.deleteOccurrence(o.getName());
}
@Test
public void testOccurrencesForNote() throws Exception {
int newCount;
int tries = 0;
int origCount = Samples.getOccurrencesForNote(noteId, PROJECT_ID);
final Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
do {
newCount = Samples.getOccurrencesForNote(noteId, PROJECT_ID);
sleep(SLEEP_TIME);
} while (newCount != 1 && tries < TRY_LIMIT);
assertEquals(0, origCount);
assertEquals(1, newCount);
//clean up
Samples.deleteOccurrence(o.getName());
}
@Test
public void testPubSub() throws Exception {
int newCount;
int tries;
String subscriptionId = "drydockOccurrences";
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(PROJECT_ID, subscriptionId);
Samples.createOccurrenceSubscription(subscriptionId, PROJECT_ID);
Subscriber subscriber = null;
Samples.MessageReceiverExample receiver = new Samples.MessageReceiverExample();
try {
subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
subscriber.startAsync().awaitRunning();
// sleep so any messages in the queue can go through and be counted before we start the test
sleep(SLEEP_TIME);
// set the initial state of our counter
int startVal = receiver.messageCount + 1;
// now, we can test adding 3 more occurrences
int endVal = startVal + 3;
for (int i = startVal; i <= endVal; i++) {
Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
System.out.println("CREATED: " + o.getName());
tries = 0;
do {
newCount = receiver.messageCount;
sleep(SLEEP_TIME);
tries += 1;
} while (newCount != i && tries < TRY_LIMIT);
System.out.println(receiver.messageCount + " : " + i);
assertEquals(i, receiver.messageCount);
Samples.deleteOccurrence(o.getName());
}
} catch (Exception e) {
fail("exception thrown");
} finally {
if (subscriber != null) {
subscriber.stopAsync();
}
//delete subscription now that we're done with it
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
subscriptionAdminClient.deleteSubscription(subscriptionName);
}
}
}
}