RegistryCenterTest.java 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

18
package org.apache.shardingsphere.orchestration.core.registry;
19

20
import org.apache.shardingsphere.orchestration.repository.api.RegistryRepository;
21 22 23 24 25 26 27
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.lang.reflect.Field;
28 29
import java.util.Collections;
import java.util.List;
30 31 32

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
33
import static org.mockito.Mockito.when;
34 35

@RunWith(MockitoJUnitRunner.class)
kimmking's avatar
kimmking 已提交
36
public final class RegistryCenterTest {
37 38
    
    @Mock
T
terrymanu 已提交
39
    private RegistryRepository registryRepository;
40
    
kimmking's avatar
kimmking 已提交
41
    private RegistryCenter registryCenter;
42 43 44
    
    @Before
    public void setUp() throws ReflectiveOperationException {
M
menghaoranss 已提交
45
        registryCenter = new RegistryCenter(registryRepository);
kimmking's avatar
kimmking 已提交
46
        Field field = registryCenter.getClass().getDeclaredField("repository");
47
        field.setAccessible(true);
T
terrymanu 已提交
48
        field.set(registryCenter, registryRepository);
49 50 51 52
    }
    
    @Test
    public void assertPersistInstanceOnline() {
kimmking's avatar
kimmking 已提交
53
        registryCenter.persistInstanceOnline();
T
terrymanu 已提交
54
        verify(registryRepository).persistEphemeral(anyString(), anyString());
55 56 57 58
    }
    
    @Test
    public void assertPersistDataSourcesNode() {
kimmking's avatar
kimmking 已提交
59
        registryCenter.persistDataSourcesNode();
M
menghaoranss 已提交
60
        verify(registryRepository).persist("/registry/datasources", "");
61
    }
62 63 64 65
    
    @Test
    public void assertPersistInstanceData() {
        registryCenter.persistInstanceData("test");
T
terrymanu 已提交
66
        verify(registryRepository).persist(anyString(), anyString());
67 68 69 70 71
    }
    
    @Test
    public void assertLoadInstanceData() {
        registryCenter.loadInstanceData();
T
terrymanu 已提交
72
        verify(registryRepository).get(anyString());
73
    }
74 75 76 77
    
    @Test
    public void assertLoadDisabledDataSources() {
        List<String> disabledDataSources = Collections.singletonList("slave_ds_0");
T
terrymanu 已提交
78
        when(registryRepository.getChildrenKeys(anyString())).thenReturn(disabledDataSources);
79
        registryCenter.loadDisabledDataSources();
T
terrymanu 已提交
80 81
        verify(registryRepository).getChildrenKeys(anyString());
        verify(registryRepository).get(anyString());
82
    }
M
menghaoranss 已提交
83
}