Java tutorial
/* * Copyright (C) 2017 FormKiQ Inc. * * 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 com.formkiq.core.service; import static org.easymock.EasyMock.expect; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.io.IOException; import java.util.Arrays; import java.util.Map; import java.util.UUID; import org.apache.commons.lang3.tuple.Pair; import org.bouncycastle.util.Strings; import org.easymock.EasyMockRunner; import org.easymock.EasyMockSupport; import org.easymock.Mock; import org.easymock.TestSubject; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.util.ReflectionTestUtils; import com.formkiq.core.domain.User; import com.formkiq.core.domain.type.FormDTO; import com.formkiq.core.form.FormFinder; import com.formkiq.core.form.JSONService; import com.formkiq.core.form.bean.ObjectBuilder; import com.formkiq.core.form.dto.ArchiveDTO; import com.formkiq.core.form.dto.FormJSON; import com.formkiq.core.form.dto.FormJSONField; import com.formkiq.core.form.dto.Workflow; import com.formkiq.core.form.dto.WorkflowOutputDocument; import com.formkiq.core.form.dto.WorkflowOutputDocx; import com.formkiq.core.form.dto.WorkflowOutputForm; import com.formkiq.core.form.dto.WorkflowOutputFormField; import com.formkiq.core.form.dto.WorkflowRoute; import com.formkiq.core.testdata.TestDataBuilder; /** * ArchiveServiceImpl Unit Tests. * */ @RunWith(EasyMockRunner.class) public class ArchiveServiceImplTest extends EasyMockSupport { /** ArchiveService. */ @TestSubject private ArchiveService service = new ArchiveServiceImpl(); /** AssetService. */ @Mock private AssetServiceRouter assetService; /** FolderService. */ @Mock private FolderService folderService; /** Mock. */ @Mock private FormDTO formDTO0; /** Mock. */ @Mock private FormDTO formDTO1; /** JSON Object Mapper. */ private JSONService jsonService = new JSONService(); /** SpringSecurityService. */ @Mock private SpringSecurityService securityService; /** Mock. */ @Mock private User user; /** * before. */ @Before public void before() { ReflectionTestUtils.setField(this.service, "jsonService", this.jsonService); } /** * testGet01(). * reset UUID workflow * @throws IOException IOException */ @Test public void testGet01() throws IOException { // given boolean resetUUID = true; String folder = UUID.randomUUID().toString(); String sha1hash = UUID.randomUUID().toString(); FormJSON form = TestDataBuilder.createSimpleForm(); WorkflowOutputForm output1 = new WorkflowOutputForm(); output1.setForm(form.getName(), form.getUUID()); WorkflowOutputDocx output2 = new WorkflowOutputDocx(); WorkflowOutputFormField outfield = new WorkflowOutputFormField(); outfield.setForm(form.getName() + "[" + form.getUUID() + "]"); output2.setFields(Arrays.asList(outfield)); Workflow workflow = TestDataBuilder.createWorkflow(form); workflow.setOutputs(Arrays.asList(output1, output2)); String uuid = workflow.getUUID(); ArchiveDTO archive = new ArchiveDTO(); archive.setWorkflow(workflow); archive.addForm(form); byte[] data = this.service.createZipFile(archive); // when expect(this.folderService.findFormData(folder, uuid)).andReturn(Pair.of(data, sha1hash)); replayAll(); ArchiveDTO result = this.service.get(folder, uuid, resetUUID).getLeft(); // then verifyAll(); assertNull(workflow.getParentUUID()); Workflow resultWorkflow = result.getWorkflow(); assertNotEquals(workflow.getUUID(), resultWorkflow.getUUID()); assertEquals(workflow.getUUID(), resultWorkflow.getParentUUID()); assertNull(form.getParentUUID()); Map<String, FormJSON> forms = result.getForms(); assertEquals(1, forms.size()); FormJSON resultform = forms.values().iterator().next(); assertNotEquals(form.getUUID(), resultform.getUUID()); assertEquals(form.getUUID(), resultform.getSourceFormUUID()); assertFalse(result.getForms().containsKey(form.getUUID())); assertTrue(result.getForms().containsKey(resultform.getUUID())); assertFalse(resultWorkflow.getSteps().contains(form.getUUID())); assertTrue(resultWorkflow.getSteps().contains(resultform.getUUID())); assertFalse(resultWorkflow.getPrintsteps().contains(form.getUUID())); assertTrue(resultWorkflow.getPrintsteps().contains(resultform.getUUID())); WorkflowOutputForm wof = (WorkflowOutputForm) result.getWorkflow().getOutputs().get(0); assertNotEquals(output1.getForm(), wof.getForm()); WorkflowOutputDocument w = (WorkflowOutputDocument) result.getWorkflow().getOutputs().get(1); assertNotEquals(output2.getFields().get(0).getForm(), w.getFields().get(0).getForm()); } /** * testCreateZipFile01(). * @throws Exception Exception */ @Test public void testCreateZipFile01() throws Exception { // given ArchiveDTO archive = ObjectBuilder.buildArchiveDTO("testwf"); archive.addForm(TestDataBuilder.createSimpleForm()); archive.addPDF("sample-form1.pdf", Strings.toByteArray("pdf")); archive.addSignature("test.sig", Strings.toByteArray("sig")); archive.addRoute(new WorkflowRoute()); archive.addObject("test.docx", Strings.toByteArray("docx")); // when byte[] data = this.service.createZipFile(archive); ArchiveDTO result = this.service.extractJSONFromZipFile(data); // then assertEquals(archive.getWorkflow().getUUID(), result.getWorkflow().getUUID()); assertFalse(archive.getForms().isEmpty()); assertEquals(archive.getForms().size(), result.getForms().size()); assertTrue(result.getForms().containsKey(archive.getForms().keySet().iterator().next())); assertFalse(archive.getPDF().isEmpty()); assertEquals(archive.getPDF().size(), result.getPDF().size()); assertTrue(result.getPDF().containsKey(archive.getPDF().keySet().iterator().next())); assertFalse(archive.getSignatures().isEmpty()); assertEquals(archive.getSignatures().size(), result.getSignatures().size()); assertTrue(result.getSignatures().containsKey(archive.getSignatures().keySet().iterator().next())); assertFalse(archive.getRoutes().isEmpty()); assertEquals(archive.getRoutes().size(), result.getRoutes().size()); assertTrue(result.getRoutes().containsKey(archive.getRoutes().keySet().iterator().next())); assertFalse(archive.getObjects().isEmpty()); assertEquals(archive.getObjects().size(), result.getObjects().size()); assertTrue(result.getObjects().containsKey(archive.getObjects().keySet().iterator().next())); } /** * Test Rest {@link FormJSONField} Source Form field. */ @Test public void testResetUUID01() { // given FormJSON form0 = TestDataBuilder.createSimpleForm(); FormJSONField ff0 = FormFinder.findField(form0, 1).get(); FormJSON form1 = TestDataBuilder.createStoreReceipt(); FormJSONField ff1 = FormFinder.findField(form0, 1).get(); ff1.setSourceform(form0.getName() + "[" + form0.getUUID() + "]"); ff1.setSourcefield(ff0.getLabel() + "[" + ff0.getId() + "]"); ArchiveDTO archive = ObjectBuilder.buildArchiveDTO("", form0, form1); // when Map<String, String> map = this.service.resetUUID(archive); // then assertEquals(2, map.size()); assertEquals(2, archive.getForms().size()); assertEquals(form0.getName() + "[" + form0.getSourceFormUUID() + "]", ff1.getSourceform()); } }