package org.academiadecodigo.bootcamp.fxlogin.service;

import org.academiadecodigo.bootcamp.fxlogin.service.bootcamp.BootcampService;
import org.academiadecodigo.bootcamp.fxlogin.service.user.UserService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class ServiceRegistryTest {

    private UserService mockedUserService;
    private UserService anotherMockedUserService;
    private BootcampService mockedBootcampService;
    private ServiceRegistry registry;

    @Before
    public void setUp() {
        mockedUserService = mock(UserService.class);
        anotherMockedUserService = mock(UserService.class);
        mockedBootcampService = mock(BootcampService.class);
//        registry = ServiceRegistry.getInstance();
    }

    @After
    public void cleanUp() {
        registry.clear();
    }

    @Test
    public void testNumberOfServices() {
        assertEquals(0, registry.size());
        registry.add(mockedUserService);
        assertEquals(1, registry.size());
        registry.add(anotherMockedUserService);
        assertEquals(1, registry.size());
        registry.add(mockedBootcampService);
        assertEquals(2, registry.size());
    }

    @Test
    public void testAddService() {
        assertEquals(0, registry.size());
        registry.add(mockedUserService);
        assertEquals(1, registry.size());
    }

    @Test
    public void testAddSameService() {
        assertEquals(0, registry.size());
        registry.add(mockedUserService);
        registry.add(anotherMockedUserService);
        assertEquals(1, registry.size());
        String name = anotherMockedUserService.getClass().getInterfaces()[0].getSimpleName();
        assertSame(anotherMockedUserService, registry.get(name));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testAddNullService() {
        registry.add(null);
        assertEquals(0, registry.size());
    }

    @Test
    public void testGetService() {
        assertEquals(0, registry.size());
        registry.add(mockedBootcampService);
        String name = mockedBootcampService.getClass().getInterfaces()[0].getSimpleName();
        assertSame(mockedBootcampService, registry.get(name));
    }

    @Test
    public void testGetUnaddedService() {
        assertNull(registry.get("UserService"));
    }

    @Test
    public void testGetNullService() {
        assertNull(registry.get(null));
    }

    @Test
    public void testRemoveService() {
        registry.add(mockedUserService);
        assertEquals(1, registry.size());
        String name = mockedUserService.getClass().getInterfaces()[0].getSimpleName();
        registry.remove(name);
        assertEquals(0, registry.size());
    }

    @Test
    public void testRemoveUnaddedService() {
        assertEquals(0, registry.size());
        registry.remove(mockedUserService.getClass().getInterfaces()[0].getSimpleName());
        assertEquals(0, registry.size());
    }

    @Test
    public void testRemoveNullService() {
        registry.remove(null);
    }
}
