Experimenting with ParameterResolver

This commit is contained in:
Alberto Rios
2018-07-31 13:28:30 +02:00
committed by Oliver Hughes
parent 24f6feb4e1
commit a2b96a4dc6
4 changed files with 87 additions and 7 deletions

View File

@@ -0,0 +1,34 @@
/*
* 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.util.List;
import reactor.util.function.Tuple2;
class BrokerProperties {
private List<Tuple2<String, String>> properties;
BrokerProperties(List<Tuple2<String, String>> properties) {
this.properties = properties;
}
List<Tuple2<String, String>> getProperties() {
return properties;
}
}

View File

@@ -0,0 +1,44 @@
/*
* 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.reflect.Field;
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 {
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return parameterContext.getParameter().getType() == BrokerProperties.class;
}
@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;
}
}

View File

@@ -23,6 +23,7 @@ import java.util.Optional;
import org.cloudfoundry.operations.applications.ApplicationEnvironments;
import org.cloudfoundry.operations.applications.ApplicationSummary;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@@ -34,8 +35,14 @@ import reactor.util.function.Tuple2;
@SpringBootTest(classes = {CloudFoundryClientConfiguration.class, CloudFoundryService.class})
@ExtendWith(SpringExtension.class)
@ExtendWith(BrokerPropertiesParameterResolver.class)
class CloudFoundryAcceptanceTest {
@BeforeEach
void setUp(BrokerProperties brokerProperties) {
initializeBroker(brokerProperties.getProperties());
}
private static final String SAMPLE_BROKER_APP_NAME = "sample-broker";
private static final String BROKER_NAME = "sample-broker-name";
private static final String SERVICE_NAME = "example";

View File

@@ -19,7 +19,6 @@ package org.springframework.cloud.appbroker.acceptance;
import java.util.Optional;
import org.cloudfoundry.operations.applications.ApplicationEnvironments;
import org.cloudfoundry.operations.applications.ApplicationSummary;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -31,18 +30,14 @@ class CreateInstanceAcceptanceTest extends CloudFoundryAcceptanceTest {
private static final String BROKER_SAMPLE_APP_CREATE = "broker-sample-app-create";
@BeforeEach
void setUp() {
initializeBroker(newArrayList(
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
void shouldPushAppWhenCreateServiceCalled() {