Skip to content

Commit 3282a23

Browse files
authored
Sc methods test (#274)
* Add current service client list and test ensuring list is accurate * google-java-format * Add generator class and refactor test * Address comments
1 parent b17856f commit 3282a23

3 files changed

Lines changed: 221 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.ads.googleads.lib;
16+
import com.google.common.base.Charsets;
17+
import com.google.common.io.CharSink;
18+
import com.google.common.io.Files;
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.lang.reflect.Method;
22+
import java.util.Arrays;
23+
import java.util.List;
24+
25+
/**
26+
* Generates a list of methods present in the GoogleAdsAllVersions class of the latest available
27+
* version of the Google Ads API and saves the output to a txt file in the test resources directory.
28+
*/
29+
public class MethodsListGenerator {
30+
public static void main(String args[])
31+
throws ClassNotFoundException, IOException, NoSuchMethodException {
32+
Method latestVersionMethod = GoogleAdsAllVersions.class.getMethod("getLatestVersion");
33+
Class cls = Class.forName(latestVersionMethod.getReturnType().getName());
34+
List<Method> methods = Arrays.asList(cls.getDeclaredMethods());
35+
36+
StringBuilder output = new StringBuilder();
37+
for (Method method : methods) {
38+
output.append(method + "\n");
39+
}
40+
41+
System.out.println("Writing the following methods to file:");
42+
System.out.printf(output.toString());
43+
44+
File file = new File("./google-ads/src/test/resources/testdata/avail_service_clients.txt");
45+
CharSink sink = Files.asCharSink(file, Charsets.UTF_8);
46+
sink.write(output);
47+
}
48+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.ads.googleads.lib;
16+
17+
import com.google.common.base.Charsets;
18+
import com.google.common.io.Resources;
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.junit.runners.JUnit4;
22+
import java.io.IOException;
23+
import java.lang.reflect.Method;
24+
import java.net.URL;
25+
import java.util.Arrays;
26+
import java.util.HashSet;
27+
import java.util.List;
28+
import java.util.Set;
29+
30+
import static org.junit.Assert.assertEquals;
31+
32+
@RunWith(JUnit4.class)
33+
public class MethodsPresenceTest {
34+
/**
35+
* Verifies that the list of all service client methods in a given GoogleAdsVersion class matches
36+
* that provided in the testdata/avail_service_clients.txt file.
37+
*
38+
* <p>The avail_service_clients.txt file should be the standard against which all releases are
39+
* compared. In the event the list of services changes in the API, this test will fail. Then,
40+
* avail_service_clients.txt should be updated so that this test passes.
41+
*/
42+
@Test
43+
public void methodsListMatches()
44+
throws ClassNotFoundException, NoSuchMethodException, IOException {
45+
// Gets the fully qualified return type of the GoogleAdsAllVersions getLatestVersion method,
46+
// which is the class from which the methods will be retrieved.
47+
Method latestVersionMethod = GoogleAdsAllVersions.class.getMethod("getLatestVersion");
48+
Class cls = Class.forName(latestVersionMethod.getReturnType().getName());
49+
50+
// Retrieves a list of the methods in the GoogleAdsVersion class using reflection and adds
51+
// each to a set.
52+
List<Method> methods = Arrays.asList(cls.getDeclaredMethods());
53+
Set<String> serviceListReflection = new HashSet<>();
54+
for (Method method : methods) {
55+
serviceListReflection.add(method.toString());
56+
}
57+
58+
// Retrieves the list of service client methods in the provided avail_service_clients.txt file.
59+
URL resource = Resources.getResource("testdata/avail_service_clients.txt");
60+
Set<String> serviceListResource =
61+
new HashSet<>(Resources.asCharSource(resource, Charsets.UTF_8).readLines());
62+
serviceListResource.remove("");
63+
64+
assertEquals(serviceListResource, serviceListReflection);
65+
}
66+
}

0 commit comments

Comments
 (0)