diff --git a/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/ImportStep1InitializeClientTablesService.java b/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/ImportStep1InitializeClientTablesService.java index 614454c2f..80574ef1a 100644 --- a/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/ImportStep1InitializeClientTablesService.java +++ b/arc-ws/src/main/java/fr/insee/arc/ws/services/importServlet/ImportStep1InitializeClientTablesService.java @@ -147,20 +147,12 @@ public void run() { executeIf(ExportSource.METADATA, () -> clientDao.createTableFamille()); executeIf(ExportSource.METADATA, () -> clientDao.createTablePeriodicite()); } catch (ArcException e) { - try { - clientDao.createTableWsKO(); - } catch (ArcException e1) { - new ArcException(ArcExceptionMessage.DATABASE_CONNECTION_FAILED).logFullException(); - } + clientDao.registerWsKO(); } finally { try { clientDao.dropTableWsPending(); } catch (ArcException e) { - try { - clientDao.createTableWsKO(); - } catch (ArcException e1) { - new ArcException(ArcExceptionMessage.DATABASE_CONNECTION_FAILED).logFullException(); - } + clientDao.registerWsKO(); } } 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 bf2020b38..2391f2ef8 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 @@ -52,6 +52,8 @@ public class ClientDao { // the tablename of the table that tracks tables left to retrieved private String tableWsTracking; + + private String tableWsInfo; private Connection connection; @@ -67,7 +69,7 @@ public ClientDao(ArcClientIdentifier arcClientIdentifier) { timestamp); this.tableWsTracking = TableNaming.buildTableNameWithTokens(environnement, ViewEnum.WS_TRACKING, client, timestamp); - + this.tableWsInfo = TableNaming.buildTableNameWithTokens(environnement, ViewEnum.WS_INFO, client, timestamp); } /** @@ -84,7 +86,6 @@ public boolean verificationClientFamille() throws ArcException { .append(" LIMIT 1);"); String bool = UtilitaireDao.get(0).executeRequestWithoutMetadata(connection, request).get(0).get(0); - return bool.equals("t"); } @@ -192,7 +193,7 @@ public void createTableTrackRetrievedTables() throws ArcException { ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder(); query.build(SQL.DROP, SQL.TABLE, SQL.IF_EXISTS, this.tableWsTracking, SQL.END_QUERY); query.build(SQL.CREATE, SQL.TABLE, this.tableWsTracking, - " (tracking_type text, nod text, table_to_retrieve text) ", SQL.END_QUERY); + " (id serial, tracking_type text, nod text, table_to_retrieve text) ", SQL.END_QUERY); UtilitaireDao.get(0).executeRequest(connection, query); registerTableToBeRetrieved(ExportTrackingType.TRACK, ArcDatabase.COORDINATOR, this.tableWsTracking); @@ -403,6 +404,7 @@ public TableToRetrieve getAClientTableByType(ExportTrackingType type) throws Arc ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder(); query.build(SQL.SELECT, "nod, table_to_retrieve", SQL.FROM, this.tableWsTracking); query.build(SQL.WHERE, "tracking_type=", query.quoteText(type.toString())); + query.build(SQL.ORDER_BY, "id"); query.build(SQL.LIMIT, "1"); Map> content = new GenericBean(UtilitaireDao.get(0).executeRequest(connection, query)) @@ -425,6 +427,7 @@ public TableToRetrieve getAClientTableByName(String tableName) throws ArcExcepti ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder(); query.build(SQL.SELECT, "nod, table_to_retrieve", SQL.FROM, this.tableWsTracking); query.build(SQL.WHERE, "table_to_retrieve=", query.quoteText(tableName)); + query.build(SQL.ORDER_BY, "id"); query.build(SQL.LIMIT, "1"); Map> content = new GenericBean(UtilitaireDao.get(0).executeRequest(connection, query)) @@ -482,8 +485,6 @@ public void dropPendingClientTables(int connectionId) throws ArcException { */ public void createTableWsInfo() throws ArcException { - String tableWsInfo = TableNaming.buildTableNameWithTokens(environnement, ViewEnum.WS_INFO, client, timestamp); - ArcPreparedStatementBuilder requete = new ArcPreparedStatementBuilder(); requete.append("\n DROP TABLE IF EXISTS " + tableWsInfo + ";"); @@ -500,8 +501,12 @@ public void createTableWsInfo() throws ArcException { } - public void createTableWsKO() throws ArcException { - registerTableToBeRetrieved(ExportTrackingType.KO, ArcDatabase.COORDINATOR, ViewEnum.WS_KO.toString()); + public void registerWsKO() { + try { + registerTableToBeRetrieved(ExportTrackingType.KO, ArcDatabase.COORDINATOR, ViewEnum.WS_KO.toString()); + } catch (ArcException e1) { + new ArcException(ArcExceptionMessage.DATABASE_CONNECTION_FAILED).logFullException(); + } } public void dropTableWsPending() throws ArcException { 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 index eaa2cf35b..a9a5a753e 100644 --- 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 @@ -6,6 +6,8 @@ import java.sql.SQLException; import org.json.JSONObject; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import fr.insee.arc.core.dataobjects.ArcPreparedStatementBuilder; @@ -23,37 +25,53 @@ public class ImportStep1InitializeClientTablesServiceTest extends ServletArc { private static final long serialVersionUID = -7832574224892526397L; - - @Test - public void testExecute() throws ArcException, SQLException { + @BeforeClass + public static void setup() throws SQLException, ArcException { 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\"}"); + } + + @AfterClass + public static void tearDown() throws SQLException, ArcException { + destroyTestData(); + } - jsonDsnStep1= validateRequest(jsonDsnStep1); - - ImportStep1InitializeClientTablesService imp = new ImportStep1InitializeClientTablesService(jsonDsnStep1); + private String executeImportStep1(JSONObject clientJsonInput) throws ArcException + { + JSONObject clientJsonInputValidated= validateRequest(clientJsonInput); + ImportStep1InitializeClientTablesService imp = new ImportStep1InitializeClientTablesService(clientJsonInputValidated); ByteArrayOutputStream bos = new ByteArrayOutputStream(); SendResponse sentResponse = new SendResponse(bos); - imp.execute(sentResponse); + return sentResponse.getWr().toString(); + } + + + @Test(expected = ArcException.class) + public void testExecuteFamilyNotValid() throws ArcException { + JSONObject clientJsonInput = new JSONObject( + "{\"familleNorme\":\"RESIL\",\"periodicite\":\"M\",\"service\":\"arcClient\",\"validiteSup\":\"2032-03-01\",\"format\":\"csv_gzip\",\"reprise\":false,\"client\":\"ARTEMIS\",\"environnement\":\"arc_bas1\"}"); + executeImportStep1(clientJsonInput); + } + + + @Test + public void testExecute() throws ArcException { + + JSONObject clientJsonInput = new JSONObject( + "{\"familleNorme\":\"DSN\",\"periodicite\":\"M\",\"service\":\"arcClient\",\"validiteSup\":\"2032-03-01\",\"format\":\"csv_gzip\",\"reprise\":false,\"client\":\"ARTEMIS\",\"environnement\":\"arc_bas1\"}"); + + executeImportStep1(clientJsonInput); testCreateAndDropWsPending(); - testCreateTableNmcl(); testCreateTableVarMetier(); testCreateTableTableMetier(); testCreateTableTableFamille(); testCreateTableTablePeriodicite(); - - - - destroyTestData(); } private void testCreateAndDropWsPending() throws ArcException { @@ -106,7 +124,7 @@ private void testCreateTableTablePeriodicite() throws ArcException { * @throws SQLException * @throws ArcException */ - private void initializeTestData() throws SQLException, ArcException { + private static void initializeTestData() throws SQLException, ArcException { ArcPreparedStatementBuilder query; @@ -176,7 +194,7 @@ private void initializeTestData() throws SQLException, ArcException { * @throws SQLException * @throws ArcException */ - private void destroyTestData() throws SQLException, ArcException { + private static void destroyTestData() throws SQLException, ArcException { ArcPreparedStatementBuilder query; diff --git a/arc-ws/src/test/java/fr/insee/arc/ws/services/importServlet/ImportStep1InitializeClientTablesServiceTestKO.java b/arc-ws/src/test/java/fr/insee/arc/ws/services/importServlet/ImportStep1InitializeClientTablesServiceTestKO.java new file mode 100644 index 000000000..bbb5504ac --- /dev/null +++ b/arc-ws/src/test/java/fr/insee/arc/ws/services/importServlet/ImportStep1InitializeClientTablesServiceTestKO.java @@ -0,0 +1,182 @@ +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.AfterClass; +import org.junit.BeforeClass; +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; +import fr.insee.arc.ws.services.importServlet.bo.ExportTrackingType; + +public class ImportStep1InitializeClientTablesServiceTestKO extends ServletArc { + + /** + * + */ + private static final long serialVersionUID = -7832574224892526397L; + + + @BeforeClass + public static void setup() throws SQLException, ArcException { + + InitializeQueryTest.buildPropertiesWithoutScalability(null); + + destroyTestData(); + initializeTestData(); + } + + @AfterClass + public static void tearDown() throws SQLException, ArcException { + destroyTestData(); + } + + private String executeImportStep1(JSONObject clientJsonInput) throws ArcException + { + JSONObject clientJsonInputValidated= validateRequest(clientJsonInput); + ImportStep1InitializeClientTablesService imp = new ImportStep1InitializeClientTablesService(clientJsonInputValidated); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + SendResponse sentResponse = new SendResponse(bos); + imp.execute(sentResponse); + return sentResponse.getWr().toString(); + } + + + @Test + public void testExecute() throws ArcException { + + JSONObject clientJsonInput = new JSONObject( + "{\"familleNorme\":\"DSN\",\"periodicite\":\"M\",\"service\":\"arcClient\",\"validiteSup\":\"2032-03-01\",\"format\":\"csv_gzip\",\"reprise\":false,\"client\":\"ARTEMIS\",\"environnement\":\"arc_bas1\"}"); + + String arcResponse = executeImportStep1(clientJsonInput); + + testCreateAndDropWsPending(arcResponse); + + testCreateTableWsKo(arcResponse); + + } + + private void testCreateAndDropWsPending(String arcResponse) 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