/*
 * Copyright 2012 Shared Learning Collaborative, LLC
 *
 * Licensed 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.slc.sli.api.init;

import static junit.framework.Assert.assertEquals;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.slc.sli.domain.Entity;
import org.slc.sli.domain.NeutralQuery;
import org.slc.sli.domain.Repository;
import org.springframework.core.io.ByteArrayResource;

/**
 * 
 *
 */
public class ApplicationInitializerTest {
    
    @InjectMocks
    private ApplicationInitializer appInit;
    
    @Mock
    private Repository<Entity> mockRepo;
    
    Properties props = new Properties();
    
    @Before
    public void setUp() throws Exception {
        appInit = new ApplicationInitializer();
        MockitoAnnotations.initMocks(this);
        
        props.put("bootstrap.app.keys", "admin,dashboard");
        props.put("bootstrap.app.dashboard.template", "applications/dashboard.json");
        props.put("bootstrap.app.dashboard.url", "https://dashboard");
        props.put("bootstrap.app.dashboard.client_id", "XXXXXXXX");
        props.put("bootstrap.app.dashboard.client_secret", "YYYYYYYYYYYY");
        props.put("bootstrap.app.admin.template", "applications/admin.json");
        props.put("bootstrap.app.admin.url", "https://admin");
        props.put("bootstrap.app.admin.client_id", "XXXXXXXX");
        props.put("bootstrap.app.admin.client_secret", "YYYYYYYYYYYY");
        saveProps();


    }
    
    private void saveProps() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        props.store(baos, "");
        appInit.bootstrapProperties = new ByteArrayResource(baos.toByteArray());
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Test
    public void testAppNotExist() throws Exception {
        final List<Map<String, Object>> apps = new ArrayList<Map<String, Object>>();
        Mockito.when(mockRepo.create(Mockito.anyString(), Mockito.anyMap())).thenAnswer(new Answer<Entity>() {
            
            @Override
            public Entity answer(InvocationOnMock invocation) throws Throwable {
                apps.add((Map<String, Object>) invocation.getArguments()[1]);
                return null;
            }
        });
        appInit.init();
        assertEquals("Two apps registered", 2, apps.size());
        assertEquals("Value replaced", "https://admin", apps.get(0).get("application_url"));
        assertEquals("Value replaced", "https://dashboard", apps.get(1).get("application_url"));
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Test
    public void testCreateWithGuid() throws Exception {
        // when a GUID is specified, we have to use an update instead of a create
        final List<Entity> apps = new ArrayList<Entity>();
        props.put("bootstrap.app.admin.guid", "111");
        props.put("bootstrap.app.dashboard.guid", "222");
        saveProps();
        Mockito.when(mockRepo.update(Mockito.anyString(), Mockito.any(Entity.class))).thenAnswer(new Answer<Boolean>() {
            
            @Override
            public Boolean answer(InvocationOnMock invocation) throws Throwable {
                apps.add((Entity) invocation.getArguments()[1]);
                return true;
            }
        });
        appInit.init();
        assertEquals("Two apps updated", 2, apps.size());
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Test
    public void testExistingApps() throws Exception {
        final List<Entity> apps = new ArrayList<Entity>();
        props.put("bootstrap.app.keys", "admin");
        saveProps();
        Entity mockEntity = Mockito.mock(Entity.class);
        Mockito.when(mockRepo.findOne(Mockito.anyString(), Mockito.any(NeutralQuery.class))).thenReturn(mockEntity);
        
        Mockito.when(mockRepo.update(Mockito.anyString(), Mockito.any(Entity.class))).thenAnswer(new Answer<Boolean>() {
            
            @Override
            public Boolean answer(InvocationOnMock invocation) throws Throwable {
                apps.add((Entity) invocation.getArguments()[1]);
                return true;
            }
        });
        appInit.init();
        assertEquals("One app updated", 1, apps.size());
    }
    
}

@@ -3491,850 +3491,8 @@
 est%0A
-    public void testCreateWithGuid() throws Exception %7B%0A        // when a GUID is specified, we have to use an update instead of a create%0A        final List%3CEntity%3E apps = new ArrayList%3CEntity%3E();%0A        props.put(%22bootstrap.app.admin.guid%22, %22111%22);%0A        props.put(%22bootstrap.app.dashboard.guid%22, %22222%22);%0A        saveProps();%0A        Mockito.when(mockRepo.update(Mockito.anyString(), Mockito.any(Entity.class))).thenAnswer(new Answer%3CBoolean%3E() %7B%0A            %0A            @Override%0A            public Boolean answer(InvocationOnMock invocation) throws Throwable %7B%0A                apps.add((Entity) invocation.getArguments()%5B1%5D);%0A                return true;%0A            %7D%0A        %7D);%0A        appInit.init();%0A        assertEquals(%22Two apps updated%22, 2, apps.size());%0A    %7D%0A    %0A    @SuppressWarnings(%7B %22unchecked%22, %22rawtypes%22 %7D)%0A    @Test%0A
     

Remove obsolete unit test