Search in sources :

Example 1 with UpdateHandler

use of org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler in project incubator-servicecomb-java-chassis by apache.

the class TestConfigCenterConfigurationSource method testDelete.

@Test
public void testDelete() throws Exception {
    ConfigCenterConfigurationSourceImpl configCenterSource = new ConfigCenterConfigurationSourceImpl();
    configCenterSource.addUpdateListener(new WatchedUpdateListener() {

        @Override
        public void updateConfiguration(WatchedUpdateResult result) {
            Assert.assertTrue(!result.getDeleted().isEmpty());
        }
    });
    UpdateHandler udateHandler = Deencapsulation.getField(configCenterSource, UpdateHandler.class);
    Map<String, Object> addedItems = new HashMap<>();
    addedItems.put("testKey", "testValue");
    configCenterSource.getCurrentData().put("testKey", "testValue");
    udateHandler.handle("delete", addedItems);
    Assert.assertTrue(configCenterSource.getCurrentData().isEmpty());
}
Also used : UpdateHandler(org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler) HashMap(java.util.HashMap) WatchedUpdateListener(com.netflix.config.WatchedUpdateListener) WatchedUpdateResult(com.netflix.config.WatchedUpdateResult) Test(org.junit.Test)

Example 2 with UpdateHandler

use of org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler in project incubator-servicecomb-java-chassis by apache.

the class TestConfigCenterClient method testConfigRefresh.

@SuppressWarnings("unchecked")
@Test
public void testConfigRefresh() {
    ConfigCenterConfigurationSourceImpl impl = new ConfigCenterConfigurationSourceImpl();
    impl.init(ConfigUtil.createLocalConfig());
    UpdateHandler updateHandler = impl.new UpdateHandler();
    HttpClientRequest request = Mockito.mock(HttpClientRequest.class);
    Mockito.when(request.headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
    Buffer rsp = Mockito.mock(Buffer.class);
    Mockito.when(rsp.toString()).thenReturn("{\"application\":{\"2\":\"2\",\"aa\":\"1\"},\"vmalledge\":{\"aa\":\"3\"}}");
    HttpClientResponse event = Mockito.mock(HttpClientResponse.class);
    Mockito.when(event.bodyHandler(Mockito.any(Handler.class))).then(invocation -> {
        Handler<Buffer> handler = invocation.getArgumentAt(0, Handler.class);
        handler.handle(rsp);
        return null;
    });
    Mockito.when(event.statusCode()).thenReturn(200);
    Buffer buf = Mockito.mock(Buffer.class);
    Mockito.when(buf.toJsonObject()).thenReturn(new JsonObject("{\"action\":\"UPDATE\",\"key\":\"vmalledge\",\"value\":\"{\\\"aa\\\":\\\"3\\\"}\"}"));
    WebSocket websocket = Mockito.mock(WebSocket.class);
    Mockito.when(websocket.handler(Mockito.any(Handler.class))).then(invocation -> {
        Handler<Buffer> handler = invocation.getArgumentAt(0, Handler.class);
        handler.handle(buf);
        return websocket;
    });
    HttpClient httpClient = Mockito.mock(HttpClient.class);
    Mockito.when(httpClient.get(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.any(Handler.class))).then(invocation -> {
        Handler<HttpClientResponse> handler = invocation.getArgumentAt(3, Handler.class);
        handler.handle(event);
        return request;
    });
    Mockito.when(httpClient.websocket(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.any(MultiMap.class), Mockito.any(Handler.class), Mockito.any(Handler.class))).then(invocation -> {
        Handler<WebSocket> handler = invocation.getArgumentAt(4, Handler.class);
        handler.handle(websocket);
        return null;
    });
    new MockUp<HttpClientWithContext>() {

        @Mock
        public void runOnContext(RunHandler handler) {
            handler.run(httpClient);
        }
    };
    ConfigCenterClient cc = new ConfigCenterClient(updateHandler);
    ParseConfigUtils parseConfigUtils = new ParseConfigUtils(updateHandler);
    MemberDiscovery memberdis = new MemberDiscovery(Arrays.asList("http://configcentertest:30103"));
    ConfigRefresh refresh = cc.new ConfigRefresh(parseConfigUtils, memberdis);
    refresh.run();
    Map<String, Object> flatItems = Deencapsulation.getField(parseConfigUtils, "flatItems");
    Assert.assertEquals(2, flatItems.size());
    Deencapsulation.setField(cc, "refreshMode", 0);
    refresh.run();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) UpdateHandler(org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler) ConfigCenterConfigurationSourceImpl(org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl) RunHandler(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext.RunHandler) UpdateHandler(org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) MockUp(mockit.MockUp) RunHandler(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext.RunHandler) WebSocket(io.vertx.core.http.WebSocket) MultiMap(io.vertx.core.MultiMap) HttpClientRequest(io.vertx.core.http.HttpClientRequest) ConfigRefresh(org.apache.servicecomb.config.client.ConfigCenterClient.ConfigRefresh) HttpClientResponse(io.vertx.core.http.HttpClientResponse) HttpClient(io.vertx.core.http.HttpClient) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 3 with UpdateHandler

use of org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler in project incubator-servicecomb-java-chassis by apache.

the class TestConfigCenterConfigurationSource method testCreate.

@Test
public void testCreate() throws Exception {
    ConfigCenterConfigurationSourceImpl configCenterSource = new ConfigCenterConfigurationSourceImpl();
    configCenterSource.addUpdateListener(new WatchedUpdateListener() {

        @Override
        public void updateConfiguration(WatchedUpdateResult result) {
            Assert.assertTrue(!result.getAdded().isEmpty());
        }
    });
    UpdateHandler udateHandler = Deencapsulation.getField(configCenterSource, UpdateHandler.class);
    Map<String, Object> addedItems = new HashMap<>();
    addedItems.put("testKey", "testValue");
    udateHandler.handle("create", addedItems);
}
Also used : UpdateHandler(org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler) HashMap(java.util.HashMap) WatchedUpdateListener(com.netflix.config.WatchedUpdateListener) WatchedUpdateResult(com.netflix.config.WatchedUpdateResult) Test(org.junit.Test)

Example 4 with UpdateHandler

use of org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler in project incubator-servicecomb-java-chassis by apache.

the class TestConfigCenterConfigurationSource method testUpdate.

@Test
public void testUpdate() throws Exception {
    ConfigCenterConfigurationSourceImpl configCenterSource = new ConfigCenterConfigurationSourceImpl();
    configCenterSource.addUpdateListener(new WatchedUpdateListener() {

        @Override
        public void updateConfiguration(WatchedUpdateResult result) {
            Assert.assertTrue(!result.getChanged().isEmpty());
        }
    });
    UpdateHandler udateHandler = Deencapsulation.getField(configCenterSource, UpdateHandler.class);
    Map<String, Object> addedItems = new HashMap<>();
    addedItems.put("testKey", "testValue");
    udateHandler.handle("set", addedItems);
}
Also used : UpdateHandler(org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler) HashMap(java.util.HashMap) WatchedUpdateListener(com.netflix.config.WatchedUpdateListener) WatchedUpdateResult(com.netflix.config.WatchedUpdateResult) Test(org.junit.Test)

Example 5 with UpdateHandler

use of org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler in project incubator-servicecomb-java-chassis by apache.

the class TestConfigCenterClient method testConfigRefreshException.

@SuppressWarnings("unchecked")
@Test
public void testConfigRefreshException() {
    ConfigCenterConfigurationSourceImpl impl = new ConfigCenterConfigurationSourceImpl();
    Map<String, String> map = new HashMap<>();
    EventManager.register(new Object() {

        @Subscribe
        public void testMsg(Object event) {
            if (event instanceof ConnFailEvent) {
                map.put("result", "Fail event trigger");
            }
            if (event instanceof ConnSuccEvent) {
                map.put("result", "Succ event trigger");
            }
        }
    });
    impl.init(ConfigUtil.createLocalConfig());
    UpdateHandler updateHandler = impl.new UpdateHandler();
    HttpClientRequest request = Mockito.mock(HttpClientRequest.class);
    Mockito.when(request.headers()).thenReturn(MultiMap.caseInsensitiveMultiMap());
    Buffer rsp = Mockito.mock(Buffer.class);
    Mockito.when(rsp.toString()).thenReturn("{\"application\":{\"2\":\"2\",\"aa\":\"1\"},\"vmalledge\":{\"aa\":\"3\"}}");
    HttpClientResponse event = Mockito.mock(HttpClientResponse.class);
    Mockito.when(event.bodyHandler(Mockito.any(Handler.class))).then(invocation -> {
        Handler<Buffer> handler = invocation.getArgumentAt(0, Handler.class);
        handler.handle(rsp);
        return null;
    });
    Mockito.when(event.statusCode()).thenReturn(400);
    Buffer buf = Mockito.mock(Buffer.class);
    Mockito.when(buf.toJsonObject()).thenReturn(new JsonObject("{\"action\":\"UPDATE\",\"key\":\"vmalledge\",\"value\":\"{\\\"aa\\\":\\\"3\\\"}\"}"));
    HttpClient httpClient = Mockito.mock(HttpClient.class);
    Mockito.when(httpClient.get(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.any(Handler.class))).then(invocation -> {
        Handler<HttpClientResponse> handler = invocation.getArgumentAt(3, Handler.class);
        handler.handle(event);
        return request;
    });
    new MockUp<HttpClientWithContext>() {

        @Mock
        public void runOnContext(RunHandler handler) {
            handler.run(httpClient);
        }
    };
    ConfigCenterClient cc = new ConfigCenterClient(updateHandler);
    ParseConfigUtils parseConfigUtils = new ParseConfigUtils(updateHandler);
    MemberDiscovery memberdis = new MemberDiscovery(Arrays.asList("http://configcentertest:30103"));
    ConfigRefresh refresh = cc.new ConfigRefresh(parseConfigUtils, memberdis);
    refresh.run();
    Assert.assertEquals("Fail event trigger", map.get("result"));
    Mockito.when(event.statusCode()).thenReturn(200);
    refresh.run();
    Assert.assertEquals("Succ event trigger", map.get("result"));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) UpdateHandler(org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler) ConfigCenterConfigurationSourceImpl(org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl) HashMap(java.util.HashMap) RunHandler(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext.RunHandler) UpdateHandler(org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) MockUp(mockit.MockUp) Subscribe(com.google.common.eventbus.Subscribe) RunHandler(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext.RunHandler) HttpClientRequest(io.vertx.core.http.HttpClientRequest) ConfigRefresh(org.apache.servicecomb.config.client.ConfigCenterClient.ConfigRefresh) HttpClientResponse(io.vertx.core.http.HttpClientResponse) HttpClient(io.vertx.core.http.HttpClient) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Aggregations

UpdateHandler (org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl.UpdateHandler)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)4 WatchedUpdateListener (com.netflix.config.WatchedUpdateListener)3 WatchedUpdateResult (com.netflix.config.WatchedUpdateResult)3 Handler (io.vertx.core.Handler)3 Buffer (io.vertx.core.buffer.Buffer)3 HttpClient (io.vertx.core.http.HttpClient)3 HttpClientRequest (io.vertx.core.http.HttpClientRequest)3 HttpClientResponse (io.vertx.core.http.HttpClientResponse)3 JsonObject (io.vertx.core.json.JsonObject)3 MockUp (mockit.MockUp)3 ConfigCenterConfigurationSourceImpl (org.apache.servicecomb.config.archaius.sources.ConfigCenterConfigurationSourceImpl)3 RunHandler (org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext.RunHandler)3 ConfigRefresh (org.apache.servicecomb.config.client.ConfigCenterClient.ConfigRefresh)2 Subscribe (com.google.common.eventbus.Subscribe)1 MultiMap (io.vertx.core.MultiMap)1 WebSocket (io.vertx.core.http.WebSocket)1