[DCAEGEN2] Remove DMaaP dependency in VES-Collector
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / common / publishing / DMaapContainer.java
1 /*
2  * ============LICENSE_START====================================
3  * VES Collector
4  * =========================================================
5  * Copyright (C) 2019-2021 Nokia. All rights reserved.
6  * Copyright (C) 2023 AT&T Intellectual Property. All rights reserved.
7  * Copyright (C) 2023 Deutsche Telekom Intellectual Property. All rights reserved.
8  * =========================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *       http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================
21  */
22
23 package org.onap.dcae.common.publishing;
24
25 import org.onap.dcae.FileReader;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.testcontainers.containers.DockerComposeContainer;
29 import org.testcontainers.containers.wait.strategy.Wait;
30
31 import java.io.File;
32 import java.net.URI;
33 import java.net.URISyntaxException;
34 import java.net.URL;
35
36 final class DMaapContainer {
37     private static final String MR_COMPOSE_RESOURCE_NAME = "dmaap-msg-router/message-router-compose.yml";
38     private static final String DOCKER_COMPOSE_FILE_PATH = getDockerComposeFilePath(MR_COMPOSE_RESOURCE_NAME);
39     static final int KAFKA_SERVICE_EXPOSED_PORT = 9092;
40     static final String KAFKA_SERVICE_NAME = "kafka";
41     private static final Logger log = LoggerFactory.getLogger(DMaapContainer.class);
42     
43     private DMaapContainer() {}
44
45
46     public static DockerComposeContainer createContainerInstance() {
47
48         URI dockercomposeuri = null;
49         try {
50             dockercomposeuri = new URI(DOCKER_COMPOSE_FILE_PATH);
51         } catch (URISyntaxException e) {
52             log.error("Error while opening docker compose file.", e);
53         }
54         return new DockerComposeContainer(
55                 new File(dockercomposeuri.getPath()))
56                 .withExposedService(KAFKA_SERVICE_NAME, KAFKA_SERVICE_EXPOSED_PORT, Wait.forListeningPort())
57                 .withLocalCompose(true);
58     }
59
60
61
62     private static String getDockerComposeFilePath(String resourceName) {
63         URL resource = DMaapContainer.class.getClassLoader()
64                 .getResource(resourceName);
65
66         if (resource != null) return resource.getFile();
67         else throw new RuntimeException(String
68                 .format("File %s does not exist", resourceName));
69     }
70 }