com.ctrip.framework.apollo.core.ConfigConsts Java Examples

The following examples show how to use com.ctrip.framework.apollo.core.ConfigConsts. 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: ConfigControllerIntegrationTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
@Sql(scripts = "/integration-test/test-release.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/test-gray-release.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testQueryGrayConfigWithDefaultClusterAndDefaultNamespaceAndIncorrectCase() throws Exception {
  AtomicBoolean stop = new AtomicBoolean();
  periodicSendMessage(executorService, assembleKey(someAppId, ConfigConsts.CLUSTER_NAME_DEFAULT, ConfigConsts.NAMESPACE_APPLICATION),
      stop);

  TimeUnit.MILLISECONDS.sleep(500);

  stop.set(true);

  ResponseEntity<ApolloConfig> response = restTemplate
      .getForEntity("http://{baseurl}/configs/{appId}/{clusterName}/{namespace}?ip={clientIp}", ApolloConfig.class,
          getHostUrl(), someAppId, ConfigConsts.CLUSTER_NAME_DEFAULT,
          ConfigConsts.NAMESPACE_APPLICATION.toUpperCase(), someClientIp);
  ApolloConfig result = response.getBody();

  assertEquals(HttpStatus.OK, response.getStatusCode());
  assertEquals("TEST-GRAY-RELEASE-KEY1", result.getReleaseKey());
  assertEquals("v1-gray", result.getConfigurations().get("k1"));
}
 
Example #2
Source File: NotificationControllerV2IntegrationTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 5000L)
@Sql(scripts = "/integration-test/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testPollNotificationWithDefaultNamespace() throws Exception {
  AtomicBoolean stop = new AtomicBoolean();
  String key = assembleKey(someAppId, someCluster, defaultNamespace);
  periodicSendMessage(executorService, key, stop);

  ResponseEntity<List<ApolloConfigNotification>> result = restTemplate.exchange(
      "http://{baseurl}/notifications/v2?appId={appId}&cluster={clusterName}&notifications={notifications}",
      HttpMethod.GET, null, typeReference,
      getHostUrl(), someAppId, someCluster,
      transformApolloConfigNotificationsToString(defaultNamespace, ConfigConsts.NOTIFICATION_ID_PLACEHOLDER));

  stop.set(true);

  List<ApolloConfigNotification> notifications = result.getBody();
  assertEquals(HttpStatus.OK, result.getStatusCode());
  assertEquals(1, notifications.size());
  assertEquals(defaultNamespace, notifications.get(0).getNamespaceName());
  assertNotEquals(0, notifications.get(0).getNotificationId());

  ApolloNotificationMessages messages = result.getBody().get(0).getMessages();
  assertEquals(1, messages.getDetails().size());
  assertTrue(messages.has(key));
  assertNotEquals(ConfigConsts.NOTIFICATION_ID_PLACEHOLDER, messages.get(key).longValue());
}
 
Example #3
Source File: XmlConfigFileTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testWhenConfigRepositoryHasErrorAndThenRecovered() throws Exception {
  Properties someProperties = new Properties();
  String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
  String someValue = "someValue";
  someProperties.setProperty(key, someValue);

  when(configRepository.getConfig()).thenThrow(new RuntimeException("someError"));

  XmlConfigFile configFile = new XmlConfigFile(someNamespace, configRepository);

  assertFalse(configFile.hasContent());
  assertNull(configFile.getContent());

  configFile.onRepositoryChange(someNamespace, someProperties);

  assertTrue(configFile.hasContent());
  assertEquals(someValue, configFile.getContent());
}
 
Example #4
Source File: NotificationControllerTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  controller = new NotificationController(watchKeysUtil, releaseMessageService, entityManagerUtil, namespaceUtil);

  someAppId = "someAppId";
  someCluster = "someCluster";
  defaultNamespace = ConfigConsts.NAMESPACE_APPLICATION;
  someDataCenter = "someDC";
  someNotificationId = 1;
  someClientIp = "someClientIp";

  when(namespaceUtil.filterNamespaceName(defaultNamespace)).thenReturn(defaultNamespace);

  deferredResults =
      (Multimap<String, DeferredResult<ResponseEntity<ApolloConfigNotification>>>) ReflectionTestUtils
          .getField(controller, "deferredResults");
}
 
Example #5
Source File: ConfigControllerIntegrationTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
@Sql(scripts = "/integration-test/test-release.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/test-release-public-default-override.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/test-gray-release.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testQueryPublicGrayConfigWithIncorrectCaseAndOverride() throws Exception {
  AtomicBoolean stop = new AtomicBoolean();
  periodicSendMessage(executorService, assembleKey(somePublicAppId, ConfigConsts.CLUSTER_NAME_DEFAULT, somePublicNamespace),
      stop);

  TimeUnit.MILLISECONDS.sleep(500);

  stop.set(true);

  ResponseEntity<ApolloConfig> response = restTemplate
      .getForEntity("http://{baseurl}/configs/{appId}/{clusterName}/{namespace}?ip={clientIp}", ApolloConfig.class,
          getHostUrl(), someAppId, someDefaultCluster, somePublicNamespace.toUpperCase(), someClientIp);
  ApolloConfig result = response.getBody();

  assertEquals(HttpStatus.OK, response.getStatusCode());
  assertEquals(
      "TEST-RELEASE-KEY5" + ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR + "TEST-GRAY-RELEASE-KEY2",
      result.getReleaseKey());
  assertEquals("override-v1", result.getConfigurations().get("k1"));
  assertEquals("gray-v2", result.getConfigurations().get("k2"));
}
 
Example #6
Source File: ApolloApplicationContextInitializer.java    From apollo with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize Apollo Configurations Just after environment is ready.
 *
 * @param environment
 */
protected void initialize(ConfigurableEnvironment environment) {

  if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
    //already initialized
    return;
  }

  String namespaces = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, ConfigConsts.NAMESPACE_APPLICATION);
  logger.debug("Apollo bootstrap namespaces: {}", namespaces);
  List<String> namespaceList = NAMESPACE_SPLITTER.splitToList(namespaces);

  CompositePropertySource composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);
  for (String namespace : namespaceList) {
    Config config = ConfigService.getConfig(namespace);

    composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config));
  }

  environment.getPropertySources().addFirst(composite);
}
 
Example #7
Source File: XmlConfigPlaceholderTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiplePropertySourcesWithSameProperties() throws Exception {
  int someTimeout = 1000;
  int anotherTimeout = someTimeout + 1;
  int someBatch = 2000;

  Config application = mock(Config.class);
  when(application.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(someTimeout));
  when(application.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch));
  mockConfig(ConfigConsts.NAMESPACE_APPLICATION, application);

  Config fxApollo = mock(Config.class);
  when(fxApollo.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(anotherTimeout));
  mockConfig(FX_APOLLO_NAMESPACE, fxApollo);

  check("spring/XmlConfigPlaceholderTest3.xml", someTimeout, someBatch);
}
 
Example #8
Source File: ConfigControllerIntegrationTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
@Sql(scripts = "/integration-test/test-release.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testQueryPublicConfigForNoAppIdPlaceHolder() throws Exception {
  ResponseEntity<ApolloConfig> response = restTemplate
      .getForEntity("http://{baseurl}/configs/{appId}/{clusterName}/{namespace}?dataCenter={dateCenter}",
          ApolloConfig.class,
          getHostUrl(), ConfigConsts.NO_APPID_PLACEHOLDER, someCluster, somePublicNamespace, someDC);
  ApolloConfig result = response.getBody();

  assertEquals("TEST-RELEASE-KEY4", result.getReleaseKey());
  assertEquals(ConfigConsts.NO_APPID_PLACEHOLDER, result.getAppId());
  assertEquals(someCluster, result.getCluster());
  assertEquals(somePublicNamespace, result.getNamespaceName());
  assertEquals("someDC-v1", result.getConfigurations().get("k1"));
  assertEquals("someDC-v2", result.getConfigurations().get("k2"));
}
 
Example #9
Source File: NotificationControllerIntegrationTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 5000L)
@Sql(scripts = "/integration-test/test-release.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testPollNotificationWithPrivateNamespaceAsFile() throws Exception {
  String namespace = "someNamespace.xml";
  AtomicBoolean stop = new AtomicBoolean();
  periodicSendMessage(executorService, assembleKey(someAppId, ConfigConsts.CLUSTER_NAME_DEFAULT, namespace), stop);

  ResponseEntity<ApolloConfigNotification> result = restTemplate
      .getForEntity(
          "http://{baseurl}/notifications?appId={appId}&cluster={clusterName}&namespace={namespace}",
          ApolloConfigNotification.class,
          getHostUrl(), someAppId, someCluster, namespace);

  stop.set(true);

  ApolloConfigNotification notification = result.getBody();
  assertEquals(HttpStatus.OK, result.getStatusCode());
  assertEquals(namespace, notification.getNamespaceName());
  assertNotEquals(0, notification.getNotificationId());
}
 
Example #10
Source File: JsonConfigFileTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testWhenConfigRepositoryHasErrorAndThenRecovered() throws Exception {
  Properties someProperties = new Properties();
  String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
  String someValue = "someValue";
  someProperties.setProperty(key, someValue);

  someSourceType = ConfigSourceType.LOCAL;

  when(configRepository.getConfig()).thenThrow(new RuntimeException("someError"));
  when(configRepository.getSourceType()).thenReturn(someSourceType);

  JsonConfigFile configFile = new JsonConfigFile(someNamespace, configRepository);

  assertFalse(configFile.hasContent());
  assertNull(configFile.getContent());
  assertEquals(ConfigSourceType.NONE, configFile.getSourceType());

  configFile.onRepositoryChange(someNamespace, someProperties);

  assertTrue(configFile.hasContent());
  assertEquals(someValue, configFile.getContent());
  assertEquals(someSourceType, configFile.getSourceType());
}
 
Example #11
Source File: NamespaceOpenApiService.java    From apollo with Apache License 2.0 6 votes vote down vote up
public List<OpenNamespaceDTO> getNamespaces(String appId, String env, String clusterName) {
  if (Strings.isNullOrEmpty(clusterName)) {
    clusterName = ConfigConsts.CLUSTER_NAME_DEFAULT;
  }

  checkNotEmpty(appId, "App id");
  checkNotEmpty(env, "Env");

  String path = String.format("envs/%s/apps/%s/clusters/%s/namespaces", escapePath(env), escapePath(appId),
      escapePath(clusterName));

  try (CloseableHttpResponse response = get(path)) {
    return gson.fromJson(EntityUtils.toString(response.getEntity()), OPEN_NAMESPACE_DTO_LIST_TYPE);
  } catch (Throwable ex) {
    throw new RuntimeException(String
        .format("Get namespaces for appId: %s, cluster: %s in env: %s failed", appId, clusterName, env), ex);
  }
}
 
Example #12
Source File: ConfigControllerIntegrationTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
@Sql(scripts = "/integration-test/test-release.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/test-release-public-dc-override.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testQueryPublicConfigWithDataCenterFoundAndOverride() throws Exception {
  ResponseEntity<ApolloConfig> response = restTemplate
      .getForEntity("http://{baseurl}/configs/{appId}/{clusterName}/{namespace}?dataCenter={dateCenter}",
          ApolloConfig.class,
          getHostUrl(), someAppId, someDefaultCluster, somePublicNamespace, someDC);
  ApolloConfig result = response.getBody();

  assertEquals(
      "TEST-RELEASE-KEY6" + ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR + "TEST-RELEASE-KEY4",
      result.getReleaseKey());
  assertEquals(someAppId, result.getAppId());
  assertEquals(someDC, result.getCluster());
  assertEquals(somePublicNamespace, result.getNamespaceName());
  assertEquals("override-someDC-v1", result.getConfigurations().get("k1"));
  assertEquals("someDC-v2", result.getConfigurations().get("k2"));
}
 
Example #13
Source File: ConfigControllerTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testQueryConfigForNoAppIdPlaceHolder() throws Exception {
  String someClientSideReleaseKey = "1";
  HttpServletResponse someResponse = mock(HttpServletResponse.class);
  String appId = ConfigConsts.NO_APPID_PLACEHOLDER;

  ApolloConfig result = configController.queryConfig(appId, someClusterName,
      defaultNamespaceName, someDataCenter, someClientSideReleaseKey,
      someClientIp, someMessagesAsString, someRequest, someResponse);

  verify(configService, never()).loadConfig(appId, someClientIp, someAppId, someClusterName, defaultNamespaceName,
      someDataCenter, someNotificationMessages);
  verify(appNamespaceService, never()).findPublicNamespaceByName(defaultNamespaceName);
  assertNull(result);
  verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
}
 
Example #14
Source File: ConfigControllerIntegrationTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
@Sql(scripts = "/integration-test/test-release.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testQueryPrivateConfigFileWithPublicNamespaceExists() throws Exception {
  String namespaceName = "anotherNamespace";
  ResponseEntity<ApolloConfig> response = restTemplate
      .getForEntity("http://{baseurl}/configs/{appId}/{clusterName}/{namespace}",
          ApolloConfig.class,
          getHostUrl(), someAppId, ConfigConsts.CLUSTER_NAME_DEFAULT, namespaceName);
  ApolloConfig result = response.getBody();

  assertEquals("TEST-RELEASE-KEY6", result.getReleaseKey());
  assertEquals(someAppId, result.getAppId());
  assertEquals(ConfigConsts.CLUSTER_NAME_DEFAULT, result.getCluster());
  assertEquals(namespaceName, result.getNamespaceName());
  assertEquals("v1-file", result.getConfigurations().get("k1"));
  assertEquals(null, result.getConfigurations().get("k2"));
}
 
Example #15
Source File: NamespacePublishInfoTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testNamespaceEverPublishedAndModifiedAfter() {
  Cluster cluster = createCluster(ConfigConsts.CLUSTER_NAME_DEFAULT);
  Namespace namespace = createNamespace(ConfigConsts.CLUSTER_NAME_DEFAULT, ConfigConsts.NAMESPACE_APPLICATION);
  Item item = createItem(namespace.getId(), "a", "b");
  Release release = createRelease("{\"a\":\"c\"}");

  when(clusterService.findParentClusters(testApp)).thenReturn(Collections.singletonList(cluster));
  when(namespaceRepository.findByAppIdAndClusterNameOrderByIdAsc(testApp, ConfigConsts.CLUSTER_NAME_DEFAULT))
      .thenReturn(Collections.singletonList(namespace));
  when(releaseService.findLatestActiveRelease(namespace)).thenReturn(release);
  when(itemService.findItemsModifiedAfterDate(anyLong(), any())).thenReturn(Collections.singletonList(item));

  Map<String, Boolean> result = namespaceService.namespacePublishInfo(testApp);

  Assert.assertEquals(1, result.size());
  Assert.assertTrue(result.get(ConfigConsts.CLUSTER_NAME_DEFAULT));

}
 
Example #16
Source File: AppNamespaceService.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Transactional
public void createDefaultAppNamespace(String appId, String createBy) {
  if (!isAppNamespaceNameUnique(appId, ConfigConsts.NAMESPACE_APPLICATION)) {
    throw new ServiceException("appnamespace not unique");
  }
  AppNamespace appNs = new AppNamespace();
  appNs.setAppId(appId);
  appNs.setName(ConfigConsts.NAMESPACE_APPLICATION);
  appNs.setComment("default app namespace");
  appNs.setFormat(ConfigFileFormat.Properties.getValue());
  appNs.setDataChangeCreatedBy(createBy);
  appNs.setDataChangeLastModifiedBy(createBy);
  appNamespaceRepository.save(appNs);

  auditService.audit(AppNamespace.class.getSimpleName(), appNs.getId(), Audit.OP.INSERT,
                     createBy);
}
 
Example #17
Source File: JavaConfigPlaceholderTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiplePropertySourcesCoverWithSameProperties() throws Exception {
  //Multimap does not maintain the strict input order of namespace.
  int someTimeout = 1000;
  int anotherTimeout = someTimeout + 1;
  int someBatch = 2000;

  Config fxApollo = mock(Config.class);
  when(fxApollo.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(someTimeout));
  when(fxApollo.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch));
  mockConfig(FX_APOLLO_NAMESPACE, fxApollo);

  Config application = mock(Config.class);
  when(application.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(anotherTimeout));
  mockConfig(ConfigConsts.NAMESPACE_APPLICATION, application);

  check(someTimeout, someBatch, AppConfig6.class);
}
 
Example #18
Source File: JavaConfigPlaceholderTest.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplicationPropertySourceWithValueInjectedAsParameter() throws Exception {
  int someTimeout = 1000;
  int someBatch = 2000;

  Config config = mock(Config.class);
  when(config.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(someTimeout));
  when(config.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch));

  mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config);

  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig5.class);

  TestJavaConfigBean2 bean = context.getBean(TestJavaConfigBean2.class);

  assertEquals(someTimeout, bean.getTimeout());
  assertEquals(someBatch, bean.getBatch());
}
 
Example #19
Source File: AbstractConfigService.java    From apollo with Apache License 2.0 6 votes vote down vote up
@Override
public Release loadConfig(String clientAppId, String clientIp, String configAppId, String configClusterName,
    String configNamespace, String dataCenter, ApolloNotificationMessages clientMessages) {
  // load from specified cluster fist
  if (!Objects.equals(ConfigConsts.CLUSTER_NAME_DEFAULT, configClusterName)) {
    Release clusterRelease = findRelease(clientAppId, clientIp, configAppId, configClusterName, configNamespace,
        clientMessages);

    if (!Objects.isNull(clusterRelease)) {
      return clusterRelease;
    }
  }

  // try to load via data center
  if (!Strings.isNullOrEmpty(dataCenter) && !Objects.equals(dataCenter, configClusterName)) {
    Release dataCenterRelease = findRelease(clientAppId, clientIp, configAppId, dataCenter, configNamespace,
        clientMessages);
    if (!Objects.isNull(dataCenterRelease)) {
      return dataCenterRelease;
    }
  }

  // fallback to default release
  return findRelease(clientAppId, clientIp, configAppId, ConfigConsts.CLUSTER_NAME_DEFAULT, configNamespace,
      clientMessages);
}
 
Example #20
Source File: ReleaseOpenApiService.java    From apollo with Apache License 2.0 6 votes vote down vote up
public OpenReleaseDTO publishNamespace(String appId, String env, String clusterName, String namespaceName,
    NamespaceReleaseDTO releaseDTO) {
  if (Strings.isNullOrEmpty(clusterName)) {
    clusterName = ConfigConsts.CLUSTER_NAME_DEFAULT;
  }
  if (Strings.isNullOrEmpty(namespaceName)) {
    namespaceName = ConfigConsts.NAMESPACE_APPLICATION;
  }

  checkNotEmpty(appId, "App id");
  checkNotEmpty(env, "Env");
  checkNotEmpty(releaseDTO.getReleaseTitle(), "Release title");
  checkNotEmpty(releaseDTO.getReleasedBy(), "Released by");

  String path = String.format("envs/%s/apps/%s/clusters/%s/namespaces/%s/releases",
      escapePath(env), escapePath(appId), escapePath(clusterName), escapePath(namespaceName));

  try (CloseableHttpResponse response = post(path, releaseDTO)) {
    return gson.fromJson(EntityUtils.toString(response.getEntity()), OpenReleaseDTO.class);
  } catch (Throwable ex) {
    throw new RuntimeException(String
        .format("Release namespace: %s for appId: %s, cluster: %s in env: %s failed", namespaceName, appId,
            clusterName, env), ex);
  }
}
 
Example #21
Source File: XmlConfigPlaceholderTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiplePropertySources() throws Exception {
  int someTimeout = 1000;
  int someBatch = 2000;

  Config application = mock(Config.class);
  when(application.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(someTimeout));
  mockConfig(ConfigConsts.NAMESPACE_APPLICATION, application);

  Config fxApollo = mock(Config.class);
  when(application.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch));
  mockConfig(FX_APOLLO_NAMESPACE, fxApollo);

  check("spring/XmlConfigPlaceholderTest3.xml", someTimeout, someBatch);
}
 
Example #22
Source File: JavaConfigPlaceholderAutoUpdateTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoUpdateWithTypeMismatch() throws Exception {
  int initialTimeout = 1000;
  int initialBatch = 2000;
  int newTimeout = 1001;
  String newBatch = "newBatch";

  Properties properties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(initialTimeout), BATCH_PROPERTY,
      String.valueOf(initialBatch));

  SimpleConfig config = prepareConfig(ConfigConsts.NAMESPACE_APPLICATION, properties);

  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig1.class);

  TestJavaConfigBean bean = context.getBean(TestJavaConfigBean.class);

  assertEquals(initialTimeout, bean.getTimeout());
  assertEquals(initialBatch, bean.getBatch());

  Properties newProperties =
      assembleProperties(TIMEOUT_PROPERTY, String.valueOf(newTimeout), BATCH_PROPERTY, newBatch);

  config.onRepositoryChange(ConfigConsts.NAMESPACE_APPLICATION, newProperties);

  TimeUnit.MILLISECONDS.sleep(300);

  assertEquals(newTimeout, bean.getTimeout());
  assertEquals(initialBatch, bean.getBatch());
}
 
Example #23
Source File: XmlConfigPlaceholderTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testApplicationPropertySource() throws Exception {
  int someTimeout = 1000;
  int someBatch = 2000;

  Config config = mock(Config.class);
  when(config.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(someTimeout));
  when(config.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch));

  mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config);

  check("spring/XmlConfigPlaceholderTest2.xml", someTimeout, someBatch);
}
 
Example #24
Source File: XmlConfigPlaceholderAutoUpdateTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoUpdateWithNewProperties() throws Exception {
  int initialTimeout = 1000;
  int newTimeout = 1001;
  int newBatch = 2001;

  Properties applicationProperties = assembleProperties(TIMEOUT_PROPERTY,
      String.valueOf(initialTimeout));

  SimpleConfig applicationConfig = prepareConfig(ConfigConsts.NAMESPACE_APPLICATION,
      applicationProperties);

  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/XmlConfigPlaceholderTest1.xml");

  TestXmlBean bean = context.getBean(TestXmlBean.class);

  assertEquals(initialTimeout, bean.getTimeout());
  assertEquals(DEFAULT_BATCH, bean.getBatch());

  Properties newApplicationProperties = assembleProperties(TIMEOUT_PROPERTY,
      String.valueOf(newTimeout), BATCH_PROPERTY, String.valueOf(newBatch));

  applicationConfig
      .onRepositoryChange(ConfigConsts.NAMESPACE_APPLICATION, newApplicationProperties);

  TimeUnit.MILLISECONDS.sleep(100);

  assertEquals(newTimeout, bean.getTimeout());
  assertEquals(newBatch, bean.getBatch());
}
 
Example #25
Source File: XMLConfigAnnotationTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test(expected = BeanCreationException.class)
public void testApolloConfigWithWrongFieldType() throws Exception {
  Config applicationConfig = mock(Config.class);

  mockConfig(ConfigConsts.NAMESPACE_APPLICATION, applicationConfig);

  getBean("spring/XmlConfigAnnotationTest2.xml", TestApolloConfigBean2.class);
}
 
Example #26
Source File: XMLConfigAnnotationTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testApolloConfig() throws Exception {
  Config applicationConfig = mock(Config.class);
  Config fxApolloConfig = mock(Config.class);

  mockConfig(ConfigConsts.NAMESPACE_APPLICATION, applicationConfig);
  mockConfig(FX_APOLLO_NAMESPACE, fxApolloConfig);

  TestApolloConfigBean1 bean = getBean("spring/XmlConfigAnnotationTest1.xml", TestApolloConfigBean1.class);

  assertEquals(applicationConfig, bean.getConfig());
  assertEquals(applicationConfig, bean.getAnotherConfig());
  assertEquals(fxApolloConfig, bean.getYetAnotherConfig());
}
 
Example #27
Source File: XmlConfigPlaceholderAutoUpdateTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoUpdateWithValueAndProperty() throws Exception {
  int initialTimeout = 1000;
  int initialBatch = 2000;
  int newTimeout = 1001;
  int newBatch = 2001;

  Properties properties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(initialTimeout),
      BATCH_PROPERTY, String.valueOf(initialBatch));

  SimpleConfig config = prepareConfig(ConfigConsts.NAMESPACE_APPLICATION, properties);

  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/XmlConfigPlaceholderTest9.xml");

  TestXmlBeanWithInjectedValue bean = context.getBean(TestXmlBeanWithInjectedValue.class);

  assertEquals(initialTimeout, bean.getTimeout());
  assertEquals(initialBatch, bean.getBatch());

  Properties newProperties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(newTimeout),
      BATCH_PROPERTY, String.valueOf(newBatch));

  config.onRepositoryChange(ConfigConsts.NAMESPACE_APPLICATION, newProperties);

  TimeUnit.MILLISECONDS.sleep(100);

  assertEquals(newTimeout, bean.getTimeout());
  assertEquals(newBatch, bean.getBatch());
}
 
Example #28
Source File: XmlConfigPlaceholderAutoUpdateTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoUpdateWithValueInjectedAsConstructorArgs() throws Exception {
  int initialTimeout = 1000;
  int initialBatch = 2000;
  int newTimeout = 1001;
  int newBatch = 2001;

  Properties properties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(initialTimeout),
      BATCH_PROPERTY, String.valueOf(initialBatch));

  SimpleConfig config = prepareConfig(ConfigConsts.NAMESPACE_APPLICATION, properties);

  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/XmlConfigPlaceholderTest8.xml");

  TestXmlBeanWithConstructorArgs bean = context.getBean(TestXmlBeanWithConstructorArgs.class);

  assertEquals(initialTimeout, bean.getTimeout());
  assertEquals(initialBatch, bean.getBatch());

  Properties newProperties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(newTimeout),
      BATCH_PROPERTY, String.valueOf(newBatch));

  config.onRepositoryChange(ConfigConsts.NAMESPACE_APPLICATION, newProperties);

  TimeUnit.MILLISECONDS.sleep(100);

  // Does not support this scenario
  assertEquals(initialTimeout, bean.getTimeout());
  assertEquals(initialBatch, bean.getBatch());
}
 
Example #29
Source File: XmlConfigPlaceholderAutoUpdateTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoUpdateWithTypeMismatch() throws Exception {
  int initialTimeout = 1000;
  int initialBatch = 2000;
  int newTimeout = 1001;
  String newBatch = "newBatch";

  Properties properties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(initialTimeout),
      BATCH_PROPERTY, String.valueOf(initialBatch));

  SimpleConfig config = prepareConfig(ConfigConsts.NAMESPACE_APPLICATION, properties);

  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/XmlConfigPlaceholderTest1.xml");

  TestXmlBean bean = context.getBean(TestXmlBean.class);

  assertEquals(initialTimeout, bean.getTimeout());
  assertEquals(initialBatch, bean.getBatch());

  Properties newProperties = assembleProperties(TIMEOUT_PROPERTY, String.valueOf(newTimeout),
      BATCH_PROPERTY, newBatch);

  config.onRepositoryChange(ConfigConsts.NAMESPACE_APPLICATION, newProperties);

  TimeUnit.MILLISECONDS.sleep(300);

  assertEquals(newTimeout, bean.getTimeout());
  assertEquals(initialBatch, bean.getBatch());
}
 
Example #30
Source File: ConfigControllerIntegrationTest.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  ReflectionTestUtils.invokeMethod(appNamespaceServiceWithCache, "reset");

  someAppId = "someAppId";
  someCluster = "someCluster";
  someNamespace = "someNamespace";
  somePublicAppId = "somePublicAppId";
  somePublicNamespace = "somePublicNamespace";
  someDC = "someDC";
  someDefaultCluster = ConfigConsts.CLUSTER_NAME_DEFAULT;
  someClientIp = "1.1.1.1";
  executorService = Executors.newSingleThreadExecutor();
}