|
| 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