/** * Licensed to Apereo under one or more contributor license agreements. See the NOTICE file * distributed with this work for additional information regarding copyright ownership. Apereo * 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 the * following location: * * <p>http://www.apache.org/licenses/LICENSE-2.0 * * <p>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.apereo.portal.layout.dao.jpa; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Set; import java.util.concurrent.Callable; import org.apereo.portal.IUserProfile; import org.apereo.portal.layout.dao.IStylesheetDescriptorDao; import org.apereo.portal.layout.dao.IStylesheetUserPreferencesDao; import org.apereo.portal.layout.om.IOutputPropertyDescriptor; import org.apereo.portal.layout.om.IStylesheetData.Scope; import org.apereo.portal.layout.om.IStylesheetDescriptor; import org.apereo.portal.layout.om.IStylesheetUserPreferences; import org.apereo.portal.security.IPerson; import org.apereo.portal.test.BasePortalJpaDaoTest; import org.apereo.portal.utils.MapPopulator; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:jpaPortalTestApplicationContext.xml") public class JpaStylesheetDescriptorDaoTest extends BasePortalJpaDaoTest { @Autowired private IStylesheetDescriptorDao stylesheetDescriptorDao; @Autowired private IStylesheetUserPreferencesDao stylesheetUserPreferencesDao; @Before public void onSetUp() throws Exception { this.execute( new Callable<Object>() { @Override public Object call() throws Exception { for (final IStylesheetDescriptor stylesheetDescriptor : stylesheetDescriptorDao.getStylesheetDescriptors()) { stylesheetDescriptorDao.deleteStylesheetDescriptor( stylesheetDescriptor); } for (final IStylesheetUserPreferences stylesheetUserPreferences : stylesheetUserPreferencesDao.getStylesheetUserPreferences()) { stylesheetUserPreferencesDao.deleteStylesheetUserPreferences( stylesheetUserPreferences); } return null; } }); } @Test public void testStylesheetDescriptorDao() throws Exception { final String ssName1 = this.execute( new Callable<String>() { @Override public String call() throws Exception { final IStylesheetDescriptor stylesheetDescriptor = stylesheetDescriptorDao.createStylesheetDescriptor( "columns", "classpath:/layout/struct/columns.xsl"); assertNotSame(-1, stylesheetDescriptor.getId()); stylesheetDescriptorDao.updateStylesheetDescriptor( stylesheetDescriptor); return stylesheetDescriptor.getName(); } }); final String ssName2 = this.execute( new Callable<String>() { @Override public String call() throws Exception { final IStylesheetDescriptor stylesheetDescriptor = stylesheetDescriptorDao.createStylesheetDescriptor( "lists", "classpath:/layout/struct/lists.xsl"); assertNotSame(-1, stylesheetDescriptor.getId()); return stylesheetDescriptor.getName(); } }); this.execute( new Callable<String>() { @Override public String call() throws Exception { final IStylesheetDescriptor stylesheetDescriptor = stylesheetDescriptorDao.getStylesheetDescriptorByName(ssName1); assertNotNull(stylesheetDescriptor); final Collection<IOutputPropertyDescriptor> outputPropertyDescriptors = stylesheetDescriptor.getOutputPropertyDescriptors(); assertEquals(0, outputPropertyDescriptors.size()); IOutputPropertyDescriptor descriptor = new OutputPropertyDescriptorImpl("propA", Scope.PERSISTENT); stylesheetDescriptor.setOutputPropertyDescriptors( Collections.singleton(descriptor)); stylesheetDescriptorDao.updateStylesheetDescriptor(stylesheetDescriptor); return null; } }); this.execute( new Callable<String>() { @Override public String call() throws Exception { final IStylesheetDescriptor stylesheetDescriptor = stylesheetDescriptorDao.getStylesheetDescriptorByName(ssName2); assertNotNull(stylesheetDescriptor); final Collection<IOutputPropertyDescriptor> outputPropertyDescriptors = stylesheetDescriptor.getOutputPropertyDescriptors(); assertEquals(0, outputPropertyDescriptors.size()); IOutputPropertyDescriptor descriptor = new OutputPropertyDescriptorImpl("propA", Scope.SESSION); stylesheetDescriptor.setOutputPropertyDescriptors( Collections.singleton(descriptor)); stylesheetDescriptorDao.updateStylesheetDescriptor(stylesheetDescriptor); return null; } }); this.execute( new Callable<String>() { @Override public String call() throws Exception { final IStylesheetDescriptor stylesheetDescriptor = stylesheetDescriptorDao.getStylesheetDescriptorByName(ssName1); assertNotNull(stylesheetDescriptor); final Collection<IOutputPropertyDescriptor> outputPropertyDescriptors = stylesheetDescriptor.getOutputPropertyDescriptors(); assertEquals(1, outputPropertyDescriptors.size()); Set<IOutputPropertyDescriptor> set = new HashSet<>(outputPropertyDescriptors); set.add(new OutputPropertyDescriptorImpl("propA", Scope.REQUEST)); stylesheetDescriptor.setOutputPropertyDescriptors(set); stylesheetDescriptorDao.updateStylesheetDescriptor(stylesheetDescriptor); return null; } }); this.execute( new Callable<String>() { @Override public String call() throws Exception { final IStylesheetDescriptor stylesheetDescriptor = stylesheetDescriptorDao.getStylesheetDescriptorByName(ssName1); assertNotNull(stylesheetDescriptor); final Collection<IOutputPropertyDescriptor> outputPropertyDescriptors = stylesheetDescriptor.getOutputPropertyDescriptors(); assertEquals(1, outputPropertyDescriptors.size()); return null; } }); } @Test public void testStylesheetUserPreferencesDao() throws Exception { final long ssdId = this.execute( new Callable<Long>() { @Override public Long call() throws Exception { final IStylesheetDescriptor stylesheetDescriptor = stylesheetDescriptorDao.createStylesheetDescriptor( "columns", "classpath:/layout/struct/columns.xsl"); final long id = stylesheetDescriptor.getId(); assertNotSame(-1, id); return id; } }); final IPerson person = mock(IPerson.class); when(person.getID()).thenReturn(1); final IUserProfile userProfile = mock(IUserProfile.class); when(userProfile.getProfileId()).thenReturn(1); final long supId = this.execute( new Callable<Long>() { @Override public Long call() throws Exception { final IStylesheetDescriptor stylesheetDescriptor = stylesheetDescriptorDao.getStylesheetDescriptor(ssdId); final IStylesheetUserPreferences stylesheetUserPreferences = stylesheetUserPreferencesDao .createStylesheetUserPreferences( stylesheetDescriptor, person, userProfile); assertNotNull(stylesheetUserPreferences); stylesheetUserPreferences.setStylesheetParameter("activeTab", "1"); stylesheetUserPreferences.setLayoutAttribute( "u1l1n1", "deletable", "false"); stylesheetUserPreferencesDao.storeStylesheetUserPreferences( stylesheetUserPreferences); return stylesheetUserPreferences.getId(); } }); this.execute( new Callable<Long>() { @Override public Long call() throws Exception { final IStylesheetUserPreferences stylesheetUserPreferences = stylesheetUserPreferencesDao.getStylesheetUserPreferences(supId); assertNotNull(stylesheetUserPreferences); assertEquals( Collections.singletonMap("activeTab", "1"), stylesheetUserPreferences .populateStylesheetParameters( new MapPopulator<String, String>()) .getMap()); assertEquals( Collections.singletonMap("deletable", "false"), stylesheetUserPreferences .populateLayoutAttributes( "u1l1n1", new MapPopulator<String, String>()) .getMap()); return null; } }); this.execute( new Callable<Long>() { @Override public Long call() throws Exception { final IStylesheetDescriptor stylesheetDescriptor = stylesheetDescriptorDao.getStylesheetDescriptor(ssdId); final IStylesheetUserPreferences stylesheetUserPreferences = stylesheetUserPreferencesDao.getStylesheetUserPreferences( stylesheetDescriptor, person, userProfile); assertNotNull(stylesheetUserPreferences); assertEquals( Collections.singletonMap("activeTab", "1"), stylesheetUserPreferences .populateStylesheetParameters( new MapPopulator<String, String>()) .getMap()); assertEquals( Collections.singletonMap("deletable", "false"), stylesheetUserPreferences .populateLayoutAttributes( "u1l1n1", new MapPopulator<String, String>()) .getMap()); stylesheetUserPreferencesDao.deleteStylesheetUserPreferences( stylesheetUserPreferences); return null; } }); } }