/*
 * Copyright 2013-2014 eBay Software Foundation
 *
 * 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 com.kylinolap.rest.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.kylinolap.common.KylinConfig;
import com.kylinolap.cube.CubeDescManager;
import com.kylinolap.cube.CubeInstance;
import com.kylinolap.cube.CubeManager;
import com.kylinolap.cube.model.CubeDesc;
import com.kylinolap.job.JobDAO;
import com.kylinolap.metadata.MetadataManager;
import com.kylinolap.rest.exception.InternalErrorException;
import com.kylinolap.rest.exception.NotFoundException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.kylinolap.job.JobInstance;
import com.kylinolap.rest.request.JobBuildRequest;
import com.kylinolap.rest.request.JobListRequest;
import com.kylinolap.rest.service.CubeService;
import com.kylinolap.rest.service.JobService;
import com.kylinolap.rest.service.ServiceTestBase;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * @author xduo
 */
public class JobControllerTest extends ServiceTestBase {

    private JobController jobSchedulerController;
    private CubeController cubeController;
    @Autowired
    JobService jobService;

    @Autowired
    CubeService cubeService;
    private static final String CUBE_NAME = "new_job_controller";


    @Before
    public void setup() throws Exception {
        super.setUp();

        jobSchedulerController = new JobController();
        jobSchedulerController.setJobService(jobService);
        cubeController = new CubeController();
        cubeController.setJobService(jobService);
        cubeController.setCubeService(cubeService);

    }

    @Test
    public void testBasics() throws IOException {

        KylinConfig testConfig = getTestConfig();
        CubeManager cubeManager = CubeManager.getInstance(testConfig);
        if (cubeManager.getCube(CUBE_NAME) != null) {
            cubeManager.dropCube(CUBE_NAME, false);
        }
        CubeDescManager cubeDescManager = CubeDescManager.getInstance(testConfig);
        CubeDesc cubeDesc = cubeDescManager.getCubeDesc("test_kylin_cube_with_slr_left_join_desc");
        CubeInstance cube = cubeManager.createCube(CUBE_NAME, "DEFAULT", cubeDesc, "test");
        assertNotNull(cube);

        JobListRequest jobRequest = new JobListRequest();
        Assert.assertNotNull(jobSchedulerController.list(jobRequest));

        JobBuildRequest jobBuildRequest = new JobBuildRequest();
        jobBuildRequest.setBuildType("BUILD");
        jobBuildRequest.setStartTime(0L);
        jobBuildRequest.setEndTime(new Date().getTime());
        JobInstance job = cubeController.rebuild(CUBE_NAME, jobBuildRequest);


        Assert.assertNotNull(jobSchedulerController.get(job.getId()));
        Map<String, String> output = jobSchedulerController.getStepOutput(job.getId(), 0);
        Assert.assertNotNull(output);
        JobDAO.getInstance(testConfig).deleteJob(job);

        // jobSchedulerController.cancel(job.getId());
    }

    @Test(expected = RuntimeException.class)
    public void testResume() throws IOException {
        JobBuildRequest jobBuildRequest = new JobBuildRequest();
        jobBuildRequest.setBuildType("BUILD");
        jobBuildRequest.setStartTime(20130331080000L);
        jobBuildRequest.setEndTime(20131212080000L);
        JobInstance job = cubeController.rebuild(CUBE_NAME, jobBuildRequest);

        jobSchedulerController.resume(job.getId());
    }
}

@@ -649,105 +649,8 @@
 r;%0A%0A
-import java.io.IOException;%0Aimport java.util.Date;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A
 impo
@@ -887,545 +887,8 @@
 AO;%0A
-import com.kylinolap.metadata.MetadataManager;%0Aimport com.kylinolap.rest.exception.InternalErrorException;%0Aimport com.kylinolap.rest.exception.NotFoundException;%0Aimport org.junit.Assert;%0Aimport org.junit.Before;%0Aimport org.junit.BeforeClass;%0Aimport org.junit.Test;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.security.authentication.TestingAuthenticationToken;%0Aimport org.springframework.security.core.Authentication;%0Aimport org.springframework.security.core.context.SecurityContextHolder;%0A%0A
 impo
@@ -1165,16 +1165,250 @@
 estBase;
+%0Aimport org.junit.After;%0Aimport org.junit.Assert;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0A%0Aimport java.io.IOException;%0Aimport java.util.Date;%0Aimport java.util.Map;
 %0A%0Aimport
@@ -1450,51 +1450,8 @@
 ull;
-%0Aimport static org.junit.Assert.assertTrue;
 %0A%0A/*
@@ -1777,16 +1777,126 @@
 ller%22;%0A%0A
+    private CubeManager cubeManager;%0A    private CubeDescManager cubeDescManager;%0A    private JobDAO jobDAO;%0A%0A
 %0A    @Be
@@ -2232,151 +2232,133 @@
 e);%0A
-%0A    %7D%0A%0A    @Test%0A    public void testBasics() throws IOException %7B%0A%0A        KylinConfig testConfig = getTestConfig();%0A        CubeManager
+        KylinConfig testConfig = getTestConfig();%0A        cubeManager = CubeManager.getInstance(testConfig);%0A       
  cube
+Desc
 Mana
@@ -2359,32 +2359,36 @@
 scManager = Cube
+Desc
 Manager.getInsta
@@ -2395,32 +2395,147 @@
 nce(testConfig);
+%0A%0A        jobDAO = JobDAO.getInstance(testConfig);%0A%0A    %7D%0A%0A    @After%0A    public void tearDown() throws Exception %7B
 %0A        if (cub
@@ -2643,86 +2643,70 @@
     
+%7D%0A%0A
     
-CubeDescManager cubeDescManager = CubeDescManager.getInstance(testConfig);
+@Test%0A    public void testBasics() throws IOException %7B
 %0A   
@@ -3558,54 +3558,146 @@
     
-J
+j
 obDAO.
-getInstance(testConfig).deleteJob(job);
+deleteJob(job);%0A        if (cubeManager.getCube(CUBE_NAME) != null) %7B%0A            cubeManager.dropCube(CUBE_NAME, false);%0A        %7D
 %0A%0A  

fix CI issue