/*
 * ProActive Parallel Suite(TM): The Java(TM) library for
 *    Parallel, Distributed, Multi-Core Computing for
 *    Enterprise Grids & Clouds
 *
 * Copyright (C) 1997-2016 INRIA/University of
 *                 Nice-Sophia Antipolis/ActiveEon
 * Contact: proactive@ow2.org or contact@activeeon.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation; version 3 of
 * the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 *
 * Initial developer(s):               The ProActive Team
 *                         http://proactive.inria.fr/team_members.htm
 */

package org.ow2.proactive.workflow_catalog.rest.controller;

import com.jayway.restassured.response.Response;
import org.apache.http.HttpStatus;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ow2.proactive.workflow_catalog.rest.Application;
import org.ow2.proactive.workflow_catalog.rest.entity.Bucket;
import org.ow2.proactive.workflow_catalog.rest.service.repository.BucketRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.google.common.truth.Truth.assertThat;
import static com.jayway.restassured.RestAssured.given;
import static com.jayway.restassured.RestAssured.when;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD;

/**
 * @author ActiveEon Team
 */
@ActiveProfiles("test")
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {Application.class})
@WebIntegrationTest
public class BucketControllerIntegrationTest {

    private static final String BUCKETS_RESOURCE = "/buckets";

    private static final String BUCKET_RESOURCE = "/buckets/{bucketId}";

    @Autowired
    private BucketRepository bucketRepository;

    @Test
    public void testCreateBucketShouldReturnSavedBucket() {
        String bucketName = "test";

        Response response =
                given().parameters("name", bucketName).
                        when().post(BUCKETS_RESOURCE);

        Object createdAt = response.getBody().jsonPath().get("created_at");
        assertThat(createdAt).isNotNull();

        response.then().assertThat()
                .statusCode(HttpStatus.SC_CREATED)
                .body("id", is(1))
                .body("name", is(bucketName));
    }

    @Test
    public void testCreateBucketShouldReturnBadRequestWithoutBody() {
        when().post(BUCKETS_RESOURCE).then().assertThat().statusCode(HttpStatus.SC_BAD_REQUEST);
    }

    @Test
    public void testGetBucketShouldReturnSavedBucket() throws Exception {
        Bucket bucket = bucketRepository.save(new Bucket("myBucket"));

        given().pathParam("bucketId", 1L).
                when().get(BUCKET_RESOURCE).then().assertThat()
                .statusCode(HttpStatus.SC_OK)
                .body("id", is(bucket.getId().intValue()))
                .body("created_at", is(bucket.getCreatedAt().toString()))
                .body("name", is(bucket.getName()));
    }

    @Test
    public void testGetBucketShouldBeNotFoundIfNonExistingId() {
        given().pathParam("bucketId", 42L).get(BUCKET_RESOURCE).then().statusCode(HttpStatus.SC_NOT_FOUND);
    }

    @Test
    public void testListBucketsShouldReturnEmptyContent() {
        when().get(BUCKETS_RESOURCE).then().assertThat()
                .statusCode(HttpStatus.SC_OK)
                .body("page.number", is(0))
                .body("page.totalElements", is(0));
    }

    @Test
    public void testListBucketsShouldReturnSavedBuckets() {
        List<Bucket> buckets = IntStream.rangeClosed(1, 25)
                .mapToObj(i -> new Bucket("bucket" + i))
                .collect(Collectors.toList());

        bucketRepository.save(buckets);

        when().get(BUCKETS_RESOURCE).then().assertThat()
                .statusCode(HttpStatus.SC_OK)
                .body("_embedded.bucketMetadataList", hasSize(20))
                .body("page.number", is(0))
                .body("page.totalElements", is(buckets.size()));
    }

}
@@ -1296,24 +1296,74 @@
 ontroller;%0A%0A
+import com.jayway.restassured.path.json.JsonPath;%0A
 import com.j
@@ -1399,16 +1399,16 @@
 sponse;%0A
-
 import o
@@ -2069,24 +2069,56 @@
 assRunner;%0A%0A
+import java.time.LocalDateTime;%0A
 import java.
@@ -2363,24 +2363,86 @@
 sured.when;%0A
+import static com.jayway.restassured.path.json.JsonPath.from;%0A
 import stati
@@ -2514,16 +2514,61 @@
 ers.is;%0A
+import static org.junit.Assert.assertEquals;%0A
 import s
@@ -4024,24 +4024,210 @@
 ket%22));%0A
-%0A
        
+ final long bucketId = bucket.getId();%0A        final String bucketName = bucket.getName();%0A        final LocalDateTime bucketCreatedAt = bucket.getCreatedAt();%0A        JsonPath jsonPath =
  given()
@@ -4253,33 +4253,16 @@
 d%22, 1L).
-%0A                
 when().g
@@ -4281,39 +4281,44 @@
 SOURCE).then
-().assertThat
+Return().jsonPath
 ()
+;
 %0A           
@@ -4318,229 +4318,256 @@
     
-        .statusCode(HttpStatus.SC_OK)%0A                .body
+LocalDateTime actualDate = LocalDateTime.parse(jsonPath.getString(%22created_at%22));%0A        assertEquals(jsonPath.getLong
 (%22id%22
+)
 , 
-is(
 bucket
-.getId().intValue()))%0A                .body(%22created_at%22, is(bucket.getCreatedAt().toString()))%0A                .body(%22name%22, is(bucket.getName())
+Id);%0A        assertEquals(jsonPath.getString(%22name%22), bucketName);%0A        assertEquals(actualDate, bucketCreatedAt
 );%0A 

Add robustness into a integration-test