This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
/
ServicesTests.java
129 lines (111 loc) · 4.87 KB
/
ServicesTests.java
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
/*
* Copyright 2020 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 com.example.servicedirectory;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import com.google.cloud.servicedirectory.v1.EndpointName;
import com.google.cloud.servicedirectory.v1.LocationName;
import com.google.cloud.servicedirectory.v1.Namespace;
import com.google.cloud.servicedirectory.v1.RegistrationServiceClient;
import com.google.cloud.servicedirectory.v1.RegistrationServiceClient.ListNamespacesPagedResponse;
import com.google.cloud.servicedirectory.v1.ServiceName;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.UUID;
import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class ServicesTests {
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String LOCATION_ID = "us-central1";
private static final String NAMESPACE_ID = "namespace-" + UUID.randomUUID().toString();
private static final String SERVICE_ID = "service-" + UUID.randomUUID().toString();
private ByteArrayOutputStream bout;
private static void requireEnvVar(String varName) {
assertNotNull(
String.format("Environment variable '%s' must be set to perform these tests.", varName),
System.getenv(varName));
}
@BeforeClass
public static void checkRequirements() {
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
requireEnvVar("GOOGLE_CLOUD_PROJECT");
}
@Before
public void setUp() throws Exception {
bout = new ByteArrayOutputStream();
System.setOut(new PrintStream(bout));
// Setup: create a namespace for the services to live in.
NamespacesCreate.createNamespace(PROJECT_ID, LOCATION_ID, NAMESPACE_ID);
}
@After
public void tearDown() throws Exception {
System.setOut(null);
bout.reset();
// Deletes all resources created during these tests.
try (RegistrationServiceClient client = RegistrationServiceClient.create()) {
// List the namespaces.
String locationPath = LocationName.format(PROJECT_ID, LOCATION_ID);
ListNamespacesPagedResponse response = client.listNamespaces(locationPath);
// Delete each namespace.
for (Namespace namespace : response.iterateAll()) {
client.deleteNamespace(namespace.getName());
}
}
}
@Test
public void testCreateService() throws Exception {
ServicesCreate.createService(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID);
String serviceName = ServiceName.format(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID);
String output = bout.toString();
assertThat(
output, CoreMatchers.containsString(String.format("Created Service: %s", serviceName)));
}
@Test
public void testResolveService() throws Exception {
// Setup: create a service.
ServicesCreate.createService(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID);
String serviceName = ServiceName.format(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID);
// Setup: Create an endpoint in the service.
EndpointsCreate.createEndpoint(
PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID, "default-endpoint");
String endpointName =
EndpointName.format(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID, "default-endpoint");
// Resolve the service with the specified ID.
ServicesResolve.resolveService(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID);
String output = bout.toString();
assertThat(
output, CoreMatchers.containsString(String.format("Resolved Service: %s", serviceName)));
assertThat(
output, CoreMatchers.containsString(String.format("Endpoints found:\n%s", endpointName)));
}
@Test
public void testDeleteService() throws Exception {
// Setup: create a service.
ServicesCreate.createService(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID);
String serviceName = ServiceName.format(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID);
// Delete the service with the specified ID.
ServicesDelete.deleteService(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID);
String output = bout.toString();
assertThat(
output, CoreMatchers.containsString(String.format("Deleted Service: %s", serviceName)));
}
}