/**
* 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.contact.service;
import junit.framework.Assert;
import org.bricket.plugin.contact.domain.ContactMessage;
import org.bricket.plugin.contact.service.impl.ContactMessageServiceImpl;
import org.bricket.plugin.contact.web.ContactFormConfig;
import org.bricket.service.BricketServiceException;
import org.bricket.testsupport.AbstractBricketContextTest;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author Ingo Renner
* @author Henning Teek
*
*/
public class ContactMessageServiceTest extends AbstractBricketContextTest {
@Autowired
private ContactMessageService contactMessageService;
private ContactFormConfig config;
@Before
public void setUp() throws BricketServiceException {
contactMessageService.init();
((ContactMessageServiceImpl) contactMessageService).setTestmode(true);
config = new ContactFormConfig();
}
@Test
public void testContactMessage() {
ContactMessage cm = createContactMessage(1);
checkContactMessage(cm);
updateContactMessage(cm);
checkContactMessage(cm);
deleteContactMessage(cm);
}
private void deleteContactMessage(ContactMessage cm) {
contactMessageService.deleteContactMessage(cm);
ContactMessage cm2 = contactMessageService.loadContactMessage(cm.getId());
Assert.assertNull("expected null but got " + cm2, cm2);
}
private void updateContactMessage(ContactMessage cm) {
cm.setMessage("Message_2");
contactMessageService.saveContactMessage(config, cm);
}
private void checkContactMessage(ContactMessage cm) {
ContactMessage cm2 = contactMessageService.loadContactMessage(cm.getId());
Assert.assertEquals(cm.getMessage(), cm2.getMessage());
// equal impl test
Assert.assertEquals(cm, cm2);
}
private ContactMessage createContactMessage(int nr) {
ContactMessage cm2 = contactMessageService.createDomainObject();
cm2.setMessage("Message_" + nr);
cm2.setDate(new DateTime());
cm2.setPhone("1234");
cm2 = contactMessageService.saveContactMessage(config, cm2);
Assert.assertTrue("expected id but got:" + cm2.getId(), cm2.getId() != null);
return cm2;
}
}