package org.apereo.cas.services.resource;

import org.apereo.cas.services.util.RegisteredServiceJsonSerializer;
import org.apereo.cas.support.events.service.CasRegisteredServiceDeletedEvent;
import org.apereo.cas.support.events.service.CasRegisteredServicesLoadedEvent;

import lombok.SneakyThrows;
import lombok.val;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.jupiter.api.Assertions.*;

/**
 * This is {@link DeleteResourceBasedRegisteredServiceWatcherTests}.
 *
 * @author Misagh Moayyed
 * @since 6.0.0
 */
public class DeleteResourceBasedRegisteredServiceWatcherTests {

    @Test
    @SneakyThrows
    public void verifyOperationNotFound() {
        val result = new AtomicBoolean(false);
        val watcher = new DeleteResourceBasedRegisteredServiceWatcher(new AbstractResourceBasedServiceRegistry(new ClassPathResource("services"),
            Collections.singletonList(new RegisteredServiceJsonSerializer()), o -> result.set(o.getClass().equals(CasRegisteredServicesLoadedEvent.class))) {
            @Override
            protected String[] getExtensions() {
                return new String[]{"json"};
            }
        });
        watcher.accept(new File("removed.json"));
        assertTrue(result.get());
    }

    @Test
    @SneakyThrows
    public void verifyOperationFoundDeleted() {
        val result = new AtomicBoolean(false);
        val registry = new AbstractResourceBasedServiceRegistry(new ClassPathResource("services"),
            Collections.singletonList(new RegisteredServiceJsonSerializer()), o -> result.set(o.getClass().equals(CasRegisteredServiceDeletedEvent.class))) {
            @Override
            protected String[] getExtensions() {
                return new String[]{"json"};
            }
        };
        var results = registry.load();
        assertFalse(results.isEmpty());
        val watcher = new DeleteResourceBasedRegisteredServiceWatcher(registry);
        watcher.accept(new File("Sample-1.json"));
        assertTrue(result.get());
        assertEquals(0, registry.size());
    }
}

@@ -2210,11 +2210,916 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void verifyTempFilesIgnored() throws Exception %7B%0A        val result = new AtomicBoolean(false);%0A        val registry = new AbstractResourceBasedServiceRegistry(new ClassPathResource(%22services%22),%0A                Collections.singletonList(new RegisteredServiceJsonSerializer()), o -%3E result.set(o.getClass().equals(CasRegisteredServiceDeletedEvent.class))) %7B%0A            @Override%0A            protected String%5B%5D getExtensions() %7B%0A                return new String%5B%5D%7B%22json%22%7D;%0A            %7D%0A        %7D;%0A        var results = registry.load();%0A        assertFalse(results.isEmpty());%0A        val watcher = new DeleteResourceBasedRegisteredServiceWatcher(registry);%0A        watcher.accept(new File(%22.Sample-1.json%22));%0A        assertFalse(result.get());%0A        watcher.accept(new File(%22Sample-1.json.swp%22));%0A        assertFalse(result.get());%0A        assertEquals(1, registry.size());%0A    %7D
 %0A%7D%0A

Test for ignoring tmp files during delete from resource based registry (#3832)