Skip to content

Commit

Permalink
feat(client): add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawven committed Aug 25, 2024
2 parents ea8c138 + 02aee66 commit 4600e2a
Show file tree
Hide file tree
Showing 8 changed files with 450 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2022 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ctrip.framework.apollo.monitor.internal.listener.impl;
import static com.ctrip.framework.apollo.core.ApolloClientSystemConsts.*;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.ctrip.framework.apollo.monitor.internal.event.ApolloClientMonitorEvent;
import org.junit.Before;
import org.junit.Test;

import java.util.Map;

public class DefaultApolloClientBootstrapArgsApiTest {

private ConfigUtil configUtil;
private DefaultApolloClientBootstrapArgsApi api;

@Before
public void setUp() {
configUtil = mock(ConfigUtil.class);
when(configUtil.getAccessKeySecret()).thenReturn("secret");
when(configUtil.isAutoUpdateInjectedSpringPropertiesEnabled()).thenReturn(true);
when(configUtil.isOverrideSystemProperties()).thenReturn(false);
when(configUtil.getDefaultLocalCacheDir()).thenReturn("/cache");
when(configUtil.getCluster()).thenReturn("default");
when(configUtil.getAppId()).thenReturn("myApp");
when(configUtil.getApolloEnv()).thenReturn(Env.DEV);
when(configUtil.getMetaServerDomainName()).thenReturn("http://meta.server");

api = new DefaultApolloClientBootstrapArgsApi(configUtil);
}

@Test
public void testGetAccessKeySecret() {
assertEquals("secret", api.getAccessKeySecret());
}

@Test
public void testGetAutoUpdateInjectedSpringProperties() {
assertTrue(api.getAutoUpdateInjectedSpringProperties());
}

@Test
public void testGetCacheDir() {
assertEquals("/cache", api.getCacheDir());
}

@Test
public void testCollect0() {
ApolloClientMonitorEvent event = mock(ApolloClientMonitorEvent.class);
when(event.getName()).thenReturn(APOLLO_ACCESS_KEY_SECRET);
when(event.getAttachmentValue(APOLLO_ACCESS_KEY_SECRET)).thenReturn("newSecret");

api.collect0(event);

assertEquals("newSecret", api.getAccessKeySecret());
}

@Test
public void testUnhandledEvent() {
ApolloClientMonitorEvent event = mock(ApolloClientMonitorEvent.class);
when(event.getName()).thenReturn("unknownEvent");
api.collect0(event);
}

@Test
public void testGetBootstrapArgs() {
Map<String, String> bootstrapArgs = api.getBootstrapArgs();
assertNotNull(bootstrapArgs);
assertTrue(bootstrapArgs.containsKey(APOLLO_ACCESS_KEY_SECRET));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2022 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ctrip.framework.apollo.monitor.internal.listener.impl;

import static com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorConstant.THROWABLE;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

import com.ctrip.framework.apollo.monitor.internal.event.ApolloClientMonitorEvent;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class DefaultApolloClientExceptionApiTest {

private DefaultApolloClientExceptionApi exceptionApi;

@Before
public void setUp() {
exceptionApi = new DefaultApolloClientExceptionApi();
}

@Test
public void testCollect0_AddsException() {
ApolloConfigException exception = new ApolloConfigException("Test Exception");
ApolloClientMonitorEvent event = mock(ApolloClientMonitorEvent.class);
when(event.getAttachmentValue(THROWABLE)).thenReturn(exception);

exceptionApi.collect0(event);

List<Exception> exceptions = exceptionApi.getApolloConfigExceptionList();
assertEquals(1, exceptions.size());
assertEquals(exception, exceptions.get(0));
}

@Test
public void testCollect0_IncrementsExceptionCount() {
ApolloConfigException exception = new ApolloConfigException("Test Exception");
ApolloClientMonitorEvent event = mock(ApolloClientMonitorEvent.class);
when(event.getAttachmentValue(THROWABLE)).thenReturn(exception);

exceptionApi.collect0(event);
exceptionApi.collect0(event);

assertEquals(2, exceptionApi.getApolloConfigExceptionList().size());
}

@Test
public void testGetApolloConfigExceptionDetails() {
ApolloConfigException exception1 = new ApolloConfigException("First Exception");
ApolloConfigException exception2 = new ApolloConfigException("Second Exception");

ApolloClientMonitorEvent event1 = mock(ApolloClientMonitorEvent.class);
ApolloClientMonitorEvent event2 = mock(ApolloClientMonitorEvent.class);

when(event1.getAttachmentValue(THROWABLE)).thenReturn(exception1);
when(event2.getAttachmentValue(THROWABLE)).thenReturn(exception2);

exceptionApi.collect0(event1);
exceptionApi.collect0(event2);

List<String> details = exceptionApi.getApolloConfigExceptionDetails();
assertEquals(2, details.size());
assertTrue(details.contains("First Exception"));
assertTrue(details.contains("Second Exception"));
}

@Test
public void testCollect0_HandlesMaxQueueSize() {
for (int i = 0; i < 25; i++) {
ApolloClientMonitorEvent event = mock(ApolloClientMonitorEvent.class);
when(event.getAttachmentValue(THROWABLE)).thenReturn(new ApolloConfigException("Exception " + i));
exceptionApi.collect0(event);
}

assertEquals(25, exceptionApi.getApolloConfigExceptionList().size());

// Add one more to exceed the size.
ApolloClientMonitorEvent overflowEvent = mock(ApolloClientMonitorEvent.class);
when(overflowEvent.getAttachmentValue(THROWABLE)).thenReturn(new ApolloConfigException("Overflow Exception"));
exceptionApi.collect0(overflowEvent);

assertEquals(25, exceptionApi.getApolloConfigExceptionList().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*
*/
package com.ctrip.framework.apollo.monitor.internal.listener;
package com.ctrip.framework.apollo.monitor.internal.listener.impl;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientMonitorEventListenerManager;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMonitorEventListener;
import org.junit.Before;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2022 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ctrip.framework.apollo.monitor.internal.listener.impl;
import static com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorConstant.APOLLO_CLIENT_NAMESPACE_NOT_FOUND;
import static com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorConstant.APOLLO_CLIENT_NAMESPACE_TIMEOUT;
import static com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorConstant.APOLLO_CLIENT_NAMESPACE_USAGE;
import static com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorConstant.NAMESPACE;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.monitor.internal.event.ApolloClientMonitorEvent;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import org.mockito.internal.util.collections.Sets;

public class DefaultApolloClientNamespaceApiTest {

private DefaultApolloClientNamespaceApi api;
private Map<String, Config> configs;
private Map<String, ConfigFile> configFiles;

@Before
public void setUp() {
configs = new HashMap<>();
configFiles = new HashMap<>();
api = new DefaultApolloClientNamespaceApi(configs, configFiles);
}

@Test
public void testCollectNamespaceNotFound() {
ApolloClientMonitorEvent event = mock(ApolloClientMonitorEvent.class);
when(event.getAttachmentValue(NAMESPACE)).thenReturn("testNamespace");
when(event.getName()).thenReturn(APOLLO_CLIENT_NAMESPACE_NOT_FOUND);

api.collect0(event);

assertEquals(1, api.getNamespace404().size());
assertTrue(api.getNamespace404().contains("testNamespace"));
}

@Test
public void testCollectNamespaceTimeout() {
ApolloClientMonitorEvent event = mock(ApolloClientMonitorEvent.class);
when(event.getAttachmentValue(NAMESPACE)).thenReturn("testNamespace");
when(event.getName()).thenReturn(APOLLO_CLIENT_NAMESPACE_TIMEOUT);

api.collect0(event);

assertEquals(1, api.getNamespaceTimeout().size());
assertTrue(api.getNamespaceTimeout().contains("testNamespace"));
}

@Test
public void testCollectNamespaceUsage() {
ApolloClientMonitorEvent event = mock(ApolloClientMonitorEvent.class);
when(event.getAttachmentValue(NAMESPACE)).thenReturn("testNamespace");
when(event.getName()).thenReturn(APOLLO_CLIENT_NAMESPACE_USAGE);

api.collect0(event);

assertEquals(1, api.getNamespaceMetrics().get("testNamespace").getUsageCount());
}

@Test
public void testGetNamespaceItemName() {
Config mockConfig = mock(Config.class);
when(mockConfig.getPropertyNames()).thenReturn(Sets.newSet("key1", "key2"));
configs.put("testNamespace", mockConfig);

assertEquals(2, api.getNamespaceItemName("testNamespace").size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2022 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ctrip.framework.apollo.monitor.internal.listener.impl;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientThreadPoolApi.ApolloThreadPoolInfo;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class DefaultApolloClientThreadPoolApiTest {

private DefaultApolloClientThreadPoolApi threadPoolApi;
private ThreadPoolExecutor remoteConfigExecutor;
private ThreadPoolExecutor abstractConfigExecutor;
private ThreadPoolExecutor abstractConfigFileExecutor;

@Before
public void setUp() {
remoteConfigExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
abstractConfigExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
abstractConfigFileExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);

threadPoolApi = new DefaultApolloClientThreadPoolApi(
remoteConfigExecutor,
abstractConfigExecutor,
abstractConfigFileExecutor
);
}

@Test
public void testExportThreadPoolMetrics() {
remoteConfigExecutor.execute(() -> {});
remoteConfigExecutor.execute(() -> {});

threadPoolApi.export0();

ApolloThreadPoolInfo info = threadPoolApi.getRemoteConfigRepositoryThreadPoolInfo();
assertEquals(0, info.getQueueSize());
assertEquals(2, info.getCompletedTaskCount());
assertEquals(2, info.getPoolSize());
}

@Test
public void testGetThreadPoolInfo() {
assertNotNull(threadPoolApi.getThreadPoolInfo());
assertEquals(3, threadPoolApi.getThreadPoolInfo().size());
}

@Test
public void testMetricsSampleUpdated() {
assertTrue(threadPoolApi.isMetricsSampleUpdated());
}

@Test
public void testGetAbstractConfigThreadPoolInfo() {
ApolloThreadPoolInfo info = threadPoolApi.getAbstractConfigThreadPoolInfo();
assertNotNull(info);
}

@Test
public void testGetAbstractConfigFileThreadPoolInfo() {
ApolloThreadPoolInfo info = threadPoolApi.getAbstractConfigFileThreadPoolInfo();
assertNotNull(info);
}
}
Loading

0 comments on commit 4600e2a

Please sign in to comment.