Skip to content

Commit 0dd39e8

Browse files
committed
Fixes patch and adds tests
1 parent 9b219f4 commit 0dd39e8

File tree

2 files changed

+100
-32
lines changed

2 files changed

+100
-32
lines changed

iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExample.java

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
*/
8181
public class DeviceRegistryExample {
8282

83+
static final String APP_NAME = "DeviceRegistryExample";
84+
8385
/** Creates a topic and grants the IoT service account access. */
8486
public static Topic createIotTopic(String projectId, String topicId) throws Exception {
8587
// Create a new topic
@@ -111,8 +113,9 @@ public static void createRegistry(String cloudRegion, String projectId, String r
111113
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
112114
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
113115
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
114-
final CloudIot service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
115-
init);
116+
final CloudIot service = new CloudIot.Builder(
117+
GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
118+
.setApplicationName(APP_NAME).build();
116119

117120
final String projectPath = "projects/" + projectId + "/locations/" + cloudRegion;
118121
final String fullPubsubPath = "projects/" + projectId + "/topics/" + pubsubTopicPath;
@@ -137,8 +140,9 @@ public static void deleteRegistry(String cloudRegion, String projectId, String r
137140
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
138141
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
139142
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
140-
final CloudIot service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
141-
init);
143+
final CloudIot service = new CloudIot.Builder(
144+
GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
145+
.setApplicationName(APP_NAME).build();
142146

143147
String projectPath = "projects/" + projectId + "/locations/" + cloudRegion;
144148
String registryPath = projectPath + "/registries/" + registryName;
@@ -154,8 +158,9 @@ public static void listDevices(String projectId, String cloudRegion, String regi
154158
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
155159
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
156160
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
157-
final CloudIot service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
158-
init);
161+
final CloudIot service = new CloudIot.Builder(
162+
GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
163+
.setApplicationName(APP_NAME).build();
159164

160165
String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/"
161166
+ registryName;
@@ -193,8 +198,9 @@ public static void createDeviceWithEs256(String deviceId, String publicKeyFilePa
193198
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
194199
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
195200
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
196-
final CloudIot service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
197-
init);
201+
final CloudIot service = new CloudIot.Builder(
202+
GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
203+
.setApplicationName(APP_NAME).build();
198204

199205
final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion
200206
+ "/registries/" + registryName;
@@ -233,8 +239,9 @@ public static void createDeviceWithRs256(String deviceId, String certificateFile
233239
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
234240
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
235241
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
236-
final CloudIot service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
237-
init);
242+
final CloudIot service = new CloudIot.Builder(
243+
GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
244+
.setApplicationName(APP_NAME).build();
238245

239246
final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion
240247
+ "/registries/" + registryName;
@@ -276,8 +283,9 @@ public static void createDeviceWithNoAuth(String deviceId, String projectId, Str
276283
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
277284
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
278285
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
279-
final CloudIot service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
280-
init);
286+
final CloudIot service = new CloudIot.Builder(
287+
GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
288+
.setApplicationName(APP_NAME).build();
281289

282290
final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion
283291
+ "/registries/" + registryName;
@@ -306,8 +314,9 @@ public static void deleteDevice(String deviceId, String projectId, String cloudR
306314
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
307315
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
308316
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
309-
final CloudIot service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
310-
init);
317+
final CloudIot service = new CloudIot.Builder(
318+
GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
319+
.setApplicationName(APP_NAME).build();
311320

312321
final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion
313322
+ "/registries/" + registryName;
@@ -324,8 +333,9 @@ public static Device getDevice(String deviceId, String projectId, String cloudRe
324333
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
325334
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
326335
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
327-
CloudIot service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
328-
init);
336+
final CloudIot service = new CloudIot.Builder(
337+
GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
338+
.setApplicationName(APP_NAME).build();
329339

330340
String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/"
331341
+ registryName;
@@ -343,8 +353,9 @@ public static List<DeviceState> getDeviceStates(
343353
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
344354
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
345355
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
346-
CloudIot service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
347-
init);
356+
final CloudIot service = new CloudIot.Builder(
357+
GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
358+
.setApplicationName(APP_NAME).build();
348359

349360
String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/"
350361
+ registryName;
@@ -365,8 +376,9 @@ public static DeviceRegistry getRegistry(
365376
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
366377
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
367378
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
368-
final CloudIot service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
369-
init);
379+
final CloudIot service = new CloudIot.Builder(
380+
GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
381+
.setApplicationName(APP_NAME).build();
370382

371383
final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion
372384
+ "/registries/" + registryName;
@@ -381,8 +393,9 @@ public static void listDeviceConfigs(
381393
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
382394
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
383395
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
384-
final CloudIot service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
385-
init);
396+
final CloudIot service = new CloudIot.Builder(
397+
GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
398+
.setApplicationName(APP_NAME).build();
386399

387400
final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion
388401
+ "/registries/" + registryName;
@@ -414,8 +427,9 @@ public static void listRegistries(String projectId, String cloudRegion)
414427
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
415428
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
416429
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
417-
final CloudIot service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
418-
init);
430+
final CloudIot service = new CloudIot.Builder(
431+
GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
432+
.setApplicationName(APP_NAME).build();
419433

420434
final String projectPath = "projects/" + projectId + "/locations/" + cloudRegion;
421435

@@ -451,8 +465,9 @@ public static void modifyCloudToDeviceConfig(String deviceId, String configData,
451465
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
452466
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
453467
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
454-
final CloudIot service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
455-
init);
468+
final CloudIot service = new CloudIot.Builder(
469+
GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
470+
.setApplicationName(APP_NAME).build();
456471

457472
final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion
458473
+ "/registries/" + registryName;
@@ -480,8 +495,9 @@ public static void patchEs256ForAuth(String deviceId, String publicKeyFilePath,
480495
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
481496
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
482497
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
483-
final CloudIot service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
484-
init);
498+
final CloudIot service = new CloudIot.Builder(
499+
GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
500+
.setApplicationName(APP_NAME).build();
485501

486502
final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion
487503
+ "/registries/" + registryName;
@@ -505,7 +521,7 @@ public static void patchEs256ForAuth(String deviceId, String publicKeyFilePath,
505521
.registries()
506522
.devices()
507523
.patch(devicePath, device)
508-
.setFields("credentials")
524+
.setUpdateMask("credentials")
509525
.execute();
510526

511527
System.out.println("Patched device is " + patchedDevice.toPrettyString());
@@ -520,8 +536,9 @@ public static void patchRsa256ForAuth(String deviceId, String publicKeyFilePath,
520536
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
521537
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
522538
HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
523-
final CloudIot service = new CloudIot(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
524-
init);
539+
final CloudIot service = new CloudIot.Builder(
540+
GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
541+
.setApplicationName(APP_NAME).build();
525542

526543
final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion
527544
+ "/registries/" + registryName;
@@ -545,7 +562,7 @@ public static void patchRsa256ForAuth(String deviceId, String publicKeyFilePath,
545562
.registries()
546563
.devices()
547564
.patch(devicePath, device)
548-
.setFields("credentials")
565+
.setUpdateMask("credentials")
549566
.execute();
550567

551568
System.out.println("Patched device is " + patchedDevice.toPrettyString());

iot/api-client/manager/src/test/java/com/example/cloud/iot/examples/ManagerIT.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,57 @@ public void tearDown() throws Exception {
5959
System.setOut(null);
6060
}
6161

62+
@Test
63+
public void testPatchRsa() throws Exception {
64+
final String deviceName = "patchme-device-rsa";
65+
topic = DeviceRegistryExample.createIotTopic(
66+
PROJECT_ID,
67+
TOPIC_ID);
68+
try {
69+
DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
70+
DeviceRegistryExample.createDeviceWithNoAuth(deviceName, PROJECT_ID, CLOUD_REGION,
71+
REGISTRY_ID);
72+
DeviceRegistryExample.patchRsa256ForAuth(deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION,
73+
REGISTRY_ID);
74+
75+
String got = bout.toString();
76+
Assert.assertTrue(got.contains("Created device: {"));
77+
} finally {
78+
DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
79+
DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
80+
}
81+
82+
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
83+
topicAdminClient.deleteTopic(topic.getNameAsTopicName());
84+
}
85+
}
86+
87+
@Test
88+
public void testPatchEs() throws Exception {
89+
final String deviceName = "patchme-device-es";
90+
topic = DeviceRegistryExample.createIotTopic(
91+
PROJECT_ID,
92+
TOPIC_ID);
93+
94+
try {
95+
DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
96+
DeviceRegistryExample.createDeviceWithNoAuth(deviceName, PROJECT_ID, CLOUD_REGION,
97+
REGISTRY_ID);
98+
DeviceRegistryExample.patchEs256ForAuth(deviceName, ES_PATH, PROJECT_ID, CLOUD_REGION,
99+
REGISTRY_ID);
100+
101+
String got = bout.toString();
102+
Assert.assertTrue(got.contains("Created device: {"));
103+
} finally {
104+
DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
105+
DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
106+
}
107+
108+
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
109+
topicAdminClient.deleteTopic(topic.getNameAsTopicName());
110+
}
111+
}
112+
62113
@Test
63114
public void testCreateDeleteUnauthDevice() throws Exception {
64115
final String deviceName = "noauth-device";

0 commit comments

Comments
 (0)