|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package kafka.test; |
| 19 | + |
| 20 | +import kafka.test.annotation.Type; |
| 21 | +import org.apache.kafka.common.security.auth.SecurityProtocol; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import java.util.LinkedHashMap; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Optional; |
| 27 | +import java.util.Properties; |
| 28 | + |
| 29 | +/** |
| 30 | + * Represents a requested configuration of a Kafka cluster for integration testing |
| 31 | + */ |
| 32 | +public class ClusterConfig { |
| 33 | + |
| 34 | + private final Type type; |
| 35 | + private final int brokers; |
| 36 | + private final int controllers; |
| 37 | + private final String name; |
| 38 | + private final boolean autoStart; |
| 39 | + |
| 40 | + private final SecurityProtocol securityProtocol; |
| 41 | + private final String listenerName; |
| 42 | + private final File trustStoreFile; |
| 43 | + |
| 44 | + private final Properties serverProperties = new Properties(); |
| 45 | + private final Properties producerProperties = new Properties(); |
| 46 | + private final Properties consumerProperties = new Properties(); |
| 47 | + private final Properties adminClientProperties = new Properties(); |
| 48 | + private final Properties saslServerProperties = new Properties(); |
| 49 | + private final Properties saslClientProperties = new Properties(); |
| 50 | + |
| 51 | + ClusterConfig(Type type, int brokers, int controllers, String name, boolean autoStart, |
| 52 | + SecurityProtocol securityProtocol, String listenerName, File trustStoreFile) { |
| 53 | + this.type = type; |
| 54 | + this.brokers = brokers; |
| 55 | + this.controllers = controllers; |
| 56 | + this.name = name; |
| 57 | + this.autoStart = autoStart; |
| 58 | + this.securityProtocol = securityProtocol; |
| 59 | + this.listenerName = listenerName; |
| 60 | + this.trustStoreFile = trustStoreFile; |
| 61 | + } |
| 62 | + |
| 63 | + public Type clusterType() { |
| 64 | + return type; |
| 65 | + } |
| 66 | + |
| 67 | + public int numBrokers() { |
| 68 | + return brokers; |
| 69 | + } |
| 70 | + |
| 71 | + public int numControllers() { |
| 72 | + return controllers; |
| 73 | + } |
| 74 | + |
| 75 | + public Optional<String> name() { |
| 76 | + return Optional.ofNullable(name); |
| 77 | + } |
| 78 | + |
| 79 | + public boolean isAutoStart() { |
| 80 | + return autoStart; |
| 81 | + } |
| 82 | + |
| 83 | + public Properties serverProperties() { |
| 84 | + return serverProperties; |
| 85 | + } |
| 86 | + |
| 87 | + public Properties producerProperties() { |
| 88 | + return producerProperties; |
| 89 | + } |
| 90 | + |
| 91 | + public Properties consumerProperties() { |
| 92 | + return consumerProperties; |
| 93 | + } |
| 94 | + |
| 95 | + public Properties adminClientProperties() { |
| 96 | + return adminClientProperties; |
| 97 | + } |
| 98 | + |
| 99 | + public Properties saslServerProperties() { |
| 100 | + return saslServerProperties; |
| 101 | + } |
| 102 | + |
| 103 | + public Properties saslClientProperties() { |
| 104 | + return saslClientProperties; |
| 105 | + } |
| 106 | + |
| 107 | + public SecurityProtocol securityProtocol() { |
| 108 | + return securityProtocol; |
| 109 | + } |
| 110 | + |
| 111 | + public Optional<String> listenerName() { |
| 112 | + return Optional.ofNullable(listenerName); |
| 113 | + } |
| 114 | + |
| 115 | + public Optional<File> trustStoreFile() { |
| 116 | + return Optional.ofNullable(trustStoreFile); |
| 117 | + } |
| 118 | + |
| 119 | + public Map<String, String> nameTags() { |
| 120 | + Map<String, String> tags = new LinkedHashMap<>(3); |
| 121 | + name().ifPresent(name -> tags.put("Name", name)); |
| 122 | + tags.put("security", securityProtocol.name()); |
| 123 | + listenerName().ifPresent(listener -> tags.put("listener", listener)); |
| 124 | + return tags; |
| 125 | + } |
| 126 | + |
| 127 | + public ClusterConfig copyOf() { |
| 128 | + ClusterConfig copy = new ClusterConfig(type, brokers, controllers, name, autoStart, securityProtocol, listenerName, trustStoreFile); |
| 129 | + copy.serverProperties.putAll(serverProperties); |
| 130 | + copy.producerProperties.putAll(producerProperties); |
| 131 | + copy.consumerProperties.putAll(consumerProperties); |
| 132 | + copy.saslServerProperties.putAll(saslServerProperties); |
| 133 | + copy.saslClientProperties.putAll(saslClientProperties); |
| 134 | + return copy; |
| 135 | + } |
| 136 | + |
| 137 | + public static Builder defaultClusterBuilder() { |
| 138 | + return new Builder(Type.ZK, 1, 1, true, SecurityProtocol.PLAINTEXT); |
| 139 | + } |
| 140 | + |
| 141 | + public static Builder clusterBuilder(Type type, int brokers, int controllers, boolean autoStart, SecurityProtocol securityProtocol) { |
| 142 | + return new Builder(type, brokers, controllers, autoStart, securityProtocol); |
| 143 | + } |
| 144 | + |
| 145 | + public static class Builder { |
| 146 | + private Type type; |
| 147 | + private int brokers; |
| 148 | + private int controllers; |
| 149 | + private String name; |
| 150 | + private boolean autoStart; |
| 151 | + private SecurityProtocol securityProtocol; |
| 152 | + private String listenerName; |
| 153 | + private File trustStoreFile; |
| 154 | + |
| 155 | + Builder(Type type, int brokers, int controllers, boolean autoStart, SecurityProtocol securityProtocol) { |
| 156 | + this.type = type; |
| 157 | + this.brokers = brokers; |
| 158 | + this.controllers = controllers; |
| 159 | + this.autoStart = autoStart; |
| 160 | + this.securityProtocol = securityProtocol; |
| 161 | + } |
| 162 | + |
| 163 | + public Builder type(Type type) { |
| 164 | + this.type = type; |
| 165 | + return this; |
| 166 | + } |
| 167 | + |
| 168 | + public Builder brokers(int brokers) { |
| 169 | + this.brokers = brokers; |
| 170 | + return this; |
| 171 | + } |
| 172 | + |
| 173 | + public Builder controllers(int controllers) { |
| 174 | + this.controllers = controllers; |
| 175 | + return this; |
| 176 | + } |
| 177 | + |
| 178 | + public Builder name(String name) { |
| 179 | + this.name = name; |
| 180 | + return this; |
| 181 | + } |
| 182 | + |
| 183 | + public Builder autoStart(boolean autoStart) { |
| 184 | + this.autoStart = autoStart; |
| 185 | + return this; |
| 186 | + } |
| 187 | + |
| 188 | + public Builder securityProtocol(SecurityProtocol securityProtocol) { |
| 189 | + this.securityProtocol = securityProtocol; |
| 190 | + return this; |
| 191 | + } |
| 192 | + |
| 193 | + public Builder listenerName(String listenerName) { |
| 194 | + this.listenerName = listenerName; |
| 195 | + return this; |
| 196 | + } |
| 197 | + |
| 198 | + public Builder trustStoreFile(File trustStoreFile) { |
| 199 | + this.trustStoreFile = trustStoreFile; |
| 200 | + return this; |
| 201 | + } |
| 202 | + |
| 203 | + public ClusterConfig build() { |
| 204 | + return new ClusterConfig(type, brokers, controllers, name, autoStart, securityProtocol, listenerName, trustStoreFile); |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments