Skip to content

Commit 91814e2

Browse files
committed
Merge branch 'conditional-tests' into 2.0.x
2 parents 9860373 + 7e12190 commit 91814e2

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed

integration-test/src/test/java/org/cloudfoundry/AbstractIntegrationTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public abstract class AbstractIntegrationTest {
4848
@Rule
4949
public final TestName testName = new TestName();
5050

51+
@Autowired
52+
@Rule
53+
public CloudFoundryVersionConditionalRule cloudFoundryVersion;
54+
5155
@Autowired
5256
protected NameFactory nameFactory;
5357

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2013-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.cloudfoundry;
18+
19+
import com.github.zafarkhaja.semver.Version;
20+
import org.junit.Assume;
21+
import org.junit.rules.MethodRule;
22+
import org.junit.runners.model.FrameworkMethod;
23+
import org.junit.runners.model.Statement;
24+
import org.springframework.core.annotation.AnnotationUtils;
25+
26+
import java.util.Optional;
27+
28+
import static org.cloudfoundry.IfCloudFoundryVersion.CloudFoundryVersion.UNSPECIFIED;
29+
30+
final class CloudFoundryVersionConditionalRule implements MethodRule {
31+
32+
private final Version server;
33+
34+
CloudFoundryVersionConditionalRule(Version server) {
35+
this.server = server;
36+
}
37+
38+
@Override
39+
public Statement apply(Statement base, FrameworkMethod method, Object target) {
40+
return new Statement() {
41+
42+
@Override
43+
public void evaluate() throws Throwable {
44+
boolean enabled = Optional.ofNullable(AnnotationUtils.findAnnotation(method.getMethod(), IfCloudFoundryVersion.class))
45+
.map(c -> isTestEnabled(c, CloudFoundryVersionConditionalRule.this.server))
46+
.orElse(true);
47+
48+
Assume.assumeTrue(String.format("Cloud Foundry version required by @IfCloudFoundryVersion is not valid for test method [%s].", method.getMethod()), enabled);
49+
50+
base.evaluate();
51+
}
52+
};
53+
}
54+
55+
private static boolean isTestEnabled(IfCloudFoundryVersion condition, Version server) {
56+
boolean enabled = true;
57+
58+
if (condition.lessThan() != UNSPECIFIED) {
59+
enabled = enabled && server.lessThan(condition.lessThan().getVersion());
60+
}
61+
62+
if (condition.lessThanOrEqualTo() != UNSPECIFIED) {
63+
enabled = enabled && server.lessThanOrEqualTo(condition.lessThanOrEqualTo().getVersion());
64+
}
65+
66+
if (condition.equalTo() != UNSPECIFIED) {
67+
enabled = enabled && server.equals(condition.equalTo().getVersion());
68+
}
69+
70+
if (condition.greaterThanOrEqualTo() != UNSPECIFIED) {
71+
enabled = enabled && server.greaterThanOrEqualTo(condition.greaterThanOrEqualTo().getVersion());
72+
}
73+
74+
if (condition.greaterThan() != UNSPECIFIED) {
75+
enabled = enabled && server.greaterThan(condition.greaterThan().getVersion());
76+
}
77+
78+
return enabled;
79+
}
80+
81+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2013-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.cloudfoundry;
18+
19+
import com.github.zafarkhaja.semver.Version;
20+
21+
import java.lang.annotation.Documented;
22+
import java.lang.annotation.ElementType;
23+
import java.lang.annotation.Inherited;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.RetentionPolicy;
26+
import java.lang.annotation.Target;
27+
28+
import static org.cloudfoundry.IfCloudFoundryVersion.CloudFoundryVersion.UNSPECIFIED;
29+
30+
@Target(ElementType.METHOD)
31+
@Retention(RetentionPolicy.RUNTIME)
32+
@Documented
33+
@Inherited
34+
public @interface IfCloudFoundryVersion {
35+
36+
CloudFoundryVersion equalTo() default UNSPECIFIED;
37+
38+
CloudFoundryVersion greaterThan() default UNSPECIFIED;
39+
40+
CloudFoundryVersion greaterThanOrEqualTo() default UNSPECIFIED;
41+
42+
CloudFoundryVersion lessThan() default UNSPECIFIED;
43+
44+
CloudFoundryVersion lessThanOrEqualTo() default UNSPECIFIED;
45+
46+
enum CloudFoundryVersion {
47+
48+
PCF_1_7(Version.forIntegers(2, 54, 0)),
49+
50+
PCF_1_8(Version.forIntegers(2, 58, 0)),
51+
52+
PCF_1_9(Version.forIntegers(2, 65, 0)),
53+
54+
UNSPECIFIED(Version.forIntegers(0));
55+
56+
private final Version version;
57+
58+
CloudFoundryVersion(Version version) {
59+
this.version = version;
60+
}
61+
62+
Version getVersion() {
63+
return this.version;
64+
}
65+
66+
}
67+
68+
}

integration-test/src/test/java/org/cloudfoundry/IntegrationTestConfiguration.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import com.fasterxml.jackson.databind.DeserializationContext;
2121
import com.fasterxml.jackson.databind.JsonDeserializer;
2222
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
23+
import com.github.zafarkhaja.semver.Version;
2324
import org.cloudfoundry.client.CloudFoundryClient;
25+
import org.cloudfoundry.client.v2.info.GetInfoRequest;
2426
import org.cloudfoundry.client.v2.organizations.CreateOrganizationRequest;
2527
import org.cloudfoundry.client.v2.spaces.CreateSpaceRequest;
2628
import org.cloudfoundry.client.v2.stacks.ListStacksRequest;
@@ -190,6 +192,18 @@ DefaultCloudFoundryOperations cloudFoundryOperations(CloudFoundryClient cloudFou
190192
.build();
191193
}
192194

195+
@Bean
196+
CloudFoundryVersionConditionalRule cloudFoundryVersionConditionalRule(CloudFoundryClient cloudFoundryClient) {
197+
return cloudFoundryClient.info()
198+
.get(GetInfoRequest.builder()
199+
.build())
200+
.map(response -> Version.valueOf(response.getApiVersion()))
201+
.map(CloudFoundryVersionConditionalRule::new)
202+
.doOnSubscribe(s -> this.logger.debug(">> CLOUD FOUNDRY VERSION <<"))
203+
.doOnSuccess(r -> this.logger.debug("<< CLOUD FOUNDRY VERSION >>"))
204+
.block();
205+
}
206+
193207
@Bean
194208
DefaultConnectionContext connectionContext(@Value("${test.apiHost}") String apiHost,
195209
@Value("${test.proxy.host:}") String proxyHost,

0 commit comments

Comments
 (0)