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 2011/04/01 18:42:42 UTC
svn commit: r1087796 [20/37] - in /incubator/rave/donations/mitre-osec: ./
conf/ db/ db/data/ db/sequences/ db/tables/ lib/ lib/apache-commons/
lib/apache-taglibs/ lib/build/ lib/build/cobertura/ lib/eclipselink/
lib/freemarker/ lib/google-collections/...
Added: incubator/rave/donations/mitre-osec/test/unit/java/org/mitre/portal/service/GadgetServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/donations/mitre-osec/test/unit/java/org/mitre/portal/service/GadgetServiceTest.java?rev=1087796&view=auto
==============================================================================
--- incubator/rave/donations/mitre-osec/test/unit/java/org/mitre/portal/service/GadgetServiceTest.java (added)
+++ incubator/rave/donations/mitre-osec/test/unit/java/org/mitre/portal/service/GadgetServiceTest.java Fri Apr 1 16:42:22 2011
@@ -0,0 +1,1372 @@
+/*
+ * 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.mitre.portal.service;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.sql.SQLIntegrityConstraintViolationException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+import org.apache.shindig.auth.SecurityToken;
+import org.apache.shindig.common.testing.FakeGadgetToken;
+import org.junit.Before;
+import org.junit.Test;
+import org.mitre.portal.model.*;
+import static org.junit.Assert.*;
+import org.mitre.portal.repository.*;
+import org.mitre.portal.security.SecurityTokenService;
+import org.mitre.portal.service.exception.GadgetAudienceNotFoundException;
+import org.mitre.portal.service.exception.GadgetNotFoundException;
+import org.mitre.portal.service.exception.GadgetUserPrefNotFoundException;
+import org.mitre.portal.service.exception.PersonGadgetNotFoundException;
+import org.mitre.portal.service.exception.UnknownGadgetException;
+import org.springframework.dao.IncorrectResultSizeDataAccessException;
+import static org.easymock.EasyMock.*;
+import org.junit.BeforeClass;
+import org.mitre.portal.model.GadgetComment;
+import org.mitre.portal.model.Person;
+import org.mitre.portal.repository.GadgetAudienceRepository;
+import org.mitre.portal.repository.GadgetAuthorTypeRepository;
+import org.mitre.portal.repository.GadgetCommentRepository;
+import org.mitre.portal.repository.GadgetRatingRepository;
+import org.mitre.portal.repository.GadgetSupportLinkTypeRepository;
+import org.mitre.portal.security.UserService;
+import org.mitre.portal.service.exception.GadgetCommentNotFoundException;
+import org.mitre.portal.service.exception.SecurityTokenException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.security.access.AccessDeniedException;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+import org.springframework.transaction.TransactionSystemException;
+
+/**
+ *
+ * @author ACARLUCCI
+ */
+@ContextConfiguration(locations={"classpath:org/mitre/portal/service-layer-test-config.xml"})
+public class GadgetServiceTest extends AbstractJUnit4SpringContextTests {
+ private final long VALID_GADGET_ID = 10L;
+ private final String VALID_GADGET_URL = "http://dummy.example.com/dummy_gadget.xml";
+ private final String VALID_CONTAINER_NAME = "default";
+ private final long VALID_GADGET_USER_PREF_ID = 1L;
+ private final Long VALID_PERSON_GADGET_ID = 10L;
+ private final String VALID_USER_ID = "100";
+ private final String VALID_GADGET_TITLE = "My Links";
+ private final String VALID_GADGET_AUTHOR = "CI&T";
+ private final String INVALID_GADGET_AUTHOR = "Venkat M";
+ private final String VALID_GADGET_AUTHOR_EMAIL = "acarlucci@example.com";
+ private final String VALID_GADGET_THUMBNAIL = "http://content.example.com/mymii_v2/gadgets/images/repository/thumbnails/mylinks.png";
+ private final String VALID_GADGET_SCREENSHOT ="http://content.example.com/mymii_v2/gadgets/images/repository/screenshots/mylinks_screenshot.png";
+ private final String VALID_QUERY_STRING = "search";
+ private final Long VALID_GADGET_HEIGHT = 0L;
+ private final Boolean VALID_GADGET_SCROLLING = false;
+ private static String VALID_SHINDIG_METADATA_RESPONSE;
+ private static String VALID_SHINDIG_METADATA_NOTFOUND_RESPONSE;
+ private static String VALID_SHINDIG_METADATA_EMPTY_RESPONSE;
+ private static String VALID_SHINDIG_METADATA_MISSING_FIELDS_RESPONSE;
+ private static String VALID_SHINDIG_METADATA_INVALID_URLS_RESPONSE;
+ private static String VALID_SHINDIG_METADATA_NO_PREFS_RESPONSE;
+ private final Long VALID_GADGET_RATING_ID = 1L;
+ private final Long INVALID_GADGET_RATING_ID = -1L;
+ private final Long VALID_GADGET_RATING = 5L;
+ private final Long VALID_GADGET_AUDIENCE_ID = 1L;
+ private final Long VALID_GADGET_COMMENT_ID = 123L;
+ private final String VALID_GADGET_VERSION = "1.0";
+
+ private Person validUser;
+ private Person invalidUser;
+ private URL validGadgetUrl;
+ private URL invalidGadgetUrl;
+ private Container container;
+ private FakeGadgetToken securityToken;
+
+ private final long INVALID_GADGET_ID = -999L;
+ private final long INVALID_GADGET_USER_PREF_ID = -999L;
+ private final long INVALID_PERSON_GADGET_ID = -999L;
+ private final String INVALID_USER_ID = "00000";
+ private final String INVALID_GADGET_URL = "http://dummy.example.com/bad_gadget_url";
+ private final long INVALID_GADGET_AUDIENCE_ID = -29292L;
+ private final Long INVALID_GADGET_COMMENT_ID = -999L;
+
+ @Autowired
+ private GadgetService gadgetService;
+ @Autowired
+ private GadgetRepository gadgetRepository;
+ @Autowired
+ private GadgetUserPrefRepository gadgetUserPrefRepository;
+ @Autowired
+ private GadgetSupportLinkTypeRepository gadgetSupportLinkTypeRepository;
+ @Autowired
+ private GadgetAuthorTypeRepository gadgetAuthorTypeRepository;
+ @Autowired
+ private GadgetAudienceRepository gadgetAudienceRepository;
+ @Autowired
+ private PersonGadgetRepository personGadgetRepository;
+ @Autowired
+ private GadgetCommentRepository gadgetCommentRepository;
+ @Autowired
+ private UserService userService;
+ @Autowired
+ private GadgetRatingRepository ratingRepository;
+ @Autowired
+ private SecurityRoleRepository securityRoleRepository;
+ @Autowired
+ private SecurityUserRoleRepository securityUserRoleRepository;
+
+ @Autowired
+ @Qualifier(value="mockShindigService")
+ private ShindigService shindigService;
+ @Autowired
+ @Qualifier(value="mockSecurityTokenService")
+ private SecurityTokenService securityTokenService;
+
+ private static final String VALID_RESPONSE_DATA_FILE = "build/tests/org/mitre/portal/service/testdata/shindig_metadata_response.json";
+ private static final String VALID_NOT_FOUND_RESPONSE_DATA_FILE = "build/tests/org/mitre/portal/service/testdata/shindig_metadata_notfound_response.json";
+ private static final String VALID_EMPTY_GADGETS_RESPONSE_DATA_FILE = "build/tests/org/mitre/portal/service/testdata/shindig_metadata_empty_gadgets_response.json";
+ private static final String VALID_MISSING_FIELDS_RESPONSE_DATA_FILE = "build/tests/org/mitre/portal/service/testdata/shindig_metadata_missing_fields_response.json";
+ private static final String VALID_RESPONSE_INVALID_URLS_DATA_FILE = "build/tests/org/mitre/portal/service/testdata/shindig_metadata_malformed_urls_response.json";
+ private static final String VALID_RESPONSE_NO_PREFS_DATA_FILE = "build/tests/org/mitre/portal/service/testdata/shindig_metadata_response_no_prefs.json";
+
+ @BeforeClass
+ public static void initClass() throws FileNotFoundException, IOException {
+ // read in the test response data from the file
+ File f = new File(VALID_RESPONSE_DATA_FILE);
+ StringBuilder sb = new StringBuilder();
+ BufferedReader reader = new BufferedReader(new FileReader(f));
+ String text = null;
+ while ((text = reader.readLine()) != null) {
+ sb.append(text);
+ }
+ reader.close();
+ VALID_SHINDIG_METADATA_RESPONSE = sb.toString();
+
+ f = new File(VALID_NOT_FOUND_RESPONSE_DATA_FILE);
+ sb = new StringBuilder();
+ reader = new BufferedReader(new FileReader(f));
+ text = null;
+ while ((text = reader.readLine()) != null) {
+ sb.append(text);
+ }
+ reader.close();
+ VALID_SHINDIG_METADATA_NOTFOUND_RESPONSE = sb.toString();
+
+ f = new File(VALID_EMPTY_GADGETS_RESPONSE_DATA_FILE);
+ sb = new StringBuilder();
+ reader = new BufferedReader(new FileReader(f));
+ text = null;
+ while ((text = reader.readLine()) != null) {
+ sb.append(text);
+ }
+ reader.close();
+ VALID_SHINDIG_METADATA_EMPTY_RESPONSE = sb.toString();
+
+ f = new File(VALID_MISSING_FIELDS_RESPONSE_DATA_FILE);
+ sb = new StringBuilder();
+ reader = new BufferedReader(new FileReader(f));
+ text = null;
+ while ((text = reader.readLine()) != null) {
+ sb.append(text);
+ }
+ reader.close();
+ VALID_SHINDIG_METADATA_MISSING_FIELDS_RESPONSE = sb.toString();
+
+ f = new File(VALID_RESPONSE_INVALID_URLS_DATA_FILE);
+ sb = new StringBuilder();
+ reader = new BufferedReader(new FileReader(f));
+ text = null;
+ while ((text = reader.readLine()) != null) {
+ sb.append(text);
+ }
+ reader.close();
+ VALID_SHINDIG_METADATA_INVALID_URLS_RESPONSE = sb.toString();
+
+ f = new File(VALID_RESPONSE_NO_PREFS_DATA_FILE);
+ sb = new StringBuilder();
+ reader = new BufferedReader(new FileReader(f));
+ text = null;
+ while ((text = reader.readLine()) != null) {
+ sb.append(text);
+ }
+ reader.close();
+ VALID_SHINDIG_METADATA_NO_PREFS_RESPONSE = sb.toString();
+ }
+
+ @Before
+ public void setup() throws MalformedURLException {
+ validUser = new Person();
+ validUser.setUserId(VALID_USER_ID);
+ invalidUser = new Person();
+ invalidUser.setUserId(INVALID_USER_ID);
+
+ validGadgetUrl = new URL(VALID_GADGET_URL);
+ invalidGadgetUrl = new URL(INVALID_GADGET_URL);
+ container = new Container();
+ container.setContainerId(1L);
+ container.setName("default");
+
+ securityToken = new FakeGadgetToken();
+ securityToken.setModuleId(VALID_PERSON_GADGET_ID.intValue());
+
+ // reset our mock objects between tests
+ reset(shindigService);
+ reset(securityTokenService);
+ reset(gadgetRepository);
+ reset(gadgetUserPrefRepository);
+ reset(gadgetSupportLinkTypeRepository);
+ reset(gadgetAuthorTypeRepository);
+ reset(gadgetAudienceRepository);
+ reset(personGadgetRepository);
+ reset(userService);
+ reset(ratingRepository);
+ reset(gadgetCommentRepository);
+ reset(securityRoleRepository);
+ reset(securityUserRoleRepository);
+ // reset the security context to no roles by default to ensure
+ // all security related methods are tested properly
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {"DUMMY"});
+ }
+
+ @Test
+ public void getGadget_validId_validGadget() throws GadgetNotFoundException {
+ expect(gadgetRepository.get(VALID_GADGET_ID)).andReturn(new Gadget());
+ replay(gadgetRepository);
+
+ Gadget g = gadgetService.getGadget(VALID_GADGET_ID);
+ assertNotNull(g);
+ }
+
+ @Test (expected = GadgetNotFoundException.class)
+ public void getGadget_invalidId_exception() throws GadgetNotFoundException {
+ expect(gadgetRepository.get(INVALID_GADGET_ID)).andThrow(new IncorrectResultSizeDataAccessException(1));
+ replay(gadgetRepository);
+
+ Gadget g = gadgetService.getGadget(INVALID_GADGET_ID);
+ }
+
+ @Test
+ public void delete_validGadget() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.REPOSITORY_ADMIN});
+
+ Gadget g = new Gadget(VALID_GADGET_ID);
+ gadgetRepository.delete(g);
+ expectLastCall();
+ replay(gadgetRepository);
+
+ gadgetService.delete(g);
+ verify(gadgetRepository);
+ }
+
+ @Test(expected=AccessDeniedException.class)
+ public void delete_accessDeniedException() {
+ Gadget g = new Gadget(VALID_GADGET_ID);
+ gadgetService.delete(g);
+ }
+
+ @Test
+ public void findGadgetFromUrl_validGadgetUrl_validContainer_validGadget() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ expect(shindigService.invokeShindigMetadataPost(validGadgetUrl, container)).andReturn(VALID_SHINDIG_METADATA_RESPONSE);
+ replay(shindigService);
+
+ Gadget g = gadgetService.findGadgetFromUrl(validGadgetUrl, container);
+ assertNotNull(g);
+ assertEquals(VALID_GADGET_AUTHOR, g.getAuthor());
+ assertEquals(VALID_GADGET_TITLE, g.getTitle());
+ assertEquals(VALID_GADGET_THUMBNAIL, g.getThumbnailUrl().toString());
+ assertEquals(VALID_GADGET_SCREENSHOT, g.getScreenshotUrl().toString());
+ assertEquals(VALID_GADGET_HEIGHT, g.getHeight());
+ assertEquals(VALID_GADGET_SCROLLING, g.isScrolling());
+ }
+
+ @Test
+ public void findGadgetFromUrl_validGadgetUrl_validContainer_emptyString_nullGadget() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ final String emptyString = "";
+ expect(shindigService.invokeShindigMetadataPost(validGadgetUrl, container)).andReturn(emptyString);
+ replay(shindigService);
+
+ Gadget g = gadgetService.findGadgetFromUrl(validGadgetUrl, container);
+ assertNull(g);
+ }
+
+ @Test
+ public void findGadgetFromUrl_emptyGadgetsResponse_nullGadget() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ expect(shindigService.invokeShindigMetadataPost(validGadgetUrl, container)).andReturn(VALID_SHINDIG_METADATA_EMPTY_RESPONSE);
+ replay(shindigService);
+
+ Gadget g = gadgetService.findGadgetFromUrl(validGadgetUrl, container);
+ assertNull(g);
+ }
+
+ @Test
+ public void findGadgetFromUrl_someMissingFields_validGadget() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ expect(shindigService.invokeShindigMetadataPost(validGadgetUrl, container)).andReturn(VALID_SHINDIG_METADATA_MISSING_FIELDS_RESPONSE);
+ replay(shindigService);
+
+ Gadget g = gadgetService.findGadgetFromUrl(validGadgetUrl, container);
+ assertNotNull(g);
+ assertEquals(VALID_GADGET_AUTHOR, g.getAuthor());
+ assertEquals(VALID_GADGET_TITLE, g.getTitle());
+ assertNull(g.getThumbnailUrl());
+ assertNull(g.getScreenshotUrl());
+ assertEquals(0L, g.getHeight().longValue());
+ assertFalse(g.isScrolling());
+ }
+
+ @Test
+ public void findGadgetFromUrl_validGadgetUrl_validContainer_invalidUrls_validGadget() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ expect(shindigService.invokeShindigMetadataPost(validGadgetUrl, container)).andReturn(VALID_SHINDIG_METADATA_INVALID_URLS_RESPONSE);
+ replay(shindigService);
+
+ Gadget g = gadgetService.findGadgetFromUrl(validGadgetUrl, container);
+ assertNotNull(g);
+ assertEquals(VALID_GADGET_AUTHOR, g.getAuthor());
+ assertEquals(VALID_GADGET_TITLE, g.getTitle());
+ assertNull(g.getThumbnailUrl());
+ assertNull(g.getScreenshotUrl());
+ assertEquals(VALID_GADGET_HEIGHT, g.getHeight());
+ assertEquals(VALID_GADGET_SCROLLING, g.isScrolling());
+ }
+
+ @Test
+ public void findGadgetFromUrl_validGadgetUrl_validContainer_noUserPrefs_validGadget() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ expect(shindigService.invokeShindigMetadataPost(validGadgetUrl, container)).andReturn(VALID_SHINDIG_METADATA_NO_PREFS_RESPONSE);
+ replay(shindigService);
+
+ Gadget g = gadgetService.findGadgetFromUrl(validGadgetUrl, container);
+ assertNotNull(g);
+ assertEquals(VALID_GADGET_AUTHOR, g.getAuthor());
+ assertEquals(VALID_GADGET_TITLE, g.getTitle());
+ assertEquals(VALID_GADGET_THUMBNAIL, g.getThumbnailUrl().toString());
+ assertEquals(VALID_GADGET_SCREENSHOT, g.getScreenshotUrl().toString());
+ assertEquals(VALID_GADGET_HEIGHT, g.getHeight());
+ assertEquals(VALID_GADGET_SCROLLING, g.isScrolling());
+ assertNull(g.getGadgetUserPrefList());
+ }
+
+ @Test
+ public void findGadgetFromUrl_invalidGadgetUrl_validContainer_nullGadget() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ expect(shindigService.invokeShindigMetadataPost(invalidGadgetUrl, container)).andReturn(VALID_SHINDIG_METADATA_NOTFOUND_RESPONSE);
+ replay(shindigService);
+
+ Gadget g = gadgetService.findGadgetFromUrl(invalidGadgetUrl, container);
+ assertNull(g);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void findGadgetFromUrl_nullGadgetUrl_validContainer_exception() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ Gadget g = gadgetService.findGadgetFromUrl(null, container);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void findGadgetFromUrl_validGadgetUrl_nullContainer_exception() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ Gadget g = gadgetService.findGadgetFromUrl(validGadgetUrl, null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void findGadgetFromUrl_validGadgetUrl_invalidContainer_exception() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ Container c = new Container();
+ Gadget g = gadgetService.findGadgetFromUrl(validGadgetUrl, c);
+ }
+
+ @Test(expected = AccessDeniedException.class)
+ public void findGadgetFromUrl_accessDeniedException() {
+ Container c = new Container();
+ gadgetService.findGadgetFromUrl(validGadgetUrl, c);
+ }
+
+ @Test
+ public void getGadgetUserPref_validId_validGadgetUserPref() throws GadgetUserPrefNotFoundException {
+ expect(gadgetUserPrefRepository.get(VALID_GADGET_USER_PREF_ID)).andReturn(new GadgetUserPref());
+ replay(gadgetUserPrefRepository);
+
+ GadgetUserPref g = gadgetService.getGadgetUserPref(VALID_GADGET_USER_PREF_ID);
+ assertNotNull(g);
+ }
+
+ @Test (expected = GadgetUserPrefNotFoundException.class)
+ public void getGadgetUserPref_invalidId_exception() throws GadgetUserPrefNotFoundException {
+ expect(gadgetUserPrefRepository.get(INVALID_GADGET_USER_PREF_ID)).andThrow(new IncorrectResultSizeDataAccessException(1));
+ replay(gadgetUserPrefRepository);
+
+ GadgetUserPref g = gadgetService.getGadgetUserPref(INVALID_GADGET_USER_PREF_ID);
+ }
+
+ @Test
+ public void findGadgetUserPrefs_validGadget_validList() {
+ Gadget g = new Gadget(VALID_GADGET_ID);
+ List<GadgetUserPref> expectedList = new ArrayList<GadgetUserPref>();
+ expectedList.add(new GadgetUserPref());
+ expect(gadgetUserPrefRepository.find(g)).andReturn(expectedList);
+ replay(gadgetUserPrefRepository);
+
+ List<GadgetUserPref> list = gadgetService.findGadgetUserPrefs(g);
+ assertNotNull(list);
+ assertFalse(list.isEmpty());
+ }
+
+ @Test
+ public void findGadgetUserPrefs_invalidGadget_emptyList() {
+ Gadget g = new Gadget(INVALID_GADGET_ID);
+ expect(gadgetUserPrefRepository.find(g)).andReturn(new ArrayList<GadgetUserPref>());
+ replay(gadgetUserPrefRepository);
+
+ List<GadgetUserPref> list = gadgetService.findGadgetUserPrefs(g);
+ assertNotNull(list);
+ assertTrue(list.isEmpty());
+ }
+
+ @Test
+ public void getPersonGadget_validId_validPersonGadget() throws PersonGadgetNotFoundException, SecurityTokenException {
+ PersonGadget pg = new PersonGadget();
+ pg.setUserId(VALID_USER_ID);
+
+ expect(userService.getCurrentAuthenticatedUser()).andReturn(validUser);
+ expect(personGadgetRepository.get(VALID_PERSON_GADGET_ID)).andReturn(pg);
+ replay(userService);
+ replay(personGadgetRepository);
+
+ PersonGadget g = gadgetService.getPersonGadget(VALID_PERSON_GADGET_ID);
+ assertNotNull(g);
+ }
+
+ @Test(expected=AccessDeniedException.class)
+ public void getPersonGadget_validId_validPersonGadget_accessDeniedException() throws PersonGadgetNotFoundException, SecurityTokenException {
+ PersonGadget pg = new PersonGadget();
+ pg.setUserId(INVALID_USER_ID);
+
+ expect(personGadgetRepository.get(VALID_PERSON_GADGET_ID)).andReturn(pg);
+ replay(personGadgetRepository);
+
+ gadgetService.getPersonGadget(VALID_PERSON_GADGET_ID);
+ }
+
+ @Test (expected = PersonGadgetNotFoundException.class)
+ public void getPersonGadget_invalidId_notFoundException() throws PersonGadgetNotFoundException, SecurityTokenException {
+ expect(personGadgetRepository.get(INVALID_PERSON_GADGET_ID)).andThrow(new IncorrectResultSizeDataAccessException(1));
+ replay(personGadgetRepository);
+
+ PersonGadget g = gadgetService.getPersonGadget(INVALID_PERSON_GADGET_ID);
+ }
+
+ @Test
+ public void getPersonGadget_validSecurityToken_validPersonGadget() throws PersonGadgetNotFoundException, SecurityTokenException {
+ PersonGadget pg = new PersonGadget();
+ pg.setPersonGadgetId(VALID_PERSON_GADGET_ID);
+ pg.setUserId(VALID_USER_ID);
+
+ expect(userService.getCurrentAuthenticatedUser()).andReturn(validUser);
+ replay(userService);
+ expect(personGadgetRepository.get(VALID_PERSON_GADGET_ID)).andReturn(pg);
+ replay(personGadgetRepository);
+ expect(securityTokenService.getEncryptedEncodedSecurityToken(pg)).andReturn("zzzqqq");
+ replay(securityTokenService);
+
+ PersonGadget g = gadgetService.getPersonGadget(securityToken);
+ assertNotNull(g);
+ }
+
+ @Test(expected=SecurityTokenException.class)
+ public void getPersonGadget_nullSecurityToken_exception() throws PersonGadgetNotFoundException, SecurityTokenException {
+ PersonGadget g = gadgetService.getPersonGadget((SecurityToken)null);
+ }
+
+ @Test(expected=AccessDeniedException.class)
+ public void getPersonGadget_accessDeniedException() throws PersonGadgetNotFoundException, SecurityTokenException {
+ PersonGadget pg = new PersonGadget();
+ pg.setPersonGadgetId(VALID_PERSON_GADGET_ID);
+ pg.setUserId(INVALID_USER_ID);
+
+ expect(personGadgetRepository.get(VALID_PERSON_GADGET_ID)).andReturn(pg);
+ replay(personGadgetRepository);
+ expect(securityTokenService.getEncryptedEncodedSecurityToken(pg)).andReturn("zzzqqq");
+ replay(securityTokenService);
+
+ gadgetService.getPersonGadget(securityToken);
+ }
+
+ @Test
+ public void findPersonGadgets_validGadget_validList() {
+ PersonGadget pg = new PersonGadget();
+ pg.setUserId(VALID_USER_ID);
+
+ List<PersonGadget> expectedList = new ArrayList<PersonGadget>();
+ expectedList.add(pg);
+ expect(personGadgetRepository.find(VALID_USER_ID)).andReturn(expectedList);
+ replay(personGadgetRepository);
+
+ List<PersonGadget> list = gadgetService.findPersonGadgets(validUser);
+ assertNotNull(list);
+ assertFalse(list.isEmpty());
+ }
+
+ @Test(expected=AccessDeniedException.class)
+ public void findPersonGadgets_accessDeniedException() {
+ gadgetService.findPersonGadgets(invalidUser);
+ }
+
+ @Test
+ public void findPersonGadgets_securityTokenException_exception() throws SecurityTokenException {
+ List<PersonGadget> expectedList = new ArrayList<PersonGadget>();
+ PersonGadget pg = new PersonGadget(VALID_PERSON_GADGET_ID);
+ pg.setUserId(VALID_USER_ID);
+ expectedList.add(pg);
+ expect(personGadgetRepository.find(VALID_USER_ID)).andReturn(expectedList);
+ replay(personGadgetRepository);
+
+ expect(securityTokenService.getEncryptedEncodedSecurityToken(pg)).andThrow(new RuntimeException("asdf"));
+ replay(securityTokenService);
+
+ List<PersonGadget> list = gadgetService.findPersonGadgets(validUser);
+ }
+
+ @Test
+ public void save_validGadget() throws GadgetAudienceNotFoundException, UnknownGadgetException {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ Gadget g = new Gadget(VALID_GADGET_ID);
+
+ expect(userService.getCurrentAuthenticatedUser()).andReturn(validUser);
+ replay(userService);
+
+ gadgetRepository.save(g);
+ expectLastCall();
+ replay(gadgetRepository);
+
+ gadgetService.save(g);
+ verify(gadgetRepository);
+ }
+
+ @Test(expected = GadgetAudienceNotFoundException.class)
+ public void save_dirtyGadgetAudience_GadgetAudienceNotFoundException() throws GadgetAudienceNotFoundException, UnknownGadgetException {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ TransactionSystemException e = new TransactionSystemException("error", new SQLIntegrityConstraintViolationException("GADGET_AUDIENCE"));
+
+ Gadget g = new Gadget(VALID_GADGET_ID);
+
+ expect(userService.getCurrentAuthenticatedUser()).andReturn(validUser);
+ replay(userService);
+
+ gadgetRepository.save(g);
+ expectLastCall().andThrow(e);
+ replay(gadgetRepository);
+
+ gadgetService.save(g);
+ verify(gadgetRepository);
+ }
+
+ @Test(expected = UnknownGadgetException.class)
+ public void save_unknownIntegrityConstraintViolation_UnknownGadgetException() throws GadgetAudienceNotFoundException, UnknownGadgetException {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ TransactionSystemException e = new TransactionSystemException("error", new SQLIntegrityConstraintViolationException("some other error"));
+
+ Gadget g = new Gadget(VALID_GADGET_ID);
+
+ expect(userService.getCurrentAuthenticatedUser()).andReturn(validUser);
+ replay(userService);
+
+ gadgetRepository.save(g);
+ expectLastCall().andThrow(e);
+ replay(gadgetRepository);
+
+ gadgetService.save(g);
+ verify(gadgetRepository);
+ }
+
+ @Test(expected = UnknownGadgetException.class)
+ public void save_unknownException_UnknownGadgetException() throws GadgetAudienceNotFoundException, UnknownGadgetException {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ Gadget g = new Gadget(VALID_GADGET_ID);
+
+ expect(userService.getCurrentAuthenticatedUser()).andReturn(validUser);
+ replay(userService);
+
+ gadgetRepository.save(g);
+ expectLastCall().andThrow(new RuntimeException("error"));
+ replay(gadgetRepository);
+
+ gadgetService.save(g);
+ verify(gadgetRepository);
+ }
+
+ @Test
+ public void save_validNewGadget() throws GadgetAudienceNotFoundException, UnknownGadgetException {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ Gadget g = new Gadget();
+
+ expect(userService.getCurrentAuthenticatedUser()).andReturn(validUser);
+ replay(userService);
+
+ gadgetRepository.save(g);
+ expectLastCall();
+ replay(gadgetRepository);
+
+ gadgetService.save(g);
+
+ assertNotNull(g.getCreatedBy());
+ assertNotNull(g.getCreatedDate());
+ }
+
+ @Test(expected=AccessDeniedException.class)
+ public void save_accessDeniedException() throws GadgetAudienceNotFoundException, UnknownGadgetException {
+ Gadget g = new Gadget();
+ gadgetService.save(g);
+ }
+
+ @Test
+ public void updateUserPrefMetaData_validExistingGadget_validUpdatedGadget() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.OFFICIAL_GADGET_DEVELOPER});
+
+ final Long PREF_2_ID = 2L;
+ final String PREF_2_DEFAULT = "default val2";
+ final String PREF_2_DISPLAY = "display name2";
+ final String PREF_2_NAME = "prefname2";
+ final boolean PREF_2_REQUIRED = false;
+
+ List<GadgetUserPref> gupList = new ArrayList<GadgetUserPref>();
+
+ // build existing gadget
+ Gadget existingGadget = new Gadget(VALID_GADGET_ID);
+ GadgetUserPref gup1 = new GadgetUserPref();
+ gup1.setGadgetUserPrefId(1L);
+ gup1.setDataType(GadgetUserPref.UserPrefDataType.STRING.toString());
+ gup1.setDefaultValue("default val");
+ gup1.setDisplayName("display name");
+ gup1.setGadget(existingGadget);
+ gup1.setName("prefname1");
+ gup1.setRequired(false);
+ gupList.add(gup1);
+
+ GadgetUserPref gup2 = new GadgetUserPref();
+ gup2.setGadgetUserPrefId(PREF_2_ID);
+ gup2.setDataType(GadgetUserPref.UserPrefDataType.STRING.toString());
+ gup2.setDefaultValue(PREF_2_DEFAULT);
+ gup2.setDisplayName(PREF_2_DISPLAY);
+ gup2.setGadget(existingGadget);
+ gup2.setName(PREF_2_NAME);
+ gup2.setRequired(PREF_2_REQUIRED);
+ gupList.add(gup2);
+
+ // gadget has two existing prefs
+ existingGadget.setGadgetUserPrefList(gupList);
+
+ // build updated gadget
+ List<GadgetUserPref> newGupList = new ArrayList<GadgetUserPref>();
+ Gadget updatedGadget = new Gadget();
+
+ // this is a new user pref
+ final String PREF_3_DEFAULT = "default val3";
+ final String PREF_3_DISPLAY = "display name3";
+ final String PREF_3_NAME = "prefname3";
+ final boolean PREF_3_REQUIRED = false;
+ GadgetUserPref gup3 = new GadgetUserPref();
+ gup3.setDataType(GadgetUserPref.UserPrefDataType.STRING.toString());
+ gup3.setDefaultValue(PREF_3_DEFAULT);
+ gup3.setDisplayName(PREF_3_DISPLAY);
+ gup3.setGadget(updatedGadget);
+ gup3.setName(PREF_3_NAME);
+ gup3.setRequired(PREF_3_REQUIRED);
+ newGupList.add(gup3);
+
+ // this is one of the existing prefs
+ final String PREF_2_NEW_DISPLAY_NAME = "new updated display";
+ GadgetUserPref gup4 = new GadgetUserPref();
+ gup4.setDataType(GadgetUserPref.UserPrefDataType.STRING.toString());
+ gup4.setDefaultValue("default val2");
+ gup4.setDisplayName(PREF_2_NEW_DISPLAY_NAME);
+ gup4.setGadget(updatedGadget);
+ gup4.setName("prefname2");
+ gup4.setRequired(false);
+ newGupList.add(gup4);
+
+ // updated gadget has one existing pref and one new pref
+ updatedGadget.setGadgetUserPrefList(newGupList);
+
+ // result should be updating of similiar pref (prefname2), adding of new (prefname3), removal of old (prefname1)
+ gadgetService.updateUserPrefMetaData(existingGadget, updatedGadget);
+
+ assertNotNull(existingGadget);
+ assertEquals(2, existingGadget.getGadgetUserPrefList().size());
+
+ boolean validatedPrefName2 = false;
+ boolean validatedPrefName3 = false;
+ for (GadgetUserPref gup : existingGadget.getGadgetUserPrefList()) {
+ if (gup.getName().equals("prefname2")) {
+ assertEquals(PREF_2_ID, gup.getGadgetUserPrefId());
+ assertEquals(PREF_2_DEFAULT, gup.getDefaultValue());
+ assertEquals(PREF_2_NEW_DISPLAY_NAME, gup.getDisplayName());
+ assertEquals(PREF_2_REQUIRED, gup.getRequired());
+ validatedPrefName2 = true;
+ } else if (gup.getName().equals("prefname3")) {
+ assertEquals(PREF_3_DEFAULT, gup.getDefaultValue());
+ assertEquals(PREF_3_DISPLAY, gup.getDisplayName());
+ assertEquals(PREF_3_REQUIRED, gup.getRequired());
+ validatedPrefName3 = true;
+ } else if (gup.getName().equals("prefname1")) {
+ fail("prefname1 was found - it should have been removed");
+ } else {
+ fail("unknown gadget user pref name found: " + gup.getName());
+ }
+ }
+
+ assertTrue("prefname2 found and validated", validatedPrefName2);
+ assertTrue("prefname3 found and validated", validatedPrefName3);
+ }
+
+ @Test(expected=AccessDeniedException.class)
+ public void updateUserPrefMetaData_accessDeniedException() {
+ gadgetService.updateUserPrefMetaData(new Gadget(), new Gadget());
+ }
+
+ @Test
+ public void getAllSupportLinkTypes_validList() {
+ List<GadgetSupportLinkType> expectedList = new ArrayList<GadgetSupportLinkType>();
+ expectedList.add(new GadgetSupportLinkType());
+ expect(gadgetSupportLinkTypeRepository.findAll()).andReturn(expectedList);
+ replay(gadgetSupportLinkTypeRepository);
+
+ List<GadgetSupportLinkType> list = gadgetService.getAllSupportLinkTypes();
+ assertNotNull(list);
+ assertFalse(list.isEmpty());
+ }
+
+ @Test
+ public void getAllAuthorTypes_validList() {
+ List<GadgetAuthorType> expectedList = new ArrayList<GadgetAuthorType>();
+ expectedList.add(new GadgetAuthorType());
+ expect(gadgetAuthorTypeRepository.findAll()).andReturn(expectedList);
+ replay(gadgetAuthorTypeRepository);
+
+ List<GadgetAuthorType> list = gadgetService.getAllAuthorTypes();
+ assertNotNull(list);
+ assertFalse(list.isEmpty());
+ }
+
+ @Test
+ public void getAllAudiences_validList() {
+ List<GadgetAudience> expectedList = new ArrayList<GadgetAudience>();
+ expectedList.add(new GadgetAudience());
+ expect(gadgetAudienceRepository.findAll()).andReturn(expectedList);
+ replay(gadgetAudienceRepository);
+
+ List<GadgetAudience> list = gadgetService.getAllAudiences();
+ assertNotNull(list);
+ assertFalse(list.isEmpty());
+ }
+
+ @Test
+ public void getAllAudiencesAssignedToGadgets_validList() {
+ List<Gadget> gadgetList = new ArrayList<Gadget>();
+ gadgetList.add(new Gadget());
+
+ List<GadgetAudience> expectedList = new ArrayList<GadgetAudience>();
+ GadgetAudience ga = new GadgetAudience();
+ ga.setGadgetList(gadgetList);
+ expectedList.add(ga);
+
+ expect(gadgetAudienceRepository.findAllAssignedToGadgets()).andReturn(expectedList);
+ replay(gadgetAudienceRepository);
+
+ List<GadgetAudience> list = gadgetService.getAllAudiencesAssignedToGadgets();
+ assertNotNull(list);
+ assertFalse(list.isEmpty());
+ for (GadgetAudience a : list) {
+ assertFalse(a.getGadgetList().isEmpty());
+ }
+ }
+
+ @Test
+ public void save_validGadgetAudience() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.APPLICATION_ADMIN});
+
+ String code = "lower";
+ GadgetAudience ga = new GadgetAudience(VALID_GADGET_AUDIENCE_ID);
+ ga.setCode(code);
+
+ gadgetAudienceRepository.save(ga);
+ expectLastCall();
+ replay(gadgetAudienceRepository);
+
+ gadgetService.save(ga);
+ verify(gadgetAudienceRepository);
+
+ // test that code gets converted to uppercase in service method
+ assertEquals(code.toUpperCase(), ga.getCode());
+ }
+
+ @Test
+ public void save_gadgetAudienceNullCode() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.APPLICATION_ADMIN});
+ GadgetAudience ga = new GadgetAudience(VALID_GADGET_AUDIENCE_ID);
+
+ gadgetAudienceRepository.save(ga);
+ expectLastCall();
+ replay(gadgetAudienceRepository);
+
+ gadgetService.save(ga);
+ verify(gadgetAudienceRepository);
+ }
+
+ @Test
+ public void save_nullGadgetAudience() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.APPLICATION_ADMIN});
+ GadgetAudience ga = null;
+
+ gadgetAudienceRepository.save(ga);
+ expectLastCall();
+ replay(gadgetAudienceRepository);
+
+ gadgetService.save(ga);
+ verify(gadgetAudienceRepository);
+ }
+
+ @Test(expected=AccessDeniedException.class)
+ public void save_validGadgetAudience_accessDeniedException() {
+ GadgetAudience ga = new GadgetAudience(VALID_GADGET_AUDIENCE_ID);
+ gadgetService.save(ga);
+ }
+
+ @Test
+ public void delete_validGadgetAudience() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.APPLICATION_ADMIN});
+ GadgetAudience ga = new GadgetAudience(VALID_GADGET_AUDIENCE_ID);
+ gadgetAudienceRepository.delete(ga);
+ expectLastCall();
+ replay(gadgetAudienceRepository);
+
+ gadgetService.delete(ga);
+ verify(gadgetAudienceRepository);
+ }
+
+ @Test(expected=AccessDeniedException.class)
+ public void delete_validGadgetAudience_accessDeniedException() {
+ GadgetAudience ga = new GadgetAudience(VALID_GADGET_AUDIENCE_ID);
+ gadgetService.delete(ga);
+ }
+
+ @Test
+ public void save_validGadgetRating() throws GadgetAudienceNotFoundException, UnknownGadgetException {
+ Gadget gadget = new Gadget(VALID_GADGET_ID);
+ GadgetRating rating = new GadgetRating(VALID_GADGET_ID, gadget, VALID_GADGET_RATING, VALID_USER_ID);
+
+ ratingRepository.save(rating);
+ expectLastCall();
+ replay(gadgetRepository);
+
+ gadgetService.save(rating);
+ verify(gadgetRepository);
+ }
+
+ @Test(expected=AccessDeniedException.class)
+ public void save_validGadgetRating_accessDeniedException() throws GadgetAudienceNotFoundException, UnknownGadgetException {
+ Gadget gadget = new Gadget(VALID_GADGET_ID);
+ GadgetRating rating = new GadgetRating(VALID_GADGET_ID, gadget, VALID_GADGET_RATING, INVALID_USER_ID);
+
+ gadgetService.save(rating);
+ }
+
+ @Test
+ public void findGadgetRating_validGadget() {
+ Gadget gadget = new Gadget(VALID_GADGET_ID);
+
+ GadgetRating expected = new GadgetRating(VALID_GADGET_ID, gadget, VALID_GADGET_RATING, VALID_USER_ID);
+ List<GadgetRating> expectedList = new ArrayList<GadgetRating>();
+ expectedList.add(expected);
+ expect(ratingRepository.find(gadget)).andReturn(expectedList);
+ replay(ratingRepository);
+
+ List<GadgetRating> result = gadgetService.getGadgetRatings(gadget);
+ assertNotNull(result);
+ assertFalse(result.isEmpty());
+ assertEquals(result, expectedList);
+ }
+
+ @Test
+ public void findgetGadgetRating_invalidGadget() {
+ Gadget gadget = new Gadget(INVALID_GADGET_ID);
+ expect(ratingRepository.find(gadget)).andReturn(null);
+ replay(ratingRepository);
+
+ List<GadgetRating> result = gadgetService.getGadgetRatings(gadget);
+ assertNull(result);
+ }
+
+ @Test
+ public void getGadgetRating_validGadgetRatingId() {
+ Gadget gadget = new Gadget(VALID_GADGET_ID);
+ GadgetRating expected = new GadgetRating(VALID_GADGET_ID, gadget, VALID_GADGET_RATING, VALID_USER_ID);
+ expect(ratingRepository.get(VALID_GADGET_ID)).andReturn(expected);
+ replay(ratingRepository);
+
+ try
+ {
+ GadgetRating response = gadgetService.getGadgetRating(VALID_GADGET_ID);
+ assertEquals(expected, response);
+ }
+ catch (GadgetNotFoundException e)
+ {
+ fail("Unable to locate gadget: "+e.getMessage());
+ }
+ catch (GadgetUserPrefNotFoundException e)
+ {
+ fail("Unable to locate gadget rating: "+e.getMessage());
+ }
+ }
+
+ @Test
+ public void getGadgetRating_invalidGadgetRatingId() {
+ Gadget gadget = new Gadget(VALID_GADGET_ID);
+ GadgetRating expected = new GadgetRating(VALID_GADGET_ID, gadget, VALID_GADGET_RATING, VALID_USER_ID);
+ expect(ratingRepository.get(INVALID_GADGET_ID)).andThrow(new IncorrectResultSizeDataAccessException(1));
+ replay(ratingRepository);
+
+ try
+ {
+ GadgetRating response = gadgetService.getGadgetRating(INVALID_GADGET_ID);
+ fail("returned a rating: "+response);
+ }
+ catch (GadgetNotFoundException e)
+ {
+ fail("Unable to locate gadget: "+e.getMessage());
+ }
+ catch (GadgetUserPrefNotFoundException e)
+ {
+
+ }
+ }
+
+ @Test
+ public void findGadgetRating_validGadget_validUser() {
+ Gadget gadget = new Gadget(VALID_GADGET_ID);
+ GadgetRating expected = new GadgetRating(VALID_GADGET_ID, gadget, VALID_GADGET_RATING, VALID_USER_ID);
+ expect(ratingRepository.find(gadget, VALID_USER_ID)).andReturn(expected);
+ replay(ratingRepository);
+
+ try
+ {
+ GadgetRating response = gadgetService.getUserGadgetRating(gadget, VALID_USER_ID);
+ assertEquals(response, expected);
+ }
+ catch (GadgetUserPrefNotFoundException e)
+ {
+ fail("Unable to locate gadget rating: "+e.getMessage());
+ }
+ }
+
+ @Test
+ public void findGadgetRating_validGadget_invalidUser() {
+
+ Gadget gadget = new Gadget(VALID_GADGET_ID);
+ expect(ratingRepository.find(gadget, INVALID_USER_ID)).andThrow(new IncorrectResultSizeDataAccessException(1));
+ replay(ratingRepository);
+
+ try
+ {
+ GadgetRating response = gadgetService.getUserGadgetRating(gadget, INVALID_USER_ID);
+ fail("Search retrieved a gadget rating: "+response);
+ }
+ catch (GadgetUserPrefNotFoundException e)
+ {
+
+ }
+ }
+
+ @Test
+ public void getGadgetRatings_validUser() {
+ Gadget gadget = new Gadget(VALID_GADGET_ID);
+ List<GadgetRating> expected = Arrays.asList(new GadgetRating(VALID_GADGET_ID, gadget, VALID_GADGET_RATING, VALID_USER_ID));
+ expect(ratingRepository.find(VALID_USER_ID)).andReturn(expected);
+ replay(ratingRepository);
+
+ List<GadgetRating> response = gadgetService.getGadgetRatings(VALID_USER_ID);
+ assertEquals(response, expected);
+ }
+
+ @Test
+ public void getGadgetRatings_invalidUser() {
+ List<GadgetRating> expected = new ArrayList<GadgetRating>();
+ expect(ratingRepository.find(INVALID_USER_ID)).andReturn(expected);
+ replay(ratingRepository);
+
+ List<GadgetRating> response = gadgetService.getGadgetRatings(INVALID_USER_ID);
+ assertEquals(response, expected);
+ }
+
+ @Test
+ public void getGadgetAudience_validId_validGadgetAudience() throws GadgetAudienceNotFoundException {
+ expect(gadgetAudienceRepository.get(VALID_GADGET_AUDIENCE_ID)).andReturn(new GadgetAudience());
+ replay(gadgetAudienceRepository);
+
+ GadgetAudience ga = gadgetService.getGadgetAudience(VALID_GADGET_AUDIENCE_ID);
+ assertNotNull(ga);
+ }
+
+ @Test (expected = GadgetAudienceNotFoundException.class)
+ public void getGadgetAudience_invalidId_exception() throws GadgetAudienceNotFoundException {
+ expect(gadgetAudienceRepository.get(INVALID_GADGET_AUDIENCE_ID)).andThrow(new IncorrectResultSizeDataAccessException(1));
+ replay(gadgetAudienceRepository);
+
+ GadgetAudience ga = gadgetService.getGadgetAudience(INVALID_GADGET_AUDIENCE_ID);
+ }
+
+ @Test
+ public void getGadgetComment_validId_validGadgetComment() throws GadgetCommentNotFoundException {
+ expect(gadgetCommentRepository.get(VALID_GADGET_COMMENT_ID)).andReturn(new GadgetComment(VALID_GADGET_COMMENT_ID));
+ replay(gadgetCommentRepository);
+
+ GadgetComment gc = gadgetService.getGadgetComment(VALID_GADGET_COMMENT_ID);
+ assertNotNull(gc);
+ }
+
+ @Test (expected = GadgetCommentNotFoundException.class)
+ public void getGadgetComment_invalidId_exception() throws GadgetCommentNotFoundException {
+ expect(gadgetCommentRepository.get(INVALID_GADGET_COMMENT_ID)).andThrow(new IncorrectResultSizeDataAccessException(1));
+ replay(gadgetCommentRepository);
+
+ gadgetService.getGadgetComment(INVALID_GADGET_COMMENT_ID);
+ }
+
+ @Test
+ public void findGadgetCommentByGadget_validGadget_validList() {
+ Gadget g = new Gadget(VALID_GADGET_ID);
+ List<GadgetComment> expectedList = new ArrayList<GadgetComment>();
+ expectedList.add(new GadgetComment());
+ expect(gadgetCommentRepository.findByGadget(g)).andReturn(expectedList);
+ replay(gadgetCommentRepository);
+
+ List<GadgetComment> list = gadgetService.findGadgetCommentsByGadget(g);
+ assertNotNull(list);
+ assertFalse(list.isEmpty());
+ }
+
+ @Test
+ public void save_validNewComment_hasRating() {
+ Gadget gadget = new Gadget(VALID_GADGET_ID);
+ gadget.setVersion(VALID_GADGET_VERSION);
+
+ GadgetRating gr = new GadgetRating();
+ gr.setRating(5L);
+ gr.setGadget(gadget);
+
+ GadgetComment gadgetComment = new GadgetComment();
+ gadgetComment.setCommentText("comments");
+ gadgetComment.setGadget(gadget);
+ gadgetComment.setUserId(VALID_USER_ID);
+
+ expect(userService.getCurrentAuthenticatedUser()).andReturn(validUser);
+ expect(ratingRepository.find(gadget, VALID_USER_ID)).andReturn(gr);
+ replay(userService);
+ replay(ratingRepository);
+ gadgetCommentRepository.save(gadgetComment);
+ expectLastCall();
+ replay(gadgetCommentRepository);
+
+ gadgetService.save(gadgetComment);
+
+ assertEquals(gadgetComment.getGadgetRating(), gr.getRating());
+ assertEquals(gadgetComment.getGadgetVersion(), gadget.getVersion());
+ assertFalse(gadgetComment.isLastModifiedByAdmin());
+ }
+
+ @Test
+ public void save_validNewComment_noRating() {
+ Gadget gadget = new Gadget(VALID_GADGET_ID);
+ gadget.setVersion(VALID_GADGET_VERSION);
+
+ GadgetComment gadgetComment = new GadgetComment();
+ gadgetComment.setCommentText("comments");
+ gadgetComment.setGadget(gadget);
+ gadgetComment.setUserId(VALID_USER_ID);
+
+ expect(userService.getCurrentAuthenticatedUser()).andReturn(validUser);
+ expect(ratingRepository.find(gadget, VALID_USER_ID)).andThrow(new RuntimeException("not found"));
+ replay(userService);
+ replay(ratingRepository);
+ gadgetCommentRepository.save(gadgetComment);
+ expectLastCall();
+ replay(gadgetCommentRepository);
+
+ gadgetService.save(gadgetComment);
+
+ assertEquals(gadgetComment.getGadgetRating(), null);
+ assertEquals(gadgetComment.getGadgetVersion(), gadget.getVersion());
+ assertFalse(gadgetComment.isLastModifiedByAdmin());
+ }
+
+ @Test(expected=AccessDeniedException.class)
+ public void save_validNewComment_accessDeniedException() {
+ Gadget gadget = new Gadget(VALID_GADGET_ID);
+ gadget.setVersion(VALID_GADGET_VERSION);
+
+ GadgetComment gadgetComment = new GadgetComment();
+ gadgetComment.setCommentText("comments");
+ gadgetComment.setGadget(gadget);
+ gadgetComment.setUserId(INVALID_USER_ID);
+
+ gadgetService.save(gadgetComment);
+ }
+
+ @Test
+ public void save_validExistingComment() {
+ Gadget gadget = new Gadget(VALID_GADGET_ID);
+ gadget.setVersion(VALID_GADGET_VERSION);
+
+ GadgetComment gadgetComment = new GadgetComment();
+ gadgetComment.setGadgetCommentId(VALID_GADGET_COMMENT_ID);
+ gadgetComment.setCommentText("comments");
+ gadgetComment.setGadget(gadget);
+ gadgetComment.setUserId(VALID_USER_ID);
+ gadgetComment.setCreatedDate(new Date());
+ gadgetComment.setGadgetRating(VALID_GADGET_RATING);
+ gadgetComment.setGadgetVersion(VALID_GADGET_VERSION);
+ gadgetComment.setLastModifiedByAdmin(false);
+ gadgetComment.setLastModifiedDate(null);
+ gadgetComment.setLastModifiedUserId(null);
+
+ expect(userService.getCurrentAuthenticatedUser()).andReturn(validUser);
+ replay(userService);
+ gadgetCommentRepository.save(gadgetComment);
+ expectLastCall();
+ replay(gadgetCommentRepository);
+
+ gadgetService.save(gadgetComment);
+
+ assertEquals(VALID_GADGET_RATING, gadgetComment.getGadgetRating());
+ assertEquals(VALID_GADGET_VERSION, gadgetComment.getGadgetVersion());
+ assertEquals(VALID_USER_ID, gadgetComment.getLastModifiedUserId());
+ }
+
+ @Test
+ public void saveAsAdmin_validNewComment() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.APPLICATION_ADMIN});
+ Gadget gadget = new Gadget(VALID_GADGET_ID);
+ gadget.setVersion(VALID_GADGET_VERSION);
+
+ GadgetComment gadgetComment = new GadgetComment();
+ gadgetComment.setCommentText("comments");
+ gadgetComment.setGadget(gadget);
+ gadgetComment.setUserId(VALID_USER_ID);
+
+ expect(userService.getCurrentAuthenticatedUser()).andReturn(validUser);
+ replay(userService);
+ gadgetCommentRepository.save(gadgetComment);
+ expectLastCall();
+ replay(gadgetCommentRepository);
+
+ gadgetService.saveAsAdmin(gadgetComment);
+
+ assertEquals(gadgetComment.getGadgetRating(), null);
+ assertEquals(gadgetComment.getGadgetVersion(), gadget.getVersion());
+ assertTrue(gadgetComment.isCreatedByAdmin());
+ }
+
+ @Test(expected=AccessDeniedException.class)
+ public void saveAsAdmin_validNewComment_accessDeniedException() {
+ Gadget gadget = new Gadget(VALID_GADGET_ID);
+ gadget.setVersion(VALID_GADGET_VERSION);
+
+ GadgetComment gadgetComment = new GadgetComment();
+ gadgetComment.setCommentText("comments");
+ gadgetComment.setGadget(gadget);
+ gadgetComment.setUserId(INVALID_USER_ID);
+
+ gadgetService.saveAsAdmin(gadgetComment);
+ }
+
+ @Test
+ public void saveAsAdmin_validExistingComment() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.APPLICATION_ADMIN});
+ Gadget gadget = new Gadget(VALID_GADGET_ID);
+ gadget.setVersion(VALID_GADGET_VERSION);
+
+ GadgetComment gadgetComment = new GadgetComment();
+ gadgetComment.setGadgetCommentId(VALID_GADGET_COMMENT_ID);
+ gadgetComment.setCommentText("comments");
+ gadgetComment.setGadget(gadget);
+ gadgetComment.setUserId(VALID_USER_ID);
+ gadgetComment.setCreatedDate(new Date());
+ gadgetComment.setGadgetRating(VALID_GADGET_RATING);
+ gadgetComment.setGadgetVersion(VALID_GADGET_VERSION);
+ gadgetComment.setLastModifiedByAdmin(false);
+ gadgetComment.setLastModifiedDate(null);
+ gadgetComment.setLastModifiedUserId(null);
+
+ expect(userService.getCurrentAuthenticatedUser()).andReturn(validUser);
+ replay(userService);
+ gadgetCommentRepository.save(gadgetComment);
+ expectLastCall();
+ replay(gadgetCommentRepository);
+
+ gadgetService.saveAsAdmin(gadgetComment);
+
+ assertEquals(VALID_GADGET_RATING, gadgetComment.getGadgetRating());
+ assertEquals(VALID_GADGET_VERSION, gadgetComment.getGadgetVersion());
+ assertEquals(VALID_USER_ID, gadgetComment.getLastModifiedUserId());
+ }
+
+ @Test
+ public void delete_validGadgetComment() {
+ GadgetComment gc = new GadgetComment(VALID_GADGET_COMMENT_ID);
+ gc.setUserId(VALID_USER_ID);
+ gadgetCommentRepository.delete(gc);
+ expectLastCall();
+ replay(gadgetCommentRepository);
+
+ gadgetService.delete(gc);
+ verify(gadgetCommentRepository);
+ }
+
+ @Test(expected=AccessDeniedException.class)
+ public void delete_validGadgetComment_accessDeniedException() {
+ GadgetComment gc = new GadgetComment(VALID_GADGET_COMMENT_ID);
+ gc.setUserId(INVALID_USER_ID);
+
+ gadgetService.delete(gc);
+ }
+
+ @Test
+ public void delete_validGadgetComment_asAdmin() {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.APPLICATION_ADMIN});
+ GadgetComment gc = new GadgetComment(VALID_GADGET_COMMENT_ID);
+ gc.setUserId(INVALID_USER_ID);
+ gadgetCommentRepository.delete(gc);
+ expectLastCall();
+ replay(gadgetCommentRepository);
+
+ gadgetService.delete(gc);
+ verify(gadgetCommentRepository);
+ }
+
+ @Test
+ public void getAllSecurityRoles()
+ {
+ List<SecurityRole> expectedList = new ArrayList<SecurityRole>();
+ expect(securityRoleRepository.findAll()).andReturn(expectedList);
+ replay(securityRoleRepository);
+
+ List<SecurityRole> secRole = gadgetService.getAllSecurityRoles();
+ assertNotNull(secRole);
+ }
+
+ @Test
+ public void getAllSecurityUserRoleByUserId_validid()
+ {
+ List<SecurityUserRole> role = new ArrayList<SecurityUserRole>();
+ role.add(new SecurityUserRole(123L, "100", "ADMIN"));
+
+ expect(securityUserRoleRepository.find(VALID_USER_ID)).andReturn(role);
+ replay(securityUserRoleRepository);
+
+ List<SecurityUserRole> secUserRole = gadgetService.getAllSecurityUserRoleByUserId(VALID_USER_ID);
+ assertNotNull(secUserRole);
+ }
+
+ @Test
+ public void save_validSecurityUserRole()
+ {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.APPLICATION_ADMIN});
+
+ long id = 123L;
+ SecurityUserRole role = new SecurityUserRole(id, VALID_USER_ID, "ADMIN");
+ role.setId(id);
+ role.setUserId(VALID_USER_ID);
+
+ securityUserRoleRepository.save(role);
+ expectLastCall();
+ replay(securityUserRoleRepository);
+
+ gadgetService.save(role);
+ verify(securityUserRoleRepository);
+
+ assertEquals(VALID_USER_ID, role.getUserId());
+ }
+
+ @Test(expected=AccessDeniedException.class)
+ public void save_validSecurityUserRole_accessDeniedException()
+ {
+ long id = 123L;
+ SecurityUserRole role = new SecurityUserRole(id, VALID_USER_ID, "ADMIN");
+ role.setId(id);
+ role.setUserId(VALID_USER_ID);
+
+ gadgetService.save(role);
+ }
+
+
+ @Test
+ public void delete_validSecurityUserRole()
+ {
+ ServiceTestsHelper.updateSecurityContextWithRoles(validUser, new String[] {ServiceTestsHelper.RoleNames.APPLICATION_ADMIN});
+
+ long id = 100;
+ String userId = "100";
+ String securityRoleName = "ADMIN";
+
+ SecurityUserRole role = new SecurityUserRole(id, userId, securityRoleName);
+ securityUserRoleRepository.delete(role);
+
+ expectLastCall();
+ replay(securityUserRoleRepository);
+
+ gadgetService.delete(role);
+ verify(securityUserRoleRepository);
+ }
+
+ @Test(expected=AccessDeniedException.class)
+ public void delete_validSecurityUserRole_accessDeniedException()
+ {
+ long id = 100;
+ String userId = "100";
+ String securityRoleName = "ADMIN";
+ SecurityUserRole role = new SecurityUserRole(id, userId, securityRoleName);
+
+ gadgetService.delete(role);
+ }
+}
Propchange: incubator/rave/donations/mitre-osec/test/unit/java/org/mitre/portal/service/GadgetServiceTest.java
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/rave/donations/mitre-osec/test/unit/java/org/mitre/portal/service/MetricsLoggingServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/donations/mitre-osec/test/unit/java/org/mitre/portal/service/MetricsLoggingServiceTest.java?rev=1087796&view=auto
==============================================================================
--- incubator/rave/donations/mitre-osec/test/unit/java/org/mitre/portal/service/MetricsLoggingServiceTest.java (added)
+++ incubator/rave/donations/mitre-osec/test/unit/java/org/mitre/portal/service/MetricsLoggingServiceTest.java Fri Apr 1 16:42:22 2011
@@ -0,0 +1,109 @@
+/*
+ * 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.mitre.portal.service;
+
+import org.mitre.portal.model.Gadget;
+import org.mitre.portal.model.Page;
+import org.mitre.portal.service.impl.DefaultMetricsLoggingService;
+import java.net.MalformedURLException;
+import org.junit.Before;
+import org.junit.Test;
+import static org.junit.Assert.*;
+import org.mitre.portal.model.Person;
+import org.mitre.portal.service.util.MetricsLoggingKeys.GadgetActions;
+
+/**
+ *
+ * @author ACARLUCCI
+ */
+public class MetricsLoggingServiceTest {
+ private static final String VALID_USER_ID = "100";
+
+ private MetricsLoggingService metricsLoggingService;
+
+ private Person validUser;
+ private Page validToPage;
+ private Page validFromPage;
+ private Gadget validGadget;
+
+
+ @Before
+ public void setup() throws MalformedURLException {
+ validUser = new Person();
+ validUser.setUsername("acarlucci");
+ validUser.setUserId(VALID_USER_ID);
+
+ validToPage = new Page();
+ validToPage.setPageId(1L);
+ validToPage.setName("Page 1");
+
+ validFromPage = new Page();
+ validFromPage.setPageId(2L);
+ validFromPage.setName("Page 2");
+
+ validGadget = new Gadget();
+ validGadget.setGadgetId(99L);
+ validGadget.setTitle("gadget title");
+
+ metricsLoggingService = new DefaultMetricsLoggingService();
+ }
+
+ @Test
+ public void logGadgetAction_addAction_validUser_validGadgetPage_validToPage() {
+ String expectedText = buildExpectedLogEntry(GadgetActions.ADD, validUser, validGadget, validToPage, null);
+ String logText = metricsLoggingService.logGadgetAction(GadgetActions.ADD, validUser, validGadget, validToPage, null);
+ assertEquals(expectedText, logText);
+ }
+
+ @Test
+ public void logGadgetAction_addAction_nullUser_nullGadget_validToPage() {
+ String expectedText = buildExpectedLogEntry(GadgetActions.ADD, null, null, validToPage, null);
+ String logText = metricsLoggingService.logGadgetAction(GadgetActions.ADD, null, null, validToPage, null);
+ assertEquals(expectedText, logText);
+ }
+
+ @Test
+ public void logGadgetAction_moveAction_validUser_validGadget_validToPage() {
+ String expectedText = buildExpectedLogEntry(GadgetActions.MOVE, validUser, validGadget, validToPage, validFromPage);
+ String logText = metricsLoggingService.logGadgetAction(GadgetActions.MOVE, validUser, validGadget, validToPage, validFromPage);
+ assertEquals(expectedText, logText);
+ }
+
+ @Test
+ public void logGadgetAction_deleteAction_validUser_validPage_validToPage() {
+ String expectedText = buildExpectedLogEntry(GadgetActions.DELETE, validUser, validGadget, null, validFromPage);
+ String logText = metricsLoggingService.logGadgetAction(GadgetActions.DELETE, validUser, validGadget, null, validFromPage);
+ assertEquals(expectedText, logText);
+ }
+
+ private String buildExpectedLogEntry(GadgetActions action, Person user, Gadget gadget, Page toPage, Page fromPage) {
+ StringBuilder sb = new StringBuilder();
+ sb.append(action)
+ .append("|sui:").append(user == null ? "null" : user.getUsername())
+ .append("|gadgetId:").append(gadget == null ? "null" : gadget.getGadgetId())
+ .append("|gadgetTitle:\"").append(gadget == null ? "null" : gadget.getTitle()).append("\"")
+ .append("|toPageId:").append(toPage == null ? "null" : toPage.getPageId())
+ .append("|toPageName:\"").append(toPage == null ? "null" : toPage.getName()).append("\"")
+ .append("|fromPageId:").append(fromPage == null ? "null" : fromPage.getPageId())
+ .append("|fromPageName:\"").append(fromPage == null ? "null" : fromPage.getName()).append("\"");
+
+ return sb.toString();
+ }
+}
\ No newline at end of file
Propchange: incubator/rave/donations/mitre-osec/test/unit/java/org/mitre/portal/service/MetricsLoggingServiceTest.java
------------------------------------------------------------------------------
svn:executable = *