Removed JPA dependencies from runtime. Test only now.

Signed-off-by: Kai Zimmermann <kai.zimmermann@bosch-si.com>
This commit is contained in:
Kai Zimmermann
2016-05-31 20:50:22 +02:00
parent e054a171ea
commit 13f9791891
54 changed files with 699 additions and 243 deletions

View File

@@ -10,11 +10,11 @@ package org.eclipse.hawkbit.repository.util;
import org.eclipse.hawkbit.ExcludePathAwareShallowETagFilter;
import org.eclipse.hawkbit.repository.ArtifactManagement;
import org.eclipse.hawkbit.repository.RepositoryConstants;
import org.eclipse.hawkbit.repository.ControllerManagement;
import org.eclipse.hawkbit.repository.DeploymentManagement;
import org.eclipse.hawkbit.repository.DistributionSetManagement;
import org.eclipse.hawkbit.repository.EntityFactory;
import org.eclipse.hawkbit.repository.RepositoryConstants;
import org.eclipse.hawkbit.repository.RolloutGroupManagement;
import org.eclipse.hawkbit.repository.RolloutManagement;
import org.eclipse.hawkbit.repository.SoftwareManagement;
@@ -39,6 +39,7 @@ import org.junit.runner.RunWith;
import org.junit.runners.model.FrameworkMethod;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.data.auditing.AuditingHandler;
@@ -58,6 +59,7 @@ import org.springframework.web.context.WebApplicationContext;
@WebAppConfiguration
@ActiveProfiles({ "test" })
@WithUser(principal = "bumlux", allSpPermissions = true, authorities = "ROLE_CONTROLLER")
@SpringApplicationConfiguration(classes = { TestConfiguration.class })
// destroy the context after each test class because otherwise we get problem
// when context is
// refreshed we e.g. get two instances of CacheManager which leads to very

View File

@@ -0,0 +1,75 @@
/**
* 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.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.io.BaseEncoding;
/**
* Hash digest utility.
*/
public final class HashGeneratorUtils {
private static final Logger LOG = LoggerFactory.getLogger(HashGeneratorUtils.class);
private HashGeneratorUtils() {
}
/**
* Generates a MD5 cryptographic string.
*
* @param message
* the plain message
* @return the cryptographic string
*/
public static String generateMD5(final byte[] message) {
return hashString(message, "MD5");
}
/**
* Generates a SHA-1 cryptographic string.
*
* @param message
* the plain message
* @return the cryptographic string
*/
public static String generateSHA1(final byte[] message) {
return hashString(message, "SHA-1");
}
/**
* Generates a SHA-256 cryptographic string.
*
* @param message
* the plain message
* @return the cryptographic string
*/
public static String generateSHA256(final byte[] message) {
return hashString(message, "SHA-256");
}
private static String hashString(final byte[] message, final String algorithm) {
try {
final MessageDigest digest = MessageDigest.getInstance(algorithm);
final byte[] hashedBytes = digest.digest(message);
return BaseEncoding.base16().lowerCase().encode(hashedBytes);
} catch (final NoSuchAlgorithmException e) {
LOG.error("Algorithm could not be found", e);
}
return null;
}
}

View File

@@ -0,0 +1,50 @@
/**
* 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.util;
import java.util.List;
import org.eclipse.hawkbit.cache.TenantAwareCacheManager;
import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.security.SystemSecurityContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
public class JpaTestRepositoryManagement implements TestRepositoryManagement {
@Autowired
private TenantAwareCacheManager cacheManager;
@Autowired
private SystemSecurityContext systemSecurityContext;
@Autowired
private SystemManagement systemManagement;
@Override
public void clearTestRepository() {
deleteAllRepos();
cacheManager.getDirectCacheNames().forEach(name -> cacheManager.getDirectCache(name).clear());
}
@Transactional
public void deleteAllRepos() {
final List<String> tenants = systemSecurityContext.runAsSystem(() -> systemManagement.findTenants());
tenants.forEach(tenant -> {
try {
systemSecurityContext.runAsSystem(() -> {
systemManagement.deleteTenant(tenant);
return null;
});
} catch (final Exception e) {
e.printStackTrace();
}
});
}
}

View File

@@ -0,0 +1,120 @@
/**
* 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.util;
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.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.context.properties.EnableConfigurationProperties;
import org.springframework.cache.Cache;
import org.springframework.cache.guava.GuavaCacheManager;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.domain.AuditorAware;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.security.concurrent.DelegatingSecurityContextExecutor;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.mongodb.MongoClientOptions;
/**
* Spring context configuration required for Dev.Environment.
*
*
*
*/
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, mode = AdviceMode.ASPECTJ, proxyTargetClass = true, securedEnabled = true)
@EnableConfigurationProperties({ HawkbitServerProperties.class, DdiSecurityProperties.class })
@Profile("test")
public class TestConfiguration implements AsyncConfigurer {
@Bean
public TestRepositoryManagement testRepositoryManagement() {
return new JpaTestRepositoryManagement();
}
@Bean
public TestdataFactory testdataFactory() {
return new TestdataFactory();
}
@Bean
public MongoClientOptions options() {
return MongoClientOptions.builder().connectTimeout(500).maxWaitTime(500).connectionsPerHost(2)
.serverSelectionTimeout(500).build();
}
@Bean
public TenantAware tenantAware() {
return new SecurityContextTenantAware();
}
@Bean
public TenancyCacheManager cacheManager() {
return new TenantAwareCacheManager(new GuavaCacheManager(), tenantAware());
}
/**
* Bean for the download id cache.
*
* @return the cache
*/
@Bean(name = CacheConstants.DOWNLOAD_ID_CACHE)
public Cache downloadIdCache() {
return cacheManager().getDirectCache(CacheConstants.DOWNLOAD_ID_CACHE);
}
@Bean
public EventBus eventBus() {
return new AsyncEventBus(asyncExecutor());
}
@Bean
public EventBusHolder eventBusHolder() {
return EventBusHolder.getInstance();
}
@Bean
public Executor asyncExecutor() {
return new DelegatingSecurityContextExecutor(Executors.newSingleThreadExecutor());
}
@Bean
public AuditorAware<String> auditorAware() {
return new SpringSecurityAuditorAware();
}
@Override
public Executor getAsyncExecutor() {
return asyncExecutor();
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new SimpleAsyncUncaughtExceptionHandler();
}
}