From 4813c36ecdfe8b5198a06be2dd700c4480c962ef Mon Sep 17 00:00:00 2001
From: Thai Duong <thaidn@gmail.com>
Date: Thu, 23 Feb 2017 11:18:24 +0000
Subject: [PATCH] Set up testing infrastructure: adding external dependencies
 and automating generation of test rules for Java tests.

GenTestRules.bzl copied from
https://gist.github.com/jart/9a9f48e216fd747275012d2f2364ac09. I
modified to make it recognize src/java/test/ as a prefix (in addition to
javatests/ and java/).

Bazel developer explained why this is optimal:
https://groups.google.com/d/msg/bazel-discuss/MFYYkJk1tFs/xltlv4m_AQAJ.

Change-Id: I8ff9d48b994efa9e7ed7a1e2c7b8afed8a05b94a
ORIGINAL_AUTHOR=Thai Duong <thaidn@gmail.com>

GitOrigin-RevId: 9abc2685664a9f85ceca6835827219531ac8c6c6
---
 WORKSPACE             |  43 +++++++++++++++--
 java/BUILD            |  62 +++++++++---------------
 java/GenTestRules.bzl | 109 ++++++++++++++++++++++++++++++++++++++++++
 proto/BUILD           |  12 ++++-
 4 files changed, 180 insertions(+), 46 deletions(-)
 create mode 100644 java/GenTestRules.bzl

diff --git a/WORKSPACE b/WORKSPACE
index ee99a1899..fafe52819 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -5,13 +5,48 @@ git_repository(
   commit = "be63ed9cb3140ec23e4df5118fca9a3f98640cf6",
 )
 
+load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_repositories")
+java_proto_repositories()
+
+load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cpp_proto_repositories")
+cpp_proto_repositories()
+
+maven_jar(
+    name = "com_fasterxml_jackson_core",
+    artifact = "com.fasterxml.jackson.core:jackson-core:2.8.6",
+)
+
+maven_jar(
+    name = "com_google_api_client",
+    artifact = "com.google.api-client:google-api-client:1.22.0",
+)
+
+maven_jar(
+    name = "com_google_cloudkms",
+    artifact = "com.google.apis:google-api-services-cloudkms:v1beta1-rev51-1.18.0-rc",
+)
+
 maven_jar(
     name = "com_google_inject_guice",
     artifact = "com.google.inject:guice:4.1.0",
 )
 
-load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_repositories")
-java_proto_repositories()
+maven_jar(
+    name = "com_google_http_client",
+    artifact = "com.google.http-client:google-http-client:1.22.0",
+)
 
-load("@org_pubref_rules_protobuf//cpp:rules.bzl", "cpp_proto_repositories")
-cpp_proto_repositories()
+maven_jar(
+    name = "com_google_http_client_jackson2",
+    artifact = "com.google.http-client:google-http-client-jackson2:1.22.0",
+)
+
+maven_jar(
+    name = "com_google_oauth_client",
+    artifact = "com.google.oauth-client:google-oauth-client:1.22.0",
+)
+
+maven_jar(
+    name = "org_codehaus_jackson",
+    artifact = "org.codehaus.jackson:jackson-core-asl:1.9.13",
+)
diff --git a/java/BUILD b/java/BUILD
index 747c0da26..bbb8ff33e 100644
--- a/java/BUILD
+++ b/java/BUILD
@@ -3,13 +3,7 @@ JAVACOPTS = [
     "-Xlint:deprecation",
 ]
 
-java_library(
-    name = "signature",
-    srcs = [
-        "src/main/java/com/google/cloud/crypto/tink/PublicKeySign.java",
-        "src/main/java/com/google/cloud/crypto/tink/PublicKeyVerify.java",
-    ],
-)
+# core
 
 java_library(
     name = "core",
@@ -31,44 +25,32 @@ COMMON_JAVA_DEPS = [
 
 # tests
 
-java_test(
-    name = "TinkProtoTest",
-    size = "small",
-    srcs = ["src/test/java/com/google/cloud/crypto/tink/TinkProtoTest.java"],
-    test_class = "com.google.cloud.crypto.tink.TinkProtoTest",
-    deps = [
-        "//proto:java_core",
-        "@junit_junit_4//jar",
-    ],
-)
+load(":GenTestRules.bzl", "GenTestRules")
 
-java_test(
-    name = "EcdsaProtoTest",
-    size = "small",
-    srcs = ["src/test/java/com/google/cloud/crypto/tink/EcdsaProtoTest.java"],
-    test_class = "com.google.cloud.crypto.tink.EcdsaProtoTest",
-    deps = [
-        "//proto:java_ecdsa",
-        "@junit_junit_4//jar",
-    ],
-)
-
-java_test(
-    name = "PrimitiveSetTest",
-    size = "small",
-    srcs = ["src/test/java/com/google/cloud/crypto/tink/PrimitiveSetTest.java"],
-    test_class = "com.google.cloud.crypto.tink.PrimitiveSetTest",
+java_library(
+    name = "generator_test",
+    testonly = 1,
+    srcs = glob([
+        "src/test/java/**/*.java",
+    ]),
     deps = COMMON_JAVA_DEPS + [
+        "//proto:java_ecdsa",
+        "@com_google_api_client//jar",
+        "@com_google_cloudkms//jar",
+        "@com_fasterxml_jackson_core//jar",
         "@junit_junit_4//jar",
     ],
 )
 
-java_test(
-    name = "RegistryTest",
-    size = "small",
-    srcs = ["src/test/java/com/google/cloud/crypto/tink/RegistryTest.java"],
-    test_class = "com.google.cloud.crypto.tink.RegistryTest",
-    deps = COMMON_JAVA_DEPS + [
-        "@junit_junit_4//jar",
+GenTestRules(
+    name = "GeneratedTestRules",
+    small_tests = glob([
+        "src/test/java/**/*ProtoTest.java",
+    ]),
+    test_files = glob([
+        "src/test/java/**/*Test.java",
+    ]),
+    deps = [
+        ":generator_test",
     ],
 )
diff --git a/java/GenTestRules.bzl b/java/GenTestRules.bzl
new file mode 100644
index 000000000..79ef33bd8
--- /dev/null
+++ b/java/GenTestRules.bzl
@@ -0,0 +1,109 @@
+# -*- mode:python; -*-
+#
+# Copyright 2016 The Bazel Authors. All rights reserved.
+#
+# 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.
+
+"""Generate java test rules from given test_files.
+
+Instead of having to create one test rule per test in the BUILD file, this rule
+provides a handy way to create a bunch of test rules for the specified test
+files.
+
+"""
+
+
+def GenTestRules(name,
+                 test_files,
+                 deps,
+                 exclude_tests=[],
+                 default_test_size="small",
+                 small_tests=[],
+                 medium_tests=[],
+                 large_tests=[],
+                 enormous_tests=[],
+                 resources=[],
+                 flaky_tests=[],
+                 tags=[],
+                 prefix="",
+                 jvm_flags=["-XX:MaxPermSize=128m"],
+                 args=[],
+                 visibility=None,
+                 shard_count=1):
+  for test in _get_test_names(test_files):
+    if test in exclude_tests:
+      continue
+    test_size = default_test_size
+    if test in small_tests:
+      test_size = "small"
+    if test in medium_tests:
+      test_size = "medium"
+    if test in large_tests:
+      test_size = "large"
+    if test in enormous_tests:
+      test_size = "enormous"
+    flaky = 0
+    if (test in flaky_tests) or ("flaky" in tags):
+      flaky = 1
+    java_class = _package_from_path(
+        PACKAGE_NAME + "/" + _strip_right(test, ".java"))
+    package = java_class[:java_class.rfind(".")]
+    native.java_test(name = prefix + test,
+                     runtime_deps = deps,
+                     resources = resources,
+                     size = test_size,
+                     jvm_flags = jvm_flags,
+                     args = args,
+                     flaky = flaky,
+                     tags = tags,
+                     test_class = java_class,
+                     visibility = visibility,
+                     shard_count = shard_count)
+
+
+def _get_test_names(test_files):
+  test_names = []
+  for test_file in test_files:
+    if not test_file.endswith("Test.java"):
+      continue
+    test_names += [test_file[:-5]]
+  return test_names
+
+
+def _package_from_path(package_path, src_impls=None):
+  src_impls = src_impls or ['src/test/java/', 'javatests/', 'java/']
+  for src_impl in src_impls:
+    if not src_impl.endswith('/'):
+      src_impl += '/'
+    index = _index_of_end(package_path, src_impl)
+    if index >= 0:
+      package_path = package_path[index:]
+      break
+  return package_path.replace('/', '.')
+
+
+def _strip_right(str, suffix):
+  """Returns str without the suffix if it ends with suffix."""
+  if str.endswith(suffix):
+    return str[0: len(str) - len(suffix)]
+  else:
+    return str
+
+
+def _index_of_end(str, part):
+  """If part is in str, return the index of the first character after part.
+  Return -1 if part is not in str."""
+  index = str.find(part)
+  if index >= 0:
+    return index + len(part)
+  return -1
\ No newline at end of file
diff --git a/proto/BUILD b/proto/BUILD
index 729485dfb..31716bda8 100644
--- a/proto/BUILD
+++ b/proto/BUILD
@@ -5,13 +5,15 @@ load("@org_pubref_rules_protobuf//java:rules.bzl", "java_proto_library")
 
 filegroup(
     name = "core",
-    srcs = ["tink.proto"],
+    srcs = [
+        "common.proto",
+        "tink.proto",
+    ],
 )
 
 filegroup(
     name = "ecdsa",
     srcs = [
-        "common.proto",
         "ecdsa.proto",
     ],
 )
@@ -39,6 +41,9 @@ cc_proto_library(
     inputs = [
         "@com_github_google_protobuf//:well_known_protos",
     ],
+    proto_deps = [
+        ":cc_core",
+    ],
     protos = [
         ":ecdsa",
     ],
@@ -69,6 +74,9 @@ java_proto_library(
     inputs = [
         "@com_github_google_protobuf//:well_known_protos",
     ],
+    proto_deps = [
+        ":java_core",
+    ],
     protos = [
         ":ecdsa",
     ],
-- 
GitLab