/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bricket.plugin.user_gallery.service;
import junit.framework.Assert;
import org.bricket.plugin.gallery.domain.Gallery;
import org.bricket.plugin.gallery.service.GalleryService;
import org.bricket.plugin.user.domain.User;
import org.bricket.plugin.user.service.UserService;
import org.bricket.plugin.user_gallery.domain.UserGallery;
import org.bricket.plugin.user_gallery.domain.UserGallery.Type;
import org.bricket.service.BricketServiceException;
import org.bricket.testsupport.AbstractBricketContextTest;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author Ingo Renner
* @author Henning Teek
*
*/
public class UserGalleryServiceTest extends AbstractBricketContextTest {
@Autowired
private UserGalleryService userGalleryService;
@Autowired
private UserService userService;
@Autowired
private GalleryService galleryService;
@Before
public void setUp() throws BricketServiceException {
userService.init();
galleryService.init();
userGalleryService.init();
}
@Test
public void testUserGallery() throws Exception {
UserGallery p = createUserGallery();
checkUserGallery(p);
updateUserGallery(p);
checkUserGallery(p);
deleteUserGallery(p);
}
private void deleteUserGallery(UserGallery ua) throws BricketServiceException {
userGalleryService.deleteUserGallery(ua);
UserGallery dua = userGalleryService.loadUserGallery(ua.getId());
Assert.assertNull("expected null but got " + dua, dua);
}
private void updateUserGallery(UserGallery ua) throws BricketServiceException {
userGalleryService.saveUserGallery(ua);
}
private void checkUserGallery(UserGallery ug) {
UserGallery mug = userGalleryService.loadUserGallery(ug.getId());
Assert.assertEquals(mug.getType(), ug.getType());
Assert.assertEquals(mug.getUser(), ug.getUser());
Assert.assertEquals(mug.getGallery(), ug.getGallery());
// equal impl test
Assert.assertEquals(ug, mug);
}
private UserGallery createUserGallery() throws BricketServiceException {
UserGallery ug = userGalleryService.createDomainObject();
ug.setType(Type.DEFAULT);
ug.setUser(createUser());
ug.setGallery(createGallery());
ug = userGalleryService.saveUserGallery(ug);
Assert.assertTrue("expected id but got:" + ug.getId(), ug.getId() != null);
return ug;
}
private User createUser() throws BricketServiceException {
User u = userService.createDomainObject();
u.setFirstname("fn1");
u.setLastname("ln1");
u.setEmail("un1@localhost");
u.setPassword("pw1");
u.setEnabled(true);
u = userService.saveUser(u);
Assert.assertTrue("expected id but got:" + u.getId(), u.getId() != null);
return u;
}
private Gallery createGallery() {
Gallery g = galleryService.createDomainObject();
g.setTitle("Title_1");
g.setDescription("Description_1");
try {
g = galleryService.saveGallery(g);
} catch (BricketServiceException e) {
Assert.fail();
}
Assert.assertTrue("expected id but got:" + g.getId(), g.getId() != null);
return g;
}
}