Search in sources :

Example 1 with PortalUtil

use of com.liferay.portal.util.PortalUtil in project liferay-ide by liferay.

the class PortalMockFactory method mockServices.

/**
 * Normaly Liferay regsiters service in utility class via Spring.
 *
 * For each utility class, we create a mock service with RETURNS_MOCKS (create not null objects for all functions).
 * We try to avoid as much NPE as possible.
 *
 * This is just a random list of services, as the need to use them pops out. It might be a good idea to
 * start with list of Liferay services and do this more thoroughly.
 *
 * When some service call needs to be implemented, just add something like:
 *   when(CompanyLocalServiceUtil.getService().getCompany(companyId)).thenReturn(company);
 */
public void mockServices() throws SystemException, PortalException {
    new AccountLocalServiceUtil().setService(mock(AccountLocalService.class, RETURNS_MOCKS));
    new AddressLocalServiceUtil().setService(mock(AddressLocalService.class, RETURNS_MOCKS));
    new BrowserTrackerLocalServiceUtil().setService(mock(BrowserTrackerLocalService.class, RETURNS_MOCKS));
    new ClusterGroupLocalServiceUtil().setService(mock(ClusterGroupLocalService.class, RETURNS_MOCKS));
    new CompanyLocalServiceUtil().setService(mock(CompanyLocalService.class, RETURNS_MOCKS));
    new ContactLocalServiceUtil().setService(mock(ContactLocalService.class, RETURNS_MOCKS));
    when(ContactLocalServiceUtil.getService().createContact(anyLong())).thenReturn(new ContactImpl());
    new CountryServiceUtil().setService(mock(CountryService.class, RETURNS_MOCKS));
    new EmailAddressLocalServiceUtil().setService(mock(EmailAddressLocalService.class, RETURNS_MOCKS));
    when(EmailAddressLocalServiceUtil.getService().createEmailAddress(anyLong())).thenReturn(new EmailAddressImpl());
    when(EmailAddressLocalServiceUtil.getService().getEmailAddresses(anyLong(), anyString(), anyLong())).thenReturn(new LinkedList<EmailAddress>());
    new PhoneLocalServiceUtil().setService(mock(PhoneLocalService.class, RETURNS_MOCKS));
    when(PhoneLocalServiceUtil.getService().getPhones(anyLong(), anyString(), anyLong())).thenReturn(new LinkedList<Phone>());
    new WebsiteLocalServiceUtil().setService(mock(WebsiteLocalService.class, RETURNS_MOCKS));
    when(WebsiteLocalServiceUtil.getService().getWebsites(anyLong(), anyString(), anyLong())).thenReturn(new LinkedList<Website>());
    new GroupLocalServiceUtil().setService(mock(GroupLocalService.class, RETURNS_MOCKS));
    when(GroupLocalServiceUtil.getService().createGroup(anyLong())).thenReturn(new GroupImpl());
    new ImageLocalServiceUtil().setService(mock(ImageLocalService.class, RETURNS_MOCKS));
    new LayoutLocalServiceUtil().setService(mock(LayoutLocalService.class, RETURNS_MOCKS));
    new LayoutPrototypeLocalServiceUtil().setService(mock(LayoutPrototypeLocalService.class, RETURNS_MOCKS));
    new LayoutSetLocalServiceUtil().setService(mock(LayoutSetLocalService.class, RETURNS_MOCKS));
    new LayoutSetPrototypeLocalServiceUtil().setService(mock(LayoutSetPrototypeLocalService.class, RETURNS_MOCKS));
    new LayoutTemplateLocalServiceUtil().setService(mock(LayoutTemplateLocalService.class, RETURNS_MOCKS));
    new LockLocalServiceUtil().setService(mock(LockLocalService.class, RETURNS_MOCKS));
    new MembershipRequestLocalServiceUtil().setService(mock(MembershipRequestLocalService.class, RETURNS_MOCKS));
    new OrgLaborLocalServiceUtil().setService(mock(OrgLaborLocalService.class, RETURNS_MOCKS));
    new OrganizationLocalServiceUtil().setService(mock(OrganizationLocalService.class, RETURNS_MOCKS));
    new PasswordPolicyLocalServiceUtil().setService(mock(PasswordPolicyLocalService.class, RETURNS_MOCKS));
    new PasswordPolicyRelLocalServiceUtil().setService(mock(PasswordPolicyRelLocalService.class, RETURNS_MOCKS));
    new PasswordTrackerLocalServiceUtil().setService(mock(PasswordTrackerLocalService.class, RETURNS_MOCKS));
    new PermissionLocalServiceUtil().setService(mock(PermissionLocalService.class, RETURNS_MOCKS));
    new PhoneLocalServiceUtil().setService(mock(PhoneLocalService.class, RETURNS_MOCKS));
    when(PhoneLocalServiceUtil.getService().createPhone(anyLong())).thenReturn(new PhoneImpl());
    new PluginSettingLocalServiceUtil().setService(mock(PluginSettingLocalService.class, RETURNS_MOCKS));
    new PortalLocalServiceUtil().setService(mock(PortalLocalService.class, RETURNS_MOCKS));
    new PortletItemLocalServiceUtil().setService(mock(PortletItemLocalService.class, RETURNS_MOCKS));
    new PortletPreferencesLocalServiceUtil().setService(mock(PortletPreferencesLocalService.class, RETURNS_MOCKS));
    new QuartzLocalServiceUtil().setService(mock(QuartzLocalService.class, RETURNS_MOCKS));
    new ReleaseLocalServiceUtil().setService(mock(ReleaseLocalService.class, RETURNS_MOCKS));
    new ResourceActionLocalServiceUtil().setService(mock(ResourceActionLocalService.class, RETURNS_MOCKS));
    new ResourceCodeLocalServiceUtil().setService(mock(ResourceCodeLocalService.class, RETURNS_MOCKS));
    new ResourcePermissionLocalServiceUtil().setService(mock(ResourcePermissionLocalService.class, RETURNS_MOCKS));
    new RoleLocalServiceUtil().setService(mock(RoleLocalService.class, RETURNS_MOCKS));
    when(RoleLocalServiceUtil.getService().hasUserRole(anyLong(), anyLong(), anyString(), anyBoolean())).thenReturn(true);
    new ServiceComponentLocalServiceUtil().setService(mock(ServiceComponentLocalService.class, RETURNS_MOCKS));
    new ShardLocalServiceUtil().setService(mock(ShardLocalService.class, RETURNS_MOCKS));
    new SubscriptionLocalServiceUtil().setService(mock(SubscriptionLocalService.class, RETURNS_MOCKS));
    new TeamLocalServiceUtil().setService(mock(TeamLocalService.class, RETURNS_MOCKS));
    new ThemeLocalServiceUtil().setService(mock(ThemeLocalService.class, RETURNS_MOCKS));
    new TicketLocalServiceUtil().setService(mock(TicketLocalService.class, RETURNS_MOCKS));
    new UserGroupGroupRoleLocalServiceUtil().setService(mock(UserGroupGroupRoleLocalService.class, RETURNS_MOCKS));
    new UserGroupLocalServiceUtil().setService(mock(UserGroupLocalService.class, RETURNS_MOCKS));
    new UserGroupRoleLocalServiceUtil().setService(mock(UserGroupRoleLocalService.class, RETURNS_MOCKS));
    new UserIdMapperLocalServiceUtil().setService(mock(UserIdMapperLocalService.class, RETURNS_MOCKS));
    new UserLocalServiceUtil().setService(mock(UserLocalService.class, RETURNS_MOCKS));
    new UserTrackerLocalServiceUtil().setService(mock(UserTrackerLocalService.class, RETURNS_MOCKS));
    new UserTrackerPathLocalServiceUtil().setService(mock(UserTrackerPathLocalService.class, RETURNS_MOCKS));
    new WebDAVPropsLocalServiceUtil().setService(mock(WebDAVPropsLocalService.class, RETURNS_MOCKS));
    new WebsiteLocalServiceUtil().setService(mock(WebsiteLocalService.class, RETURNS_MOCKS));
    new WorkflowDefinitionLinkLocalServiceUtil().setService(mock(WorkflowDefinitionLinkLocalService.class, RETURNS_MOCKS));
    new WorkflowInstanceLinkLocalServiceUtil().setService(mock(WorkflowInstanceLinkLocalService.class, RETURNS_MOCKS));
    new ListTypeServiceUtil().setService(mock(ListTypeService.class, RETURNS_MOCKS));
    when(ListTypeServiceUtil.getService().getListTypes(anyString())).thenReturn(Arrays.asList((ListType) new ListTypeImpl()));
    new PortalUtil().setPortal(spy(new PortalImpl()));
    new PortletPreferencesFactoryUtil().setPortletPreferencesFactory(mock(PortletPreferencesFactory.class, RETURNS_MOCKS));
    new DigesterUtil().setDigester(mock(Digester.class, RETURNS_MOCKS));
    new HttpUtil().setHttp(mock(Http.class, RETURNS_MOCKS));
    new AuthTokenUtil().setAuthToken(mock(AuthToken.class, RETURNS_MOCKS));
    new SAXReaderUtil().setSAXReader(SAXReaderImpl.getInstance());
    new LanguageUtil().setLanguage(mock(Language.class, RETURNS_MOCKS));
    // language.get(Locale l, String key) should always return key
    Answer<String> languageAnswer = new Answer<String>() {

        public String answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            return (String) args[1];
        }
    };
    when(LanguageUtil.getLanguage().get(any(Locale.class), anyString())).thenAnswer(languageAnswer);
    when(LanguageUtil.getLanguage().get(any(PageContext.class), anyString())).thenAnswer(languageAnswer);
    new BrowserSnifferUtil().setBrowserSniffer(mock(BrowserSniffer.class, RETURNS_MOCKS));
    new CounterLocalServiceUtil().setService(mock(CounterLocalService.class, RETURNS_MOCKS));
    when(CounterLocalServiceUtil.getService().increment(anyString())).thenReturn(new Random().nextLong());
    new PortletLocalServiceUtil().setService(mock(PortletLocalService.class, RETURNS_MOCKS));
    companyMockFactory.setupClassNames();
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) SAXReaderUtil(com.liferay.portal.kernel.xml.SAXReaderUtil) PortletPreferencesFactoryUtil(com.liferay.portlet.PortletPreferencesFactoryUtil) PortalImpl(com.liferay.portal.util.PortalImpl) PortletPreferencesFactory(com.liferay.portlet.PortletPreferencesFactory) PortalUtil(com.liferay.portal.util.PortalUtil) AuthTokenUtil(com.liferay.portal.security.auth.AuthTokenUtil) Locale(java.util.Locale) LanguageUtil(com.liferay.portal.kernel.language.LanguageUtil) Random(java.util.Random) PageContext(javax.servlet.jsp.PageContext) BrowserSnifferUtil(com.liferay.portal.kernel.servlet.BrowserSnifferUtil) BrowserSniffer(com.liferay.portal.kernel.servlet.BrowserSniffer) Language(com.liferay.portal.kernel.language.Language) CounterLocalService(com.liferay.counter.service.CounterLocalService) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AuthToken(com.liferay.portal.security.auth.AuthToken) CounterLocalServiceUtil(com.liferay.counter.service.CounterLocalServiceUtil)

Aggregations

CounterLocalService (com.liferay.counter.service.CounterLocalService)1 CounterLocalServiceUtil (com.liferay.counter.service.CounterLocalServiceUtil)1 Language (com.liferay.portal.kernel.language.Language)1 LanguageUtil (com.liferay.portal.kernel.language.LanguageUtil)1 BrowserSniffer (com.liferay.portal.kernel.servlet.BrowserSniffer)1 BrowserSnifferUtil (com.liferay.portal.kernel.servlet.BrowserSnifferUtil)1 SAXReaderUtil (com.liferay.portal.kernel.xml.SAXReaderUtil)1 AuthToken (com.liferay.portal.security.auth.AuthToken)1 AuthTokenUtil (com.liferay.portal.security.auth.AuthTokenUtil)1 PortalImpl (com.liferay.portal.util.PortalImpl)1 PortalUtil (com.liferay.portal.util.PortalUtil)1 PortletPreferencesFactory (com.liferay.portlet.PortletPreferencesFactory)1 PortletPreferencesFactoryUtil (com.liferay.portlet.PortletPreferencesFactoryUtil)1 Locale (java.util.Locale)1 Random (java.util.Random)1 PageContext (javax.servlet.jsp.PageContext)1 Matchers.anyString (org.mockito.Matchers.anyString)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1