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 668d53c
Show file tree
Hide file tree
Showing 10 changed files with 463 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ public class DefaultApolloClientExceptionApi extends
AbstractApolloClientMonitorEventListener implements
ApolloClientExceptionMonitorApi, ApolloClientJmxExceptionMBean {

private static final int MAX_EXCEPTIONS_SIZE = ApolloInjector.getInstance(ConfigUtil.class)
.getMonitorExceptionQueueSize();
private final BlockingQueue<ApolloConfigException> exceptions = new ArrayBlockingQueue<>(
MAX_EXCEPTIONS_SIZE);
private final AtomicInteger exceptionNum = new AtomicInteger(0);
private int monitorExceptionQueueSize;
private BlockingQueue<ApolloConfigException> exceptions;

public DefaultApolloClientExceptionApi() {
super(TAG_ERROR);
monitorExceptionQueueSize = ApolloInjector.getInstance(ConfigUtil.class)
.getMonitorExceptionQueueSize();
exceptions = new ArrayBlockingQueue<>(
monitorExceptionQueueSize);
}

@Override
Expand All @@ -70,7 +72,7 @@ public void collect0(ApolloClientMonitorEvent event) {
}

private void addExceptionToQueue(ApolloConfigException exception) {
if (exceptions.size() >= MAX_EXCEPTIONS_SIZE) {
if (exceptions.size() >= monitorExceptionQueueSize) {
exceptions.poll();
}
exceptions.add(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ public class ConfigMonitorInitializerTest {
@Before
public void setUp() {
mockConfigUtil = mock(ConfigUtil.class);
when(mockConfigUtil.getMonitorExceptionQueueSize()).thenReturn(100);
when(mockConfigUtil.getClientMonitorEnabled()).thenReturn(false);
mockLogger = mock(Logger.class);
mockManager = mock(DefaultApolloClientMonitorEventListenerManager.class);
mockMetricsExporter = mock(ApolloClientMetricsExporter.class);
mockConfigManager = mock(DefaultConfigManager.class);
mockConfigMonitor = mock(DefaultConfigMonitor.class);


// Mock static methods
MockInjector.setInstance(ConfigUtil.class, mockConfigUtil);
Expand All @@ -60,8 +63,6 @@ public void setUp() {

// Reset static state before each test
ConfigMonitorInitializer.reset();
when(mockConfigUtil.getMonitorExceptionQueueSize()).thenReturn(100);
when(mockConfigUtil.getClientMonitorEnabled()).thenReturn(false);
}

@Test
Expand Down
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());
}
}
Loading

0 comments on commit 668d53c

Please sign in to comment.