@Test @RunWithCustomExecutor public void testExecute() throws Exception { VitamThreadUtils.getVitamSession().setTenantId(1); WorkerParameters params = mock(WorkerParameters.class); HandlerIO handlerIO = mock(HandlerIO.class); File defaultStorageInformation = PropertiesUtils.getResourceFile("PrepareStorageInfoActionHandler/defaultStorageInformation.json"); when(storageClient.getStorageInformation(eq(VitamConfiguration.getDefaultStrategy()))).thenReturn(JsonHandler.getFromFile(defaultStorageInformation)); File testStorageInformation = PropertiesUtils.getResourceFile("PrepareStorageInfoActionHandler/testStorageInformation.json"); when(storageClient.getStorageInformation(eq("test"))).thenReturn(JsonHandler.getFromFile(testStorageInformation)); File output = new File(temporaryFolder.newFolder(), "storageInfo.json"); File input = PropertiesUtils.getResourceFile("PrepareStorageInfoActionHandler/ingestContractWithDetail.json"); String filePath = output.getAbsolutePath(); when(handlerIO.getOutput(0)).thenReturn(new ProcessingUri(UriPrefix.WORKSPACE, filePath)); when(handlerIO.getNewLocalFile(filePath)).thenReturn(output); when(handlerIO.getInput(0)).thenReturn(input); try (PrepareStorageInfoActionHandler instance = new PrepareStorageInfoActionHandler(storageClientFactory)){ ItemStatus response = instance.execute(params, handlerIO); assertEquals(StatusCode.OK, response.getGlobalStatus()); verify(handlerIO).addOutputResult(0, output, true, false); JsonNode storageInfo = JsonHandler.getFromFile(output); assertThat(storageInfo.get(VitamConfiguration.getDefaultStrategy())).isNotNull(); assertThat(storageInfo.get(VitamConfiguration.getDefaultStrategy()).get(SedaConstants.TAG_NB).asInt()).isEqualTo(2); assertThat((storageInfo.get(VitamConfiguration.getDefaultStrategy()).get(SedaConstants.OFFER_IDS)).size()).isEqualTo(2); assertThat((storageInfo.get(VitamConfiguration.getDefaultStrategy()).get(SedaConstants.OFFER_IDS)).get(0).asText()).isEqualTo("offer1"); assertThat((storageInfo.get(VitamConfiguration.getDefaultStrategy()).get(SedaConstants.OFFER_IDS)).get(1).asText()).isEqualTo("offer2"); assertThat(storageInfo.get(VitamConfiguration.getDefaultStrategy()).get(SedaConstants.STRATEGY_ID).asText()).isEqualTo(VitamConfiguration.getDefaultStrategy()); assertThat(storageInfo.get("test")).isNotNull(); assertThat(storageInfo.get("test").get(SedaConstants.TAG_NB).asInt()).isEqualTo(3); assertThat((storageInfo.get("test").get(SedaConstants.OFFER_IDS)).size()).isEqualTo(3); assertThat((storageInfo.get("test").get(SedaConstants.OFFER_IDS)).get(0).asText()).isEqualTo("offer1"); assertThat((storageInfo.get("test").get(SedaConstants.OFFER_IDS)).get(1).asText()).isEqualTo("offer2"); assertThat((storageInfo.get("test").get(SedaConstants.OFFER_IDS)).get(2).asText()).isEqualTo("offer3"); assertThat(storageInfo.get("test").get(SedaConstants.STRATEGY_ID).asText()).isEqualTo("test"); } }
@Override public ItemStatus execute(WorkerParameters params, HandlerIO handlerIO) { checkMandatoryParameters(params); final ItemStatus itemStatus = new ItemStatus(HANDLER_ID); try { checkMandatoryIOParameter(handlerIO); ManagementContractModel managementContract = loadManagementContractFromWorkspace(handlerIO); Map<String, JsonNode> storageCapacityNodeByStrategyId = new HashMap<>(); try (final StorageClient storageClient = storageClientFactory.getClient()) { storageCapacityNodeByStrategyId.put(VitamConfiguration.getDefaultStrategy(), storageClient.getStorageInformation(VitamConfiguration.getDefaultStrategy())); if (managementContract != null && managementContract.getStorage() != null) { if (managementContract.getStorage().getUnitStrategy() != null && !storageCapacityNodeByStrategyId .containsKey(managementContract.getStorage().getUnitStrategy())) { storageCapacityNodeByStrategyId.put(managementContract.getStorage().getUnitStrategy(), storageClient.getStorageInformation(managementContract.getStorage().getUnitStrategy())); } if (managementContract.getStorage().getObjectGroupStrategy() != null && !storageCapacityNodeByStrategyId .containsKey(managementContract.getStorage().getObjectGroupStrategy())) { storageCapacityNodeByStrategyId.put(managementContract.getStorage().getObjectGroupStrategy(), storageClient.getStorageInformation( managementContract.getStorage().getObjectGroupStrategy())); } if (managementContract.getStorage().getObjectStrategy() != null && !storageCapacityNodeByStrategyId .containsKey(managementContract.getStorage().getObjectStrategy())) { storageCapacityNodeByStrategyId.put(managementContract.getStorage().getObjectStrategy(), storageClient .getStorageInformation(managementContract.getStorage().getObjectStrategy())); } } } ObjectNode strategiesInformation = JsonHandler.createObjectNode(); for(Entry<String, JsonNode> stategyStorageCapacities : storageCapacityNodeByStrategyId.entrySet()) { final StorageInformation[] storageInformation = JsonHandler.getFromJsonNode(stategyStorageCapacities.getValue().get("capacities"), StorageInformation[].class); strategiesInformation.set(stategyStorageCapacities.getKey(), generateStorageInfoNode(stategyStorageCapacities.getKey(), storageInformation)); } storeStrategiesStorageInfo(handlerIO, strategiesInformation); itemStatus.increment(StatusCode.OK); } catch (ProcessingException | StorageNotFoundClientException | StorageServerClientException | InvalidParseOperationException e) { LOGGER.error(e); itemStatus.increment(StatusCode.FATAL); } return new ItemStatus(HANDLER_ID).setItemsStatus(HANDLER_ID, itemStatus); }
PrepareStorageInfoActionHandler extends ActionHandler { @Override public ItemStatus execute(WorkerParameters params, HandlerIO handlerIO) { checkMandatoryParameters(params); final ItemStatus itemStatus = new ItemStatus(HANDLER_ID); try { checkMandatoryIOParameter(handlerIO); ManagementContractModel managementContract = loadManagementContractFromWorkspace(handlerIO); Map<String, JsonNode> storageCapacityNodeByStrategyId = new HashMap<>(); try (final StorageClient storageClient = storageClientFactory.getClient()) { storageCapacityNodeByStrategyId.put(VitamConfiguration.getDefaultStrategy(), storageClient.getStorageInformation(VitamConfiguration.getDefaultStrategy())); if (managementContract != null && managementContract.getStorage() != null) { if (managementContract.getStorage().getUnitStrategy() != null && !storageCapacityNodeByStrategyId .containsKey(managementContract.getStorage().getUnitStrategy())) { storageCapacityNodeByStrategyId.put(managementContract.getStorage().getUnitStrategy(), storageClient.getStorageInformation(managementContract.getStorage().getUnitStrategy())); } if (managementContract.getStorage().getObjectGroupStrategy() != null && !storageCapacityNodeByStrategyId .containsKey(managementContract.getStorage().getObjectGroupStrategy())) { storageCapacityNodeByStrategyId.put(managementContract.getStorage().getObjectGroupStrategy(), storageClient.getStorageInformation( managementContract.getStorage().getObjectGroupStrategy())); } if (managementContract.getStorage().getObjectStrategy() != null && !storageCapacityNodeByStrategyId .containsKey(managementContract.getStorage().getObjectStrategy())) { storageCapacityNodeByStrategyId.put(managementContract.getStorage().getObjectStrategy(), storageClient .getStorageInformation(managementContract.getStorage().getObjectStrategy())); } } } ObjectNode strategiesInformation = JsonHandler.createObjectNode(); for(Entry<String, JsonNode> stategyStorageCapacities : storageCapacityNodeByStrategyId.entrySet()) { final StorageInformation[] storageInformation = JsonHandler.getFromJsonNode(stategyStorageCapacities.getValue().get("capacities"), StorageInformation[].class); strategiesInformation.set(stategyStorageCapacities.getKey(), generateStorageInfoNode(stategyStorageCapacities.getKey(), storageInformation)); } storeStrategiesStorageInfo(handlerIO, strategiesInformation); itemStatus.increment(StatusCode.OK); } catch (ProcessingException | StorageNotFoundClientException | StorageServerClientException | InvalidParseOperationException e) { LOGGER.error(e); itemStatus.increment(StatusCode.FATAL); } return new ItemStatus(HANDLER_ID).setItemsStatus(HANDLER_ID, itemStatus); } }
PrepareStorageInfoActionHandler extends ActionHandler { @Override public ItemStatus execute(WorkerParameters params, HandlerIO handlerIO) { checkMandatoryParameters(params); final ItemStatus itemStatus = new ItemStatus(HANDLER_ID); try { checkMandatoryIOParameter(handlerIO); ManagementContractModel managementContract = loadManagementContractFromWorkspace(handlerIO); Map<String, JsonNode> storageCapacityNodeByStrategyId = new HashMap<>(); try (final StorageClient storageClient = storageClientFactory.getClient()) { storageCapacityNodeByStrategyId.put(VitamConfiguration.getDefaultStrategy(), storageClient.getStorageInformation(VitamConfiguration.getDefaultStrategy())); if (managementContract != null && managementContract.getStorage() != null) { if (managementContract.getStorage().getUnitStrategy() != null && !storageCapacityNodeByStrategyId .containsKey(managementContract.getStorage().getUnitStrategy())) { storageCapacityNodeByStrategyId.put(managementContract.getStorage().getUnitStrategy(), storageClient.getStorageInformation(managementContract.getStorage().getUnitStrategy())); } if (managementContract.getStorage().getObjectGroupStrategy() != null && !storageCapacityNodeByStrategyId .containsKey(managementContract.getStorage().getObjectGroupStrategy())) { storageCapacityNodeByStrategyId.put(managementContract.getStorage().getObjectGroupStrategy(), storageClient.getStorageInformation( managementContract.getStorage().getObjectGroupStrategy())); } if (managementContract.getStorage().getObjectStrategy() != null && !storageCapacityNodeByStrategyId .containsKey(managementContract.getStorage().getObjectStrategy())) { storageCapacityNodeByStrategyId.put(managementContract.getStorage().getObjectStrategy(), storageClient .getStorageInformation(managementContract.getStorage().getObjectStrategy())); } } } ObjectNode strategiesInformation = JsonHandler.createObjectNode(); for(Entry<String, JsonNode> stategyStorageCapacities : storageCapacityNodeByStrategyId.entrySet()) { final StorageInformation[] storageInformation = JsonHandler.getFromJsonNode(stategyStorageCapacities.getValue().get("capacities"), StorageInformation[].class); strategiesInformation.set(stategyStorageCapacities.getKey(), generateStorageInfoNode(stategyStorageCapacities.getKey(), storageInformation)); } storeStrategiesStorageInfo(handlerIO, strategiesInformation); itemStatus.increment(StatusCode.OK); } catch (ProcessingException | StorageNotFoundClientException | StorageServerClientException | InvalidParseOperationException e) { LOGGER.error(e); itemStatus.increment(StatusCode.FATAL); } return new ItemStatus(HANDLER_ID).setItemsStatus(HANDLER_ID, itemStatus); } PrepareStorageInfoActionHandler(); @VisibleForTesting  PrepareStorageInfoActionHandler(StorageClientFactory storageClientFactory); }
PrepareStorageInfoActionHandler extends ActionHandler { @Override public ItemStatus execute(WorkerParameters params, HandlerIO handlerIO) { checkMandatoryParameters(params); final ItemStatus itemStatus = new ItemStatus(HANDLER_ID); try { checkMandatoryIOParameter(handlerIO); ManagementContractModel managementContract = loadManagementContractFromWorkspace(handlerIO); Map<String, JsonNode> storageCapacityNodeByStrategyId = new HashMap<>(); try (final StorageClient storageClient = storageClientFactory.getClient()) { storageCapacityNodeByStrategyId.put(VitamConfiguration.getDefaultStrategy(), storageClient.getStorageInformation(VitamConfiguration.getDefaultStrategy())); if (managementContract != null && managementContract.getStorage() != null) { if (managementContract.getStorage().getUnitStrategy() != null && !storageCapacityNodeByStrategyId .containsKey(managementContract.getStorage().getUnitStrategy())) { storageCapacityNodeByStrategyId.put(managementContract.getStorage().getUnitStrategy(), storageClient.getStorageInformation(managementContract.getStorage().getUnitStrategy())); } if (managementContract.getStorage().getObjectGroupStrategy() != null && !storageCapacityNodeByStrategyId .containsKey(managementContract.getStorage().getObjectGroupStrategy())) { storageCapacityNodeByStrategyId.put(managementContract.getStorage().getObjectGroupStrategy(), storageClient.getStorageInformation( managementContract.getStorage().getObjectGroupStrategy())); } if (managementContract.getStorage().getObjectStrategy() != null && !storageCapacityNodeByStrategyId .containsKey(managementContract.getStorage().getObjectStrategy())) { storageCapacityNodeByStrategyId.put(managementContract.getStorage().getObjectStrategy(), storageClient .getStorageInformation(managementContract.getStorage().getObjectStrategy())); } } } ObjectNode strategiesInformation = JsonHandler.createObjectNode(); for(Entry<String, JsonNode> stategyStorageCapacities : storageCapacityNodeByStrategyId.entrySet()) { final StorageInformation[] storageInformation = JsonHandler.getFromJsonNode(stategyStorageCapacities.getValue().get("capacities"), StorageInformation[].class); strategiesInformation.set(stategyStorageCapacities.getKey(), generateStorageInfoNode(stategyStorageCapacities.getKey(), storageInformation)); } storeStrategiesStorageInfo(handlerIO, strategiesInformation); itemStatus.increment(StatusCode.OK); } catch (ProcessingException | StorageNotFoundClientException | StorageServerClientException | InvalidParseOperationException e) { LOGGER.error(e); itemStatus.increment(StatusCode.FATAL); } return new ItemStatus(HANDLER_ID).setItemsStatus(HANDLER_ID, itemStatus); } PrepareStorageInfoActionHandler(); @VisibleForTesting  PrepareStorageInfoActionHandler(StorageClientFactory storageClientFactory); static final String getId(); @Override ItemStatus execute(WorkerParameters params, HandlerIO handlerIO); @Override void checkMandatoryIOParameter(HandlerIO handler); }
PrepareStorageInfoActionHandler extends ActionHandler { @Override public ItemStatus execute(WorkerParameters params, HandlerIO handlerIO) { checkMandatoryParameters(params); final ItemStatus itemStatus = new ItemStatus(HANDLER_ID); try { checkMandatoryIOParameter(handlerIO); ManagementContractModel managementContract = loadManagementContractFromWorkspace(handlerIO); Map<String, JsonNode> storageCapacityNodeByStrategyId = new HashMap<>(); try (final StorageClient storageClient = storageClientFactory.getClient()) { storageCapacityNodeByStrategyId.put(VitamConfiguration.getDefaultStrategy(), storageClient.getStorageInformation(VitamConfiguration.getDefaultStrategy())); if (managementContract != null && managementContract.getStorage() != null) { if (managementContract.getStorage().getUnitStrategy() != null && !storageCapacityNodeByStrategyId .containsKey(managementContract.getStorage().getUnitStrategy())) { storageCapacityNodeByStrategyId.put(managementContract.getStorage().getUnitStrategy(), storageClient.getStorageInformation(managementContract.getStorage().getUnitStrategy())); } if (managementContract.getStorage().getObjectGroupStrategy() != null && !storageCapacityNodeByStrategyId .containsKey(managementContract.getStorage().getObjectGroupStrategy())) { storageCapacityNodeByStrategyId.put(managementContract.getStorage().getObjectGroupStrategy(), storageClient.getStorageInformation( managementContract.getStorage().getObjectGroupStrategy())); } if (managementContract.getStorage().getObjectStrategy() != null && !storageCapacityNodeByStrategyId .containsKey(managementContract.getStorage().getObjectStrategy())) { storageCapacityNodeByStrategyId.put(managementContract.getStorage().getObjectStrategy(), storageClient .getStorageInformation(managementContract.getStorage().getObjectStrategy())); } } } ObjectNode strategiesInformation = JsonHandler.createObjectNode(); for(Entry<String, JsonNode> stategyStorageCapacities : storageCapacityNodeByStrategyId.entrySet()) { final StorageInformation[] storageInformation = JsonHandler.getFromJsonNode(stategyStorageCapacities.getValue().get("capacities"), StorageInformation[].class); strategiesInformation.set(stategyStorageCapacities.getKey(), generateStorageInfoNode(stategyStorageCapacities.getKey(), storageInformation)); } storeStrategiesStorageInfo(handlerIO, strategiesInformation); itemStatus.increment(StatusCode.OK); } catch (ProcessingException | StorageNotFoundClientException | StorageServerClientException | InvalidParseOperationException e) { LOGGER.error(e); itemStatus.increment(StatusCode.FATAL); } return new ItemStatus(HANDLER_ID).setItemsStatus(HANDLER_ID, itemStatus); } PrepareStorageInfoActionHandler(); @VisibleForTesting  PrepareStorageInfoActionHandler(StorageClientFactory storageClientFactory); static final String getId(); @Override ItemStatus execute(WorkerParameters params, HandlerIO handlerIO); @Override void checkMandatoryIOParameter(HandlerIO handler);  }