|
| 1 | +/* |
| 2 | + * Copyright 2026 Bloomberg Finance L.P. |
| 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 | +package com.bloomberg.bmq.it.util; |
| 17 | + |
| 18 | +import com.google.gson.Gson; |
| 19 | +import com.google.gson.GsonBuilder; |
| 20 | +import com.google.gson.JsonObject; |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.nio.file.Paths; |
| 26 | + |
| 27 | +/** |
| 28 | + * Loads and modifies BlazingMQ broker configuration for CI testing. |
| 29 | + * |
| 30 | + * <p>Supports modifying: |
| 31 | + * |
| 32 | + * <ul> |
| 33 | + * <li>bmqbrkrcfg.json: log file path, stats file path, TCP port |
| 34 | + * <li>clusters.json: node endpoint port, storage locations |
| 35 | + * </ul> |
| 36 | + */ |
| 37 | +public class BmqBrokerConfiguration { |
| 38 | + |
| 39 | + private static final String BROKER_CONFIG_FILE = "bmqbrkrcfg.json"; |
| 40 | + private static final String CLUSTERS_CONFIG_FILE = "clusters.json"; |
| 41 | + private static final String DOMAINS_DIR = "domains"; |
| 42 | + |
| 43 | + private final Gson gson = new Gson(); |
| 44 | + private final Gson prettyGson = new GsonBuilder().setPrettyPrinting().create(); |
| 45 | + |
| 46 | + private final Path configPath; |
| 47 | + private JsonObject brokerConfigJson; |
| 48 | + private JsonObject clustersConfigJson; |
| 49 | + |
| 50 | + public static BmqBrokerConfiguration createFromDefaultPath() throws IOException { |
| 51 | + // Find config directory relative to this class's location |
| 52 | + Path classPath = |
| 53 | + Paths.get( |
| 54 | + BmqBrokerConfiguration.class |
| 55 | + .getProtectionDomain() |
| 56 | + .getCodeSource() |
| 57 | + .getLocation() |
| 58 | + .getPath()); |
| 59 | + // Navigate from target/test-classes to src/test/docker/config |
| 60 | + Path configPath = classPath.getParent().getParent().resolve("src/test/docker/config"); |
| 61 | + return new BmqBrokerConfiguration(configPath); |
| 62 | + } |
| 63 | + |
| 64 | + public static BmqBrokerConfiguration createFromPath(Path configPath) throws IOException { |
| 65 | + return new BmqBrokerConfiguration(configPath); |
| 66 | + } |
| 67 | + |
| 68 | + private BmqBrokerConfiguration(Path configPath) throws IOException { |
| 69 | + this.configPath = configPath; |
| 70 | + |
| 71 | + Path brokerConfigPath = configPath.resolve(BROKER_CONFIG_FILE); |
| 72 | + brokerConfigJson = |
| 73 | + gson.fromJson( |
| 74 | + new String(Files.readAllBytes(brokerConfigPath), StandardCharsets.UTF_8), |
| 75 | + JsonObject.class); |
| 76 | + |
| 77 | + Path clustersConfigPath = configPath.resolve(CLUSTERS_CONFIG_FILE); |
| 78 | + clustersConfigJson = |
| 79 | + gson.fromJson( |
| 80 | + new String(Files.readAllBytes(clustersConfigPath), StandardCharsets.UTF_8), |
| 81 | + JsonObject.class); |
| 82 | + } |
| 83 | + |
| 84 | + // ==================== bmqbrkrcfg.json accessors ==================== |
| 85 | + |
| 86 | + /** taskConfig/logController/fileName */ |
| 87 | + public String getLogFileName() { |
| 88 | + return brokerConfigJson |
| 89 | + .getAsJsonObject("taskConfig") |
| 90 | + .getAsJsonObject("logController") |
| 91 | + .get("fileName") |
| 92 | + .getAsString(); |
| 93 | + } |
| 94 | + |
| 95 | + public void setLogFileName(String fileName) { |
| 96 | + brokerConfigJson |
| 97 | + .getAsJsonObject("taskConfig") |
| 98 | + .getAsJsonObject("logController") |
| 99 | + .addProperty("fileName", fileName); |
| 100 | + } |
| 101 | + |
| 102 | + /** appConfig/stats/printer/file */ |
| 103 | + public String getStatsFile() { |
| 104 | + return brokerConfigJson |
| 105 | + .getAsJsonObject("appConfig") |
| 106 | + .getAsJsonObject("stats") |
| 107 | + .getAsJsonObject("printer") |
| 108 | + .get("file") |
| 109 | + .getAsString(); |
| 110 | + } |
| 111 | + |
| 112 | + public void setStatsFile(String file) { |
| 113 | + brokerConfigJson |
| 114 | + .getAsJsonObject("appConfig") |
| 115 | + .getAsJsonObject("stats") |
| 116 | + .getAsJsonObject("printer") |
| 117 | + .addProperty("file", file); |
| 118 | + } |
| 119 | + |
| 120 | + /** appConfig/networkInterfaces/tcpInterface/port */ |
| 121 | + public int getTcpPort() { |
| 122 | + return brokerConfigJson |
| 123 | + .getAsJsonObject("appConfig") |
| 124 | + .getAsJsonObject("networkInterfaces") |
| 125 | + .getAsJsonObject("tcpInterface") |
| 126 | + .get("port") |
| 127 | + .getAsInt(); |
| 128 | + } |
| 129 | + |
| 130 | + public void setTcpPort(int port) { |
| 131 | + brokerConfigJson |
| 132 | + .getAsJsonObject("appConfig") |
| 133 | + .getAsJsonObject("networkInterfaces") |
| 134 | + .getAsJsonObject("tcpInterface") |
| 135 | + .addProperty("port", port); |
| 136 | + } |
| 137 | + |
| 138 | + // ==================== clusters.json accessors ==================== |
| 139 | + |
| 140 | + private JsonObject getFirstCluster() { |
| 141 | + return clustersConfigJson.getAsJsonArray("myClusters").get(0).getAsJsonObject(); |
| 142 | + } |
| 143 | + |
| 144 | + private JsonObject getFirstNode() { |
| 145 | + return getFirstCluster().getAsJsonArray("nodes").get(0).getAsJsonObject(); |
| 146 | + } |
| 147 | + |
| 148 | + /** myClusters[0]/nodes[0]/transport/tcp/endpoint */ |
| 149 | + public String getNodeEndpoint() { |
| 150 | + return getFirstNode() |
| 151 | + .getAsJsonObject("transport") |
| 152 | + .getAsJsonObject("tcp") |
| 153 | + .get("endpoint") |
| 154 | + .getAsString(); |
| 155 | + } |
| 156 | + |
| 157 | + public void setNodeEndpoint(String endpoint) { |
| 158 | + getFirstNode() |
| 159 | + .getAsJsonObject("transport") |
| 160 | + .getAsJsonObject("tcp") |
| 161 | + .addProperty("endpoint", endpoint); |
| 162 | + } |
| 163 | + |
| 164 | + /** Updates the port in the node endpoint (tcp://localhost:PORT) */ |
| 165 | + public void setNodeEndpointPort(int port) { |
| 166 | + String endpoint = getNodeEndpoint(); |
| 167 | + String newEndpoint = endpoint.replaceFirst(":\\d+$", ":" + port); |
| 168 | + setNodeEndpoint(newEndpoint); |
| 169 | + } |
| 170 | + |
| 171 | + /** myClusters[0]/partitionConfig/location */ |
| 172 | + public String getPartitionLocation() { |
| 173 | + return getFirstCluster().getAsJsonObject("partitionConfig").get("location").getAsString(); |
| 174 | + } |
| 175 | + |
| 176 | + public void setPartitionLocation(String location) { |
| 177 | + getFirstCluster().getAsJsonObject("partitionConfig").addProperty("location", location); |
| 178 | + } |
| 179 | + |
| 180 | + /** myClusters[0]/partitionConfig/archiveLocation */ |
| 181 | + public String getArchiveLocation() { |
| 182 | + return getFirstCluster() |
| 183 | + .getAsJsonObject("partitionConfig") |
| 184 | + .get("archiveLocation") |
| 185 | + .getAsString(); |
| 186 | + } |
| 187 | + |
| 188 | + public void setArchiveLocation(String location) { |
| 189 | + getFirstCluster() |
| 190 | + .getAsJsonObject("partitionConfig") |
| 191 | + .addProperty("archiveLocation", location); |
| 192 | + } |
| 193 | + |
| 194 | + // ==================== Save ==================== |
| 195 | + |
| 196 | + /** |
| 197 | + * Saves configuration files to the specified directory, including domains. |
| 198 | + * |
| 199 | + * @param outputPath directory to save bmqbrkrcfg.json, clusters.json, and domains/ |
| 200 | + */ |
| 201 | + public void saveTo(Path outputPath) throws IOException { |
| 202 | + Files.createDirectories(outputPath); |
| 203 | + |
| 204 | + Files.write( |
| 205 | + outputPath.resolve(BROKER_CONFIG_FILE), |
| 206 | + prettyGson.toJson(brokerConfigJson).getBytes(StandardCharsets.UTF_8)); |
| 207 | + |
| 208 | + Files.write( |
| 209 | + outputPath.resolve(CLUSTERS_CONFIG_FILE), |
| 210 | + prettyGson.toJson(clustersConfigJson).getBytes(StandardCharsets.UTF_8)); |
| 211 | + |
| 212 | + // Copy domains directory |
| 213 | + Path srcDomains = configPath.resolve(DOMAINS_DIR); |
| 214 | + if (Files.isDirectory(srcDomains)) { |
| 215 | + Path destDomains = outputPath.resolve(DOMAINS_DIR); |
| 216 | + Files.createDirectories(destDomains); |
| 217 | + Files.list(srcDomains) |
| 218 | + .filter(p -> p.toString().endsWith(".json")) |
| 219 | + .forEach( |
| 220 | + src -> { |
| 221 | + try { |
| 222 | + Files.copy(src, destDomains.resolve(src.getFileName())); |
| 223 | + } catch (IOException e) { |
| 224 | + throw new RuntimeException("Failed to copy domain: " + src, e); |
| 225 | + } |
| 226 | + }); |
| 227 | + } |
| 228 | + } |
| 229 | +} |
0 commit comments