package org.apereo.cas.services;

import lombok.extern.slf4j.Slf4j;
import org.apereo.cas.authentication.CoreAuthenticationTestUtils;
import org.junit.Test;

import java.util.Arrays;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
 * @author Misagh Moayyed
 * @since 5.2.0
 */
@Slf4j
public class ServiceRegistryInitializerTests {

    @Test
    public void ensureInitFromJsonDoesNotCreateDuplicates() {
        RegisteredService initialService = CoreAuthenticationTestUtils.getRegisteredService();

        final ServicesManager servicesManager = mock(ServicesManager.class);
        final ServiceRegistry jsonServiceRegistry = mock(ServiceRegistry.class);
        when(jsonServiceRegistry.load()).thenReturn(Arrays.asList(initialService));

        final ServiceRegistry serviceRegistry = new InMemoryServiceRegistry();
        final ServiceRegistryInitializer serviceRegistryInitializer = new ServiceRegistryInitializer(jsonServiceRegistry, serviceRegistry,
            servicesManager, true);

        serviceRegistryInitializer.initServiceRegistryIfNecessary();
        assertThat(serviceRegistry.size()).isEqualTo(1);

        initialService = CoreAuthenticationTestUtils.getRegisteredService();
        when(jsonServiceRegistry.load()).thenReturn(Arrays.asList(initialService));

        serviceRegistryInitializer.initServiceRegistryIfNecessary();
        assertThat(serviceRegistry.size()).isEqualTo(1);
    }
}

@@ -65,74 +65,8 @@
 4j;%0A
-import org.apereo.cas.authentication.CoreAuthenticationTestUtils;%0A
 impo
@@ -157,17 +157,26 @@
 ertions.
-*
+assertThat
 ;%0Aimport
@@ -431,49 +431,11 @@
 e = 
-CoreAuthenticationTestUtils.getRegistered
+new
 Serv
@@ -1096,49 +1096,11 @@
 e = 
-CoreAuthenticationTestUtils.getRegistered
+new
 Serv
@@ -1322,11 +1322,336 @@
 );%0A    %7D
+%0A%0A    private RegisteredService newService() %7B%0A        final RegisteredService service = mock(RegisteredService.class);%0A        when(service.getServiceId()).thenReturn(%22%5Ehttps?://.*%22);%0A        when(service.getName()).thenReturn(%22Test%22);%0A        when(service.getDescription()).thenReturn(%22Test%22);%0A        return service;%0A    %7D
 %0A%7D%0A

fix test dependency