/test-infra/camel-test-infra-mongodb/src/test/java/org/apache/camel/test/infra/mongodb/services/MongoDBLocalContainerService.java

https://github.com/gnodet/camel · Java · 78 lines · 51 code · 11 blank · 16 comment · 4 complexity · c78646ee3751ba21d87a61d8bcd20497 MD5 · raw file

  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. package org.apache.camel.test.infra.mongodb.services;
  18. import org.apache.camel.test.infra.common.services.ContainerService;
  19. import org.apache.camel.test.infra.mongodb.common.MongoDBProperties;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import org.testcontainers.containers.MongoDBContainer;
  23. public class MongoDBLocalContainerService implements MongoDBService, ContainerService<MongoDBContainer> {
  24. private static final Logger LOG = LoggerFactory.getLogger(MongoDBLocalContainerService.class);
  25. private static final int DEFAULT_MONGODB_PORT = 27017;
  26. private final MongoDBContainer container;
  27. public MongoDBLocalContainerService() {
  28. this(System.getProperty("mongodb.container"));
  29. }
  30. public MongoDBLocalContainerService(String containerName) {
  31. if (containerName == null || containerName.isEmpty()) {
  32. container = new MongoDBContainer();
  33. } else {
  34. container = new MongoDBContainer(containerName);
  35. }
  36. }
  37. @Override
  38. public String getReplicaSetUrl() {
  39. return String.format("mongodb://%s:%s", container.getContainerIpAddress(),
  40. container.getMappedPort(DEFAULT_MONGODB_PORT));
  41. }
  42. @Override
  43. public String getConnectionAddress() {
  44. return container.getContainerIpAddress() + ":" + container.getMappedPort(DEFAULT_MONGODB_PORT);
  45. }
  46. @Override
  47. public void registerProperties() {
  48. System.setProperty(MongoDBProperties.MONGODB_URL, getReplicaSetUrl());
  49. System.setProperty(MongoDBProperties.MONGODB_CONNECTION_ADDRESS, getConnectionAddress());
  50. }
  51. @Override
  52. public void initialize() {
  53. LOG.info("Trying to start the MongoDB service");
  54. container.start();
  55. registerProperties();
  56. LOG.info("MongoDB service running at {}", container.getReplicaSetUrl());
  57. }
  58. @Override
  59. public void shutdown() {
  60. LOG.info("Stopping the MongoDB container");
  61. container.stop();
  62. }
  63. @Override
  64. public MongoDBContainer getContainer() {
  65. return container;
  66. }
  67. }