VESCollector Test optimization
[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  * =========================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=====================================
20  */
21
22 package org.onap.dcae.common.publishing;
23
24 import org.onap.dcae.FileReader;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.testcontainers.containers.DockerComposeContainer;
28
29 import java.io.File;
30 import java.net.URI;
31 import java.net.URISyntaxException;
32 import java.net.URL;
33
34 final class DMaapContainer {
35     private static final String MR_COMPOSE_RESOURCE_NAME = "dmaap-msg-router/message-router-compose.yml";
36     private static final String DOCKER_COMPOSE_FILE_PATH = getDockerComposeFilePath(MR_COMPOSE_RESOURCE_NAME);
37     static final int DMAAP_SERVICE_EXPOSED_PORT = 3904;
38     static final String DMAAP_SERVICE_NAME = "onap-dmaap";
39     private static final Logger log = LoggerFactory.getLogger(DMaapContainer.class);
40     
41     private DMaapContainer() {}
42
43
44     public static DockerComposeContainer createContainerInstance() {
45
46         URI dockercomposeuri = null;
47         try {
48             dockercomposeuri = new URI(DOCKER_COMPOSE_FILE_PATH);
49         } catch (URISyntaxException e) {
50             log.error("Error while opening docker compose file.", e);
51         }
52         return new DockerComposeContainer(
53                 new File(dockercomposeuri.getPath()))
54                 .withExposedService(DMAAP_SERVICE_NAME, DMAAP_SERVICE_EXPOSED_PORT)
55                 .withLocalCompose(true);
56     }
57
58
59
60     private static String getDockerComposeFilePath(String resourceName) {
61         URL resource = DMaapContainer.class.getClassLoader()
62                 .getResource(resourceName);
63
64         if (resource != null) return resource.getFile();
65         else throw new RuntimeException(String
66                 .format("File %s does not exist", resourceName));
67     }
68 }