diff --git a/src/kundera-cassandra/src/test/java/com/impetus/kundera/query/ResultIteratorEmbeddableTest.java b/src/kundera-cassandra/src/test/java/com/impetus/kundera/query/ResultIteratorEmbeddableTest.java new file mode 100644 index 000000000..3959816ef --- /dev/null +++ b/src/kundera-cassandra/src/test/java/com/impetus/kundera/query/ResultIteratorEmbeddableTest.java @@ -0,0 +1,194 @@ +/** + * Copyright 2013 Impetus Infotech. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.impetus.kundera.query; + + +import java.util.Date; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.UUID; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.impetus.client.crud.compositeType.CassandraCompoundKey; +import com.impetus.client.crud.compositeType.CassandraPrimeUser; +import com.impetus.client.persistence.CassandraCli; +import com.impetus.kundera.metadata.model.KunderaMetadata; + +/** + * @author vivek.mishra + * junit for {@link IResultIterator}. + */ +public class ResultIteratorEmbeddableTest +{ + + + /** The emf. */ + private EntityManagerFactory emf; + + /** The em. */ + private EntityManager em; + + + private static final String PERSISTENCE_UNIT = "composite_pu"; + + + + /** The Constant logger. */ + private static final Logger logger = LoggerFactory.getLogger(ResultIteratorEmbeddableTest.class); + + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception + { + CassandraCli.cassandraSetUp(); + CassandraCli.initClient(); + + KunderaMetadata.INSTANCE.setApplicationMetadata(null); + + emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT); + em = emf.createEntityManager(); + } + + + + @Test + public void testScrollViaCQL3() throws Exception + { + onScroll(); + } + + + private void onScroll() + { + Object p1 = prepareData("1", 10); + Object p2 = prepareData("2", 15); + Object p3 = prepareData("3", 20); + + em.persist(p1); + em.persist(p2); + em.persist(p3); + + em.flush(); + em.clear(); + final String queryWithoutClause = "Select p from CassandraPrimeUser p "; + + assertOnScroll(queryWithoutClause,3); + + final String queryWithClause = "Select p from CassandraPrimeUser p where p.name = vivek"; + + assertOnScroll(queryWithClause,3); + + final String queryWithAndClause = "Select p from CassandraPrimeUser p where p.name = vivek and p.key.userId = 'mevivs2' and p.key.tweetId = 15"; + + assertOnScroll(queryWithAndClause,1); + + final String queryWithLTClause = "Select p from CassandraPrimeUser p where p.name = vivek and p.key.userId = 'mevivs1' and p.key.tweetId < 15"; + + assertOnScroll(queryWithLTClause,1); + + final String queryWithLTGTClause = "Select p from CassandraPrimeUser p where p.name = vivek and p.key.userId = 'mevivs2' and p.key.tweetId > 10 and p.key.tweetId < 20"; + + assertOnScroll(queryWithLTGTClause,1); + + + String queryWithIdClause = "Select p from CassandraPrimeUser p where p.key.userId = 'mevivs2' "; + assertOnScroll(queryWithIdClause,1); + + String queryWithIdEmbeddableKeyClause = "Select p from CassandraPrimeUser p where p.key.userId = 'mevivs2' and p.key.tweetId = 15"; + assertOnScroll(queryWithIdEmbeddableKeyClause,1); + } + + + + private void assertOnScroll(final String queryWithoutClause, int expectedCount) + { + Query query = (Query) em.createQuery(queryWithoutClause, + CassandraPrimeUser.class); + + assertOnFetch(query, 3, expectedCount); + assertOnFetch(query,2,expectedCount); // less records + + assertOnFetch(query,4,expectedCount); // more fetch size than available in db. + assertOnFetch(query,3,expectedCount); // more fetch size than available in db. + + assertOnFetch(query,null,expectedCount); //set to null; + + } + + private void assertOnFetch(Query query, Integer fetchSize, int available) + { + query.setFetchSize(fetchSize); + int counter=0; + Iterator iter = query.iterate(); + + while (iter.hasNext()) + { + Assert.assertNotNull(iter.next()); + counter++; + } + + Assert.assertEquals(counter, fetchSize == null || available < fetchSize?available:fetchSize); + try + { + iter.next(); + Assert.fail(); + } + catch (NoSuchElementException nsex) + { + Assert.assertNotNull(nsex.getMessage()); + } + } + + /** + * CompositeUserDataType + * + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception + { + em.close(); + emf.close(); + + CassandraCli.truncateColumnFamily("CompositeCassandra", "CompositeUser"); + } + + private Object prepareData(String rowkey, int tweetId) + { + UUID timeLineId = UUID.randomUUID(); + Date currentDate = new Date(); + CassandraCompoundKey key = new CassandraCompoundKey("mevivs"+rowkey, tweetId, timeLineId); + CassandraPrimeUser user = new CassandraPrimeUser(key); + user.setTweetBody("my first tweet"); + user.setTweetDate(currentDate); + user.setName("vivek"); + return user; + } + + +} diff --git a/src/kundera-mongo/src/main/java/com/impetus/client/mongodb/query/MongoDBQuery.java b/src/kundera-mongo/src/main/java/com/impetus/client/mongodb/query/MongoDBQuery.java index 429198cce..1bed8cb75 100644 --- a/src/kundera-mongo/src/main/java/com/impetus/client/mongodb/query/MongoDBQuery.java +++ b/src/kundera-mongo/src/main/java/com/impetus/client/mongodb/query/MongoDBQuery.java @@ -147,7 +147,7 @@ public Object getSingleResult() isSingleResult = true; List results = getResultList(); isSingleResult = false; - return results.isEmpty() ? results : results.get(0); + return results.isEmpty() ? null : results.get(0); } @Override diff --git a/src/kundera-mongo/src/test/java/com/impetus/client/crud/PersonMongoTest.java b/src/kundera-mongo/src/test/java/com/impetus/client/crud/PersonMongoTest.java index 46b889627..c14e7d72f 100644 --- a/src/kundera-mongo/src/test/java/com/impetus/client/crud/PersonMongoTest.java +++ b/src/kundera-mongo/src/test/java/com/impetus/client/crud/PersonMongoTest.java @@ -130,6 +130,17 @@ public void onInsertMongo() throws Exception Assert.assertNotNull(results); Assert.assertEquals(2, results.size()); Assert.assertEquals(Month.JAN, results.get(0).getMonth()); + + query = em.createQuery("select p from PersonMongo p"); + query.setMaxResults(1); + PersonMongo result = (PersonMongo)(query.getSingleResult()); + Assert.assertNotNull(result); + Assert.assertEquals(Month.JAN, result.getMonth()); + + query = em.createQuery("select p from PersonMongo p where p.personName = kuldeep"); + result = (PersonMongo)(query.getSingleResult()); + Assert.assertNull(result); + selectIdQuery();