package com.paradigmkick.sample.querydsl;

import static com.paradigmkick.sample.querydsl.QCustomer.customer;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasProperty;

import com.mysema.query.jpa.JPQLQueryFactory;

import java.util.Date;

import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import junit.framework.TestCase;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = QuerydslSampleApplication.class)
public class CustomerRepositoryTest extends TestCase {

  @Autowired
  private CustomerRepository customerRepository;

  @Autowired
  private JPQLQueryFactory queryFactory;

  @Test
  public void simpleTest() {
    // given
    customerRepository.save(new Customer("Luke", "Skywalker", new Date(2014, 0, 1)));
    customerRepository.save(new Customer("Anakin", "Skywalker", new Date(2014, 0, 2)));

    // expect
    assertThat(
        queryFactory.from(customer).where(customer.lastName.eq("Skywalker")).list(customer),
        contains(
            hasProperty("firstName", equalTo("Luke")),
            hasProperty("firstName", equalTo("Anakin"))
        )
    );
  }
}
@@ -347,32 +347,181 @@
 ry;%0A
-%0Aimport java.util.Date;%0A
+import com.mysema.query.support.Expressions;%0Aimport com.mysema.query.types.ConstantImpl;%0Aimport com.mysema.query.types.Ops;%0A%0Aimport java.util.Date;%0A%0Aimport org.junit.Ignore;
 %0Aimp
@@ -1582,10 +1582,726 @@
   );%0A  %7D
+%0A%0A  @Ignore // doesn't work :(%0A  @Test%0A  public void averageAgeTest() %7B%0A    // given%0A    customerRepository.save(new Customer(%22Anakin%22, %22Skywalker%22, new Date(1986, 0, 1, 0, 0, 0)));%0A    customerRepository.save(new Customer(%22Luke%22, %22Skywalker%22, new Date(1986, 0, 1, 0, 0, 10)));%0A    Date now = new Date(1986, 0, 1, 0, 0, 20);%0A%0A    // expect%0A    assertThat(%0A        queryFactory.from(customer).singleResult(%0A            Expressions.numberOperation(%0A                Long.class,%0A                Ops.DateTimeOps.DIFF_SECONDS,%0A                ConstantImpl.create(now),%0A                customer.birthDate%0A            ).avg()%0A        ),%0A        equalTo(15L) // average of 20s (age of Anakin) and 10s (age of Luke)%0A    );%0A  %7D
 %0A%7D

Try to implement average customer age query.