|
| 1 | +/** |
| 2 | + * Copyright (C) 2021 Red Hat, Inc. (https://github.com/Commonjava/service-parent) |
| 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 org.commonjava.service.storage.util; |
| 17 | + |
| 18 | +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; |
| 19 | +import org.testcontainers.containers.CassandraContainer; |
| 20 | +import org.testcontainers.utility.DockerImageName; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Paths; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +public class CassandraTestResource implements QuarkusTestResourceLifecycleManager { |
| 29 | + |
| 30 | + static DockerImageName dockerImageName = DockerImageName.parse("cassandra:3.11.10"); |
| 31 | + static CassandraContainer<?> cassandraContainer = new CassandraContainer<>(dockerImageName) |
| 32 | + .withExposedPorts(9042); |
| 33 | + |
| 34 | + @Override |
| 35 | + public Map<String, String> start() { |
| 36 | + cassandraContainer.start(); |
| 37 | + |
| 38 | + // Load CQL script if it exists |
| 39 | + try { |
| 40 | + String scriptPath = "src/test/resources/cql/load.cql"; |
| 41 | + if (Files.exists(Paths.get(scriptPath))) { |
| 42 | + String cqlScript = Files.readString(Paths.get(scriptPath)).trim(); |
| 43 | + if (!cqlScript.isEmpty()) { |
| 44 | + // Split by semicolon and execute each statement |
| 45 | + String[] statements = cqlScript.split(";"); |
| 46 | + for (String statement : statements) { |
| 47 | + String trimmed = statement.trim(); |
| 48 | + if (!trimmed.isEmpty() && !trimmed.startsWith("--") && !trimmed.startsWith("//")) { |
| 49 | + cassandraContainer.getSession().execute(trimmed); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } catch (IOException e) { |
| 55 | + // Script loading is optional, continue without it |
| 56 | + } |
| 57 | + |
| 58 | + HashMap<String, String> map = new HashMap<>(); |
| 59 | + map.put("cassandra.host", cassandraContainer.getHost()); |
| 60 | + map.put("cassandra.port", String.valueOf(cassandraContainer.getMappedPort(9042))); |
| 61 | + map.put("cassandra.user", cassandraContainer.getUsername()); |
| 62 | + map.put("cassandra.pass", cassandraContainer.getPassword()); |
| 63 | + return map; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void stop() { |
| 68 | + if (cassandraContainer != null) { |
| 69 | + cassandraContainer.stop(); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments