package models;

import com.avaje.ebean.Ebean;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import play.test.FakeApplication;
import play.test.Helpers;
import support.EbeanUtil;

import java.lang.reflect.ParameterizedType;

public class ModelTest<T> {
    protected static FakeApplication app;
    protected static EbeanUtil ebeanUiUtil;
    protected Class<T> type;


    @SuppressWarnings("unchecked")
    public ModelTest() {
        this.type = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        ebeanUiUtil = new EbeanUtil<T>(type);
    }

    @BeforeClass
    public static void startApp() {
        app = Helpers.fakeApplication(Helpers.inMemoryDatabase());
        Helpers.start(app);
    }

    @AfterClass
    public static void stopApp() {
        Helpers.stop(app);
    }

    @Before
    public void beginTransaction() throws Exception {
        Ebean.beginTransaction();
    }

    @After
    public void rollbackTransaction() {
        if (Ebean.currentTransaction() != null) {
            Ebean.rollbackTransaction();
        }
    }

    /**
     * Returns the first user. (id : 2 / name : hobi)
     *
     * @return User
     */
    protected User getTestUser() {
        return User.findById(2l);
    }

    /**
     * Returns user.
     *
     * @param userId
     * @return
     */
    protected User getTestUser(Long userId) {
        return User.findById(userId);
    }

    @SuppressWarnings("unchecked")
    protected void flush(T model) {
        ebeanUiUtil.flush(model);
    }

    protected void flush(Long id) {
        ebeanUiUtil.flush(id);
    }

    protected void flush() {
        flush(1l);
    }

}

@@ -424,17 +424,16 @@
  type;%0A%0A
-%0A
     @Sup
@@ -570,16 +570,33 @@
 class())
+%0A                
 .getActu
@@ -1240,24 +1240,25 @@
 hobi)%0A     *
+ 
 %0A     * @ret
@@ -1385,16 +1385,17 @@
 .%0A     *
+ 
 %0A     * 
@@ -1473,24 +1473,24 @@
 g userId) %7B%0A
-
         retu
@@ -1518,24 +1518,397 @@
 Id);%0A    %7D%0A%0A
+    /**%0A     * Returns the first project. (id : 1 / name : nForge4java)%0A     * %0A     * @return Project%0A     */%0A    protected Project getTestProject() %7B%0A        return Project.findById(1l);%0A    %7D%0A%0A    /**%0A     * Returns project.%0A     * %0A     * @return Project%0A     */%0A    protected Project getTestProject(Long projectId) %7B%0A        return Project.findById(projectId);%0A    %7D%0A%0A
     @Suppres

add getTestProject method