/*
 * 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 java.io.IOException;
import java.util.List;
import nl.b3p.viewer.config.services.FeatureSource;
import nl.b3p.viewer.config.services.SolrConf;
import nl.b3p.viewer.util.TestUtil;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.mockito.Mockito;

/**
 *
 * @author Meine Toonen <meinetoonen@b3partners.nl>
 */
public class AttributeSourceActionBeanTest extends TestUtil {

    private AttributeSourceActionBean instance = new AttributeSourceActionBean();

    @Test
    public void testDeleteAttributeSource() throws SolrServerException, IOException{
        FeatureSource fs = entityManager.find(FeatureSource.class, 1L);

        List<FeatureSource> sources = entityManager.createQuery("FROM FeatureSource").getResultList();
        int numSources = sources.size();

        List<SolrConf> confs = entityManager.createQuery("FROM SolrConf").getResultList();
        assertEquals(1,confs.size());

        SolrConf conf = confs.get(0);
        assertEquals(2,conf.getIndexAttributes().size());
        assertEquals(2,conf.getResultAttributes().size());

        instance.setFeatureSource(fs);
        SolrServer server = Mockito.mock(SolrServer.class);

        instance.deleteFeatureSource(entityManager,server);

        Mockito.verify(server).deleteByQuery("searchConfig:1");
        sources = entityManager.createQuery("FROM FeatureSource").getResultList();
        assertEquals(numSources - 1,sources.size());

        confs = entityManager.createQuery("FROM SolrConf").getResultList();
        assertEquals(0,confs.size());


        List attrs = entityManager.createNativeQuery("select b.attribute_ from solr_conf_index_attributes b").getResultList();
        assertEquals(0, attrs.size());

        attrs = entityManager.createNativeQuery("select b.attribute_ from solr_conf_result_attributes b").getResultList();
        assertEquals(0, attrs.size());
    }
}

@@ -304,28 +304,142 @@
 ervices.
-FeatureSourc
+AttributeDescriptor;%0Aimport nl.b3p.viewer.config.services.FeatureSource;%0Aimport nl.b3p.viewer.config.services.SimpleFeatureTyp
 e;%0Aimpor
@@ -666,16 +666,62 @@
 Equals;%0A
+import static org.junit.Assert.assertNotNull;%0A
 import o
@@ -1140,16 +1140,42 @@
 s, 1L);%0A
+        assertNotNull(fs);
 %0A       
@@ -1310,16 +1310,413 @@
 .size();
+%0A        int numAttributesFromSource = 0;%0A        List%3CSimpleFeatureType%3E types =  fs.getFeatureTypes();%0A        for (SimpleFeatureType type : types) %7B%0A            numAttributesFromSource += type.getAttributes().size();%0A        %7D%0A%0A        List%3CAttributeDescriptor%3E attributes = entityManager.createQuery(%22FROM AttributeDescriptor%22).getResultList();%0A        int totalAttributes = attributes.size();
 %0A%0A      
@@ -2472,17 +2472,16 @@
 ze());%0A%0A
-%0A
         
@@ -2565,13 +2565,14 @@
 onf_
-index
+result
 _att
@@ -2648,24 +2648,29 @@
         attr
+ibute
 s = entityMa
@@ -2685,123 +2685,132 @@
 eate
-Native
 Query(%22
-select b.attribute_ from solr_conf_result_attributes b%22).getResultList();%0A        assertEquals(0
+FROM AttributeDescriptor%22).getResultList();%0A        assertEquals(totalAttributes - numAttributesFromSource
 , attr
+ibute
 s.si

Test for correct deletion of attributes