/*
 * 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.
 */

package org.apache.shardingsphere.governance.core.registry.service.state;

import org.apache.shardingsphere.governance.repository.spi.RegistryCenterRepository;
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;
import java.util.Collections;
import java.util.List;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public final class DataSourceStatusRegistryServiceTest {
    
    @Mock
    private RegistryCenterRepository registryCenterRepository;
    
    private DataSourceStatusRegistryService dataSourceStatusRegistryService;
    
    @Before
    public void setUp() throws ReflectiveOperationException {
        dataSourceStatusRegistryService = new DataSourceStatusRegistryService(registryCenterRepository);
        Field field = dataSourceStatusRegistryService.getClass().getDeclaredField("repository");
        field.setAccessible(true);
        field.set(dataSourceStatusRegistryService, registryCenterRepository);
    }
    
    @Test
    public void assertLoadDisabledDataSources() {
        List<String> disabledDataSources = Collections.singletonList("replica_ds_0");
        when(registryCenterRepository.getChildrenKeys(anyString())).thenReturn(disabledDataSources);
        dataSourceStatusRegistryService.loadDisabledDataSources("replica_query_db");
        verify(registryCenterRepository).getChildrenKeys(anyString());
        verify(registryCenterRepository).get(anyString());
    }
    
    // TODO add update assertions
}

@@ -919,47 +919,368 @@
 nce.
-repository.spi.RegistryCenterRepository
+core.registry.RegistryCenterNode;%0Aimport org.apache.shardingsphere.governance.core.registry.RegistryCenterNodeStatus;%0Aimport org.apache.shardingsphere.governance.repository.spi.RegistryCenterRepository;%0Aimport org.apache.shardingsphere.infra.rule.event.impl.DataSourceDisabledEvent;%0Aimport org.apache.shardingsphere.infra.rule.event.impl.PrimaryDataSourceEvent
 ;%0Aim
@@ -2781,45 +2781,1353 @@
   %7D%0A
+%0A
     
-%0A    // TODO add update assertions
+@Test%0A    public void assertUpdateDataSourceDisabledState() %7B%0A        assertUpdateDataSourceState(true, RegistryCenterNodeStatus.DISABLED.toString());%0A    %7D%0A%0A    @Test%0A    public void assertUpdateDataSourceEnabledState() %7B%0A        assertUpdateDataSourceState(false, %22%22);%0A    %7D%0A%0A    private void assertUpdateDataSourceState(final boolean isDisabled, final String value) %7B%0A        String schemaName = %22replica_query_db%22;%0A        String dataSourceName = %22replica_ds_0%22;%0A        DataSourceDisabledEvent dataSourceDisabledEvent = new DataSourceDisabledEvent(schemaName, dataSourceName, isDisabled);%0A        dataSourceStatusRegistryService.update(dataSourceDisabledEvent);%0A        verify(registryCenterRepository).persist(new RegistryCenterNode().getDataSourcePath(schemaName, dataSourceName), value);%0A    %7D%0A%0A    @Test%0A    public void assertUpdatePrimaryDataSourceState() %7B%0A        String schemaName = %22replica_query_db%22;%0A        String groupName = %22group1%22;%0A        String dataSourceName = %22replica_ds_0%22;%0A        PrimaryDataSourceEvent primaryDataSourceEvent = new PrimaryDataSourceEvent(schemaName, groupName, dataSourceName);%0A        dataSourceStatusRegistryService.update(primaryDataSourceEvent);%0A        verify(registryCenterRepository).persist(new RegistryCenterNode().getPrimaryDataSourcePath(schemaName, groupName), dataSourceName);%0A    %7D
 %0A%7D%0A

Add update from event test cases for DataSourceStatusRegistryService (#10430)