Test.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.web.lib.mvc.properties.test;

  21. import java.io.File;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import java.util.Properties;

  25. import org.openspcoop2.core.mvc.properties.Config;
  26. import org.openspcoop2.core.mvc.properties.provider.ExternalResources;
  27. import org.openspcoop2.core.mvc.properties.utils.DBPropertiesUtils;
  28. import org.openspcoop2.core.mvc.properties.utils.XSDValidator;
  29. import org.openspcoop2.core.mvc.properties.utils.serializer.JaxbDeserializer;
  30. import org.openspcoop2.pdd.core.byok.DriverBYOKUtilities;
  31. import org.openspcoop2.utils.LoggerWrapperFactory;
  32. import org.openspcoop2.utils.xml.AbstractValidatoreXSD;
  33. import org.openspcoop2.web.lib.mvc.Costanti;
  34. import org.openspcoop2.web.lib.mvc.DataElement;
  35. import org.openspcoop2.web.lib.mvc.DataElementType;
  36. import org.openspcoop2.web.lib.mvc.byok.LockUtilities;
  37. import org.openspcoop2.web.lib.mvc.properties.beans.BaseItemBean;
  38. import org.openspcoop2.web.lib.mvc.properties.beans.ConfigBean;
  39. import org.openspcoop2.web.lib.mvc.properties.utils.ReadPropertiesUtilities;

  40. /**    
  41.  * Test
  42.  *
  43.  * @author Pintori Giuliano (pintori@link.it)
  44.  * @author $Author$
  45.  * @version $Rev$, $Date$
  46.  */
  47. public class Test {
  48.     public static void main(String[] args) {
  49.         try {
  50.             System.out.println("TEST CONFIG 1");
  51.            
  52.             DriverBYOKUtilities driverBYOKUtilities = new DriverBYOKUtilities(false, null, null);
  53.             LockUtilities lockUtilities = new LockUtilities(driverBYOKUtilities, false, null, null, null, null, false);
  54.            
  55.             File f = new File(Test.class.getResource("Test.xml").toURI());
  56.            
  57.             System.out.println("Validazione XSD del file in corso...");
  58.            
  59.             AbstractValidatoreXSD validator = XSDValidator.getXSDValidator(LoggerWrapperFactory.getLogger(Test.class));
  60.            
  61.             validator.valida(f);
  62.             System.out.println("Validazione XSD della configurazione completata con successo");
  63.            
  64.            
  65.             JaxbDeserializer xmlReader = new JaxbDeserializer();
  66.            
  67.             Config configDaFile = xmlReader.readConfig(f);
  68.            
  69.             System.out.println("Check Config JAVA Exists ["+(configDaFile != null)+"]");
  70.            
  71.             Map<String, String> mapDB = null;
  72.            
  73.             Map<String, Properties> mappaDB = DBPropertiesUtils.toMultiMap(mapDB, ReadPropertiesUtilities.getListaNomiProperties(configDaFile));
  74.            
  75.             ExternalResources externalResources = null;
  76.            
  77.             ConfigBean configurazioneAdd = ReadPropertiesUtilities.leggiConfigurazione(configDaFile, mappaDB, externalResources);
  78.            
  79.             System.out.println("ConfigurazioneAdd Resolve Conditions");
  80.            
  81.             configurazioneAdd.updateConfigurazione(configDaFile);
  82.            
  83.             System.out.println("ConfigurazioneAdd IN Pagina: ");
  84.            
  85.             Map<String, String> mapNameValue = new HashMap<>();
  86.             for (BaseItemBean<?> item : configurazioneAdd.getListaItem()) {
  87.                 DataElement de = item.toDataElement(configurazioneAdd, mapNameValue, externalResources, lockUtilities);
  88.                 System.out.println("Item ["+de.getName()+"] Type [" +de.getType() +"] Label ["+de.getLabel()+"] Value ["+de.getValue()+"]");
  89.             }
  90.                        
  91.             System.out.println("----------------------");
  92.            
  93.            
  94.            
  95.             System.out.println("Simulazione POSTBACK");
  96.            
  97.             configurazioneAdd.setValueFromRequest("usernameAction", Costanti.CHECK_BOX_ENABLED_ABILITATO, externalResources, lockUtilities);
  98.             configurazioneAdd.setValueFromRequest("keystoreType", "pkcs12", externalResources, lockUtilities);
  99.            
  100.             System.out.println("ConfigurazioneAdd Resolve Conditions POSTBACK");
  101.            
  102.             configurazioneAdd.updateConfigurazione(configDaFile);
  103.            
  104.             System.out.println("ConfigurazioneAdd IN Pagina: ");
  105.            
  106.             mapNameValue = new HashMap<>();
  107.             for (BaseItemBean<?> item : configurazioneAdd.getListaItem()) {
  108.                 DataElement de = item.toDataElement(configurazioneAdd, mapNameValue, externalResources, lockUtilities);
  109.                 if(de.getType().equals(DataElementType.CHECKBOX.toString()) || de.getType().equals(DataElementType.SELECT.toString()) )
  110.                     System.out.println("Item ["+de.getName()+"] Type [" +de.getType() +"] Label ["+de.getLabel()+"] SelectedValue ["+de.getSelected()+"]");
  111.                 else
  112.                     System.out.println("Item ["+de.getName()+"] Type [" +de.getType() +"] Label ["+de.getLabel()+"] Value ["+de.getValue()+"]");
  113.             }
  114.            
  115.             System.out.println("----------------------");
  116.            
  117.            
  118.             System.out.println("Simulazione EDIT FINALE ADD ---> Clicco SALVA....");
  119.            
  120.             configurazioneAdd.setValueFromRequest("usernameAction", Costanti.CHECK_BOX_ENABLED_ABILITATO, externalResources, lockUtilities);
  121.             configurazioneAdd.setValueFromRequest("keystoreType", "pkcs12", externalResources, lockUtilities);
  122.             configurazioneAdd.setValueFromRequest("keystore", "/tmp/keystoreAdd.jks", externalResources, lockUtilities);
  123.             configurazioneAdd.setValueFromRequest("encryptAction", Costanti.CHECK_BOX_ENABLED_ABILITATO, externalResources, lockUtilities);
  124.             configurazioneAdd.setValueFromRequest("encryptSignatureAction", Costanti.CHECK_BOX_ENABLED_ABILITATO, externalResources, lockUtilities);
  125.            
  126.            
  127.             System.out.println("Simulazione EDIT FINALE ADD ---> Validazione Input utente");
  128.            
  129.             configurazioneAdd.validazioneInputUtente("nome", "descrizione", configDaFile, externalResources);
  130.            
  131.             System.out.println("----------------------");
  132.            
  133.             mappaDB = configurazioneAdd.getPropertiesMap();
  134.            
  135.             System.out.println("Values --> MAP Parziale:");
  136.            
  137.             for (String mapKey : mappaDB.keySet()) {
  138.                 System.out.println("Contenuto Mappa ["+mapKey+"]:  ");
  139.                 Properties properties = mappaDB.get(mapKey);
  140.                
  141.                 for (Object propKey: properties.keySet()) {
  142.                     System.out.println("Item ["+propKey+"] Value ["+properties.get(propKey)+"]");
  143.                 }
  144.                
  145.                 System.out.println("----------------------");
  146.             }
  147.            
  148.             System.out.println("MAP Parziale --> MAP DB:");
  149.            
  150.             mapDB = DBPropertiesUtils.toMap(mappaDB);
  151.            
  152.             for (String propKey: mapDB.keySet()) {
  153.                 System.out.println("Item ["+propKey+"] Value ["+mapDB.get(propKey)+"]");
  154.             }
  155.            
  156.             System.out.println("----------------------\n\n\n\n\n\n\n\n\n\n\n");
  157.            
  158.            
  159.            
  160.             System.out.println("Ricarico configurazione da DB ");
  161.            
  162.             System.out.println("MAP DB --> MAP Parziale:");
  163.            
  164.             mappaDB = DBPropertiesUtils.toMultiMap(mapDB);
  165.            
  166.             for (String mapKey : mappaDB.keySet()) {
  167.                 System.out.println("Contenuto Mappa ["+mapKey+"]:  ");
  168.                 Properties properties = mappaDB.get(mapKey);
  169.                
  170.                 for (Object propKey: properties.keySet()) {
  171.                     System.out.println("Item ["+propKey+"] Value ["+properties.get(propKey)+"]");
  172.                 }
  173.                
  174.                 System.out.println("----------------------");
  175.             }
  176.            
  177.             System.out.println("Ricarico configurazione da DB ");
  178.            
  179.             ConfigBean configurazioneChange = ReadPropertiesUtilities.leggiConfigurazione(configDaFile, mappaDB, externalResources);
  180.            
  181.             System.out.println("----------------------");
  182.            
  183.             System.out.println("configurazioneChange Resolve Conditions");
  184.            
  185.             configurazioneChange.setValueFromRequest("keystoreType", "jks", externalResources, lockUtilities);
  186.             configurazioneChange.setValueFromRequest("keystore", "/tmp/keystore.jks", externalResources, lockUtilities);
  187.             configurazioneChange.setValueFromRequest("timeToLive", "120", externalResources, lockUtilities);
  188.            
  189.             configurazioneChange.updateConfigurazione(configDaFile);
  190.            
  191.             System.out.println("configurazioneChange IN Pagina: ");
  192.            
  193.             mapNameValue = new HashMap<>();
  194.             for (BaseItemBean<?> item : configurazioneChange.getListaItem()) {
  195.                 DataElement de = item.toDataElement(configurazioneAdd, mapNameValue, externalResources, lockUtilities);
  196.                 if(de.getType().equals(DataElementType.CHECKBOX.toString()) || de.getType().equals(DataElementType.SELECT.toString()) )
  197.                     System.out.println("Item ["+de.getName()+"] Type [" +de.getType() +"] Label ["+de.getLabel()+"] SelectedValue ["+de.getSelected()+"]");
  198.                 else
  199.                     System.out.println("Item ["+de.getName()+"] Type [" +de.getType() +"] Label ["+de.getLabel()+"] Value ["+de.getValue()+"]");
  200.             }
  201.                        
  202.             System.out.println("Simulazione EDIT FINALE CHANGE ---> Validazione Input utente");
  203.            
  204.             configurazioneChange.validazioneInputUtente("nome", "descrizione", configDaFile, externalResources);
  205.            
  206.             System.out.println("----------------------");
  207.            
  208.            
  209.             Map<String, Properties> mappaDestinazione = configurazioneChange.getPropertiesMap();    
  210.            
  211.             System.out.println("Values --> MAP Parziale:");
  212.            
  213.             for (String mapKey : mappaDestinazione.keySet()) {
  214.                 System.out.println("Contenuto Mappa ["+mapKey+"]:  ");
  215.                 Properties properties = mappaDestinazione.get(mapKey);
  216.                
  217.                 for (Object propKey: properties.keySet()) {
  218.                     System.out.println("Item ["+propKey+"] Value ["+properties.get(propKey)+"]");
  219.                 }
  220.                
  221.                 System.out.println("----------------------");
  222.             }
  223.            
  224.             System.out.println("MAP Parziale --> MAP DB:");
  225.            
  226.             mapDB = DBPropertiesUtils.toMap(mappaDestinazione);
  227.            
  228.             for (String propKey: mapDB.keySet()) {
  229.                 System.out.println("Item ["+propKey+"] Value ["+mapDB.get(propKey)+"]");
  230.             }
  231.            
  232.             System.out.println("----------------------");
  233.            
  234.            
  235.             System.out.println("TEST CONFIG 1 FINE ");
  236.         }catch(Exception e) {
  237.             e.printStackTrace(System.err);
  238.         }
  239.     }
  240. }