diff --git a/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/ServletArc.java b/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/ServletArc.java index 85100d8f1..be752441f 100644 --- a/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/ServletArc.java +++ b/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/ServletArc.java @@ -71,7 +71,7 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) { if (request.getParameter("requests") != null) { - dsnRequest = buildRequest(request); + dsnRequest = validateRequest(new JSONObject(request.getParameter("requests"))); if (SecurityDao.securityAccessAndTracing(request, response, dsnRequest)) { @@ -97,10 +97,7 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) { * @param request * @return */ - private JSONObject buildRequest(HttpServletRequest request) { - - // get parameters from request - JSONObject returned = new JSONObject(request.getParameter("requests")); + protected JSONObject validateRequest(JSONObject returned) { if (returned.isNull(JsonKeys.FORMAT.getKey())) { returned.put(JsonKeys.FORMAT.getKey(), ExportFormat.BINARY.getFormat()); diff --git a/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/actions/SendResponse.java b/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/actions/SendResponse.java index 03a11026f..d6319e32e 100644 --- a/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/actions/SendResponse.java +++ b/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/actions/SendResponse.java @@ -1,8 +1,8 @@ package fr.insee.arc.ws.services.importServlet.actions; import java.io.IOException; +import java.io.OutputStream; -import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import org.apache.logging.log4j.LogManager; @@ -17,36 +17,39 @@ */ public class SendResponse { - private static final Logger LOGGER = LogManager.getLogger(SendResponse.class); + private static final Logger LOGGER = LogManager.getLogger(SendResponse.class); - private ServletOutputStream wr; + private OutputStream wr; private HttpServletResponse response; - public SendResponse( HttpServletResponse response ){ + public SendResponse(OutputStream os) { + this.wr = os; + } + + public SendResponse(HttpServletResponse response) { this.response = response; try { this.response.setBufferSize(128 * 1024); - this.wr=this.response.getOutputStream(); - } - catch (IOException e) { + this.wr = this.response.getOutputStream(); + } catch (IOException e) { StaticLoggerDispatcher.error(LOGGER, "** Error in servlet SendResponse **"); } } - - - /**Ecrit la chaîne de caractères dans le flux de réponse compressé. + /** + * Ecrit la chaîne de caractères dans le flux de réponse compressé. + * * @param string */ - public void send( String string ){ - try { - this.wr.write( string.getBytes() );//"UTF-8" - } catch (IOException ex) { - LoggerHelper.errorGenTextAsComment(getClass(), "send()", LOGGER, ex); - } + public void send(String string) { + try { + this.wr.write(string.getBytes());// "UTF-8" + } catch (IOException ex) { + LoggerHelper.errorGenTextAsComment(getClass(), "send()", LOGGER, ex); + } } - public void sendError(ArcException e){ + public void sendError(ArcException e) { try { this.response.sendError(500, e.getMessage()); } catch (IOException e1) { @@ -54,11 +57,11 @@ public void sendError(ArcException e){ } } - - /**Fermeture du flux. + /** + * Fermeture du flux. * */ - public void endSending(){ + public void endSending() { try { this.wr.flush(); this.wr.close(); @@ -67,8 +70,8 @@ public void endSending(){ } } - public ServletOutputStream getWr() { + public OutputStream getWr() { return wr; } - + } diff --git a/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/dao/ClientDao.java b/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/dao/ClientDao.java index 0e4305945..bf2020b38 100644 --- a/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/dao/ClientDao.java +++ b/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/dao/ClientDao.java @@ -6,7 +6,6 @@ import java.util.List; import java.util.Map; -import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.json.JSONObject; diff --git a/arc-ws/src/test/java/fr/insee/arc/ws/services/importServlet/ImportStep1InitializeClientTablesServiceTest.java b/arc-ws/src/test/java/fr/insee/arc/ws/services/importServlet/ImportStep1InitializeClientTablesServiceTest.java new file mode 100644 index 000000000..eaa2cf35b --- /dev/null +++ b/arc-ws/src/test/java/fr/insee/arc/ws/services/importServlet/ImportStep1InitializeClientTablesServiceTest.java @@ -0,0 +1,193 @@ +package fr.insee.arc.ws.services.importServlet; + +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.sql.SQLException; + +import org.json.JSONObject; +import org.junit.Test; + +import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder; +import fr.insee.arc.utils.dao.SQL; +import fr.insee.arc.utils.dao.UtilitaireDao; +import fr.insee.arc.utils.exception.ArcException; +import fr.insee.arc.utils.query.InitializeQueryTest; +import fr.insee.arc.ws.services.importServlet.actions.SendResponse; + +public class ImportStep1InitializeClientTablesServiceTest extends ServletArc { + + /** + * + */ + private static final long serialVersionUID = -7832574224892526397L; + + + + @Test + public void testExecute() throws ArcException, SQLException { + + InitializeQueryTest.buildPropertiesWithoutScalability(null); + + destroyTestData(); + initializeTestData(); + + JSONObject jsonDsnStep1 = new JSONObject( + "{\"familleNorme\":\"DSN\",\"periodicite\":\"M\",\"service\":\"arcClient\",\"validiteSup\":\"2032-03-01\",\"format\":\"csv_gzip\",\"reprise\":false,\"client\":\"ARTEMIS\",\"environnement\":\"arc_bas1\"}"); + + jsonDsnStep1= validateRequest(jsonDsnStep1); + + ImportStep1InitializeClientTablesService imp = new ImportStep1InitializeClientTablesService(jsonDsnStep1); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + SendResponse sentResponse = new SendResponse(bos); + + imp.execute(sentResponse); + + testCreateAndDropWsPending(); + + testCreateTableNmcl(); + testCreateTableVarMetier(); + testCreateTableTableMetier(); + testCreateTableTableFamille(); + testCreateTableTablePeriodicite(); + + + + destroyTestData(); + } + + private void testCreateAndDropWsPending() throws ArcException { + + // check that the parallel thread that create tables drop the table ws_pending + + // it should be done in less than 50 iteration, test data is very little + int maxIteration = 50; + int i=0; + + while (i0); + assertTrue(i + assertTrue(UtilitaireDao.get(0).isTableExiste(InitializeQueryTest.c, "arc_bas1.ARTEMIS_%_nmcl_table1")); + assertTrue(UtilitaireDao.get(0).isTableExiste(InitializeQueryTest.c, "arc_bas1.ARTEMIS_%_nmcl_table2")); + } + + private void testCreateTableVarMetier() throws ArcException { + // table image created should be like arc_bas1.ARTEMIS_timestamp_ + assertTrue(UtilitaireDao.get(0).isTableExiste(InitializeQueryTest.c, "arc_bas1.ARTEMIS_%_mod_variable_metier")); + } + + private void testCreateTableTableMetier() throws ArcException { + // table image created should be like arc_bas1.ARTEMIS_timestamp_ + assertTrue(UtilitaireDao.get(0).isTableExiste(InitializeQueryTest.c, "arc_bas1.ARTEMIS_%_mod_table_metier")); + } + + private void testCreateTableTableFamille() throws ArcException { + // table image created should be like arc_bas1.ARTEMIS_timestamp_ + assertTrue(UtilitaireDao.get(0).isTableExiste(InitializeQueryTest.c, "arc_bas1.ARTEMIS_%_ext_mod_famille")); + } + + private void testCreateTableTablePeriodicite() throws ArcException { + // table image created should be like arc_bas1.ARTEMIS_timestamp_ + assertTrue(UtilitaireDao.get(0).isTableExiste(InitializeQueryTest.c, "arc_bas1.ARTEMIS_%_ext_mod_periodicite")); + } + + + /** + * initialize data for the tests + * @throws SQLException + * @throws ArcException + */ + private void initializeTestData() throws SQLException, ArcException { + + ArcPreparedStatementBuilder query; + + query = new ArcPreparedStatementBuilder(); + + query.append("CREATE SCHEMA arc;"); + query.append("CREATE SCHEMA arc_bas1;"); + + + // family and client tables + query.append("CREATE TABLE arc.ihm_client AS "); + query.append("SELECT 'DSN' as id_famille,'ARTEMIS' as id_application UNION ALL "); + query.append("SELECT 'DSN' as id_famille,'DSNFLASH' as id_application"); + query.append(SQL.END_QUERY); + + query.append("CREATE TABLE arc.ihm_famille AS SELECT 'DSN' as id_famille"); + query.append(SQL.END_QUERY); + + query.append("CREATE TABLE arc_bas1.mod_table_metier AS "); + query.append("SELECT 'DSN' as id_famille,'mapping_dsn_test1_ok' as nom_table_metier UNION ALL "); + query.append("SELECT 'PASRAU' as id_famille,'mapping_pasrau_test_ok' as nom_table_metier"); + query.append(SQL.END_QUERY); + + query.append("CREATE TABLE arc_bas1.mod_variable_metier AS SELECT 'DSN' as id_famille, 'mapping_dsn_test1_ok' as nom_table_metier, 'id_source' as nom_variable_metier"); + query.append(SQL.END_QUERY); + + // pilotage tables + query.append("CREATE TABLE arc_bas1.pilotage_fichier AS "); + query.append("SELECT 'file_to_retrieve.xml' as id_source, 'PHASE3V1' as id_norme, '2023-10-01' as validite,'M' as periodicite"); + query.append(", 'MAPPING' as phase_traitement, '{OK}'::text[] as etat_traitement, '2023-11-30 10:29:47.000'::timestamp as date_traitement"); + query.append(", null::text[] as client, null::timestamp[] as date_client"); + query.append(" UNION ALL "); + // file that mustn't be retrieved when reprise is false and family is DSN + query.append("SELECT 'file_not_to_retrieve_when_reprise_false.xml' as id_source, 'PHASE3V1' as id_norme, '2023-10-01' as validite,'M' as periodicite"); + query.append(", 'MAPPING' as phase_traitement, '{OK}'::text[] as etat_traitement, '2023-11-30 10:29:47.000'::timestamp as date_traitement"); + query.append(", '{ARTEMIS}'::text[] as client, '{2023-11-30 10:29:47.000}'::timestamp[] as date_client");; + query.append(SQL.END_QUERY); + + // norme table used to retrieve family of data + query.append("CREATE TABLE arc_bas1.norme AS "); + query.append("SELECT 'PHASE3V1' as id_norme, 'DSN' as id_famille UNION ALL "); + query.append("SELECT 'PASRAU' as id_norme, 'PASRAU' as id_famille"); + query.append(SQL.END_QUERY); + + // data tables containing two files + // one had already been retrieved by client 'ARTEMIS', the other hadn't been retrieved yet + query.append("CREATE TABLE arc_bas1.mapping_dsn_test1_ok AS "); + query.append("SELECT 'file_to_retrieve.xml' as id_source, 'data_of_file_to_retrieve' as data UNION ALL "); + query.append("SELECT 'file_not_to_retrieve_when_reprise_false.xml' as id_source, 'data_of_file_not_to_retrieve_when_reprise_false' as data"); + query.append(SQL.END_QUERY); + + // nomenclature tables + query.append("CREATE TABLE arc_bas1.nmcl_table1 AS SELECT 1 as data"); + query.append(SQL.END_QUERY); + query.append("CREATE TABLE arc_bas1.nmcl_table2 AS SELECT 1 as data"); + query.append(SQL.END_QUERY); + query.append("CREATE TABLE arc.ext_mod_periodicite AS SELECT 1 as id, 'A' as VAL"); + query.append(SQL.END_QUERY); + + UtilitaireDao.get(0).executeImmediate(InitializeQueryTest.c, query); + } + + + + /** + * destroy data for the tests + * @throws SQLException + * @throws ArcException + */ + private void destroyTestData() throws SQLException, ArcException { + + ArcPreparedStatementBuilder query; + + query = new ArcPreparedStatementBuilder(); + + query.append("DROP SCHEMA IF EXISTS arc CASCADE;"); + query.append("DROP SCHEMA IF EXISTS arc_bas1 CASCADE;"); + UtilitaireDao.get(0).executeImmediate(InitializeQueryTest.c, query); + } + + + + +}