/**
 * Copyright 2010 OpenEngSB Division, Vienna University of Technology
 *
 * 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.openengsb.core.common.workflow.model;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.util.Set;

import org.junit.Before;
import org.junit.Test;
import org.openengsb.core.common.workflow.ProcessBagException;

public class ProcessBagTest {
    private ProcessBag pb;

    @Before
    public void init() throws Exception {
        pb = new ProcessBag();
    }

    @Test
    public void constructorAndGetMethods_shouldReturnCorrectValues() throws Exception {
        pb = new ProcessBag("4711", "c", "test-user");

        assertEquals(pb.getProcessId(), "4711");
        assertEquals(pb.getContext(), "c");
        assertEquals(pb.getUser(), "test-user");
    }

    @Test
    public void getSetProperty_shouldSetAndGetProperValues() throws Exception {
        pb = new ProcessBag("4711", "c", "test-user");
        pb.addProperty("test", "42");
        assertTrue(pb.containsProperty("test"));
        assertEquals(pb.getProperty("test"), "42");
    }

    @Test(expected = ProcessBagException.class)
    public void addProperty_shouldReturnException() throws ProcessBagException {
        pb.addProperty("test", "42");
        pb.addProperty("test", "42");
    }

    @Test
    public void getPropertyClass_shouldReturnStringClass() throws Exception {
        pb.addProperty("test", "42");
        assertTrue(pb.getPropertyClass("test") == String.class);
    }

    @Test
    public void getPropertyKeyList_shouldReturnTwo() throws Exception {
        pb.addProperty("test", "42");
        pb.addProperty("number", 42);

        Set<String> list = pb.getPropertyKeyList();
        assertThat(list.size(), is(2));
    }

    @Test
    public void removeProperty_shouldContainStringOnly() throws Exception {
        pb.addProperty("number", 42);
        pb.addProperty("string", "42");
        pb.removeProperty("number");

        Set<String> list = pb.getPropertyKeyList();
        assertTrue(list.contains("string"));
        assertFalse(list.contains("number"));
    }

    @Test
    public void getPropertyCount_shouldReturnTwo() throws Exception {
        pb.addProperty("test", "42");
        pb.addProperty("number", 42);

        assertTrue(pb.getPropertyCount() == 2);
    }

    @Test
    public void addOrReplaceProperty_shouldOverwriteValue() throws Exception {
        pb.addProperty("test", "42");
        pb.addOrReplaceProperty("test", "43");
        assertEquals(pb.getProperty("test"), "43");
    }

    @Test
    public void removeAllProperties_shouldDeleteEverything() throws Exception {
        pb.addProperty("test", "42");
        pb.addProperty("number", 42);

        pb.removeAllProperties();
        assertTrue(pb.getPropertyCount() == 0);
    }
}

@@ -920,16 +920,167 @@
 til.Set;
+%0Aimport java.util.concurrent.Callable;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.Future;%0Aimport java.util.concurrent.TimeUnit;
 %0A%0Aimport
@@ -3648,11 +3648,683 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void setProcessIdLater_shouldBlockGetter() throws Exception %7B%0A        Callable%3CString%3E task = new Callable%3CString%3E() %7B%0A            @Override%0A            public String call() throws Exception %7B%0A                return pb.getProcessId();%0A            %7D%0A        %7D;%0A        Future%3CString%3E processIdFuture = Executors.newSingleThreadExecutor().submit(task);%0A%0A        Thread.sleep(100);%0A        // this task should block now%0A        assertThat(processIdFuture.isDone(), is(false));%0A%0A        // just call it to make sure it isn't blocking%0A        pb.getPropertyCount();%0A%0A        pb.setProcessId(%22foo%22);%0A        processIdFuture.get(1, TimeUnit.SECONDS);%0A    %7D
 %0A%7D%0A

add additional propertybag-test