Java tutorial
/* * Copyright 2014. Vadim Baranov * * 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.vader.ecm.dao.impl; import org.joda.time.DateTime; import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; import org.vader.ecm.dao.DocumentDao; import org.vader.ecm.dao.PartyDao; import org.vader.ecm.domain.Document; import org.vader.ecm.domain.ExternalParty; import org.vader.ecm.domain.ExternalPartyType; import org.vader.ecm.domain.Grant; import org.vader.ecm.domain.MapDocument; import org.vader.ecm.domain.TxtDocument; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import java.util.EnumSet; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat; /** * @author Vadim Baranov */ @Transactional @DirtiesContext @ContextConfiguration(locations = { "classpath:/spring/test-app-context.xml", "classpath:/spring/backend-local-context.xml", "classpath:/spring/dao-context.xml" }) @RunWith(SpringJUnit4ClassRunner.class) public class DocumentDaoImplITCase { @Autowired private DocumentDao sut; @Autowired private PartyDao partyDao; @PersistenceContext private EntityManager entityManager; @After public void tearDown() throws Exception { flushAndClear(); } private void flushAndClear() { entityManager.flush(); entityManager.clear(); } @Test public void testSaveTxt() throws Exception { final ExternalPartyType apmAccountType = partyDao.getExternalPartyType("APM_ACCOUNT"); final ExternalParty account = createExternalParty(apmAccountType, "100"); final TxtDocument txtDocument = new TxtDocument(); txtDocument.setContent("txt"); txtDocument.setAuthorId(1L); txtDocument.setCreateTs(DateTime.now()); txtDocument.setSubject("Subject"); txtDocument.setTitle("title"); txtDocument.setSignature(new byte[0]); txtDocument.setCreator(account); for (int i = 0; i < 100; i++) { txtDocument.getContractors().add(createExternalParty(apmAccountType, String.valueOf(500 + i))); } sut.save(txtDocument); flushAndClear(); final TxtDocument loadedTxtDocument = sut.getById(txtDocument.getId(), TxtDocument.class); assertThat(loadedTxtDocument, is(equalTo(txtDocument))); assertThat(loadedTxtDocument.getCreator(), is(notNullValue())); assertThat(loadedTxtDocument.getCreator().getId(), is(txtDocument.getCreator().getId())); assertThat(loadedTxtDocument.getCreator().getGrants(), containsInAnyOrder(Grant.DOCUMENT_READ, Grant.DOCUMENT_WRITE)); assertThat(((ExternalParty) loadedTxtDocument.getCreator()).getExternalId(), is(account.getExternalId())); assertThat(((ExternalParty) loadedTxtDocument.getCreator()).getExternalPartyType(), is(account.getExternalPartyType())); } private ExternalParty createExternalParty(ExternalPartyType apmAccountType, String externalId) { final ExternalParty account = new ExternalParty(); account.setExternalId(externalId); account.setExternalPartyType(apmAccountType); account.setGrants(EnumSet.of(Grant.DOCUMENT_READ, Grant.DOCUMENT_WRITE)); entityManager.persist(account); return account; } @Test public void testSaveMap() throws Exception { final MapDocument mapDocument = new MapDocument(); mapDocument.getAttributes().put("key1", "name1"); mapDocument.getAttributes().put("key2", "name2"); mapDocument.setAuthorId(1L); mapDocument.setCreateTs(DateTime.now()); mapDocument.setSubject("Subject"); mapDocument.setTitle("title"); mapDocument.setSignature(new byte[0]); sut.save(mapDocument); flushAndClear(); final MapDocument loadedMapDocument = sut.getById(mapDocument.getId(), MapDocument.class); assertThat(loadedMapDocument, is(equalTo(mapDocument))); } @Test public void testGetByIdNotExisted() throws Exception { assertThat(sut.getById(1L), is(nullValue())); } @Test public void testGetByIdExisted() throws Exception { final MapDocument mapDocument = new MapDocument(); mapDocument.getAttributes().put("key1", "name1"); mapDocument.getAttributes().put("key2", "name2"); mapDocument.setAuthorId(1L); mapDocument.setCreateTs(DateTime.now()); mapDocument.setSubject("Subject"); mapDocument.setTitle("title"); mapDocument.setSignature(new byte[0]); sut.save(mapDocument); flushAndClear(); assertThat(sut.getById(mapDocument.getId()), is(equalTo((Document) mapDocument))); } @Test public void testFindMapDocumentsByAttribute() throws Exception { final MapDocument mapDocument = new MapDocument(); mapDocument.getAttributes().put("key1", "name1"); mapDocument.getAttributes().put("key2", "name2"); mapDocument.setAuthorId(1L); mapDocument.setCreateTs(DateTime.now()); mapDocument.setSubject("Subject"); mapDocument.setTitle("title"); mapDocument.setSignature(new byte[0]); sut.save(mapDocument); flushAndClear(); assertThat(sut.findMapDocumentsByAttribute("key1", "name1"), hasSize(1)); assertThat(sut.findMapDocumentsByAttribute("key2", "name2"), hasSize(1)); assertThat(sut.findMapDocumentsByAttribute("key1", "name2"), is(empty())); assertThat(sut.findMapDocumentsByAttribute("key2", "name1"), is(empty())); assertThat(sut.findMapDocumentsByAttribute("k", "n"), is(empty())); } }