|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.logging.testing; |
| 18 | + |
| 19 | +import com.google.cloud.AuthCredentials; |
| 20 | +import com.google.cloud.RetryParams; |
| 21 | +import com.google.cloud.logging.LoggingOptions; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStream; |
| 25 | +import java.util.UUID; |
| 26 | +import java.util.logging.Level; |
| 27 | +import java.util.logging.Logger; |
| 28 | + |
| 29 | +/** |
| 30 | + * Utility to create a remote logging configuration for testing. Logging options can be obtained via |
| 31 | + * the {@link #options()} method. Returned options have custom {@link LoggingOptions#retryParams()}: |
| 32 | + * {@link RetryParams#maxRetryDelayMillis()} is {@code 30000}, |
| 33 | + * {@link RetryParams#totalRetryPeriodMillis()} is {@code 120000} and |
| 34 | + * {@link RetryParams#initialRetryDelayMillis()} is {@code 250}. |
| 35 | + * {@link LoggingOptions#initialTimeout()} is set to 60000, {@link LoggingOptions#maxTimeout()} is |
| 36 | + * set to {@code 240000} and {@link LoggingOptions#timeoutMultiplier()} is set to {@code 1.5}. |
| 37 | + */ |
| 38 | +public class RemoteLoggingHelper { |
| 39 | + |
| 40 | + private static final Logger log = Logger.getLogger(RemoteLoggingHelper.class.getName()); |
| 41 | + private final LoggingOptions options; |
| 42 | + |
| 43 | + private RemoteLoggingHelper(LoggingOptions options) { |
| 44 | + this.options = options; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns a {@link LoggingOptions} object to be used for testing. |
| 49 | + */ |
| 50 | + public LoggingOptions options() { |
| 51 | + return options; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Creates a {@code RemoteLoggingHelper} object for the given project id and JSON key input |
| 56 | + * stream. |
| 57 | + * |
| 58 | + * @param projectId id of the project to be used for running the tests |
| 59 | + * @param keyStream input stream for a JSON key |
| 60 | + * @return A {@code RemoteLoggingHelper} object for the provided options |
| 61 | + * @throws com.google.cloud.logging.testing.RemoteLoggingHelper.LoggingHelperException if |
| 62 | + * {@code keyStream} is not a valid JSON key stream |
| 63 | + */ |
| 64 | + public static RemoteLoggingHelper create(String projectId, InputStream keyStream) |
| 65 | + throws LoggingHelperException { |
| 66 | + try { |
| 67 | + LoggingOptions storageOptions = LoggingOptions.builder() |
| 68 | + .authCredentials(AuthCredentials.createForJson(keyStream)) |
| 69 | + .projectId(projectId) |
| 70 | + .retryParams(retryParams()) |
| 71 | + .initialTimeout(60000) |
| 72 | + .maxTimeout(120000) |
| 73 | + .timeoutMultiplier(1.5) |
| 74 | + .build(); |
| 75 | + return new RemoteLoggingHelper(storageOptions); |
| 76 | + } catch (IOException ex) { |
| 77 | + if (log.isLoggable(Level.WARNING)) { |
| 78 | + log.log(Level.WARNING, ex.getMessage()); |
| 79 | + } |
| 80 | + throw LoggingHelperException.translate(ex); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Creates a {@code RemoteLoggingHelper} object using default project id and authentication |
| 86 | + * credentials. |
| 87 | + */ |
| 88 | + public static RemoteLoggingHelper create() throws LoggingHelperException { |
| 89 | + LoggingOptions loggingOptions = LoggingOptions.builder() |
| 90 | + .retryParams(retryParams()) |
| 91 | + .initialTimeout(60000) |
| 92 | + .maxTimeout(240000) |
| 93 | + .timeoutMultiplier(1.5) |
| 94 | + .build(); |
| 95 | + return new RemoteLoggingHelper(loggingOptions); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Formats a resource name for testing purpose. This method appends a random UUID to the provided |
| 100 | + * name. |
| 101 | + */ |
| 102 | + public static String formatForTest(String name) { |
| 103 | + return name + "-" + UUID.randomUUID().toString(); |
| 104 | + } |
| 105 | + |
| 106 | + private static RetryParams retryParams() { |
| 107 | + return RetryParams.builder() |
| 108 | + .maxRetryDelayMillis(30000) |
| 109 | + .totalRetryPeriodMillis(120000) |
| 110 | + .initialRetryDelayMillis(250) |
| 111 | + .build(); |
| 112 | + } |
| 113 | + |
| 114 | + public static class LoggingHelperException extends RuntimeException { |
| 115 | + |
| 116 | + private static final long serialVersionUID = 2617749404172557196L; |
| 117 | + |
| 118 | + public LoggingHelperException(String message, Throwable cause) { |
| 119 | + super(message, cause); |
| 120 | + } |
| 121 | + |
| 122 | + public static LoggingHelperException translate(Exception ex) { |
| 123 | + return new LoggingHelperException(ex.getMessage(), ex); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments