NacosRepository implements ConfigurationRepository { @Override public void watch(final String key, final DataChangedEventListener listener) { try { String dataId = pathToKey(key); String group = nacosProperties.getValue(NacosPropertyKey.GROUP); configService.addListener(dataId, group, new Listener() { @Override public Executor getExecutor() { return null; } @Override public void receiveConfigInfo(final String configInfo) { listener.onChange(new DataChangedEvent(key, configInfo, ChangedType.UPDATED)); } }); } catch (final NacosException ex) { log.debug("Nacos watch key exception for: {}", ex.toString()); } }  @Override void init(final String name, final GovernanceCenterConfiguration config); @Override String get(final String key); @Override List<String> getChildrenKeys(final String key); @Override void persist(final String key, final String value); @Override void watch(final String key, final DataChangedEventListener listener); @Override void delete(final String key); @Override void close(); @Override String getType();  }
@Test public void assertWatch() throws NacosException { final String expectValue = "expectValue"; String[] actualValue = {null}; doAnswer(AdditionalAnswers.answerVoid(getListenerAnswer(expectValue))).when(configService).addListener(anyString(), anyString(), any(Listener.class)); DataChangedEventListener listener = dataChangedEvent -> actualValue[0] = dataChangedEvent.getValue(); REPOSITORY.watch("/sharding/test", listener); assertThat(actualValue[0], is(expectValue)); }
@Test public void assertWatchUpdatedChangedType() throws NacosException { final String expectValue = "expectValue"; String[] actualValue = {null}; ChangedType[] actualType = {null}; doAnswer(AdditionalAnswers.answerVoid(getListenerAnswer(expectValue))).when(configService).addListener(anyString(), anyString(), any(Listener.class)); DataChangedEventListener listener = dataChangedEvent -> { actualValue[0] = dataChangedEvent.getValue(); actualType[0] = dataChangedEvent.getChangedType(); }; REPOSITORY.watch("/sharding/test", listener); assertThat(actualValue[0], is(expectValue)); assertThat(actualType[0], is(ChangedType.UPDATED)); }
@Test public void assertWatchDeletedChangedType() throws NacosException { ChangedType[] actualType = {null}; doAnswer(AdditionalAnswers.answerVoid(getListenerAnswer(null))).when(configService).addListener(anyString(), anyString(), any(Listener.class)); DataChangedEventListener listener = dataChangedEvent -> actualType[0] = dataChangedEvent.getChangedType(); REPOSITORY.watch("/sharding/test", listener); assertThat(actualType[0], is(ChangedType.UPDATED)); }
@Test public void assertWatchWhenThrowException() throws NacosException { ChangedType[] actualType = {null}; doThrow(NacosException.class).when(configService).addListener(anyString(), anyString(), any(Listener.class)); DataChangedEventListener listener = dataChangedEvent -> actualType[0] = dataChangedEvent.getChangedType(); REPOSITORY.watch("/sharding/test", listener); assertNull(actualType[0]); }