Merge pull request #278 from bsinno/feature_boot_13_sec_41
Upgrade Spring Boot and Security
This commit is contained in:
@@ -8,7 +8,8 @@
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.eclipse.hawkbit</groupId>
|
||||
@@ -120,7 +121,7 @@
|
||||
<groupId>javax.el</groupId>
|
||||
<artifactId>javax.el-api</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ru.yandex.qatools.allure</groupId>
|
||||
<artifactId>allure-junit-adaptor</artifactId>
|
||||
@@ -140,37 +141,19 @@
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.bsc.maven</groupId>
|
||||
<artifactId>maven-processor-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>process</id>
|
||||
<goals>
|
||||
<goal>process</goal>
|
||||
</goals>
|
||||
<phase>generate-sources</phase>
|
||||
<configuration>
|
||||
<processors>
|
||||
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
|
||||
</processors>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-jpamodelgen</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<!-- Static weaver for EclipseLink -->
|
||||
<plugin>
|
||||
<groupId>com.ethlo.persistence.tools</groupId>
|
||||
<artifactId>eclipselink-maven-plugin</artifactId>
|
||||
<version>2.6.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>modelgen</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>modelgen</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<phase>process-classes</phase>
|
||||
<goals>
|
||||
|
||||
@@ -56,10 +56,10 @@ import org.eclipse.hawkbit.security.SecurityTokenGenerator;
|
||||
import org.eclipse.hawkbit.security.SystemSecurityContext;
|
||||
import org.eclipse.hawkbit.tenancy.TenantAware;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.orm.jpa.EntityScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -85,9 +85,9 @@ import com.google.common.eventbus.EventBus;
|
||||
@EnableAspectJAutoProxy
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
@EnableAutoConfiguration
|
||||
@EnableConfigurationProperties(RepositoryProperties.class)
|
||||
@EnableScheduling
|
||||
@EntityScan("org.eclipse.hawkbit.repository.jpa.model")
|
||||
public class RepositoryApplicationConfiguration extends JpaBaseConfiguration {
|
||||
@Autowired
|
||||
private EventBus eventBus;
|
||||
|
||||
@@ -12,12 +12,12 @@ import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import org.eclipse.hawkbit.cache.TenantAwareCacheManager;
|
||||
import org.eclipse.hawkbit.repository.test.util.AbstractIntegrationTestWithMongoDB;
|
||||
import org.eclipse.hawkbit.repository.test.util.AbstractIntegrationTest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
|
||||
@SpringApplicationConfiguration(classes = { org.eclipse.hawkbit.RepositoryApplicationConfiguration.class })
|
||||
public abstract class AbstractJpaIntegrationTestWithMongoDB extends AbstractIntegrationTestWithMongoDB {
|
||||
public abstract class AbstractJpaIntegrationTestWithMongoDB extends AbstractIntegrationTest {
|
||||
|
||||
@PersistenceContext
|
||||
protected EntityManager entityManager;
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.jpa;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.eclipse.hawkbit.repository.exception.ArtifactDeleteFailedException;
|
||||
import org.eclipse.hawkbit.repository.exception.ArtifactUploadFailedException;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaSoftwareModule;
|
||||
import org.eclipse.hawkbit.repository.model.Artifact;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import ru.yandex.qatools.allure.annotations.Description;
|
||||
import ru.yandex.qatools.allure.annotations.Features;
|
||||
import ru.yandex.qatools.allure.annotations.Stories;
|
||||
|
||||
@Features("Component Tests - Repository")
|
||||
@Stories("Artifact Management")
|
||||
public class ArtifactManagementFailedMongoDBTest extends AbstractJpaIntegrationTestWithMongoDB {
|
||||
|
||||
@Test
|
||||
@Description("Trys and fails to delete or create local artifact with a down mongodb and checks if expected ArtifactDeleteFailedException is thrown.")
|
||||
public void deleteArtifactsWithNoMongoDb() throws UnknownHostException, IOException {
|
||||
// ensure baseline
|
||||
assertThat(artifactRepository.findAll()).isEmpty();
|
||||
|
||||
// prepare test
|
||||
JpaSoftwareModule sm = new JpaSoftwareModule(softwareManagement.findSoftwareModuleTypeByKey("os"), "name 1",
|
||||
"version 1", null, null);
|
||||
sm = softwareModuleRepository.save(sm);
|
||||
|
||||
final Artifact result = artifactManagement.createLocalArtifact(new RandomGeneratedInputStream(5 * 1024),
|
||||
sm.getId(), "file1", false);
|
||||
|
||||
assertThat(artifactRepository.findAll()).hasSize(1);
|
||||
|
||||
mongodExecutable.stop();
|
||||
try {
|
||||
artifactManagement.deleteLocalArtifact(result.getId());
|
||||
fail("deletion should have failed");
|
||||
} catch (final ArtifactDeleteFailedException e) {
|
||||
|
||||
}
|
||||
|
||||
try {
|
||||
artifactManagement.createLocalArtifact(new RandomGeneratedInputStream(5 * 1024), sm.getId(), "file2",
|
||||
false);
|
||||
fail("Should not have worked with MongoDb down.");
|
||||
} catch (final ArtifactUploadFailedException e) {
|
||||
|
||||
}
|
||||
|
||||
assertThat(artifactRepository.findAll()).hasSize(1);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@After
|
||||
public void cleanCurrentCollection() {
|
||||
// no need to clean, is stopped already
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.jpa;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.eclipse.hawkbit.repository.exception.ArtifactUploadFailedException;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaSoftwareModule;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import ru.yandex.qatools.allure.annotations.Description;
|
||||
import ru.yandex.qatools.allure.annotations.Features;
|
||||
import ru.yandex.qatools.allure.annotations.Stories;
|
||||
|
||||
/**
|
||||
* Addition tests next to {@link ArtifactManagementTest} with no running MongoDB
|
||||
*
|
||||
*/
|
||||
@Features("Component Tests - Repository")
|
||||
@Stories("Artifact Management")
|
||||
public class ArtifactManagementNoMongoDbTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void initialize() {
|
||||
// set property to mongoPort which does not start any mongoDB of
|
||||
// parallel test execution
|
||||
System.setProperty("spring.data.mongodb.port", "1020");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Checks if the expected ArtifactUploadFailedException is thrown in case of MongoDB down")
|
||||
public void createLocalArtifactWithMongoDbDown() throws IOException {
|
||||
JpaSoftwareModule sm = new JpaSoftwareModule(softwareManagement.findSoftwareModuleTypeByKey("os"), "name 1",
|
||||
"version 1", null, null);
|
||||
sm = softwareModuleRepository.save(sm);
|
||||
|
||||
final byte random[] = RandomStringUtils.random(5 * 1024).getBytes();
|
||||
|
||||
try {
|
||||
artifactManagement.createLocalArtifact(new ByteArrayInputStream(random), sm.getId(), "file1", false);
|
||||
fail("Should not have worked with MongoDb down.");
|
||||
} catch (final ArtifactUploadFailedException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,14 +15,12 @@ import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.eclipse.hawkbit.im.authentication.SpPermission;
|
||||
import org.eclipse.hawkbit.repository.ArtifactManagement;
|
||||
import org.eclipse.hawkbit.repository.exception.ArtifactDeleteFailedException;
|
||||
import org.eclipse.hawkbit.repository.exception.InsufficientPermissionException;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaExternalArtifact;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaExternalArtifactProvider;
|
||||
@@ -241,36 +239,6 @@ public class ArtifactManagementTest extends AbstractJpaIntegrationTestWithMongoD
|
||||
assertThat(artifactRepository.findAll()).hasSize(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Trys and fails to delete local artifact with a down mongodb and checks if expected ArtifactDeleteFailedException is thrown.")
|
||||
public void deleteArtifactsWithNoMongoDb() throws UnknownHostException, IOException {
|
||||
// ensure baseline
|
||||
assertThat(artifactRepository.findAll()).isEmpty();
|
||||
|
||||
// prepare test
|
||||
JpaSoftwareModule sm = new JpaSoftwareModule(softwareManagement.findSoftwareModuleTypeByKey("os"), "name 1",
|
||||
"version 1", null, null);
|
||||
sm = softwareModuleRepository.save(sm);
|
||||
|
||||
final Artifact result = artifactManagement.createLocalArtifact(new RandomGeneratedInputStream(5 * 1024),
|
||||
sm.getId(), "file1", false);
|
||||
|
||||
assertThat(artifactRepository.findAll()).hasSize(1);
|
||||
|
||||
internalShutDownMongo();
|
||||
try {
|
||||
artifactManagement.deleteLocalArtifact(result.getId());
|
||||
fail("deletion should have failed");
|
||||
} catch (final ArtifactDeleteFailedException e) {
|
||||
|
||||
}
|
||||
setupMongo();
|
||||
|
||||
assertThat(artifactRepository.findAll()).hasSize(1);
|
||||
assertThat(artifactManagement.findArtifact(result.getId())).isEqualTo(result);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Test the deletion of an artifact metadata where the binary is still linked to another "
|
||||
+ "metadata element. The expected result is that the metadata is deleted but the binary kept.")
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
|
||||
Copyright (c) 2015 Bosch Software Innovations GmbH and others.
|
||||
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
which accompanies this distribution, and is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
|
||||
-->
|
||||
<configuration>
|
||||
<include resource="org/springframework/boot/logging/logback/base.xml" />
|
||||
|
||||
<Root level="INFO">
|
||||
<appender-ref ref="CONSOLE" />
|
||||
</Root>
|
||||
|
||||
</configuration>
|
||||
@@ -6,22 +6,25 @@
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.test.util;
|
||||
package org.eclipse.hawkbit;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.eclipse.hawkbit.HawkbitServerProperties;
|
||||
import org.eclipse.hawkbit.cache.CacheConstants;
|
||||
import org.eclipse.hawkbit.cache.TenancyCacheManager;
|
||||
import org.eclipse.hawkbit.cache.TenantAwareCacheManager;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.helper.EventBusHolder;
|
||||
import org.eclipse.hawkbit.repository.test.util.JpaTestRepositoryManagement;
|
||||
import org.eclipse.hawkbit.repository.test.util.TestRepositoryManagement;
|
||||
import org.eclipse.hawkbit.repository.test.util.TestdataFactory;
|
||||
import org.eclipse.hawkbit.security.DdiSecurityProperties;
|
||||
import org.eclipse.hawkbit.security.SecurityContextTenantAware;
|
||||
import org.eclipse.hawkbit.security.SpringSecurityAuditorAware;
|
||||
import org.eclipse.hawkbit.tenancy.TenantAware;
|
||||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
|
||||
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.guava.GuavaCacheManager;
|
||||
@@ -45,11 +48,11 @@ import com.mongodb.MongoClientOptions;
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true, mode = AdviceMode.ASPECTJ, proxyTargetClass = true, securedEnabled = true)
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true, mode = AdviceMode.PROXY, proxyTargetClass = false, securedEnabled = true)
|
||||
@EnableConfigurationProperties({ HawkbitServerProperties.class, DdiSecurityProperties.class })
|
||||
@Profile("test")
|
||||
@EnableAutoConfiguration
|
||||
public class TestConfiguration implements AsyncConfigurer {
|
||||
|
||||
@Bean
|
||||
public TestRepositoryManagement testRepositoryManagement() {
|
||||
return new JpaTestRepositoryManagement();
|
||||
@@ -12,6 +12,7 @@ import static org.eclipse.hawkbit.im.authentication.SpPermission.SpringEvalExpre
|
||||
import static org.eclipse.hawkbit.im.authentication.SpPermission.SpringEvalExpressions.SYSTEM_ROLE;
|
||||
|
||||
import org.eclipse.hawkbit.ExcludePathAwareShallowETagFilter;
|
||||
import org.eclipse.hawkbit.TestConfiguration;
|
||||
import org.eclipse.hawkbit.repository.ArtifactManagement;
|
||||
import org.eclipse.hawkbit.repository.ControllerManagement;
|
||||
import org.eclipse.hawkbit.repository.DeploymentManagement;
|
||||
@@ -48,9 +49,12 @@ import org.springframework.core.env.Environment;
|
||||
import org.springframework.data.auditing.AuditingHandler;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.gridfs.GridFsOperations;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
@@ -58,6 +62,8 @@ import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import de.flapdoodle.embed.mongo.MongodExecutable;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ActiveProfiles({ "test" })
|
||||
@@ -68,6 +74,7 @@ import org.springframework.web.context.WebApplicationContext;
|
||||
// refreshed we e.g. get two instances of CacheManager which leads to very
|
||||
// strange test failures.
|
||||
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
|
||||
@TestPropertySource(properties = { "spring.data.mongodb.port=0", "spring.mongodb.embedded.version=3.2.7" })
|
||||
public abstract class AbstractIntegrationTest implements EnvironmentAware {
|
||||
protected static Logger LOG = null;
|
||||
|
||||
@@ -149,6 +156,12 @@ public abstract class AbstractIntegrationTest implements EnvironmentAware {
|
||||
@Autowired
|
||||
protected TestdataFactory testdataFactory;
|
||||
|
||||
@Autowired
|
||||
protected GridFsOperations operations;
|
||||
|
||||
@Autowired
|
||||
protected MongodExecutable mongodExecutable;
|
||||
|
||||
@Rule
|
||||
public final WithSpringAuthorityRule securityRule = new WithSpringAuthorityRule();
|
||||
|
||||
@@ -187,6 +200,11 @@ public abstract class AbstractIntegrationTest implements EnvironmentAware {
|
||||
testRepositoryManagement.clearTestRepository();
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanCurrentCollection() {
|
||||
operations.delete(new Query());
|
||||
}
|
||||
|
||||
protected DefaultMockMvcBuilder createMvcWebAppContext() {
|
||||
return MockMvcBuilders.webAppContextSetup(context)
|
||||
.addFilter(new DosFilter(100, 10, "127\\.0\\.0\\.1|\\[0:0:0:0:0:0:0:1\\]", "(^192\\.168\\.)",
|
||||
|
||||
@@ -1,110 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.test.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.gridfs.GridFsOperations;
|
||||
|
||||
import de.flapdoodle.embed.mongo.Command;
|
||||
import de.flapdoodle.embed.mongo.MongodExecutable;
|
||||
import de.flapdoodle.embed.mongo.MongodStarter;
|
||||
import de.flapdoodle.embed.mongo.config.ArtifactStoreBuilder;
|
||||
import de.flapdoodle.embed.mongo.config.DownloadConfigBuilder;
|
||||
import de.flapdoodle.embed.mongo.config.IMongodConfig;
|
||||
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
|
||||
import de.flapdoodle.embed.mongo.config.Net;
|
||||
import de.flapdoodle.embed.mongo.config.RuntimeConfigBuilder;
|
||||
import de.flapdoodle.embed.mongo.distribution.Version;
|
||||
import de.flapdoodle.embed.process.config.store.HttpProxyFactory;
|
||||
import de.flapdoodle.embed.process.runtime.Network;
|
||||
|
||||
/**
|
||||
* Test class that contains MonfoDb start and stop for the test
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractIntegrationTestWithMongoDB extends AbstractIntegrationTest {
|
||||
|
||||
protected static volatile MongodExecutable mongodExecutable = null;
|
||||
private static final AtomicInteger mongoLease = new AtomicInteger(0);
|
||||
private static volatile Integer port;
|
||||
|
||||
@Autowired
|
||||
protected GridFsOperations operations;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupMongo() throws UnknownHostException, IOException {
|
||||
mongoLease.incrementAndGet();
|
||||
if (mongodExecutable == null) {
|
||||
final Command command = Command.MongoD;
|
||||
|
||||
final RuntimeConfigBuilder runtimeConfig = new RuntimeConfigBuilder().defaults(command);
|
||||
|
||||
if (port == null) {
|
||||
port = new FreePortFileWriter(28017, 28090, "./target/freeports").getPort();
|
||||
System.setProperty("spring.data.mongodb.port", String.valueOf(port));
|
||||
}
|
||||
|
||||
Version version = Version.V3_0_8;
|
||||
if (System.getProperty("inf.mongodb.version") != null) {
|
||||
version = Version
|
||||
.valueOf("V" + System.getProperty("inf.mongodb.version").trim().replaceAll("\\.", "_"));
|
||||
}
|
||||
|
||||
if (System.getProperty("http.proxyHost") != null) {
|
||||
runtimeConfig
|
||||
.artifactStore(
|
||||
new ArtifactStoreBuilder().defaults(command)
|
||||
.download(new DownloadConfigBuilder().defaultsForCommand(command)
|
||||
.proxyFactory(new HttpProxyFactory(
|
||||
System.getProperty("http.proxyHost").trim(), Integer
|
||||
.valueOf(System.getProperty("http.proxyPort"))))));
|
||||
}
|
||||
|
||||
final IMongodConfig mongodConfig = new MongodConfigBuilder().version(version)
|
||||
.net(new Net("127.0.0.1", port, Network.localhostIsIPv6())).build();
|
||||
|
||||
final MongodStarter starter = MongodStarter.getInstance(runtimeConfig.build());
|
||||
mongodExecutable = starter.prepare(mongodConfig);
|
||||
mongodExecutable.start();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanCurrentCollection() {
|
||||
operations.delete(new Query());
|
||||
}
|
||||
|
||||
public static void internalShutDownMongo() {
|
||||
if (mongodExecutable != null && mongoLease.decrementAndGet() <= 0) {
|
||||
mongodExecutable.stop();
|
||||
mongodExecutable = null;
|
||||
}
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void shutdownMongo() throws UnknownHostException, IOException {
|
||||
if (mongodExecutable != null && mongoLease.decrementAndGet() <= 0) {
|
||||
mongodExecutable.stop();
|
||||
mongodExecutable = null;
|
||||
}
|
||||
port = null;
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.test.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
|
||||
/**
|
||||
*
|
||||
* Look for a free port.
|
||||
*/
|
||||
public class FreePortFileWriter {
|
||||
|
||||
private final String filePortPath;
|
||||
private final int from;
|
||||
private final int to;
|
||||
|
||||
/**
|
||||
* @param from
|
||||
* port range from (start point)
|
||||
* @param to
|
||||
* port range to (end point)
|
||||
*/
|
||||
public FreePortFileWriter(final int from, final int to, final String filePortPath) {
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
this.filePortPath = filePortPath;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return findFree();
|
||||
}
|
||||
|
||||
protected int findFree() {
|
||||
for (int i = from; i <= to; i++) {
|
||||
if (isFree(i)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("No free port in range " + from + ":" + to);
|
||||
}
|
||||
|
||||
boolean isFree(final int port) {
|
||||
try {
|
||||
final File portFile = new File(filePortPath + File.separator + port + ".port");
|
||||
portFile.getParentFile().mkdirs();
|
||||
if (portFile.exists()) {
|
||||
return false;
|
||||
}
|
||||
boolean isFree = false;
|
||||
final ServerSocket sock = new ServerSocket();
|
||||
sock.setReuseAddress(true);
|
||||
sock.bind(new InetSocketAddress(port));
|
||||
if (portFile.createNewFile()) {
|
||||
portFile.deleteOnExit();
|
||||
isFree = true;
|
||||
}
|
||||
sock.close();
|
||||
return isFree;
|
||||
// We rely on an exception thrown to determine availability or
|
||||
// not availability and don't want to log the exception.
|
||||
} catch (@SuppressWarnings({ "squid:S2221", "squid:S1166" }) final Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user