Adding AppBrokerTestProperties

This commit is contained in:
Alberto Rios
2018-07-31 14:02:30 +02:00
committed by Oliver Hughes
parent a2b96a4dc6
commit b9e4267e35
7 changed files with 63 additions and 44 deletions

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2016-2018. the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.appbroker.acceptance;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AppBrokerTestProperties {
String[] value();
}

View File

@@ -16,18 +16,15 @@
package org.springframework.cloud.appbroker.acceptance;
import java.util.List;
import reactor.util.function.Tuple2;
class BrokerProperties {
private List<Tuple2<String, String>> properties;
private String[] properties;
BrokerProperties(List<Tuple2<String, String>> properties) {
BrokerProperties(String[] properties) {
this.properties = properties;
}
List<Tuple2<String, String>> getProperties() {
String[] getProperties() {
return properties;
}

View File

@@ -16,14 +16,12 @@
package org.springframework.cloud.appbroker.acceptance;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Optional;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.springframework.util.ReflectionUtils;
class BrokerPropertiesParameterResolver implements ParameterResolver {
@@ -34,11 +32,15 @@ class BrokerPropertiesParameterResolver implements ParameterResolver {
@Override
public BrokerProperties resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Optional<Object> testInstance = extensionContext.getTestInstance();
Field field = ReflectionUtils.findField(testInstance.get().getClass(), "properties");
ReflectionUtils.makeAccessible(field);
BrokerProperties brokerProperties = (BrokerProperties)ReflectionUtils.getField(field, testInstance.get());
return brokerProperties;
String[] properties = getValueHolderProperties(extensionContext);
return new BrokerProperties(properties);
}
private static String[] getValueHolderProperties(ExtensionContext extensionContext) {
Optional<Method> testInstance = extensionContext.getTestMethod();
return testInstance
.map(method -> method.getAnnotation(AppBrokerTestProperties.class).value())
.orElseGet(() -> new String[]{});
}
}

View File

@@ -31,7 +31,6 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.appbroker.acceptance.fixtures.cf.CloudFoundryClientConfiguration;
import org.springframework.cloud.appbroker.acceptance.fixtures.cf.CloudFoundryService;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import reactor.util.function.Tuple2;
@SpringBootTest(classes = {CloudFoundryClientConfiguration.class, CloudFoundryService.class})
@ExtendWith(SpringExtension.class)
@@ -60,7 +59,7 @@ class CloudFoundryAcceptanceTest {
cleanup();
}
void initializeBroker(List<Tuple2<String, String>> properties) {
void initializeBroker(String[] properties) {
cleanup();
cloudFoundryService.pushAppNoStart(SAMPLE_BROKER_APP_NAME, getSampleAppPath());

View File

@@ -23,23 +23,21 @@ import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.util.Lists.newArrayList;
import static reactor.util.function.Tuples.of;
class CreateInstanceAcceptanceTest extends CloudFoundryAcceptanceTest {
private static final String BROKER_SAMPLE_APP_CREATE = "broker-sample-app-create";
private static final BrokerProperties properties = new BrokerProperties(newArrayList(
of("spring.cloud.appbroker.apps[0].name", BROKER_SAMPLE_APP_CREATE),
of("spring.cloud.appbroker.apps[0].path", "classpath:demo.jar"),
of("spring.cloud.appbroker.apps[0].environment.ENV_VAR_1", "value1"),
of("spring.cloud.appbroker.apps[0].environment.ENV_VAR_2", "value2"),
of("spring.cloud.appbroker.apps[0].properties.spring.cloud.deployer.memory", "2G"),
of("spring.cloud.appbroker.apps[0].properties.spring.cloud.deployer.count", "2")
));
@Test
@AppBrokerTestProperties({
"spring.cloud.appbroker.apps[0].name=" + BROKER_SAMPLE_APP_CREATE,
"spring.cloud.appbroker.apps[0].path=classpath:demo.jar",
"spring.cloud.appbroker.apps[0].path=classpath:demo.jar",
"spring.cloud.appbroker.apps[0].environment.ENV_VAR_1=value1",
"spring.cloud.appbroker.apps[0].environment.ENV_VAR_2=value2",
"spring.cloud.appbroker.apps[0].properties.spring.cloud.deployer.memory=2G",
"spring.cloud.appbroker.apps[0].properties.spring.cloud.deployer.count=2"
})
void shouldPushAppWhenCreateServiceCalled() {
// when a service instance is created
createServiceInstance();

View File

@@ -18,28 +18,20 @@ package org.springframework.cloud.appbroker.acceptance;
import java.util.Optional;
import org.cloudfoundry.operations.applications.ApplicationSummary;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.util.Lists.newArrayList;
import static reactor.util.function.Tuples.of;
class DeleteInstanceAcceptanceTest extends CloudFoundryAcceptanceTest {
private static final String BROKER_SAMPLE_APP_DELETE = "broker-sample-app-delete";
@BeforeEach
void setUp() {
initializeBroker(newArrayList(
of("spring.cloud.appbroker.apps[0].name", BROKER_SAMPLE_APP_DELETE),
of("spring.cloud.appbroker.apps[0].path", "classpath:demo.jar")
)
);
}
@Test
@AppBrokerTestProperties({
"spring.cloud.appbroker.apps[0].name=" + BROKER_SAMPLE_APP_DELETE,
"spring.cloud.appbroker.apps[0].path=classpath:demo.jar"
})
void shouldDeleteAppsWhenDeleteServiceCalled() {
// given a service instance is created
createServiceInstance();

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.appbroker.acceptance.fixtures.cf;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.cloudfoundry.operations.CloudFoundryOperations;
@@ -39,7 +40,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
@Service
public class CloudFoundryService {
@@ -162,12 +162,13 @@ public class CloudFoundryService {
.block();
}
public void setBrokerAppEnvironment(List<Tuple2<String, String>> properties) {
public void setBrokerAppEnvironment(String[] properties) {
Flux<Void> catalogPublishers = getCatalogPublishers();
Flux<Void> appBrokerCFPublishers = getAppBrokerCFPublishers();
Flux<Void> appBrokerApplicationPublishers = Flux.concat(properties
.stream()
.map(tuple -> setEnvRequest(tuple.getT1(), tuple.getT2()))
Flux<Void> appBrokerApplicationPublishers = Flux.concat(Arrays.stream(properties)
.filter(property -> property.contains("="))
.map(property -> property.split("="))
.map(property -> setEnvRequest(property[0], property[1]))
.collect(Collectors.toList()));
Flux.concat(catalogPublishers, appBrokerCFPublishers, appBrokerApplicationPublishers).blockLast();