/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.b3p.viewer.admin.stripes;

import nl.b3p.viewer.config.app.Application;
import nl.b3p.viewer.util.TestUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

/**
 *
 * @author Meine Toonen meinetoonen@b3partners.nl
 */
public class ApplicationStartMapActionBeanTest extends TestUtil{
    private static final Log log = LogFactory.getLog(ApplicationStartMapActionBeanTest.class);
    private ApplicationStartMapActionBean instance = new ApplicationStartMapActionBean();
    
    
    public ApplicationStartMapActionBeanTest() {
        String sc = "[{ \"id\" : \"3\", \"type\" : \"level\"}, { \"id\" : \"4\", \"type\" : \"level\"}, { \"id\" : \"5\", \"type\" : \"level\"},{ \"id\" : \"6\", \"type\" : \"level\"}]";
        
        
        instance.setSelectedContent(sc);
        instance.setCheckedLayersString("[]");
        int a = 0;
    }
    

   
    @Test
    public void testRemoveStartLayer(){
        
        initData(true);
        try {
            Application app = entityManager.find(Application.class, 1L);
            int expectedStartLayerSize = app.getStartLayers().size();
            int expectedStartLevelSize = app.getStartLevels().size();
            int expectedRootStartLevelSize = app.getRoot().getStartLevels().size() * 2;

            Application mashup = app.createMashup("mashup", entityManager,false);
            entityManager.persist(mashup);

            entityManager.getTransaction().commit();
            entityManager.getTransaction().begin();
            instance.setApplication(mashup);
            
            JSONObject removeString = new JSONObject();
            removeString.put("type", "layer");
            removeString.put("id", 3L);
            JSONArray ar = new JSONArray();
            ar.put(removeString);
            instance.setRemovedRecordsString(ar.toString());
            
            instance.saveStartMap(entityManager);
            
            
            assertEquals(expectedStartLayerSize - 1, mashup.getStartLayers().size());
            assertEquals(expectedStartLevelSize, mashup.getStartLevels().size());

            assertEquals(expectedRootStartLevelSize, app.getRoot().getStartLevels().size());
        }catch(Exception e){
            log.error ("fout",e);
            assert(false);
        }
    }
   
    @Test
    public void testRemoveStartLevel(){
        
        initData(true);
        try {
            int expectedStartLayerSize = app.getStartLayers().size();
            int expectedStartLevelSize = app.getStartLevels().size();
            int expectedRootStartLevelSize = app.getRoot().getStartLevels().size() * 2;

            Application mashup = app.createMashup("mashup", entityManager,false);
            entityManager.persist(mashup);

            entityManager.getTransaction().commit();
            entityManager.getTransaction().begin();
            instance.setApplication(mashup);
            
            JSONObject removeString = new JSONObject();
            removeString.put("type", "level");
            removeString.put("id", 3L);
            JSONArray ar = new JSONArray();
            ar.put(removeString);
            instance.setRemovedRecordsString(ar.toString());
            
            instance.saveStartMap(entityManager);
            
            assertEquals(expectedStartLayerSize -1, mashup.getStartLayers().size());
            assertEquals(expectedStartLevelSize- 1, mashup.getStartLevels().size());

            assertEquals(expectedRootStartLevelSize, app.getRoot().getStartLevels().size());
        }catch(Exception e){
            log.error (e);
            assert(false);
        }
    }
    
}

@@ -817,24 +817,56 @@
 Bean();%0A    
+private Application application;
 %0A    %0A    pu
@@ -1331,27 +1331,9 @@
     
-try %7B%0A            A
+a
 ppli
@@ -1343,12 +1343,8 @@
 ion 
-app 
 = en
@@ -1383,24 +1383,35 @@
 ss, 1L);%0A   
+     try %7B%0A
          int
@@ -1399,32 +1399,35 @@
  try %7B%0A         
+   
 int expectedStar
@@ -1434,32 +1434,40 @@
 tLayerSize = app
+lication
 .getStartLayers(
@@ -1512,32 +1512,40 @@
 tLevelSize = app
+lication
 .getStartLevels(
@@ -1594,32 +1594,40 @@
 tLevelSize = app
+lication
 .getRoot().getSt
@@ -1679,32 +1679,40 @@
 ion mashup = app
+lication
 .createMashup(%22m
@@ -2536,32 +2536,40 @@
 rtLevelSize, app
+lication
 .getRoot().getSt
@@ -2774,32 +2774,97 @@
 initData(true);%0A
+        application = entityManager.find(Application.class, 1L);%0A
         try %7B%0A  
@@ -2897,32 +2897,40 @@
 tLayerSize = app
+lication
 .getStartLayers(
@@ -2979,24 +2979,32 @@
 elSize = app
+lication
 .getStartLev
@@ -3061,24 +3061,32 @@
 elSize = app
+lication
 .getRoot().g
@@ -3150,16 +3150,24 @@
 up = app
+lication
 .createM
@@ -3996,16 +3996,24 @@
 ize, app
+lication
 .getRoot

Test use correct application