org.springframework.boot.autoconfigure.condition.ConditionOutcome Java Examples

The following examples show how to use org.springframework.boot.autoconfigure.condition.ConditionOutcome. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OnSpringBootVersion.java    From sofa-ark with Apache License 2.0 8 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    Map<String, Object> springBootVersion = metadata
        .getAnnotationAttributes(ConditionalOnSpringBootVersion.class.getCanonicalName());
    if (springBootVersion == null || springBootVersion.get("version") == null) {
        return new ConditionOutcome(false, "No specified spring boot version.");
    }
    ConditionalOnSpringBootVersion.Version version = (ConditionalOnSpringBootVersion.Version) springBootVersion
        .get("version");
    if (ConditionalOnSpringBootVersion.Version.ANY.equals(version)) {
        return new ConditionOutcome(true, "Conditional on Any Spring Boot.");
    } else if (ConditionalOnSpringBootVersion.Version.OneX.equals(version)) {
        if (SpringBootVersion.getVersion().startsWith("1")) {
            return new ConditionOutcome(true, "Conditional on OneX Spring Boot.");
        } else {
            return new ConditionOutcome(false, "Conditional on OneX Spring Boot.");
        }
    } else if (ConditionalOnSpringBootVersion.Version.TwoX.equals(version)) {
        if (SpringBootVersion.getVersion().startsWith("2")) {
            return new ConditionOutcome(true, "Conditional on TwoX Spring Boot.");
        } else {
            return new ConditionOutcome(false, "Conditional on TwoX Spring Boot.");
        }
    }
    throw new IllegalStateException("Error Spring Boot Version.");
}
 
Example #2
Source File: ResourceServerTokenServicesConfiguration.java    From spring-security-oauth2-boot with Apache License 2.0 6 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
	ConditionMessage.Builder message = ConditionMessage.forCondition("OAuth TokenInfo Condition");
	Environment environment = context.getEnvironment();
	Boolean preferTokenInfo = environment.getProperty("security.oauth2.resource.prefer-token-info",
			Boolean.class);
	if (preferTokenInfo == null) {
		preferTokenInfo = environment.resolvePlaceholders("${OAUTH2_RESOURCE_PREFERTOKENINFO:true}")
				.equals("true");
	}
	String tokenInfoUri = environment.getProperty("security.oauth2.resource.token-info-uri");
	String userInfoUri = environment.getProperty("security.oauth2.resource.user-info-uri");
	if (!StringUtils.hasLength(userInfoUri) && !StringUtils.hasLength(tokenInfoUri)) {
		return ConditionOutcome.match(message.didNotFind("user-info-uri property").atAll());
	}
	if (StringUtils.hasLength(tokenInfoUri) && preferTokenInfo) {
		return ConditionOutcome.match(message.foundExactly("preferred token-info-uri property"));
	}
	return ConditionOutcome.noMatch(message.didNotFind("token info").atAll());
}
 
Example #3
Source File: PrefixPropertyCondition.java    From loc-framework with MIT License 6 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
    AnnotatedTypeMetadata metadata) {
  String prefix = (String) attribute(metadata, "prefix");
  Class<?> value = (Class<?>) attribute(metadata, "value");
  ConfigurableEnvironment environment = (ConfigurableEnvironment) context.getEnvironment();
  try {
    new Binder(ConfigurationPropertySources.from(environment.getPropertySources()))
        .bind(prefix, Bindable.of(value))
        .orElseThrow(
            () -> new FatalBeanException("Could not bind DataSourceSettings properties"));
    return new ConditionOutcome(true, String.format("Map property [%s] is not empty", prefix));
  } catch (Exception e) {
    //ignore
  }
  return new ConditionOutcome(false, String.format("Map property [%s] is empty", prefix));
}
 
Example #4
Source File: OnGcpEnvironmentCondition.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
/**
 * Determines whether the current runtime environment matches the one passed through the annotation.
 * @param context the spring context at the point in time the condition is being evaluated
 * @param metadata annotation metadata containing all acceptable GCP environments
 * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException if no GcpEnvironmentProvider is found in
 * spring context
 */
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {

	Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnGcpEnvironment.class.getName());
	GcpEnvironment[] targetEnvironments = (GcpEnvironment[]) attributes.get("value");
	Assert.notNull(targetEnvironments, "Value attribute of ConditionalOnGcpEnvironment cannot be null.");

	GcpEnvironmentProvider environmentProvider = context.getBeanFactory().getBean(GcpEnvironmentProvider.class);
	GcpEnvironment currentEnvironment = environmentProvider.getCurrentEnvironment();

	if (Arrays.stream(targetEnvironments).noneMatch((env) -> env == currentEnvironment)) {
		return new ConditionOutcome(false, "Application is not running on any of "
				+ Arrays.stream(targetEnvironments)
				.map(GcpEnvironment::toString)
				.collect(Collectors.joining(", ")));
	}

	return new ConditionOutcome(true, "Application is running on " + currentEnvironment);
}
 
Example #5
Source File: OSSCondition.java    From jeecg-boot-with-activiti with MIT License 6 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
	String sourceClass = "";
	if (metadata instanceof ClassMetadata) {
		sourceClass = ((ClassMetadata) metadata).getClassName();
	}
	ConditionMessage.Builder message = ConditionMessage.forCondition("OSS", sourceClass);
	Environment environment = context.getEnvironment();
	try {
		BindResult<OSSType> specified = Binder.get(environment).bind("oss.type", OSSType.class);
		if (!specified.isBound()) {
			return ConditionOutcome.match(message.because("automatic OSS type"));
		}
		OSSType required = OSSConfigurations.getType(((AnnotationMetadata) metadata).getClassName());
		if (specified.get() == required) {
			return ConditionOutcome.match(message.because(specified.get() + " OSS type"));
		}
	}
	catch (BindException ex) {
	}
	return ConditionOutcome.noMatch(message.because("unknown OSS type"));
}
 
Example #6
Source File: OnLocalPlatform.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {

	Iterable<CloudProfileProvider> cloudProfileProviders = ServiceLoader.load(CloudProfileProvider.class);
	boolean onLocalPlatform = true;
	for (CloudProfileProvider cloudProfileProvider : cloudProfileProviders) {
		if (cloudProfileProvider.isCloudPlatform(context.getEnvironment())) {
			onLocalPlatform = false;
		}
	}
	if (onLocalPlatform) {
		return new ConditionOutcome(onLocalPlatform, "On local platform.");
	} else {
		return new ConditionOutcome(onLocalPlatform, "On cloud platform.");
	}
}
 
Example #7
Source File: FlowableTaskEventRegistryCondition.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected Map<String, ConditionOutcome> getConditionOutcomes() {
    boolean jmsEnabled = environment.getProperty("flowable.task.app.jms-enabled", Boolean.class, false);
    boolean kafkaEnabled = environment.getProperty("flowable.task.app.kafka-enabled", Boolean.class, false);
    boolean rabbitEnabled = environment.getProperty("flowable.task.app.rabbit-enabled", Boolean.class, false);
    Map<String, ConditionOutcome> conditions = new HashMap<>();

    if (!jmsEnabled) {
        conditions.put("org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration",
            ConditionOutcome.noMatch("Property flowable.task.app.jms-enabled was not set to true")
        );
    }

    if (!kafkaEnabled) {
        conditions.put("org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration",
            ConditionOutcome.noMatch("Property flowable.task.app.kafka-enabled was not set to true")
        );
    }

    if (!rabbitEnabled) {
        conditions.put("org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration",
            ConditionOutcome.noMatch("Property flowable.task.app.rabbit-enabled was not set to true")
        );
    }
    return conditions;
}
 
Example #8
Source File: VelocityLayoutCondition.java    From velocity-spring-boot-project with Apache License 2.0 6 votes vote down vote up
protected ConditionOutcome getMatchOutcome(Environment environment) {

        boolean velocityEnabled = environment.getProperty(VELOCITY_ENABLED_PROPERTY_NAME, boolean.class, true);

        if (!velocityEnabled) {
            return ConditionOutcome.noMatch("The velocity layout is disabled , caused by " +
                    VELOCITY_ENABLED_PROPERTY_NAME + " = false");
        }

        boolean velocityLayoutEnabled = environment.getProperty(VELOCITY_LAYOUT_ENABLED_PROPERTY_NAME, boolean.class,
                DEFAULT_VELOCITY_LAYOUT_ENABLED_VALUE);

        if (!velocityLayoutEnabled) {
            return ConditionOutcome.noMatch("The velocity layout is disabled , caused by " +
                    VELOCITY_LAYOUT_ENABLED_PROPERTY_NAME + " = false");
        }

        return ConditionOutcome.match("The velocity layout is enabled !");

    }
 
Example #9
Source File: RedissonAutoConfiguration.java    From redisson-spring-boot with Apache License 2.0 6 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    String type = context.getEnvironment().getProperty("redisson.type");
    if (type == null || type.isEmpty() || type.trim().isEmpty()) {
        type = RedissonType.SINGLE.name();
    }
    ConditionMessage.Builder condition = ConditionMessage.forCondition("RedissonCondition",
            String.format("(redisson.type=%s)", type));
    if (type.equalsIgnoreCase(RedissonType.NONE.name())) {
        return noMatch(condition.found("matched value").items(Style.QUOTE, type));
    }
    Set<String> relaxedTypes = Arrays.stream(RedissonType.values())
            .filter(t -> t != RedissonType.NONE)
            .map(Enum::name)
            .map(name -> Arrays.asList(name, name.toLowerCase(), name.toUpperCase()))
            .flatMap(List::stream)
            .collect(toSet());
    if (relaxedTypes.contains(type)) {
        return match(condition.found("matched value").items(Style.QUOTE, type));
    } else {
        return noMatch(condition.because("has unrecognized value '" + type + "'"));
    }
}
 
Example #10
Source File: EnableWebSecurityCondition.java    From cola-cloud with MIT License 6 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    String[] enablers = context.getBeanFactory()
            .getBeanNamesForAnnotation(EnableWebSecurity.class);
    ConditionMessage.Builder message = ConditionMessage
            .forCondition("@EnableWebSecurity Condition");
    if (enablers != null && enablers.length > 0) {
        return ConditionOutcome.match(message
                .found("@EnableWebSecurity annotation on Application")
                .items(enablers));
    }

    return ConditionOutcome.noMatch(message.didNotFind(
            "@EnableWebSecurity annotation " + "on Application")
            .atAll());
}
 
Example #11
Source File: SpringBootAdminClientEnabledCondition.java    From spring-boot-admin with Apache License 2.0 6 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata annotatedTypeMetadata) {
	ClientProperties clientProperties = getClientProperties(context);

	if (!clientProperties.isEnabled()) {
		return ConditionOutcome
				.noMatch("Spring Boot Client is disabled, because 'spring.boot.admin.client.enabled' is false.");
	}

	if (clientProperties.getUrl().length == 0) {
		return ConditionOutcome
				.noMatch("Spring Boot Client is disabled, because 'spring.boot.admin.client.url' is empty.");
	}

	return ConditionOutcome.match();
}
 
Example #12
Source File: EncryptSpringBootConditionTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertNotMatch() {
    MockEnvironment mockEnvironment = new MockEnvironment();
    mockEnvironment.setProperty("spring.shardingsphere.rules.sharding.binding-tables", "t_order");
    ConditionContext context = Mockito.mock(ConditionContext.class);
    AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
    when(context.getEnvironment()).thenReturn(mockEnvironment);
    EncryptSpringBootCondition condition = new EncryptSpringBootCondition();
    ConditionOutcome matchOutcome = condition.getMatchOutcome(context, metadata);
    assertThat(matchOutcome.isMatch(), is(false));
}
 
Example #13
Source File: ZipkinSQSCredentialsConfiguration.java    From zipkin-aws with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata a) {

  String queueUrl = context.getEnvironment().getProperty(PROPERTY_NAME);

  return isEmpty(queueUrl)
      ? ConditionOutcome.noMatch(PROPERTY_NAME + " isn't set")
      : ConditionOutcome.match();
}
 
Example #14
Source File: ZipkinKinesisCredentialsConfiguration.java    From zipkin-aws with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata a) {

  String stsRoleArn = context.getEnvironment().getProperty(PROPERTY_NAME);

  return isEmpty(stsRoleArn)
      ? ConditionOutcome.noMatch(PROPERTY_NAME + " isn't set")
      : ConditionOutcome.match();
}
 
Example #15
Source File: BasicClientSecurityConfigured.java    From microservices-dashboard with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
		AnnotatedTypeMetadata metadata) {
	ConditionMessage.Builder message = ConditionMessage
			.forCondition("Basic Client Security Configured Condition");
	BasicClientSecurityProperties basicClientSecurityProperties =
			getBasicClientSecurityProperties(context.getEnvironment());
	if (basicClientSecurityProperties.isConfigured()) {
		return ConditionOutcome.match(message.because("Both the username and password have been configured"));
	}
	return ConditionOutcome.noMatch(message.because("Both the username and password should be configured"));
}
 
Example #16
Source File: MySQLAutoconfiguration.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    ConditionMessage.Builder message = ConditionMessage.forCondition("Hibernate");

    return Arrays.stream(CLASS_NAMES).filter(className -> ClassUtils.isPresent(className, context.getClassLoader())).map(className -> ConditionOutcome.match(message.found("class").items(Style.NORMAL, className))).findAny()
            .orElseGet(() -> ConditionOutcome.noMatch(message.didNotFind("class", "classes").items(Style.NORMAL, Arrays.asList(CLASS_NAMES))));
}
 
Example #17
Source File: EncryptSpringBootConditionTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertMatch() {
    MockEnvironment mockEnvironment = new MockEnvironment();
    mockEnvironment.setProperty("spring.shardingsphere.rules.encrypt.encryptors.aes_encryptor.type", "AES");
    ConditionContext context = Mockito.mock(ConditionContext.class);
    AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
    when(context.getEnvironment()).thenReturn(mockEnvironment);
    EncryptSpringBootCondition condition = new EncryptSpringBootCondition();
    ConditionOutcome matchOutcome = condition.getMatchOutcome(context, metadata);
    assertThat(matchOutcome.isMatch(), is(true));
}
 
Example #18
Source File: JetCacheCondition.java    From jetcache with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
    ConfigTree ct = new ConfigTree((ConfigurableEnvironment) conditionContext.getEnvironment(), "jetcache.");
    if (match(ct, "local.") || match(ct, "remote.")) {
        return ConditionOutcome.match();
    } else {
        return ConditionOutcome.noMatch("no match for " + cacheTypes[0]);
    }
}
 
Example #19
Source File: NotEnableOauth2SsoCondition.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
	String ssoClass = "org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso";
	boolean ssoClientAnnotationExists = ClassUtils.isPresent(ssoClass, null);
	if(!ssoClientAnnotationExists){
		return ConditionOutcome.match("EnableOAuth2Sso not exists!");
	}
	String[] beanNames = context.getBeanFactory().getBeanNamesForAnnotation(EnableOAuth2Sso.class);
	if(beanNames==null || beanNames.length==0){
		return ConditionOutcome.match("not @EnableOAuth2Sso bean found!");
	}
	return ConditionOutcome.noMatch("@EnableOAuth2Sso sso client!");
}
 
Example #20
Source File: CamelSSLAutoConfiguration.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata annotatedTypeMetadata) {
    Binder binder = Binder.get(context.getEnvironment());
    Map<String, Object> sslProperties = binder.bind("camel.ssl.config", Bindable.mapOf(String.class, Object.class)).orElse(Collections.emptyMap());
    ConditionMessage.Builder message = ConditionMessage.forCondition("camel.ssl.config");
    if (sslProperties.size() > 0) {
        return ConditionOutcome.match(message.because("enabled"));
    }

    return ConditionOutcome.noMatch(message.because("not enabled"));
}
 
Example #21
Source File: ShardingSpringBootConditionTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertMatch() {
    MockEnvironment mockEnvironment = new MockEnvironment();
    mockEnvironment.setProperty("spring.shardingsphere.rules.sharding.binding-tables", "t_order");
    ConditionContext context = Mockito.mock(ConditionContext.class);
    AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
    when(context.getEnvironment()).thenReturn(mockEnvironment);
    ShardingSpringBootCondition condition = new ShardingSpringBootCondition();
    ConditionOutcome matchOutcome = condition.getMatchOutcome(context, metadata);
    assertThat(matchOutcome.isMatch(), is(true));
}
 
Example #22
Source File: ZipkinSQSCollectorModule.java    From zipkin-aws with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata a) {

  String queueUrl = context.getEnvironment().getProperty(PROPERTY_NAME);

  return isEmpty(queueUrl)
      ? ConditionOutcome.noMatch(PROPERTY_NAME + " isn't set")
      : ConditionOutcome.match();
}
 
Example #23
Source File: FunctionDetectorCondition.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
	if (context != null &&  context.getBeanFactory() != null) {

		String[] functionTypes = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context.getBeanFactory(), Function.class, true, false);
		String[] consumerTypes = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context.getBeanFactory(), Consumer.class, true, false);
		String[] biFunctionTypes = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context.getBeanFactory(), BiFunction.class, true, false);
		String[] biConsumerTypes = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context.getBeanFactory(), BiConsumer.class, true, false);

		List<String> functionComponents = new ArrayList<>();

		functionComponents.addAll(Arrays.asList(functionTypes));
		functionComponents.addAll(Arrays.asList(consumerTypes));
		functionComponents.addAll(Arrays.asList(biFunctionTypes));
		functionComponents.addAll(Arrays.asList(biConsumerTypes));

		List<String> kafkaStreamsFunctions = pruneFunctionBeansForKafkaStreams(functionComponents, context);
		if (!CollectionUtils.isEmpty(kafkaStreamsFunctions)) {
			return ConditionOutcome.match("Matched. Function/BiFunction/Consumer beans found");
		}
		else {
			return ConditionOutcome.noMatch("No match. No Function/BiFunction/Consumer beans found");
		}
	}
	return ConditionOutcome.noMatch("No match. No Function/BiFunction/Consumer beans found");
}
 
Example #24
Source File: EnabledKeyCondition.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
	AnnotationAttributes attrubutes = getAnnotationAttributes(metadata);
	
	String mainKey = getMainEnabledKey(context.getEnvironment(), attrubutes);
	if(StringUtils.isNotBlank(mainKey) && !isEnabled(context.getEnvironment(), mainKey)){
		return ConditionOutcome.noMatch("main property ["+mainKey+"] is not enabled");
	}
	
	String key = getEnabledKey(context.getEnvironment(), attrubutes);
	if(!isEnabled(context.getEnvironment(), key)){
		return ConditionOutcome.noMatch("property ["+key+"] is not enabled");
	}
	return ConditionOutcome.match("property ["+key+"] is enabled");
}
 
Example #25
Source File: EclipselinkJpaAutoconfiguration.java    From booties with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(final ConditionContext context, final AnnotatedTypeMetadata metadata) {
    for (String className : CLASS_NAMES) {
        if (ClassUtils.isPresent(className, context.getClassLoader())) {
            return ConditionOutcome.match("found EclipselinkEntityManager class");
        }
    }

    return ConditionOutcome.noMatch("did not find EclipselinkEntityManager class");
}
 
Example #26
Source File: MasterSlaveSpringBootConditionTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertNotMatch() {
    MockEnvironment mockEnvironment = new MockEnvironment();
    mockEnvironment.setProperty("spring.shardingsphere.rules.encrypt.encryptors.aes_encryptor.type", "AES");
    mockEnvironment.setProperty("spring.shardingsphere.rules.shadow.column", "user_id");
    ConditionContext context = Mockito.mock(ConditionContext.class);
    AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class);
    when(context.getEnvironment()).thenReturn(mockEnvironment);
    MasterSlaveSpringBootCondition condition = new MasterSlaveSpringBootCondition();
    ConditionOutcome matchOutcome = condition.getMatchOutcome(context, metadata);
    assertThat(matchOutcome.isMatch(), is(false));
}
 
Example #27
Source File: OnGcpEnvironmentConditionTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testPositiveOutcomeForMultipleEnvironments() {
	setUpAnnotationValue(new GcpEnvironment[] { GcpEnvironment.COMPUTE_ENGINE, GcpEnvironment.KUBERNETES_ENGINE });
	when(this.mockGcpEnvironmentProvider.getCurrentEnvironment()).thenReturn(GcpEnvironment.KUBERNETES_ENGINE);

	OnGcpEnvironmentCondition onGcpEnvironmentCondition = new OnGcpEnvironmentCondition();
	ConditionOutcome outcome = onGcpEnvironmentCondition.getMatchOutcome(this.mockContext, this.mockMetadata);

	assertThat(outcome.isMatch()).isTrue();
	assertThat(outcome.getMessage()).isEqualTo("Application is running on KUBERNETES_ENGINE");
}
 
Example #28
Source File: OnGcpEnvironmentConditionTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testNegativeOutcomeForMultipleEnvironments() {
	setUpAnnotationValue(new GcpEnvironment[] { GcpEnvironment.COMPUTE_ENGINE, GcpEnvironment.KUBERNETES_ENGINE });
	when(this.mockGcpEnvironmentProvider.getCurrentEnvironment()).thenReturn(GcpEnvironment.UNKNOWN);

	OnGcpEnvironmentCondition onGcpEnvironmentCondition = new OnGcpEnvironmentCondition();
	ConditionOutcome outcome = onGcpEnvironmentCondition.getMatchOutcome(this.mockContext, this.mockMetadata);

	assertThat(outcome.isMatch()).isFalse();
	assertThat(outcome.getMessage())
			.isEqualTo("Application is not running on any of COMPUTE_ENGINE, KUBERNETES_ENGINE");
}
 
Example #29
Source File: OnGcpEnvironmentConditionTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testNegativeOutcome() {
	setUpAnnotationValue(new GcpEnvironment[] { GcpEnvironment.COMPUTE_ENGINE });
	when(this.mockGcpEnvironmentProvider.getCurrentEnvironment()).thenReturn(GcpEnvironment.UNKNOWN);

	OnGcpEnvironmentCondition onGcpEnvironmentCondition = new OnGcpEnvironmentCondition();
	ConditionOutcome outcome = onGcpEnvironmentCondition.getMatchOutcome(this.mockContext, this.mockMetadata);

	assertThat(outcome.isMatch()).isFalse();
	assertThat(outcome.getMessage()).isEqualTo("Application is not running on any of COMPUTE_ENGINE");
}
 
Example #30
Source File: ZipkinKafkaStreamFactoryAutoConfiguration.java    From zipkin-sparkstreaming with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata a) {
  String bootstrap = context.getEnvironment().getProperty(BOOTSTRAP);
  String connect = context.getEnvironment().getProperty(CONNECT);
  return (bootstrap == null || bootstrap.isEmpty()) && (connect == null || connect.isEmpty()) ?
      ConditionOutcome.noMatch("neither " + BOOTSTRAP + " nor " + CONNECT + " are set") :
      ConditionOutcome.match();
}