You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rave.apache.org by mf...@apache.org on 2012/12/14 18:39:37 UTC
svn commit: r1421993 [2/2] - in /rave/branches/mongo/rave-components:
rave-core/src/main/java/org/apache/rave/portal/model/
rave-mongodb/src/main/java/org/apache/rave/portal/model/conversion/impl/
rave-mongodb/src/main/java/org/apache/rave/portal/repos...
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/model/conversion/impl/MongoDbWidgetConverterTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/model/conversion/impl/MongoDbWidgetConverterTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/model/conversion/impl/MongoDbWidgetConverterTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/model/conversion/impl/MongoDbWidgetConverterTest.java Fri Dec 14 17:39:24 2012
@@ -0,0 +1,241 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.rave.portal.model.conversion.impl;
+
+import com.google.common.collect.Lists;
+import org.apache.rave.portal.model.*;
+import org.apache.rave.portal.model.impl.*;
+import org.apache.rave.portal.repository.CategoryRepository;
+import org.apache.rave.portal.repository.UserRepository;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Date;
+
+import static org.easymock.EasyMock.createNiceMock;
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+/**
+ * This is a test for the MongoDb Widget Converter
+ */
+public class MongoDbWidgetConverterTest {
+
+ private UserRepository userRepository;
+ private CategoryRepository categoryRepository;
+ private MongoDbWidgetConverter converter;
+ private static final long ID = 1234L;
+ public static final Date DATE = new Date();
+
+ @Before
+ public void setUp() {
+
+ userRepository = createNiceMock(UserRepository.class);
+ categoryRepository = createNiceMock(CategoryRepository.class);
+ converter = new MongoDbWidgetConverter();
+ converter.setUserRepository(userRepository);
+ converter.setCategoryRepository(categoryRepository);
+ }
+
+ @Test
+ public void hydrate_valid() {
+
+ MongoDbWidget dehydrated = new MongoDbWidget();
+
+ dehydrated.setComments(Lists.<WidgetComment>newLinkedList());
+ MongoDbWidgetComment wc = new MongoDbWidgetComment();
+ dehydrated.getComments().add(wc);
+
+ dehydrated.setTags(Lists.<WidgetTag>newLinkedList());
+ MongoDbWidgetTag wt = new MongoDbWidgetTag();
+ dehydrated.getTags().add(wt);
+
+ converter.hydrate(dehydrated);
+
+ assertThat(wc.getUserRepository(), is(sameInstance(userRepository)));
+ assertThat(wt.getUserRepository(), is(sameInstance(userRepository)));
+ assertThat(dehydrated.getCategoryRepository(), is(sameInstance(categoryRepository)));
+ assertThat(dehydrated.getUserRepository(), is(sameInstance(userRepository)));
+
+ }
+
+ @Test
+ public void hydrate_Tags_Null_Comments_Null() {
+ MongoDbWidget dehydrated = new MongoDbWidget();
+ converter.hydrate(dehydrated);
+ assertThat(dehydrated.getCategoryRepository(), is(sameInstance(categoryRepository)));
+ assertThat(dehydrated.getUserRepository(), is(sameInstance(userRepository)));
+ }
+
+ @Test
+ public void hydrate_null() {
+ converter.hydrate(null);
+ assertThat(true, is(true));
+ }
+
+ @Test
+ public void hydrate_NotMongoWidget_NotMongoComments() {
+ WidgetTag widgetTag = new WidgetTagImpl();
+ WidgetComment widgetComment = new WidgetCommentImpl();
+
+ MongoDbWidget widget = new MongoDbWidget();
+ widget.setComments(Arrays.asList(widgetComment));
+ widget.setTags(Arrays.asList(widgetTag));
+
+ converter.hydrate(widget);
+
+ assertTrue(widgetTag instanceof WidgetTagImpl);
+ assertTrue(widgetComment instanceof WidgetCommentImpl);
+ }
+
+ @Test
+ public void convert_widget() {
+
+ MongoDbWidget converted;
+ Widget source = new WidgetImpl(ID);
+
+ User user = new UserImpl(ID);
+ source.setOwner(user);
+ source.setUrl("http://mitre.org");
+ source.setType("type");
+ source.setTitle("title");
+ source.setTitleUrl("http://title.org");
+ source.setThumbnailUrl("http://thumbnail.org");
+ source.setScreenshotUrl("http://screenshot.org");
+ source.setAuthor("author");
+ source.setAuthorEmail("authorEmail");
+ source.setDescription("description");
+ source.setWidgetStatus(WidgetStatus.PREVIEW);
+ source.setComments(Lists.<WidgetComment>newLinkedList());
+ source.setDisableRendering(true);
+ source.setFeatured(true);
+
+ source.setCategories(Lists.<Category>newLinkedList());
+ Category c = new CategoryImpl(ID);
+ source.getCategories().add(c);
+
+ source.setComments(Lists.<WidgetComment>newLinkedList());
+ WidgetComment wc = new WidgetCommentImpl(ID);
+ wc.setUser(user);
+ wc.setCreatedDate(DATE);
+ wc.setLastModifiedDate(DATE);
+ wc.setText("text");
+ wc.setWidgetId(ID);
+ source.getComments().add(wc);
+
+ source.setTags(Lists.<WidgetTag>newLinkedList());
+ WidgetTag wt = new WidgetTagImpl();
+ wt.setUser(user);
+ wt.setCreatedDate(DATE);
+ wt.setWidgetId(ID);
+ Tag tag = new TagImpl();
+ wt.setTag(tag);
+ source.getTags().add(wt);
+
+ source.setRatings(Lists.<WidgetRating>newLinkedList());
+ WidgetRating wr = new WidgetRatingImpl();
+ source.getRatings().add(wr);
+
+ converted = converter.convert(source);
+
+ assertThat(converted.getUrl(), is(equalTo("http://mitre.org")));
+ assertThat(converted.getType(), is(equalTo("type")));
+ assertThat(converted.getTitle(), is(equalTo("title")));
+ assertThat(converted.getTitleUrl(), is(equalTo("http://title.org")));
+ assertThat(converted.getThumbnailUrl(), is(equalTo("http://thumbnail.org")));
+ assertThat(converted.getScreenshotUrl(), is(equalTo("http://screenshot.org")));
+ assertThat(converted.getAuthor(), is(equalTo("author")));
+ assertThat(converted.getAuthorEmail(), is(equalTo("authorEmail")));
+ assertThat(converted.getDescription(), is(equalTo("description")));
+ assertThat(converted.getWidgetStatus(), is(WidgetStatus.PREVIEW));
+ assertNotNull(converted.getComments());
+
+ //Test convertCategories method
+ assertNotNull(converted.getCategoryIds());
+ assertThat(converted.getCategoryIds().get(0), is(equalTo(ID)));
+ assertNull(converted.getCategoryRepository());
+
+ //Test convertComments method
+ assertNotNull(converted.getComments());
+ assertThat(converted.getComments().get(0), is(instanceOf(MongoDbWidgetComment.class)));
+ assertThat(((MongoDbWidgetComment) (converted.getComments().get(0))).getUserId(), is(equalTo(ID)));
+ assertThat(converted.getComments().get(0).getId(), is(equalTo(ID)));
+ assertNull(((MongoDbWidgetComment) (converted.getComments().get(0))).getUserRepository());
+ assertThat(converted.getComments().get(0).getCreatedDate(), is(equalTo(DATE)));
+ assertThat(converted.getComments().get(0).getLastModifiedDate(), is(equalTo(DATE)));
+ assertThat(converted.getComments().get(0).getText(), is(equalTo("text")));
+ assertThat(converted.getComments().get(0).getWidgetId(), is(equalTo(ID)));
+
+ //Test convertTags method
+ assertNotNull(converted.getTags());
+ assertThat(converted.getTags().get(0), is(instanceOf(MongoDbWidgetTag.class)));
+ assertNotNull(converted.getTags().get(0).getTag());
+ assertThat(converted.getTags().get(0).getTag(), is(sameInstance(tag)));
+ assertThat(converted.getTags().get(0).getCreatedDate(), is(equalTo(DATE)));
+ assertThat(((MongoDbWidgetTag) (converted.getTags().get(0))).getUserId(), is(equalTo(ID)));
+ assertThat(converted.getTags().get(0).getWidgetId(), is(equalTo(ID)));
+
+ //Test convertRatings method
+ assertNotNull(converted.getRatings());
+ assertThat(converted.getRatings().get(0), is(equalTo(wr)));
+ assertNotNull(converted.getRatings().get(0).getId());
+ assertThat(converted.getRatings().get(0).getWidgetId(), is(equalTo(ID)));
+
+
+ }
+
+ @Test
+ public void convert_Null() {
+ Widget source = new WidgetImpl();
+ MongoDbWidget converted = converter.convert(source);
+
+ assertNull(converted.getOwnerId());
+ assertNotNull(converted.getCategories());//the getter sets this field
+ assertNotNull(converted.getCategoryIds());//the getter sets this field
+ assertNotNull(converted.getComments());
+ assertNotNull(converted.getTags());
+ assertNotNull(converted.getRatings());
+ }
+
+ @Test
+ public void convert_MongoInstance() {
+ Widget source = new MongoDbWidget();
+ WidgetComment comment = new MongoDbWidgetComment();
+ comment.setUser(new UserImpl());
+ source.setComments(Arrays.asList(comment));
+
+ WidgetTag tag = new MongoDbWidgetTag();
+ tag.setUser(new UserImpl());
+ source.setTags(Arrays.asList(tag));
+
+ WidgetRating rating = new WidgetRatingImpl();
+ rating.setId((long)123);
+ source.setRatings(Arrays.asList(rating));
+
+ Widget converted = converter.convert(source);
+
+ assertTrue(converted.getComments().get(0) instanceof MongoDbWidgetComment);
+ assertTrue(converted.getTags().get(0) instanceof MongoDbWidgetTag);
+ assertThat(converted.getRatings().get(0).getId(), is(equalTo((long)123)));
+ }
+
+
+}
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbApplicationDataRepositoryTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbApplicationDataRepositoryTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbApplicationDataRepositoryTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbApplicationDataRepositoryTest.java Fri Dec 14 17:39:24 2012
@@ -1,3 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.rave.portal.repository.impl;
import org.apache.rave.portal.model.ApplicationData;
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbCategoryRepositoryTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbCategoryRepositoryTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbCategoryRepositoryTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbCategoryRepositoryTest.java Fri Dec 14 17:39:24 2012
@@ -1,3 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.rave.portal.repository.impl;
import org.apache.rave.portal.model.Category;
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbOauthConsumerStoreRepositoryTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbOauthConsumerStoreRepositoryTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbOauthConsumerStoreRepositoryTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbOauthConsumerStoreRepositoryTest.java Fri Dec 14 17:39:24 2012
@@ -1,3 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.rave.portal.repository.impl;
import org.apache.rave.portal.model.OAuthConsumerStore;
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPageLayoutRepositoryTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPageLayoutRepositoryTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPageLayoutRepositoryTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPageLayoutRepositoryTest.java Fri Dec 14 17:39:24 2012
@@ -1,3 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.rave.portal.repository.impl;
import org.apache.commons.lang.NotImplementedException;
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPageTemplateRepositoryTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPageTemplateRepositoryTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPageTemplateRepositoryTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPageTemplateRepositoryTest.java Fri Dec 14 17:39:24 2012
@@ -1,3 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.rave.portal.repository.impl;
import org.apache.rave.portal.model.MongoDbPageTemplate;
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPersonRepositoryTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPersonRepositoryTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPersonRepositoryTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPersonRepositoryTest.java Fri Dec 14 17:39:24 2012
@@ -0,0 +1,249 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.rave.portal.repository.impl;
+
+import com.google.common.collect.Lists;
+import org.apache.rave.portal.model.*;
+import org.apache.rave.portal.model.conversion.HydratingConverterFactory;
+import org.apache.rave.portal.model.impl.*;
+import org.apache.rave.portal.repository.MongoPageOperations;
+import org.apache.rave.portal.repository.MongoUserOperations;
+import org.apache.rave.portal.repository.MongoWidgetOperations;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.springframework.data.mongodb.core.MongoOperations;
+import org.springframework.data.mongodb.core.query.Query;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Random;
+
+import static org.easymock.EasyMock.*;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.*;
+import static org.springframework.data.mongodb.core.query.Criteria.where;
+import static org.springframework.data.mongodb.core.query.Query.query;
+
+/**
+ * Test for MongoDb Person Repository class
+ */
+public class MongoDbPersonRepositoryTest {
+
+ private MongoUserOperations template;
+ private MongoPageOperations pageTemplate;
+ private MongoWidgetOperations widgetOperations;
+ private MongoDbPersonRepository repo;
+
+ @Before
+ public void setUp() {
+ template = createMock(MongoUserOperations.class);
+ pageTemplate = createMock(MongoPageOperations.class);
+ widgetOperations = createMock(MongoWidgetOperations.class);
+ repo = new MongoDbPersonRepository();
+ repo.setTemplate(template);
+ repo.setPageTemplate(pageTemplate);
+ repo.setWidgetOperations(widgetOperations);
+ }
+
+ @Test
+ public void findFriends_username() {
+ String username = "username";
+ MongoDbUser user = new MongoDbUser(1234L);
+ user.setUsername(username);
+ List<MongoDbPersonAssociation> friends = Lists.newLinkedList();
+ MongoDbPersonAssociation friend1 = new MongoDbPersonAssociation(1111L, FriendRequestStatus.ACCEPTED, MongoDbPersonAssociation.Direction.INCOMING);
+ friends.add(friend1);
+ MongoDbUser friend_1 = new MongoDbUser(1111L);
+ user.setFriends(friends);
+
+ expect(template.findOne(isA(Query.class))).andReturn(user);
+ expect(template.get(1111L)).andReturn(friend_1);
+ replay(template);
+ List<Person> results = repo.findFriends(username);
+ assertNotNull(results.get(0));
+ assertThat(results.size(), is(equalTo(1)));
+
+ }
+
+
+ @Test
+ public void findFriends_appId() {
+ String username = "username";
+ String appId = "www.test.com";
+ MongoDbUser user = new MongoDbUser(1111L);
+ user.setUsername(username);
+ List<MongoDbPersonAssociation> friends = Lists.newLinkedList();
+ MongoDbPersonAssociation friend = new MongoDbPersonAssociation(2222L, FriendRequestStatus.ACCEPTED, MongoDbPersonAssociation.Direction.INCOMING);
+ friends.add(friend);
+ user.setFriends(friends);
+
+ Widget w = new WidgetImpl();
+ w.setUrl(appId);
+ Page page = new PageImpl();
+ page.setOwner(user);
+
+ List<Region> regions = Lists.newLinkedList();
+ Region r = new RegionImpl();
+ RegionWidget rw = new RegionWidgetImpl();
+ List<RegionWidget> regionWidgets = Lists.newLinkedList();
+ regionWidgets.add(rw);
+ r.setRegionWidgets(regionWidgets);
+ regions.add(r);
+ page.setRegions(regions);
+
+ List<Page> pages = Lists.newLinkedList();
+ pages.add(page);
+ List<Long> ids = Lists.newArrayList();
+ ids.add(2222L);
+
+ expect(template.findOne(isA(Query.class))).andReturn(user);
+ replay(template);
+ expect(widgetOperations.findOne(isA(Query.class))).andReturn(w);
+ replay(widgetOperations);
+ expect(pageTemplate.find(query(where("ownerId").in(ids).and("regions").elemMatch(where("regionWidgets").elemMatch(where("widgetId").is(w.getId())))))).andReturn(pages);
+ replay(pageTemplate);
+
+ List<Person> results = repo.findFriends(username, appId);
+ assertThat(results.get(0).getUsername(), is(equalTo(username)));
+ assertThat(results.size(), is(equalTo(1)));
+
+ }
+
+ @Test
+ public void findFriendsWithFriend() {
+ String username = "follower";
+ String friendUsername = "followed";
+ MongoDbUser follower = new MongoDbUser(1111L);
+ MongoDbUser followed = new MongoDbUser(2222L);
+ MongoDbPersonAssociation friend = new MongoDbPersonAssociation(3333L, FriendRequestStatus.ACCEPTED, MongoDbPersonAssociation.Direction.INCOMING);
+ MongoDbPersonAssociation friend2 = new MongoDbPersonAssociation(4444L, FriendRequestStatus.ACCEPTED, MongoDbPersonAssociation.Direction.INCOMING);
+ List<MongoDbPersonAssociation> friends_follower = Lists.newLinkedList();
+ List<MongoDbPersonAssociation> friends_followed = Lists.newLinkedList();
+
+ friends_follower.add(friend);
+ friends_followed.add(friend);
+ friends_followed.add(friend2);
+ follower.setFriends(friends_follower);
+ followed.setFriends(friends_followed);
+
+ expect(template.findOne(isA(Query.class))).andReturn(follower);
+ expect(template.findOne(isA(Query.class))).andReturn(followed);
+ expect(template.get(3333L)).andReturn(follower);
+ replay(template);
+
+ List<Person> results = repo.findFriendsWithFriend(username, friendUsername);
+ assertThat(results.size(), is(equalTo(1)));
+
+ }
+
+ @Test
+ public void addFriend() {
+ String username = "Carol";
+ String friendUsername = "Amy";
+ MongoDbUser carol = new MongoDbUser(1111L);
+ MongoDbUser amy = new MongoDbUser(2222L);
+
+ List<MongoDbPersonAssociation> carols_friends = Lists.newArrayList();
+ MongoDbPersonAssociation friendC = new MongoDbPersonAssociation();
+ carols_friends.add(friendC);
+ List<MongoDbPersonAssociation> amys_friends = Lists.newArrayList();
+ MongoDbPersonAssociation friendA = new MongoDbPersonAssociation();
+ amys_friends.add(friendA);
+ carol.setFriends(carols_friends);
+ amy.setFriends(amys_friends);
+
+ expect(template.findOne(isA(Query.class))).andReturn(carol);
+ expect(template.findOne(isA(Query.class))).andReturn(amy);
+ expect(template.save(isA(MongoDbUser.class))).andReturn(amy);
+ expect(template.save(isA(MongoDbUser.class))).andReturn(carol);
+ replay(template);
+
+ boolean result = repo.addFriend(friendUsername, username);
+ assertTrue(result);
+ }
+
+ @Test
+ public void findFriendsAndRequests() {
+ HashMap<String, List<Person>> results;
+ String username = "username";
+ MongoDbUser user = new MongoDbUser();
+
+ List<MongoDbPersonAssociation> friends = Lists.newArrayList();
+ MongoDbPersonAssociation friend_accepted = new MongoDbPersonAssociation(1111L, FriendRequestStatus.ACCEPTED, MongoDbPersonAssociation.Direction.INCOMING);
+ MongoDbPersonAssociation friend_sent = new MongoDbPersonAssociation(2222L, FriendRequestStatus.SENT, MongoDbPersonAssociation.Direction.INCOMING);
+ MongoDbPersonAssociation friend_received = new MongoDbPersonAssociation(3333L, FriendRequestStatus.RECEIVED, MongoDbPersonAssociation.Direction.INCOMING);
+ MongoDbPersonAssociation friend_received2 = new MongoDbPersonAssociation(4444L, FriendRequestStatus.RECEIVED, MongoDbPersonAssociation.Direction.INCOMING);
+ MongoDbUser friend_a = new MongoDbUser(1111L);
+ MongoDbUser friend_s = new MongoDbUser(2222L);
+ MongoDbUser friend_r = new MongoDbUser(3333L);
+ MongoDbUser friend_r2 = new MongoDbUser(4444L);
+
+ friends.add(friend_sent);
+ friends.add(friend_accepted);
+ friends.add(friend_received);
+ friends.add(friend_received2);
+ user.setFriends(friends);
+
+ expect(template.findOne(isA(Query.class))).andReturn(user);
+ expect(template.get(1111L)).andReturn(friend_a);
+ expect(template.findOne(isA(Query.class))).andReturn(user);
+ expect(template.get(2222L)).andReturn(friend_s);
+ expect(template.findOne(isA(Query.class))).andReturn(user);
+ expect(template.get(3333L)).andReturn(friend_r);
+ expect(template.get(4444L)).andReturn(friend_r2);
+ replay(template);
+
+ results = repo.findFriendsAndRequests(username);
+ assertNotNull(results);
+ assertThat(results.size(), equalTo(3));
+ assertThat(results.get("received").size(), is(equalTo(2)));
+ }
+
+ @Test
+ public void acceptFriendRequest() {
+ String username = "username";
+ String friendUsername = "friendUsername";
+ MongoDbUser carol = new MongoDbUser(1111L);
+ MongoDbUser amy = new MongoDbUser(2222L);
+
+ List<MongoDbPersonAssociation> carols_friends = Lists.newArrayList();
+ MongoDbPersonAssociation friendA = new MongoDbPersonAssociation(1111L, FriendRequestStatus.SENT, MongoDbPersonAssociation.Direction.OUTGOING);
+ carols_friends.add(friendA);
+ List<MongoDbPersonAssociation> amys_friends = Lists.newArrayList();
+ MongoDbPersonAssociation friendC = new MongoDbPersonAssociation(2222L, FriendRequestStatus.RECEIVED, MongoDbPersonAssociation.Direction.INCOMING);
+
+ amys_friends.add(friendC);
+ carol.setFriends(carols_friends);
+ amy.setFriends(amys_friends);
+
+ expect(template.findOne(isA(Query.class))).andReturn(carol);
+ expect(template.findOne(isA(Query.class))).andReturn(amy);
+ expect(template.save(isA(MongoDbUser.class))).andReturn(carol);
+ expect(template.save(isA(MongoDbUser.class))).andReturn(amy);
+ replay(template);
+
+ boolean result = repo.acceptFriendRequest(friendUsername, username);
+ assertTrue(result);
+
+ }
+}
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPortalPreferenceRepositoryTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPortalPreferenceRepositoryTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPortalPreferenceRepositoryTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbPortalPreferenceRepositoryTest.java Fri Dec 14 17:39:24 2012
@@ -1,3 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.rave.portal.repository.impl;
import org.apache.rave.portal.model.MongoDbPortalPreference;
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbRegionRepositoryTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbRegionRepositoryTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbRegionRepositoryTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbRegionRepositoryTest.java Fri Dec 14 17:39:24 2012
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.rave.portal.repository.impl;
+
+import com.google.common.collect.Lists;
+import org.apache.rave.portal.model.Page;
+import org.apache.rave.portal.model.Region;
+import org.apache.rave.portal.model.impl.PageImpl;
+import org.apache.rave.portal.model.impl.RegionImpl;
+import org.apache.rave.portal.repository.MongoPageOperations;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.data.mongodb.core.query.Criteria;
+import org.springframework.data.mongodb.core.query.Query;
+
+import java.util.List;
+
+import static org.easymock.EasyMock.*;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.sameInstance;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Test for MongoDb Region Repository
+ */
+public class MongoDbRegionRepositoryTest {
+
+ private MongoPageOperations template;
+ private MongoDbRegionRepository repo;
+
+ @Before
+ public void setUp(){
+ template = createMock(MongoPageOperations.class);
+ repo = new MongoDbRegionRepository();
+ repo.setTemplate(template);
+
+ }
+
+ @Test
+ public void get(){
+ Page page = new PageImpl(1234L);
+ List<Region> regions = Lists.newArrayList();
+ Region region = new RegionImpl(1111L);
+ regions.add(region);
+ page.setRegions(regions);
+
+ expect( template.findOne(new Query(Criteria.where("regions").elemMatch(Criteria.where("_id").is(1111L))))).andReturn(page);
+ replay(template);
+
+ Region result = repo.get(1111L);
+ assertNotNull(result);
+ assertThat(result, is(equalTo(region)));
+ assertThat(result.getId(), equalTo(1111L));
+
+ }
+
+ @Test
+ public void get_null(){
+ Page page = new PageImpl(1234L);
+ List<Region> regions = Lists.newArrayList();
+ Region region = new RegionImpl(1111L);
+ regions.add(region);
+ page.setRegions(regions);
+
+ expect(template.findOne(new Query(Criteria.where("regions").elemMatch(Criteria.where("_id").is(2222L))))).andReturn(page);
+ replay(template);
+
+ Region result = repo.get(2222L);
+ assertNull(result);
+
+ }
+
+ @Test
+ public void save(){
+ Page page = new PageImpl(1234L);
+ List<Region> regions = Lists.newArrayList();
+ Region item = new RegionImpl(1111L);
+ regions.add(item);
+ page.setRegions(regions);
+
+ expect(template.findOne(new Query(Criteria.where("regions").elemMatch(Criteria.where("_id").is(1111L))))).andReturn(page);
+ expect(template.save(isA(Page.class))).andReturn(page);
+ replay(template);
+
+ Region result = repo.save(item);
+ assertThat(result.getId(), is(equalTo(1111L)));
+ assertThat(result, is(sameInstance(item)));
+
+ }
+
+ @Test
+ public void save_null(){
+ Page page = new PageImpl(1234L);
+ List<Region> regions = Lists.newArrayList();
+ Region item = new RegionImpl();
+ item.setPage(page);
+ regions.add(item);
+ page.setRegions(regions);
+
+ expect(template.get(1234L)).andReturn(page);
+ expect(template.save(isA(Page.class))).andReturn(page);
+ replay(template);
+
+ Region result = repo.save(item);
+ assertNull(result.getId());
+
+ }
+
+ @Test
+ public void delete(){
+ Page page = new PageImpl(1234L);
+ List<Region> regions = Lists.newArrayList();
+ Region item = new RegionImpl(1111L);
+ regions.add(item);
+ page.setRegions(regions);
+
+ expect(template.findOne(new Query(Criteria.where("regions").elemMatch(Criteria.where("_id").is(1111L))))).andReturn(page);
+ expect(template.save(isA(Page.class))).andReturn(page);
+ replay(template);
+
+ repo.delete(item);
+
+ }
+
+ @Test (expected = IllegalStateException.class)
+ public void delete_null(){
+ Page page = new PageImpl(1234L);
+ List<Region> regions = Lists.newArrayList();
+ Region item = new RegionImpl();
+ regions.add(item);
+ page.setRegions(regions);
+
+ repo.delete(item);
+ }
+
+}
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbRegionWidgetRepositoryTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbRegionWidgetRepositoryTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbRegionWidgetRepositoryTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbRegionWidgetRepositoryTest.java Fri Dec 14 17:39:24 2012
@@ -1,3 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.rave.portal.repository.impl;
import org.apache.rave.portal.model.*;
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbUserRepositoryTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbUserRepositoryTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbUserRepositoryTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbUserRepositoryTest.java Fri Dec 14 17:39:24 2012
@@ -1,3 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.rave.portal.repository.impl;
import org.apache.rave.portal.model.MongoDbUser;
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetCommentRepositoryTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetCommentRepositoryTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetCommentRepositoryTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetCommentRepositoryTest.java Fri Dec 14 17:39:24 2012
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.rave.portal.repository.impl;
+
+import com.google.common.collect.Lists;
+import org.apache.rave.portal.model.User;
+import org.apache.rave.portal.model.Widget;
+import org.apache.rave.portal.model.WidgetComment;
+import org.apache.rave.portal.model.impl.UserImpl;
+import org.apache.rave.portal.model.impl.WidgetCommentImpl;
+import org.apache.rave.portal.model.impl.WidgetImpl;
+import org.apache.rave.portal.repository.MongoWidgetOperations;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.easymock.EasyMock.*;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.springframework.data.mongodb.core.query.Criteria.where;
+import static org.springframework.data.mongodb.core.query.Query.query;
+
+/**
+ * Test for MongoDb Widget Comment Repository class
+ */
+public class MongoDbWidgetCommentRepositoryTest {
+
+ private MongoWidgetOperations template;
+ private MongoDbWidgetCommentRepository repo;
+
+ @Before
+ public void setUp(){
+ template = createMock(MongoWidgetOperations.class);
+ repo = new MongoDbWidgetCommentRepository();
+ repo.setTemplate(template);
+
+ }
+
+ @Test
+ public void deleteAll(){
+ Long userId = 1234L;
+ List<Widget> widgets = Lists.newArrayList();
+ Widget w = new WidgetImpl();
+ List<WidgetComment> comments = Lists.newArrayList();
+ WidgetComment wc1 = new WidgetCommentImpl();
+ WidgetComment wc2 = new WidgetCommentImpl();
+ User user = new UserImpl(1234L);
+ wc1.setUser(user);
+ wc2.setUser(user);
+ comments.add(wc1);
+ comments.add(wc2);
+ w.setComments(comments);
+ widgets.add(w);
+
+ expect(template.find(query(where("comments").elemMatch(where("userId").is(userId))))).andReturn(widgets);
+ expect(template.save(isA(Widget.class))).andReturn(w);
+ expect(template.find(query(where("comments").elemMatch(where("userId").is(userId))))).andReturn(widgets);
+ expect(template.save(isA(Widget.class))).andReturn(w);
+ replay(template);
+
+ int count = repo.deleteAll(userId);
+ assertThat(count, is(equalTo(2)));
+ }
+
+ @Test
+ public void deleteAll_zero(){
+ Long userId = 1234L;
+ List<Widget> widgets = Lists.newArrayList();
+ Widget w = new WidgetImpl();
+ List<WidgetComment> comments = Lists.newArrayList();
+ WidgetComment wc1 = new WidgetCommentImpl();
+ WidgetComment wc2 = new WidgetCommentImpl();
+ User user = new UserImpl(1111L);
+ wc1.setUser(user);
+ wc2.setUser(user);
+ comments.add(wc1);
+ comments.add(wc2);
+ w.setComments(comments);
+ widgets.add(w);
+
+ expect(template.find(query(where("comments").elemMatch(where("userId").is(userId))))).andReturn(widgets);
+ replay(template);
+
+ int count = repo.deleteAll(userId);
+ assertThat(count, is(equalTo(0)));
+
+ }
+
+ @Test
+ public void get(){
+ Long id = 1234L;
+ List<WidgetComment> comments = Lists.newArrayList();
+ Widget widget = new WidgetImpl();
+ WidgetComment wc = new WidgetCommentImpl(id);
+ comments.add(wc);
+ widget.setComments(comments);
+
+ expect(template.findOne(query(where("comments").elemMatch(where("_id").is(id))))).andReturn(widget);
+ replay(template);
+
+ WidgetComment result = repo.get(id);
+ assertThat(result.getId(), is(equalTo(id)));
+ }
+
+ @Test
+ public void get_null(){
+ Long id = 1234L;
+ List<WidgetComment> comments = Lists.newArrayList();
+ Widget widget = new WidgetImpl();
+ WidgetComment wc = new WidgetCommentImpl(1111L);
+ comments.add(wc);
+ widget.setComments(comments);
+
+ expect(template.findOne(query(where("comments").elemMatch(where("_id").is(id))))).andReturn(null);
+ replay(template);
+
+ WidgetComment result = repo.get(id);
+ assertNull(result);
+
+ }
+
+}
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetRatingRepositoryTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetRatingRepositoryTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetRatingRepositoryTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetRatingRepositoryTest.java Fri Dec 14 17:39:24 2012
@@ -1,3 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.rave.portal.repository.impl;
import org.apache.rave.portal.model.Widget;
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetRepositoryTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetRepositoryTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetRepositoryTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetRepositoryTest.java Fri Dec 14 17:39:24 2012
@@ -19,94 +19,400 @@
package org.apache.rave.portal.repository.impl;
-import org.junit.Ignore;
-import org.junit.runner.RunWith;
-import org.springframework.test.annotation.DirtiesContext;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import org.apache.rave.portal.model.*;
+import org.apache.rave.portal.model.impl.TagImpl;
+import org.apache.rave.portal.model.impl.UserImpl;
+import org.apache.rave.portal.model.impl.WidgetImpl;
+import org.apache.rave.portal.model.impl.WidgetRatingImpl;
+import org.apache.rave.portal.model.util.WidgetStatistics;
+import org.apache.rave.portal.repository.MongoWidgetOperations;
+import org.apache.rave.portal.repository.StatisticsAggregator;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.data.mongodb.core.query.Criteria;
+import org.springframework.data.mongodb.core.query.Query;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.easymock.EasyMock.*;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.springframework.data.mongodb.core.query.Criteria.where;
+import static org.springframework.data.mongodb.core.query.Query.query;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.easymock.EasyMock.*;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.springframework.data.mongodb.core.query.Criteria.where;
+import static org.springframework.data.mongodb.core.query.Query.query;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.easymock.EasyMock.*;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.springframework.data.mongodb.core.query.Criteria.where;
+import static org.springframework.data.mongodb.core.query.Query.query;
/**
-* Created with IntelliJ IDEA.
-* User: mfranklin
-* Date: 10/14/12
-* Time: 8:14 PM
+* Test for MongoDb Widget Repository class
*/
-@Ignore
-@RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration(locations = {"classpath:test-applicationContext.xml"})
-@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
+
public class MongoDbWidgetRepositoryTest {
-// @Autowired
-// WidgetRepository widgetRepository;
-//
-// @Autowired
-// UserRepository userRepository;
-//
-// @Autowired
-// CategoryRepository categoryRepository;
-//
-// @Autowired
-// Mongo mongo;
-//
-// @After
-// public void tearDown() {
-// mongo.dropDatabase("rave");
-// }
-//
-// @Test
-// public void save_basic() {
-// List<Category> categoryList = Arrays.asList((Category)new CategoryImpl("GOO"), new CategoryImpl("FOO"));
-// categoryRepository.save(categoryList.get(0));
-// categoryRepository.save(categoryList.get(1));
-//
-// User user1 = new MongoDbUser(12345L);
-// user1.setDisplayName("GEORGE DOE");
-// userRepository.save(user1);
-//
-// User user2 = new MongoDbUser(12345L);
-// user2.setDisplayName("JANE DOE");
-// userRepository.save(user2);
-//
-// Widget widget = new WidgetImpl();
-// widget.setUrl("http://localhost:8080/demogadgets/test.xml");
-// widget.setAuthor("mfranklin");
-// widget.setAuthorEmail("developer@apache.org");
-// widget.setDescription("DESCRIPTION");
-// widget.setCategories(categoryList);
-// widget.setOwner(user2);
-// widget.setTitle("TITLE");
-// widget.setTitleUrl("http://title.com");
-// widget.setType("OpenSocial");
-// widget.setWidgetStatus(WidgetStatus.PUBLISHED);
-//
-// WidgetComment widgetComment = new WidgetCommentImpl();
-// widgetComment.setText("BOO HOO");
-// widgetComment.setCreatedDate(new Date());
-// widgetComment.setLastModifiedDate(new Date());
-// widgetComment.setUser(user1);
-// widget.setComments(Arrays.asList(widgetComment));
-//
-// WidgetRating rating = new WidgetRatingImpl();
-// rating.setScore(10);
-// rating.setUserId(user2.getId());
-// widget.setRatings(Arrays.asList(rating));
-//
-// WidgetTag tag = new WidgetTagImpl();
-// tag.setCreatedDate(new Date());
-// tag.setUser(user2);
-// tag.setTag(new TagImpl("TEST"));
-// widget.setTags(Arrays.asList(tag));
-//
-// Widget saved = widgetRepository.save(widget);
-//
-// Widget fromDb = widgetRepository.get(saved.getId());
-// assertThat(fromDb.getOwner().getId(), is(equalTo(widget.getOwner().getId())));
-// assertThat(fromDb.getRatings().get(0).getScore(), is(equalTo(widget.getRatings().get(0).getScore())));
-// assertThat(fromDb.getComments().get(0).getUser().getId(), is(equalTo(widget.getComments().get(0).getUser().getId())));
-// assertThat(fromDb.getComments().get(0).getText(), is(equalTo(widget.getComments().get(0).getText())));
-// assertThat(fromDb.getTags().get(0).getUser().getId(), is(equalTo(widget.getTags().get(0).getUser().getId())));
-// assertThat(fromDb.getTags().get(0).getTag().getKeyword(), is(equalTo(widget.getTags().get(0).getTag().getKeyword())));
-//
-// }
+ private MongoWidgetOperations template;
+ private StatisticsAggregator statsAggregator;
+ private MongoDbWidgetRepository repo;
+
+
+
+ @Before
+ public void setUp(){
+ template = createMock(MongoWidgetOperations.class);
+ statsAggregator = createMock(StatisticsAggregator.class);
+ repo = new MongoDbWidgetRepository();
+ repo.setTemplate(template);
+ repo.setStatsAggregator(statsAggregator);
+
+ }
+
+ @Test
+ public void getAll(){
+ List<Widget> widgets = Lists.newArrayList();
+ Widget w = new WidgetImpl();
+ w.setTitle("B");
+ Widget w2 = new WidgetImpl();
+ w.setTitle("A");
+ widgets.add(w);
+ widgets.add(w2);
+
+ expect(template.find(isA(Query.class))).andReturn(widgets);
+ replay(template);
+
+ List<Widget> result = repo.getAll();
+ assertThat(result.size(), is(equalTo(2)));
+ assertThat(result.get(0), is(equalTo(w)));
+
+ }
+
+ @Test
+ public void getLimitedList(){
+ int offset = 2;
+ int pageSize = 10;
+ List<Widget> widgets = Lists.newArrayList();
+ Widget w = new WidgetImpl();
+ w.setTitle("B");
+ Widget w2 = new WidgetImpl();
+ w.setTitle("A");
+ widgets.add(w);
+ widgets.add(w2);
+
+ expect(template.find(isA(Query.class))).andReturn(widgets);
+ replay(template);
+
+ List<Widget> result = repo.getLimitedList(offset, pageSize);
+ assertThat(result.size(), is(equalTo(2)));
+
+ }
+
+ @Test
+ public void getCountAll(){
+ long count = 0;
+
+ expect(template.count(isA(Query.class))).andReturn(count);
+ replay(template);
+
+ count = repo.getCountAll();
+ assertNotNull(count);
+ }
+
+ @Test
+ public void getByFreeTextSearch(){
+ int offset = 2;
+ int pageSize = 10;
+ String searchTerm = "test";
+ List<Widget> widgets = Lists.newArrayList();
+ Widget w = new WidgetImpl();
+ w.setTitle("A");
+ w.setDescription("test");
+ widgets.add(w);
+
+ expect(template.find(isA(Query.class))).andReturn(widgets);
+ replay(template);
+
+ List<Widget> result = repo.getByFreeTextSearch(searchTerm, offset, pageSize);
+ assertNotNull(result);
+ assertThat(result.size(), is(equalTo(1)));
+ }
+
+ @Test
+ public void getCountFreeTextSearch(){
+ long count = 0;
+ String searchTerm = "test";
+ List<Widget> widgets = Lists.newArrayList();
+ Widget w = new WidgetImpl();
+ Widget w2 = new WidgetImpl();
+ w.setTitle("A");
+ w.setDescription("test");
+ widgets.add(w);
+ widgets.add(w2);
+
+ expect(template.count(isA(Query.class))).andReturn(count);
+ replay(template);
+
+ count = repo.getCountFreeTextSearch(searchTerm);
+ assertNotNull(count);
+
+ }
+
+ @Test
+ public void getByStatus(){
+ int offset = 2;
+ int pageSize = 10;
+ List<Widget> widgets = Lists.newArrayList();
+ Widget w = new WidgetImpl();
+ w.setWidgetStatus(WidgetStatus.PUBLISHED);
+ Widget w2 = new WidgetImpl();
+ w2.setWidgetStatus(WidgetStatus.PREVIEW);
+ w.setTitle("A");
+ w.setDescription("test");
+ widgets.add(w);
+ widgets.add(w2);
+
+ expect(template.find(isA(Query.class))).andReturn(widgets);
+ replay(template);
+
+ List<Widget> result = repo.getByStatus(WidgetStatus.PUBLISHED, offset, pageSize);
+ assertNotNull(result);
+
+ }
+
+ @Test
+ public void getCountByStatus(){
+ long count = 0;
+ List<Widget> widgets = Lists.newArrayList();
+ Widget w = new WidgetImpl();
+ w.setWidgetStatus(WidgetStatus.PUBLISHED);
+ Widget w2 = new WidgetImpl();
+ w2.setWidgetStatus(WidgetStatus.PREVIEW);
+ w.setTitle("A");
+ w.setDescription("test");
+ widgets.add(w);
+ widgets.add(w2);
+
+ expect(template.count(isA(Query.class))).andReturn(count);
+ replay(template);
+
+ count = repo.getCountByStatus(WidgetStatus.PUBLISHED);
+ assertNotNull(count);
+
+ }
+
+ @Test
+ public void getByStatusAndTypeAndFreeTextSearch(){
+ int offset = 2;
+ int pageSize = 10;
+ String type = "type";
+ String searchTerm = "test" ;
+ List<Widget> widgets = Lists.newArrayList();
+ Widget w = new WidgetImpl();
+ w.setWidgetStatus(WidgetStatus.PUBLISHED);
+ Widget w2 = new WidgetImpl();
+ w2.setWidgetStatus(WidgetStatus.PREVIEW);
+ w.setTitle("A");
+ w.setDescription("test");
+ w.setType(type);
+ widgets.add(w);
+ widgets.add(w2);
+
+ expect(template.find(isA(Query.class))).andReturn(widgets);
+ replay(template);
+
+ List<Widget> result = repo.getByStatusAndTypeAndFreeTextSearch(WidgetStatus.PUBLISHED, type, searchTerm, offset, pageSize);
+ assertNotNull(result);
+
+ }
+
+ @Test
+ public void getByStatusAndTypeAndFreeTextSearch_null(){
+ int offset = 2;
+ int pageSize = 10;
+ String type = "type";
+ String searchTerm = "test" ;
+ List<Widget> widgets = Lists.newArrayList();
+ Widget w = new WidgetImpl();
+ Widget w2 = new WidgetImpl();
+ widgets.add(w);
+ widgets.add(w2);
+
+ expect(template.find(isA(Query.class))).andReturn(widgets);
+ replay(template);
+
+ List<Widget> result = repo.getByStatusAndTypeAndFreeTextSearch(WidgetStatus.PUBLISHED, type, searchTerm, offset, pageSize);
+ assertNotNull(result);
+
+ }
+
+ @Test
+ public void getCountByStatusAndTypeAndFreeText(){
+ long count = 0;
+ String type = "type";
+ String searchTerm = "test" ;
+ List<Widget> widgets = Lists.newArrayList();
+ Widget w = new WidgetImpl();
+ w.setWidgetStatus(WidgetStatus.PUBLISHED);
+ Widget w2 = new WidgetImpl();
+ w2.setWidgetStatus(WidgetStatus.PREVIEW);
+ w.setTitle("A");
+ w.setDescription("test");
+ widgets.add(w);
+ widgets.add(w2);
+
+ expect(template.count(isA(Query.class))).andReturn(count);
+ replay(template);
+
+ count = repo.getCountByStatusAndTypeAndFreeText(WidgetStatus.PUBLISHED, type, searchTerm);
+ assertNotNull(count);
+
+ }
+
+ @Test
+ public void getByOwmer(){
+ int offset = 2;
+ int pageSize = 10;
+ User owner = new UserImpl(1234L);
+ List<Widget> widgets = Lists.newArrayList();
+ Widget w = new WidgetImpl();
+ w.setOwner(owner);
+ widgets.add(w);
+
+ expect(template.find(isA(Query.class))).andReturn(widgets);
+ replay(template);
+
+ List<Widget> result = repo.getByOwner(owner, offset, pageSize);
+ assertNotNull(result);
+ }
+
+ @Test
+ public void getcountByOwner(){
+ long count = 0;
+ int offset = 2;
+ int pageSize = 10;
+ User owner = new UserImpl(1234L);
+ List<Widget> widgets = Lists.newArrayList();
+ Widget w = new WidgetImpl();
+ w.setOwner(owner);
+ widgets.add(w);
+
+ expect(template.count(isA(Query.class))).andReturn(count);
+ replay(template);
+
+ count = repo.getCountByOwner(owner, offset, pageSize);
+ assertNotNull(count);
+ }
+
+ @Test
+ public void getByUrl(){
+ String widgetUrl = "www.test.com";
+ Widget widget = new WidgetImpl();
+ widget.setUrl(widgetUrl);
+
+ expect(template.findOne(new Query(where("url").is(widgetUrl)))).andReturn(widget);
+ replay(template);
+
+ Widget result = repo.getByUrl(widgetUrl);
+ assertThat(result, is(equalTo(widget)));
+ assertThat(result.getUrl(), is(equalTo(widgetUrl)));
+
+ }
+
+ @Test
+ public void getWidgetStatistics(){
+ long widget_id = 1111L;
+ long user_id = 2222L;
+
+ WidgetStatistics ws = new WidgetStatistics();
+
+ expect(statsAggregator.getWidgetStatistics(widget_id, user_id)).andReturn(ws);
+ replay(statsAggregator);
+
+ ws = repo.getWidgetStatistics(widget_id, user_id);
+ assertNotNull(ws);
+
+ }
+
+ @Test
+ public void getAllWidgetStatistics(){
+ long user_id = 2222L;
+ Map<Long, WidgetStatistics> ws = Maps.newHashMap();
+
+ expect(statsAggregator.getAllWidgetStatistics(user_id)).andReturn(ws);
+ replay(statsAggregator);
+
+ ws = repo.getAllWidgetStatistics(user_id);
+ assertNotNull(ws);
+
+ }
+
+ @Test
+ public void getUserWidgetRatings(){
+ long userId = 1234L;
+ Map<Long, WidgetRating> wr = Maps.newHashMap();
+ List<Widget> widgets = Lists.newArrayList();
+ List<WidgetRating> widget_ratings = Lists.newArrayList();
+ Widget widget = new WidgetImpl(1111L);
+ WidgetRating rating1 = new WidgetRatingImpl();
+ WidgetRating rating2 = new WidgetRatingImpl();
+ rating1.setUserId(userId);
+ rating2.setUserId(5555L);
+ widget_ratings.add(rating1);
+ widget_ratings.add(rating2);
+ widget.setRatings(widget_ratings);
+ widgets.add(widget);
+ Query q = query(where("ratings").elemMatch(where("userId").is(userId)));
+
+ expect(template.find(q)).andReturn(widgets);
+ replay(template);
+
+ wr = repo.getUsersWidgetRatings(userId);
+ assertNotNull(wr);
+ }
+
+ @Test
+ public void getWidgetByTag(){
+ int offset = 2;
+ int pagesize = 10;
+ String tagKeyword = "test";
+ List<Widget> widgets = Lists.newArrayList();
+ Widget widget = new WidgetImpl();
+ Tag tag = new TagImpl();
+ tag.setKeyword(tagKeyword);
+ expect(template.find(isA(Query.class))).andReturn(widgets);
+ replay(template);
+ }
+
+ public void setTemplate(MongoWidgetOperations template) {
+ this.template = template;
+ }
+
+ public void setStatsAggregator(StatisticsAggregator statsAggregator) {
+ this.statsAggregator = statsAggregator;
+ }
+
}
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetTagRepositoryTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetTagRepositoryTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetTagRepositoryTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MongoDbWidgetTagRepositoryTest.java Fri Dec 14 17:39:24 2012
@@ -1,3 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.rave.portal.repository.impl;
import org.apache.rave.exception.NotSupportedException;
Modified: rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MonogoDbMapReduceStatisticsAggregatorTest.java
URL: http://svn.apache.org/viewvc/rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MonogoDbMapReduceStatisticsAggregatorTest.java?rev=1421993&r1=1421992&r2=1421993&view=diff
==============================================================================
--- rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MonogoDbMapReduceStatisticsAggregatorTest.java (original)
+++ rave/branches/mongo/rave-components/rave-mongodb/src/test/java/org/apache/rave/portal/repository/impl/MonogoDbMapReduceStatisticsAggregatorTest.java Fri Dec 14 17:39:24 2012
@@ -21,8 +21,8 @@ package org.apache.rave.portal.repositor
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
-import org.apache.rave.portal.model.WidgetRatingsMapReduceResult;
-import org.apache.rave.portal.model.WidgetUsersMapReduceResult;
+import org.apache.rave.portal.model.*;
+import org.apache.rave.portal.model.impl.UserImpl;
import org.apache.rave.portal.model.util.WidgetStatistics;
import org.apache.rave.portal.repository.StatisticsAggregator;
import org.apache.rave.portal.repository.util.CollectionNames;
@@ -30,11 +30,12 @@ import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.data.mongodb.core.MongoOperations;
+import org.springframework.data.mongodb.core.mapreduce.MapReduceCounts;
import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions;
+import org.springframework.data.mongodb.core.mapreduce.MapReduceResults;
+import org.springframework.data.mongodb.core.query.Query;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import static org.apache.rave.portal.repository.impl.MongoDbMapReduceStatisticsAggregator.*;
import static org.apache.rave.portal.repository.util.CollectionNames.*;
@@ -42,6 +43,9 @@ import static org.easymock.EasyMock.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.springframework.data.mongodb.core.query.Criteria.where;
+import static org.springframework.data.mongodb.core.query.Query.query;
public class MonogoDbMapReduceStatisticsAggregatorTest {
@@ -55,6 +59,66 @@ public class MonogoDbMapReduceStatistics
}
@Test
+ public void getStatistics_Case0(){
+ long widget_id = 123;
+ long user_id = 321;
+ Query statsQuery = query(where("widgetId").is(widget_id));
+ Query pageQuery = query(where("regions").elemMatch(where("regionWidgets").elemMatch(where("widgetId").is(widget_id))));
+ MapReduceResults<WidgetRatingsMapReduceResult> widgetStats = createMock(MapReduceResults.class);
+ MapReduceCounts mapReduceCounts = createMock(MapReduceCounts.class);
+ List<MongoDbPage> pages = new ArrayList<MongoDbPage>();
+
+ expect(mongoOperations.mapReduce(statsQuery, WIDGET_COLLECTION, RATINGS_MAP, RATINGS_REDUCE, WidgetRatingsMapReduceResult.class)).andReturn(widgetStats);
+ expect(mongoOperations.find(pageQuery, MongoDbPage.class, PAGE_COLLECTION)).andReturn(pages);
+ expect(widgetStats.getCounts()).andReturn(mapReduceCounts);
+ expect(mapReduceCounts.getOutputCount()).andReturn(0);
+ replay(mongoOperations, widgetStats, mapReduceCounts);
+
+ WidgetStatistics result = aggregator.getWidgetStatistics(widget_id, user_id);
+
+ assertTrue(result.getTotalUserCount() == 0);
+ }
+
+ @Test
+ public void getStatistics_Case1(){
+ long widget_id = 123;
+ long user_id = 321;
+ Query statsQuery = query(where("widgetId").is(widget_id));
+ Query pageQuery = query(where("regions").elemMatch(where("regionWidgets").elemMatch(where("widgetId").is(widget_id))));
+ MapReduceResults<WidgetRatingsMapReduceResult> widgetStats = createMock(MapReduceResults.class);
+ MapReduceCounts mapReduceCounts = createMock(MapReduceCounts.class);
+ WidgetRatingsMapReduceResult statsResult = new WidgetRatingsMapReduceResult();
+ List<MongoDbPage> pages = new ArrayList<MongoDbPage>();
+ Iterator iter = createMock(Iterator.class);
+
+ //add page to pages
+ Page page = new MongoDbPage();
+ User owner = new UserImpl();
+ ((UserImpl)owner).setId(234L);
+ page.setOwner(owner);
+ pages.add((MongoDbPage)page);
+
+ //add same id to cover branches
+ Page page_2 = new MongoDbPage();
+ User owner_2 = new UserImpl();
+ ((UserImpl)owner_2).setId(234L);
+ page_2.setOwner(owner_2);
+ pages.add((MongoDbPage)page_2);
+
+ expect(mongoOperations.mapReduce(statsQuery, WIDGET_COLLECTION, RATINGS_MAP, RATINGS_REDUCE, WidgetRatingsMapReduceResult.class)).andReturn(widgetStats);
+ expect(mongoOperations.find(pageQuery, MongoDbPage.class, PAGE_COLLECTION)).andReturn(pages);
+ expect(widgetStats.getCounts()).andReturn(mapReduceCounts);
+ expect(mapReduceCounts.getOutputCount()).andReturn(1);
+ expect(widgetStats.iterator()).andReturn(iter);
+ expect(iter.next()).andReturn(statsResult);
+ replay(mongoOperations, widgetStats, mapReduceCounts, iter);
+
+ WidgetStatistics result = aggregator.getWidgetStatistics(widget_id, user_id);
+
+ assertThat(result.getTotalUserCount(), is(equalTo(1)));
+ }
+
+ @Test
public void getAllStatistics_valid() {
Map<Long, Long> userRatings = getRatingsMap();