Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit test #268

Merged
merged 2 commits into from Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,50 @@
package org.apache.dubbo.admin.common.util;
sunbufu marked this conversation as resolved.
Show resolved Hide resolved

import org.junit.Test;

import static org.junit.Assert.*;
sunbufu marked this conversation as resolved.
Show resolved Hide resolved

public class CoderUtilTest {

@Test
public void MD5_16bit() {
assertNull(CoderUtil.MD5_16bit(null));

String input = "dubbo";
String output = "2CC9DEED96FE012E";
assertEquals(output, CoderUtil.MD5_16bit(input));
}

@Test
public void MD5_32bit() {
String input = null;
assertNull(CoderUtil.MD5_32bit(input));

input = "dubbo";
String output = "AA4E1B8C2CC9DEED96FE012EF2E0752A";
assertEquals(output, CoderUtil.MD5_32bit(input));
}

@Test
public void MD5_32bit1() {
byte[] input = null;
assertNull(CoderUtil.MD5_32bit(input));

input = "dubbo".getBytes();
String output = "AA4E1B8C2CC9DEED96FE012EF2E0752A";
assertEquals(output, CoderUtil.MD5_32bit(input));
}

@Test
public void decodeBase64() {
try {
CoderUtil.decodeBase64(null);
fail("when param is null, this should throw exception");
} catch (Exception e) {
}

String input = "ZHViYm8=";
String output = "dubbo";
assertEquals(output, CoderUtil.decodeBase64(input));
}
}
@@ -0,0 +1,86 @@
package org.apache.dubbo.admin.service;
sunbufu marked this conversation as resolved.
Show resolved Hide resolved

import org.apache.dubbo.admin.common.util.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.registry.Registry;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

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

@RunWith(SpringJUnit4ClassRunner.class)
public class RegistryServerSyncTest {

@Mock
private Registry registry;

@InjectMocks
private RegistryServerSync registryServerSync;

@Test
public void testGetRegistryCache() {
registryServerSync.getRegistryCache();
}

@Test
public void testAfterPropertiesSet() throws Exception {
registryServerSync.afterPropertiesSet();
verify(registry).subscribe(any(URL.class), any(RegistryServerSync.class));
}

@Test
public void testDestroy() throws Exception {
registryServerSync.destroy();
verify(registry).unsubscribe(any(URL.class), any(RegistryServerSync.class));
}

@Test
public void testNotify() {
registryServerSync.notify(null);
registryServerSync.notify(Collections.emptyList());

// when url.getProtocol is not empty protocol
URL consumerUrl = mock(URL.class);
URL providerUrl = mock(URL.class);

when(consumerUrl.getParameter(Constants.CATEGORY_KEY, Constants.PROVIDERS_CATEGORY)).thenReturn(org.apache.dubbo.common.Constants.CONSUMER_PROTOCOL);
when(consumerUrl.getServiceInterface()).thenReturn("org.apache.dubbo.consumer");
when(consumerUrl.getServiceKey()).thenReturn("org.apache.dubbo.consumer");
when(consumerUrl.toFullString()).thenReturn("consumer://192.168.1.10/sunbufu.dubbo.consumer?application=dubbo&category=consumer&check=false&dubbo=2.7.0&interface=sunbufu.dubbo.consumer&loadbalabce=roundrobin&mehods=sayHi,sayGoodBye&owner=sunbufu&pid=18&protocol=dubbo&side=consumer&timeout=3000&timestamp=1548127407769");
when(providerUrl.getParameter(Constants.CATEGORY_KEY, Constants.PROVIDERS_CATEGORY)).thenReturn(org.apache.dubbo.common.Constants.PROVIDER_PROTOCOL);
when(providerUrl.getServiceInterface()).thenReturn("org.apache.dubbo.provider");
when(providerUrl.getServiceKey()).thenReturn("org.apache.dubbo.provider");
when(providerUrl.toFullString()).thenReturn("consumer://192.168.1.10/sunbufu.dubbo.consumer?application=dubbo&category=consumer&check=false&dubbo=2.6.2&interface=sunbufu.dubbo.consumer&loadbalabce=roundrobin&mehods=sayHi,sayGoodBye&owner=sunbufu&pid=18&protocol=dubbo&side=consumer&timeout=3000&timestamp=1548127407769");

registryServerSync.notify(Arrays.asList(consumerUrl, consumerUrl, providerUrl));

ConcurrentMap<String, Map<String, URL>> consumerMap = registryServerSync.getRegistryCache().get(org.apache.dubbo.common.Constants.CONSUMER_PROTOCOL);
assertTrue(consumerMap.keySet().contains("org.apache.dubbo.consumer"));
ConcurrentMap<String, Map<String, URL>> providerMap = registryServerSync.getRegistryCache().get(org.apache.dubbo.common.Constants.PROVIDER_PROTOCOL);
assertTrue(providerMap.keySet().contains("org.apache.dubbo.provider"));

// when url.getProtocol is empty protocol
when(consumerUrl.getProtocol()).thenReturn(org.apache.dubbo.common.Constants.EMPTY_PROTOCOL);
when(consumerUrl.getParameter(Constants.GROUP_KEY)).thenReturn("dubbo");
when(consumerUrl.getParameter(Constants.VERSION_KEY)).thenReturn("2.7.0");
registryServerSync.notify(Collections.singletonList(consumerUrl));

assertTrue(!consumerMap.keySet().contains("org.apache.dubbo.consumer"));

// when url's group or version is ANY_VALUE (*)
when(providerUrl.getProtocol()).thenReturn(org.apache.dubbo.common.Constants.EMPTY_PROTOCOL);
when(providerUrl.getParameter(Constants.GROUP_KEY)).thenReturn(Constants.ANY_VALUE);
registryServerSync.notify(Collections.singletonList(providerUrl));

assertTrue(!providerMap.keySet().contains("org.apache.dubbo.provider"));
}
}