package com.groupon.jenkins.mongo;

import com.groupon.jenkins.dynamic.build.cause.ManualBuildCause;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
import hudson.model.Cause;
import hudson.model.CauseAction;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.mongodb.morphia.mapping.MappedField;
import org.mongodb.morphia.mapping.Mapper;

import java.util.Arrays;
import java.util.List;

import static org.mockito.Mockito.when;

public class CauseActionConverterTest {

    //decode
    @Test
    public void should_return_null_if_dbObject_is_null() throws Exception {
        Assert.assertNull( new CauseActionConverter().decode(null,null,null));
    }
    @Test
    public void should_get_cause_from_dbObject() throws Exception {
        BasicDBObject cause1DbObject = new BasicDBObject("cause1", "cause1");
        DBObject causes = new BasicDBObjectBuilder().add("causes", Arrays.asList(cause1DbObject)).get();

        Mapper mapper = Mockito.mock(Mapper.class);
        Cause.UserIdCause cause1 = new Cause.UserIdCause();
        when(mapper.fromDBObject(null,cause1DbObject,null)).thenReturn(cause1);

        CauseActionConverter converter = new CauseActionConverter();
        converter.setMapper(mapper);
        CauseAction action =  converter.decode(CauseAction.class,causes,Mockito.mock(MappedField.class));

        Assert.assertEquals(1, action.getCauses().size());
        Assert.assertEquals(cause1, action.getCauses().get(0));

    }
    //end decode

    //encode
    @Test
    public void should_return_null_if_object_is_null() throws Exception {
        Assert.assertNull( new CauseActionConverter().encode(null,null));
    }
    @Test
    public void should_convert_cause_action_to_old_format() throws Exception {
        Cause.UserIdCause cause1 = new Cause.UserIdCause();
        Mapper mapper = Mockito.mock(Mapper.class);
        when(mapper.toDBObject(cause1)).thenReturn(new BasicDBObject("cause1","cause1"));

        CauseAction causeAction = new CauseAction(cause1);
        CauseActionConverter converter = new CauseActionConverter();
        converter.setMapper(mapper);

        DBObject dbObject = (DBObject) converter.encode(causeAction, null);

        Assert.assertEquals(dbObject.get("className"),CauseAction.class.getName() );
        Assert.assertNotNull(dbObject.get("causes"));
        List dbCauses = ((List) dbObject.get("causes"));
        Assert.assertEquals(1, dbCauses.size());
        Assert.assertEquals("cause1", ((BasicDBObject)dbCauses.get(0)).get("cause1"));
    }
    //end encode
}

@@ -90,24 +90,87 @@
 BuildCause;%0A
+import com.groupon.jenkins.dynamic.build.cause.NullBuildCause;%0A
 import com.m
@@ -1127,36 +1127,24 @@
        Cause
-.UserIdCause
  cause1 = ne
@@ -1141,35 +1141,32 @@
 ause1 = new 
-Cause.UserI
+NullBuil
 dCause();%0A  
@@ -1884,28 +1884,16 @@
    Cause
-.UserIdCause
  cause1 
@@ -1902,19 +1902,16 @@
 new 
-Cause.UserI
+NullBuil
 dCau

Fix failing test