/*
 * 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 org.apache.kylin.rest.controller;

import java.io.IOException;
import java.io.StringWriter;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kylinolap.cube.model.CubeDesc;
import org.apache.kylin.rest.request.CubeRequest;
import org.apache.kylin.rest.service.CubeService;
import org.apache.kylin.rest.service.JobService;
import org.apache.kylin.rest.service.ServiceTestBase;

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

    private CubeController cubeController;
    private CubeDescController cubeDescController;

    @Autowired
    CubeService cubeService;
    @Autowired
    JobService jobService;

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

        cubeController = new CubeController();
        cubeController.setCubeService(cubeService);
        cubeController.setJobService(jobService);
        cubeDescController = new CubeDescController();
        cubeDescController.setCubeService(cubeService);
    }

    @Test
    public void testBasics() throws IOException {
        CubeDesc[] cubes = (CubeDesc[]) cubeDescController.getCube("test_kylin_cube_with_slr_ready");
        Assert.assertNotNull(cubes);
        Assert.assertNotNull(cubeController.getSql("test_kylin_cube_with_slr_ready", "20130331080000_20131212080000"));
        Assert.assertNotNull(cubeController.getCubes(null, null, 0, 5));

        CubeDesc cube = cubes[0];
        CubeDesc newCube = new CubeDesc();
        String newCubeName = cube.getName() + "_test_save";
        newCube.setName(newCubeName);
        newCube.setModelName(cube.getModelName());
        newCube.setModel(cube.getModel());
        newCube.setDimensions(cube.getDimensions());
        newCube.setHBaseMapping(cube.getHBaseMapping());
        newCube.setMeasures(cube.getMeasures());
        newCube.setConfig(cube.getConfig());
        newCube.setRowkey(cube.getRowkey());

        ObjectMapper mapper = new ObjectMapper();
        StringWriter stringWriter = new StringWriter();
        mapper.writeValue(stringWriter, newCube);

        CubeRequest cubeRequest = new CubeRequest();
        cubeRequest.setCubeDescData(stringWriter.toString());
        cubeRequest = cubeController.saveCubeDesc(cubeRequest);

        cubeController.deleteCube(newCubeName);
    }

}

@@ -2660,24 +2660,191 @@
 Rowkey());%0A%0A
+        newCube.getModel().setName(newCubeName + %22_model_desc%22 + System.currentTimeMillis());//generate a random model%0A        newCube.getModel().setLastModified(0);%0A%0A
         Obje
@@ -2852,17 +2852,25 @@
 tMapper 
-m
+cubeDescM
 apper = 
@@ -2910,22 +2910,203 @@
 gWriter 
-string
+cubeDescWriter = new StringWriter();%0A        cubeDescMapper.writeValue(cubeDescWriter, newCube);%0A%0A        ObjectMapper modelDescMapper = new ObjectMapper();%0A        StringWriter modelDesc
 Writer =
@@ -3135,16 +3135,25 @@
        m
+odelDescM
 apper.wr
@@ -3161,22 +3161,25 @@
 teValue(
-string
+modelDesc
 Writer, 
@@ -3177,32 +3177,43 @@
 cWriter, newCube
+.getModel()
 );%0A%0A        Cube
@@ -3293,14 +3293,82 @@
 ata(
-string
+cubeDescWriter.toString());%0A        cubeRequest.setModelDescData(modelDesc
 Writ

fix test case