package org.springframework.data.ebean.querychannel;

import com.google.common.collect.Maps;
import io.ebean.Query;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.ebean.sample.config.SampleConfig;
import org.springframework.data.ebean.sample.domain.User;
import org.springframework.data.ebean.sample.domain.UserInfo;
import org.springframework.data.ebean.sample.domain.UserRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertEquals;

/**
 * @author Xuegui Yuan
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SampleConfig.class)
public class EbeanQueryChannelServiceIntegrationTests {
  // Test fixture
  User user;
  @Autowired
  private EbeanQueryChannelService ebeanQueryChannelService;
  @Autowired
  private UserRepository repository;

  @Test
  public void createSqlQueryMappingColumns() {
    user = new User("Xuegui", "Yuan", "yuanxuegui@163.com");
    user.setAge(29);
    user = repository.save(user);
    String sql1 = "select first_name, last_name, email_address from user where last_name= :lastName";
    String sql2 = "select first_name as firstName, last_name as lastName, email_address as emailAddress from user where last_name= :lastName";
    Map<String, String> columnsMapping = Maps.newHashMap();
    columnsMapping.put("first_name", "firstName");
    columnsMapping.put("last_name", "lastName");

    Query<UserInfo> query1 = ebeanQueryChannelService.createSqlQuery(UserInfo.class,
        sql1);
    Query<UserInfo> query2 = ebeanQueryChannelService.createSqlQuery(UserInfo.class,
        sql2);
    Query<UserInfo> query3 = ebeanQueryChannelService.createSqlQueryMappingColumns(UserInfo.class,
        sql1, columnsMapping);

    query1.setParameter("lastName", "Yuan");
    query2.setParameter("lastName", "Yuan");
    query3.setParameter("lastName", "Yuan");
    UserInfo userInfo1 = query1.findOne();
    UserInfo userInfo2 = query2.findOne();
    UserInfo userInfo3 = query3.findOne();
    assertEquals("Xuegui", userInfo1.getFirstName());
    assertEquals("yuanxuegui@163.com", userInfo1.getEmailAddress());
    assertEquals("Xuegui", userInfo2.getFirstName());
    assertEquals("yuanxuegui@163.com", userInfo2.getEmailAddress());
    assertEquals("Xuegui", userInfo3.getFirstName());
    assertEquals("yuanxuegui@163.com", userInfo3.getEmailAddress());
  }

  @Test
  public void createNamedQuery() {
    UserInfo userInfo = ebeanQueryChannelService.createNamedQuery(UserInfo.class,
        "userInfoByEmail").setParameter("emailAddress",
        "yuanxuegui@163.com").findOne();
    assertEquals("Xuegui", userInfo.getFirstName());
    assertEquals("yuanxuegui@163.com", userInfo.getEmailAddress());
  }

//  @Test
//  public void createNamedQueryWhere() {
//    UserInfo userInfo = ebeanQueryChannelService.createNamedQuery(UserInfo.class,
//        "userInfo").where()
//        .eq("emailAddress", "yuanxuegui@163.com").findUnique();
//    assertEquals("Xuegui", userInfo.getFirstName());
//    assertEquals("yuanxuegui@163.com", userInfo.getEmailAddress());
//  }

}
@@ -1132,36 +1132,48 @@
 er(%22
-Xuegui%22, %22Yuan%22, %22yuanxuegui
+QueryChannel%22, %22Test%22, %22testquerychannel
 @163
@@ -2017,36 +2017,36 @@
 er(%22lastName%22, %22
-Yuan
+Test
 %22);%0D%0A    query2.
@@ -2063,36 +2063,36 @@
 er(%22lastName%22, %22
-Yuan
+Test
 %22);%0D%0A    query3.
@@ -2117,20 +2117,20 @@
 Name%22, %22
-Yuan
+Test
 %22);%0D%0A   
@@ -2268,38 +2268,44 @@
   assertEquals(%22
-Xuegui
+QueryChannel
 %22, userInfo1.get
@@ -2333,34 +2333,40 @@
 sertEquals(%22
-yuanxuegui
+testquerychannel
 @163.com%22, u
@@ -2405,38 +2405,44 @@
   assertEquals(%22
-Xuegui
+QueryChannel
 %22, userInfo2.get
@@ -2470,34 +2470,40 @@
 sertEquals(%22
-yuanxuegui
+testquerychannel
 @163.com%22, u
@@ -2542,38 +2542,44 @@
   assertEquals(%22
-Xuegui
+QueryChannel
 %22, userInfo3.get
@@ -2607,34 +2607,40 @@
 sertEquals(%22
-yuanxuegui
+testquerychannel
 @163.com%22, u
@@ -2870,26 +2870,32 @@
        %22
-yuanxuegui
+testquerychannel
 @163.com
@@ -2919,38 +2919,44 @@
   assertEquals(%22
-Xuegui
+QueryChannel
 %22, userInfo.getF
@@ -2983,34 +2983,40 @@
 sertEquals(%22
-yuanxuegui
+testquerychannel
 @163.com%22, u
@@ -3051,18 +3051,16 @@
 %0A  %7D%0D%0A%0D%0A
-//
   @Test%0D
@@ -3060,18 +3060,16 @@
  @Test%0D%0A
-//
   public
@@ -3101,18 +3101,16 @@
 re() %7B%0D%0A
-//
     User
@@ -3184,18 +3184,16 @@
 class,%0D%0A
-//
         
@@ -3213,18 +3213,16 @@
 here()%0D%0A
-//
         
@@ -3242,26 +3242,32 @@
 ress%22, %22
-yuanxuegui
+testquerychannel
 @163.com
@@ -3280,26 +3280,24 @@
 dUnique();%0D%0A
-//
     assertEq
@@ -3302,22 +3302,28 @@
 Equals(%22
-Xuegui
+QueryChannel
 %22, userI
@@ -3344,18 +3344,16 @@
 me());%0D%0A
-//
     asse
@@ -3366,18 +3366,24 @@
 ls(%22
-yuanxuegui
+testquerychannel
 @163
@@ -3419,18 +3419,16 @@
 ss());%0D%0A
-//
   %7D%0D%0A%0D%0A%7D

fix test bug