EVCacheServerEurekaInstanceConfigBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) { if (!isAssignableValue(EurekaInstanceConfigBean.class, bean)) { return bean; } final EurekaInstanceConfigBean config = (EurekaInstanceConfigBean) bean; if (isAmazonDataCenter(config)) { return bean; } final AmazonInfo amazonInfo = createAmazonInfo(config); config.setDataCenterInfo(amazonInfo); LOGGER.warn("DataCenter changed. {}", amazonInfo); return bean; } EVCacheServerEurekaInstanceConfigBeanPostProcessor(final ConfigurableEnvironment environment,
                                                       final EurekaClientConfigBean eurekaClientConfigBean); @Override Object postProcessBeforeInitialization(final Object bean, final String beanName); @Override Object postProcessAfterInitialization(final Object bean, final String beanName);  }
@Test void should_be_set_metadata_using_default_when_not_exists_property() { eurekaInstanceConfigBean.setInstanceId("test1"); assertThat(processor.postProcessAfterInitialization(eurekaInstanceConfigBean, "beanName")) .isEqualTo(eurekaInstanceConfigBean); final AmazonInfo dataCenterInfo = (AmazonInfo) eurekaInstanceConfigBean.getDataCenterInfo(); assertThat(dataCenterInfo.getMetadata()).containsEntry("availability-zone", "defaultZone") .containsEntry("ami-id", "n/a") .containsEntry("instance-id", "test1") .containsEntry("public-hostname", hostInfo.getHostname()) .containsEntry("public-ipv4", hostInfo.getIpAddress()) .containsEntry("local-ipv4", hostInfo.getIpAddress()); }
@Test void should_be_set_availabilityZone_using_clientConfig_when_not_exists_property() { final String[] zones = {"zone1", "zone2"}; doReturn("region").when(eurekaClientConfigBean).getRegion(); doReturn(zones).when(eurekaClientConfigBean).getAvailabilityZones("region"); assertThat(processor.postProcessAfterInitialization(eurekaInstanceConfigBean, "beanName")) .isEqualTo(eurekaInstanceConfigBean); final AmazonInfo dataCenterInfo = (AmazonInfo) eurekaInstanceConfigBean.getDataCenterInfo(); assertThat(dataCenterInfo.getMetadata()).containsEntry("availability-zone", zones[0]); }
@Test void should_be_do_nothing_when_not_EurekaInstanceConfigBean() { assertThat(processor.postProcessAfterInitialization(eurekaClientConfigBean, "beanName")) .isEqualTo(eurekaClientConfigBean); verify(eurekaInstanceConfigBean, never()).setDataCenterInfo(any()); }
@Test void should_be_do_nothing_when_dataCenter_is_amazon() { doReturn(AmazonInfo.Builder.newBuilder().build()).when(eurekaInstanceConfigBean).getDataCenterInfo(); assertThat(processor.postProcessAfterInitialization(eurekaInstanceConfigBean, "beanName")) .isEqualTo(eurekaInstanceConfigBean); verify(eurekaInstanceConfigBean, never()).setDataCenterInfo(any()); }
@Test void should_be_set_metadata_when_exists_property() { final Map<String, Object> metadata = new HashMap<>(); metadata.put("evcache.availability-zone", "test1"); metadata.put("evcache.ami-id", "test2"); metadata.put("evcache.instance-id", "test3"); metadata.put("evcache.public-hostname", "test4"); metadata.put("evcache.public-ipv4", "test5"); metadata.put("evcache.local-ipv4", "test6"); environment.getPropertySources().addLast(new MapPropertySource("test", metadata)); assertThat(processor.postProcessAfterInitialization(eurekaInstanceConfigBean, "beanName")) .isEqualTo(eurekaInstanceConfigBean); final AmazonInfo dataCenterInfo = (AmazonInfo) eurekaInstanceConfigBean.getDataCenterInfo(); assertThat(dataCenterInfo.getMetadata()).containsEntry("availability-zone", "test1") .containsEntry("ami-id", "test2") .containsEntry("instance-id", "test3") .containsEntry("public-hostname", "test4") .containsEntry("public-ipv4", "test5") .containsEntry("local-ipv4", "test6"); }