Search in sources :

Example 6 with DefaultConfigFile

use of com.tencent.polaris.configuration.client.internal.DefaultConfigFile in project polaris-java by polarismesh.

the class ConfigFileTest method testGetJsonArray.

@Test
public void testGetJsonArray() {
    String content = "[\n" + "\t{\n" + "\t\t\"name\":\"zhangsan\",\n" + "\t\t\"age\":18,\n" + "\t\t\"labels\": {\n" + "\t\t  \"key1\":\"value1\"\n" + "\t\t}\n" + "\t},\n" + "\t{\n" + "\t\t\"name\":\"lisi\",\n" + "\t\t\"age\":20,\n" + "\t\t\"labels\": {\n" + "\t\t  \"key1\":\"value2\"\n" + "\t\t}\n" + "\t}\n" + "]";
    when(configFileRepo.getContent()).thenReturn(content);
    ConfigFile configFile = new DefaultConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName, configFileRepo, configFileConfig);
    Assert.assertEquals(content, configFile.getContent());
    List<ConfigFileTestUtils.User> users = configFile.asJson(new TypeToken<List<ConfigFileTestUtils.User>>() {
    }.getType(), null);
    Assert.assertNotNull(users);
    Assert.assertEquals(2, users.size());
    for (ConfigFileTestUtils.User user : users) {
        Assert.assertNotNull(user.getName());
        Assert.assertTrue(user.getAge() > 0);
        Assert.assertTrue(user.getLabels().size() > 0);
    }
}
Also used : DefaultConfigFile(com.tencent.polaris.configuration.client.internal.DefaultConfigFile) ConfigFile(com.tencent.polaris.configuration.api.core.ConfigFile) TypeToken(com.google.gson.reflect.TypeToken) DefaultConfigFile(com.tencent.polaris.configuration.client.internal.DefaultConfigFile) Test(org.junit.Test)

Example 7 with DefaultConfigFile

use of com.tencent.polaris.configuration.client.internal.DefaultConfigFile in project polaris-java by polarismesh.

the class ConfigFileTest method testDeleteContent.

@Test
public void testDeleteContent() throws InterruptedException {
    String content = "hello";
    when(configFileRepo.getContent()).thenReturn(content);
    DefaultConfigFile configFile = new DefaultConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName, configFileRepo, configFileConfig);
    Assert.assertEquals(content, configFile.getContent());
    Assert.assertTrue(configFile.hasContent());
    AtomicBoolean check = new AtomicBoolean(false);
    configFile.addChangeListener(new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent event) {
            check.set(event.getNewValue() == null && event.getOldValue().equals(content) && event.getChangeType() == ChangeType.DELETED);
        }
    });
    configFile.onChange(ConfigFileTestUtils.assembleDefaultConfigFileMeta(), null);
    TimeUnit.MILLISECONDS.sleep(100);
    Assert.assertTrue(check.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfigFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent) ConfigFileChangeListener(com.tencent.polaris.configuration.api.core.ConfigFileChangeListener) DefaultConfigFile(com.tencent.polaris.configuration.client.internal.DefaultConfigFile) Test(org.junit.Test)

Example 8 with DefaultConfigFile

use of com.tencent.polaris.configuration.client.internal.DefaultConfigFile in project polaris-java by polarismesh.

the class ConfigFileTest method testModifiedContent.

@Test
public void testModifiedContent() throws InterruptedException {
    String content = "hello";
    when(configFileRepo.getContent()).thenReturn(content);
    DefaultConfigFile configFile = new DefaultConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName, configFileRepo, configFileConfig);
    Assert.assertEquals(content, configFile.getContent());
    Assert.assertTrue(configFile.hasContent());
    String newContent = "hello2";
    AtomicBoolean check = new AtomicBoolean(false);
    configFile.addChangeListener(new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent event) {
            check.set(event.getNewValue().equals(newContent) && event.getOldValue().equals(content) && event.getChangeType() == ChangeType.MODIFIED);
        }
    });
    configFile.onChange(ConfigFileTestUtils.assembleDefaultConfigFileMeta(), "hello2");
    TimeUnit.MILLISECONDS.sleep(100);
    Assert.assertTrue(check.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfigFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent) ConfigFileChangeListener(com.tencent.polaris.configuration.api.core.ConfigFileChangeListener) DefaultConfigFile(com.tencent.polaris.configuration.client.internal.DefaultConfigFile) Test(org.junit.Test)

Example 9 with DefaultConfigFile

use of com.tencent.polaris.configuration.client.internal.DefaultConfigFile in project polaris-java by polarismesh.

the class ConfigFileTest method testContentNotChanged.

@Test
public void testContentNotChanged() throws InterruptedException {
    String content = "hello";
    when(configFileRepo.getContent()).thenReturn(content);
    DefaultConfigFile configFile = new DefaultConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName, configFileRepo, configFileConfig);
    AtomicBoolean check = new AtomicBoolean(false);
    configFile.addChangeListener(new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent event) {
            check.set(event.getNewValue().equals(content) && event.getOldValue().equals(content) && event.getChangeType() == ChangeType.NOT_CHANGED);
        }
    });
    configFile.onChange(ConfigFileTestUtils.assembleDefaultConfigFileMeta(), content);
    TimeUnit.MILLISECONDS.sleep(100);
    Assert.assertTrue(check.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfigFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent) ConfigFileChangeListener(com.tencent.polaris.configuration.api.core.ConfigFileChangeListener) DefaultConfigFile(com.tencent.polaris.configuration.client.internal.DefaultConfigFile) Test(org.junit.Test)

Aggregations

DefaultConfigFile (com.tencent.polaris.configuration.client.internal.DefaultConfigFile)9 Test (org.junit.Test)9 ConfigFileChangeEvent (com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent)6 ConfigFileChangeListener (com.tencent.polaris.configuration.api.core.ConfigFileChangeListener)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 ConfigFile (com.tencent.polaris.configuration.api.core.ConfigFile)3 TypeToken (com.google.gson.reflect.TypeToken)1