/*
 * Copyright 2016 Hewlett-Packard Development Company, L.P.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
 */

package com.hp.autonomy.frontend.find.core.savedsearches;

import com.hp.autonomy.frontend.find.core.test.AbstractFindIT;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.hamcrest.core.Is.isA;
import static org.junit.Assert.*;

public abstract class AbstractSavedQueryServiceIT extends AbstractFindIT {

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired private SavedQueryService savedQueryService;

    @Test
    @DirtiesContext
    public void createFetchDelete() {
        final String title = "Any old saved search";
        final SavedQuery savedQuery = new SavedQuery.Builder()
                .setTitle(title)
                .build();

        final SavedQuery entity = savedQueryService.create(savedQuery);
        assertThat(entity.getId(), isA(Long.class));
        assertNotNull(entity.getId());

        entity.setQueryText("*");
        final SavedQuery updatedEntity = savedQueryService.update(entity);
        assertEquals(updatedEntity.getQueryText(), "*");

        final Set<SavedQuery> fetchedEntities = savedQueryService.getAll();
        assertEquals(fetchedEntities.size(), 1);
        assertEquals(fetchedEntities.iterator().next().getTitle(), title);

        savedQueryService.deleteById(updatedEntity.getId());
        assertEquals(savedQueryService.getAll().size(), 0);
    }

    @Test
    @Transactional
    public void getAllReturnsNothing() throws Exception {
        assertThat(savedQueryService.getAll(), is(empty()));
    }
}

@@ -299,16 +299,47 @@
 FindIT;%0A
+import org.joda.time.DateTime;%0A
 import o
@@ -353,16 +353,16 @@
 t.Test;%0A
-
 import o
@@ -746,16 +746,93 @@
 ert.*;%0A%0A
+@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)%0A
 public a
@@ -1030,28 +1030,8 @@
 est%0A
-    @DirtiesContext%0A
     
@@ -1988,16 +1988,16 @@
 ption %7B%0A
-
         
@@ -2054,11 +2054,2119 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void checkUserAuditDataInserted() %7B%0A        final SavedQuery savedQuery = new SavedQuery.Builder()%0A                .setTitle(%22title%22)%0A                .build();%0A%0A        savedQueryService.create(savedQuery);%0A%0A        assertNotNull(savedQuery.getId());%0A        assertThat(savedQuery.getUser().getUserId(), isA(Long.class));%0A        assertNotNull(savedQuery.getUser().getUserId());%0A    %7D%0A%0A    @Test%0A    public void checkTimeAuditDataInsertedUpdated() %7B%0A        SavedQuery savedQuery = new SavedQuery.Builder()%0A                .setTitle(%22title%22)%0A                .build();%0A%0A        savedQuery = savedQueryService.create(savedQuery);%0A        assertNotNull(savedQuery.getId());%0A        assertThat(savedQuery.getDateCreated(), isA(DateTime.class));%0A        assertThat(savedQuery.getDateModified(), isA(DateTime.class));%0A        assertTrue(savedQuery.getDateCreated().isEqual(savedQuery.getDateModified().toInstant()));%0A        // Safe to assume completed in a day%0A        // TODO: mock out the datetime service used by spring auditing to check this properly%0A        assertTrue(savedQuery.getDateCreated().plusHours(1).isAfterNow());%0A%0A        savedQuery.setQueryText(%22*%22);%0A        savedQuery = savedQueryService.update(savedQuery);%0A        assertThat(savedQuery.getDateCreated(), isA(DateTime.class));%0A        assertThat(savedQuery.getDateModified(), isA(DateTime.class));%0A        assertTrue(savedQuery.getDateModified().isAfter(savedQuery.getDateCreated().toInstant()));%0A    %7D%0A%0A    @Test%0A    public void checkUserNotDuplicated() %7B%0A        final SavedQuery savedQuery1 = new SavedQuery.Builder()%0A                .setTitle(%22title1%22)%0A                .build();%0A%0A        final SavedQuery savedQuery2 = new SavedQuery.Builder()%0A                .setTitle(%22title2%22)%0A                .build();%0A%0A        savedQueryService.create(savedQuery1);%0A        savedQueryService.create(savedQuery2);%0A%0A        assertNotNull(savedQuery1.getUser().getUserId());%0A        assertNotNull(savedQuery2.getUser().getUserId());%0A        assertEquals(savedQuery1.getUser().getUserId(), savedQuery2.getUser().getUserId());%0A    %7D
 %0A%7D%0A

Add better integration test coverage for saved queries [rev: Dan Grayling]