@Test @RunWithCustomExecutor public void givenWellFormedProfileMetadataThenImportSuccessfully() throws Exception { VitamThreadUtils.getVitamSession().setTenantId(TENANT_ID); final File fileMetadataProfile = PropertiesUtils.getResourceFile("profile_ok.json"); final List<ProfileModel> profileModelList = JsonHandler.getFromFileAsTypeReference(fileMetadataProfile, new TypeReference<List<ProfileModel>>() { }); final RequestResponse response = profileService.createProfiles(profileModelList); assertThat(response.isOk()).isTrue(); final RequestResponseOK<ProfileModel> responseCast = (RequestResponseOK<ProfileModel>) response; assertThat(responseCast.getResults()).hasSize(2); }
@Override public RequestResponse<ProfileModel> createProfiles(List<ProfileModel> profileModelList) throws VitamException { ParametersChecker.checkParameter(PROFILE_IS_MANDATORY_PARAMETER, profileModelList); if (profileModelList.isEmpty()) { return new RequestResponseOK<>(); } String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); boolean slaveMode = vitamCounterService .isSlaveFunctionnalCollectionOnTenant(SequenceType.PROFILE_SEQUENCE.getCollection(), ParameterHelper.getTenantParameter()); ProfileManager manager = new ProfileManager(logbookClient, eip); manager.logStarted(PROFILES_IMPORT_EVENT, null); final Set<String> profileIdentifiers = new HashSet<>(); final Set<String> profileNames = new HashSet<>(); ArrayNode profilesToPersist; final VitamError error = getVitamError(VitamCode.PROFILE_FILE_IMPORT_ERROR.getItem(), "Global create profile error", StatusCode.KO) .setHttpCode( Response.Status.BAD_REQUEST.getStatusCode()); try { for (final ProfileModel pm : profileModelList) { if (null != pm.getId()) { error.addToErrors(getVitamError(VitamCode.PROFILE_VALIDATION_ERROR.getItem(), RejectionCause.rejectIdNotAllowedInCreate(pm.getName()).getReason(), StatusCode.KO)); continue; } if (ParametersChecker.isNotEmpty(pm.getIdentifier())) { if (profileIdentifiers.contains(pm.getIdentifier())) { error.addToErrors( getVitamError(VitamCode.PROFILE_VALIDATION_ERROR.getItem(), "Duplicate profiles", StatusCode.KO) .setMessage( "Profile identifier " + pm.getIdentifier() + " already exists in the json")); continue; } else { profileIdentifiers.add(pm.getIdentifier()); } } profileNames.add(pm.getName()); if (manager.validateProfile(pm, error)) { pm.setId(GUIDFactory.newProfileGUID(ParameterHelper.getTenantParameter()).getId()); } if (pm.getTenant() == null) { pm.setTenant(ParameterHelper.getTenantParameter()); } if (slaveMode) { final Optional<ProfileValidator.RejectionCause> result = manager.checkEmptyIdentifierSlaveModeValidator().validate(pm); result.ifPresent(t -> error .addToErrors(new VitamError(VitamCode.PROFILE_VALIDATION_ERROR.getItem()).setDescription(result .get().getReason()).setMessage(ProfileManager.DUPLICATE_IN_DATABASE))); } } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(c -> c.getDescription()) .collect(Collectors.joining(",")); manager.logValidationError(PROFILES_IMPORT_EVENT, null, errorsDetails, error.getErrors().get(0).getMessage()); return error; } profilesToPersist = JsonHandler.createArrayNode(); for (final ProfileModel pm : profileModelList) { setIdentifier(slaveMode, pm); final ObjectNode profileNode = (ObjectNode) JsonHandler.toJsonNode(pm); JsonNode jsonNode = profileNode.remove(VitamFieldsHelper.id()); if (jsonNode != null) { profileNode.set(_ID, jsonNode); } JsonNode hashTenant = profileNode.remove(VitamFieldsHelper.tenant()); if (hashTenant != null) { profileNode.set(_TENANT, hashTenant); } profilesToPersist.add(profileNode); } mongoAccess.insertDocuments(profilesToPersist, FunctionalAdminCollections.PROFILE).close(); functionalBackupService.saveCollectionAndSequence( eip, PROFILE_BACKUP_EVENT, FunctionalAdminCollections.PROFILE, eip.toString() ); } catch (final Exception exp) { LOGGER.error(exp); final String err = new StringBuilder("Import profiles error : ").append(exp.getMessage()).toString(); manager.logFatalError(PROFILES_IMPORT_EVENT, null, err); return getVitamError(VitamCode.PROFILE_FILE_IMPORT_ERROR.getItem(), err, StatusCode.KO).setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(PROFILES_IMPORT_EVENT, null, null); return new RequestResponseOK<ProfileModel>().addAllResults(profileModelList) .setHttpCode(Response.Status.CREATED.getStatusCode()); }
ProfileServiceImpl implements ProfileService { @Override public RequestResponse<ProfileModel> createProfiles(List<ProfileModel> profileModelList) throws VitamException { ParametersChecker.checkParameter(PROFILE_IS_MANDATORY_PARAMETER, profileModelList); if (profileModelList.isEmpty()) { return new RequestResponseOK<>(); } String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); boolean slaveMode = vitamCounterService .isSlaveFunctionnalCollectionOnTenant(SequenceType.PROFILE_SEQUENCE.getCollection(), ParameterHelper.getTenantParameter()); ProfileManager manager = new ProfileManager(logbookClient, eip); manager.logStarted(PROFILES_IMPORT_EVENT, null); final Set<String> profileIdentifiers = new HashSet<>(); final Set<String> profileNames = new HashSet<>(); ArrayNode profilesToPersist; final VitamError error = getVitamError(VitamCode.PROFILE_FILE_IMPORT_ERROR.getItem(), "Global create profile error", StatusCode.KO) .setHttpCode( Response.Status.BAD_REQUEST.getStatusCode()); try { for (final ProfileModel pm : profileModelList) { if (null != pm.getId()) { error.addToErrors(getVitamError(VitamCode.PROFILE_VALIDATION_ERROR.getItem(), RejectionCause.rejectIdNotAllowedInCreate(pm.getName()).getReason(), StatusCode.KO)); continue; } if (ParametersChecker.isNotEmpty(pm.getIdentifier())) { if (profileIdentifiers.contains(pm.getIdentifier())) { error.addToErrors( getVitamError(VitamCode.PROFILE_VALIDATION_ERROR.getItem(), "Duplicate profiles", StatusCode.KO) .setMessage( "Profile identifier " + pm.getIdentifier() + " already exists in the json")); continue; } else { profileIdentifiers.add(pm.getIdentifier()); } } profileNames.add(pm.getName()); if (manager.validateProfile(pm, error)) { pm.setId(GUIDFactory.newProfileGUID(ParameterHelper.getTenantParameter()).getId()); } if (pm.getTenant() == null) { pm.setTenant(ParameterHelper.getTenantParameter()); } if (slaveMode) { final Optional<ProfileValidator.RejectionCause> result = manager.checkEmptyIdentifierSlaveModeValidator().validate(pm); result.ifPresent(t -> error .addToErrors(new VitamError(VitamCode.PROFILE_VALIDATION_ERROR.getItem()).setDescription(result .get().getReason()).setMessage(ProfileManager.DUPLICATE_IN_DATABASE))); } } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(c -> c.getDescription()) .collect(Collectors.joining(",")); manager.logValidationError(PROFILES_IMPORT_EVENT, null, errorsDetails, error.getErrors().get(0).getMessage()); return error; } profilesToPersist = JsonHandler.createArrayNode(); for (final ProfileModel pm : profileModelList) { setIdentifier(slaveMode, pm); final ObjectNode profileNode = (ObjectNode) JsonHandler.toJsonNode(pm); JsonNode jsonNode = profileNode.remove(VitamFieldsHelper.id()); if (jsonNode != null) { profileNode.set(_ID, jsonNode); } JsonNode hashTenant = profileNode.remove(VitamFieldsHelper.tenant()); if (hashTenant != null) { profileNode.set(_TENANT, hashTenant); } profilesToPersist.add(profileNode); } mongoAccess.insertDocuments(profilesToPersist, FunctionalAdminCollections.PROFILE).close(); functionalBackupService.saveCollectionAndSequence( eip, PROFILE_BACKUP_EVENT, FunctionalAdminCollections.PROFILE, eip.toString() ); } catch (final Exception exp) { LOGGER.error(exp); final String err = new StringBuilder("Import profiles error : ").append(exp.getMessage()).toString(); manager.logFatalError(PROFILES_IMPORT_EVENT, null, err); return getVitamError(VitamCode.PROFILE_FILE_IMPORT_ERROR.getItem(), err, StatusCode.KO).setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(PROFILES_IMPORT_EVENT, null, null); return new RequestResponseOK<ProfileModel>().addAllResults(profileModelList) .setHttpCode(Response.Status.CREATED.getStatusCode()); } }
ProfileServiceImpl implements ProfileService { @Override public RequestResponse<ProfileModel> createProfiles(List<ProfileModel> profileModelList) throws VitamException { ParametersChecker.checkParameter(PROFILE_IS_MANDATORY_PARAMETER, profileModelList); if (profileModelList.isEmpty()) { return new RequestResponseOK<>(); } String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); boolean slaveMode = vitamCounterService .isSlaveFunctionnalCollectionOnTenant(SequenceType.PROFILE_SEQUENCE.getCollection(), ParameterHelper.getTenantParameter()); ProfileManager manager = new ProfileManager(logbookClient, eip); manager.logStarted(PROFILES_IMPORT_EVENT, null); final Set<String> profileIdentifiers = new HashSet<>(); final Set<String> profileNames = new HashSet<>(); ArrayNode profilesToPersist; final VitamError error = getVitamError(VitamCode.PROFILE_FILE_IMPORT_ERROR.getItem(), "Global create profile error", StatusCode.KO) .setHttpCode( Response.Status.BAD_REQUEST.getStatusCode()); try { for (final ProfileModel pm : profileModelList) { if (null != pm.getId()) { error.addToErrors(getVitamError(VitamCode.PROFILE_VALIDATION_ERROR.getItem(), RejectionCause.rejectIdNotAllowedInCreate(pm.getName()).getReason(), StatusCode.KO)); continue; } if (ParametersChecker.isNotEmpty(pm.getIdentifier())) { if (profileIdentifiers.contains(pm.getIdentifier())) { error.addToErrors( getVitamError(VitamCode.PROFILE_VALIDATION_ERROR.getItem(), "Duplicate profiles", StatusCode.KO) .setMessage( "Profile identifier " + pm.getIdentifier() + " already exists in the json")); continue; } else { profileIdentifiers.add(pm.getIdentifier()); } } profileNames.add(pm.getName()); if (manager.validateProfile(pm, error)) { pm.setId(GUIDFactory.newProfileGUID(ParameterHelper.getTenantParameter()).getId()); } if (pm.getTenant() == null) { pm.setTenant(ParameterHelper.getTenantParameter()); } if (slaveMode) { final Optional<ProfileValidator.RejectionCause> result = manager.checkEmptyIdentifierSlaveModeValidator().validate(pm); result.ifPresent(t -> error .addToErrors(new VitamError(VitamCode.PROFILE_VALIDATION_ERROR.getItem()).setDescription(result .get().getReason()).setMessage(ProfileManager.DUPLICATE_IN_DATABASE))); } } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(c -> c.getDescription()) .collect(Collectors.joining(",")); manager.logValidationError(PROFILES_IMPORT_EVENT, null, errorsDetails, error.getErrors().get(0).getMessage()); return error; } profilesToPersist = JsonHandler.createArrayNode(); for (final ProfileModel pm : profileModelList) { setIdentifier(slaveMode, pm); final ObjectNode profileNode = (ObjectNode) JsonHandler.toJsonNode(pm); JsonNode jsonNode = profileNode.remove(VitamFieldsHelper.id()); if (jsonNode != null) { profileNode.set(_ID, jsonNode); } JsonNode hashTenant = profileNode.remove(VitamFieldsHelper.tenant()); if (hashTenant != null) { profileNode.set(_TENANT, hashTenant); } profilesToPersist.add(profileNode); } mongoAccess.insertDocuments(profilesToPersist, FunctionalAdminCollections.PROFILE).close(); functionalBackupService.saveCollectionAndSequence( eip, PROFILE_BACKUP_EVENT, FunctionalAdminCollections.PROFILE, eip.toString() ); } catch (final Exception exp) { LOGGER.error(exp); final String err = new StringBuilder("Import profiles error : ").append(exp.getMessage()).toString(); manager.logFatalError(PROFILES_IMPORT_EVENT, null, err); return getVitamError(VitamCode.PROFILE_FILE_IMPORT_ERROR.getItem(), err, StatusCode.KO).setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(PROFILES_IMPORT_EVENT, null, null); return new RequestResponseOK<ProfileModel>().addAllResults(profileModelList) .setHttpCode(Response.Status.CREATED.getStatusCode()); } ProfileServiceImpl(MongoDbAccessAdminImpl mongoAccess,
        VitamCounterService vitamCounterService, FunctionalBackupService functionalBackupService); }
ProfileServiceImpl implements ProfileService { @Override public RequestResponse<ProfileModel> createProfiles(List<ProfileModel> profileModelList) throws VitamException { ParametersChecker.checkParameter(PROFILE_IS_MANDATORY_PARAMETER, profileModelList); if (profileModelList.isEmpty()) { return new RequestResponseOK<>(); } String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); boolean slaveMode = vitamCounterService .isSlaveFunctionnalCollectionOnTenant(SequenceType.PROFILE_SEQUENCE.getCollection(), ParameterHelper.getTenantParameter()); ProfileManager manager = new ProfileManager(logbookClient, eip); manager.logStarted(PROFILES_IMPORT_EVENT, null); final Set<String> profileIdentifiers = new HashSet<>(); final Set<String> profileNames = new HashSet<>(); ArrayNode profilesToPersist; final VitamError error = getVitamError(VitamCode.PROFILE_FILE_IMPORT_ERROR.getItem(), "Global create profile error", StatusCode.KO) .setHttpCode( Response.Status.BAD_REQUEST.getStatusCode()); try { for (final ProfileModel pm : profileModelList) { if (null != pm.getId()) { error.addToErrors(getVitamError(VitamCode.PROFILE_VALIDATION_ERROR.getItem(), RejectionCause.rejectIdNotAllowedInCreate(pm.getName()).getReason(), StatusCode.KO)); continue; } if (ParametersChecker.isNotEmpty(pm.getIdentifier())) { if (profileIdentifiers.contains(pm.getIdentifier())) { error.addToErrors( getVitamError(VitamCode.PROFILE_VALIDATION_ERROR.getItem(), "Duplicate profiles", StatusCode.KO) .setMessage( "Profile identifier " + pm.getIdentifier() + " already exists in the json")); continue; } else { profileIdentifiers.add(pm.getIdentifier()); } } profileNames.add(pm.getName()); if (manager.validateProfile(pm, error)) { pm.setId(GUIDFactory.newProfileGUID(ParameterHelper.getTenantParameter()).getId()); } if (pm.getTenant() == null) { pm.setTenant(ParameterHelper.getTenantParameter()); } if (slaveMode) { final Optional<ProfileValidator.RejectionCause> result = manager.checkEmptyIdentifierSlaveModeValidator().validate(pm); result.ifPresent(t -> error .addToErrors(new VitamError(VitamCode.PROFILE_VALIDATION_ERROR.getItem()).setDescription(result .get().getReason()).setMessage(ProfileManager.DUPLICATE_IN_DATABASE))); } } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(c -> c.getDescription()) .collect(Collectors.joining(",")); manager.logValidationError(PROFILES_IMPORT_EVENT, null, errorsDetails, error.getErrors().get(0).getMessage()); return error; } profilesToPersist = JsonHandler.createArrayNode(); for (final ProfileModel pm : profileModelList) { setIdentifier(slaveMode, pm); final ObjectNode profileNode = (ObjectNode) JsonHandler.toJsonNode(pm); JsonNode jsonNode = profileNode.remove(VitamFieldsHelper.id()); if (jsonNode != null) { profileNode.set(_ID, jsonNode); } JsonNode hashTenant = profileNode.remove(VitamFieldsHelper.tenant()); if (hashTenant != null) { profileNode.set(_TENANT, hashTenant); } profilesToPersist.add(profileNode); } mongoAccess.insertDocuments(profilesToPersist, FunctionalAdminCollections.PROFILE).close(); functionalBackupService.saveCollectionAndSequence( eip, PROFILE_BACKUP_EVENT, FunctionalAdminCollections.PROFILE, eip.toString() ); } catch (final Exception exp) { LOGGER.error(exp); final String err = new StringBuilder("Import profiles error : ").append(exp.getMessage()).toString(); manager.logFatalError(PROFILES_IMPORT_EVENT, null, err); return getVitamError(VitamCode.PROFILE_FILE_IMPORT_ERROR.getItem(), err, StatusCode.KO).setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(PROFILES_IMPORT_EVENT, null, null); return new RequestResponseOK<ProfileModel>().addAllResults(profileModelList) .setHttpCode(Response.Status.CREATED.getStatusCode()); } ProfileServiceImpl(MongoDbAccessAdminImpl mongoAccess,
        VitamCounterService vitamCounterService, FunctionalBackupService functionalBackupService); @Override RequestResponse<ProfileModel> createProfiles(List<ProfileModel> profileModelList); @Override RequestResponse importProfileFile(String profileIdentifier,
        InputStream profileFile); @Override Response downloadProfileFile(String profileIdentifier); @Override RequestResponse<ProfileModel> updateProfile(String identifier, JsonNode jsonDsl); @Override RequestResponse<ProfileModel> updateProfile(ProfileModel profileModel, JsonNode jsonDsl); @Override ProfileModel findByIdentifier(String identifier); @Override RequestResponseOK<ProfileModel> findProfiles(JsonNode queryDsl); @Override void close(); }
ProfileServiceImpl implements ProfileService { @Override public RequestResponse<ProfileModel> createProfiles(List<ProfileModel> profileModelList) throws VitamException { ParametersChecker.checkParameter(PROFILE_IS_MANDATORY_PARAMETER, profileModelList); if (profileModelList.isEmpty()) { return new RequestResponseOK<>(); } String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); boolean slaveMode = vitamCounterService .isSlaveFunctionnalCollectionOnTenant(SequenceType.PROFILE_SEQUENCE.getCollection(), ParameterHelper.getTenantParameter()); ProfileManager manager = new ProfileManager(logbookClient, eip); manager.logStarted(PROFILES_IMPORT_EVENT, null); final Set<String> profileIdentifiers = new HashSet<>(); final Set<String> profileNames = new HashSet<>(); ArrayNode profilesToPersist; final VitamError error = getVitamError(VitamCode.PROFILE_FILE_IMPORT_ERROR.getItem(), "Global create profile error", StatusCode.KO) .setHttpCode( Response.Status.BAD_REQUEST.getStatusCode()); try { for (final ProfileModel pm : profileModelList) { if (null != pm.getId()) { error.addToErrors(getVitamError(VitamCode.PROFILE_VALIDATION_ERROR.getItem(), RejectionCause.rejectIdNotAllowedInCreate(pm.getName()).getReason(), StatusCode.KO)); continue; } if (ParametersChecker.isNotEmpty(pm.getIdentifier())) { if (profileIdentifiers.contains(pm.getIdentifier())) { error.addToErrors( getVitamError(VitamCode.PROFILE_VALIDATION_ERROR.getItem(), "Duplicate profiles", StatusCode.KO) .setMessage( "Profile identifier " + pm.getIdentifier() + " already exists in the json")); continue; } else { profileIdentifiers.add(pm.getIdentifier()); } } profileNames.add(pm.getName()); if (manager.validateProfile(pm, error)) { pm.setId(GUIDFactory.newProfileGUID(ParameterHelper.getTenantParameter()).getId()); } if (pm.getTenant() == null) { pm.setTenant(ParameterHelper.getTenantParameter()); } if (slaveMode) { final Optional<ProfileValidator.RejectionCause> result = manager.checkEmptyIdentifierSlaveModeValidator().validate(pm); result.ifPresent(t -> error .addToErrors(new VitamError(VitamCode.PROFILE_VALIDATION_ERROR.getItem()).setDescription(result .get().getReason()).setMessage(ProfileManager.DUPLICATE_IN_DATABASE))); } } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(c -> c.getDescription()) .collect(Collectors.joining(",")); manager.logValidationError(PROFILES_IMPORT_EVENT, null, errorsDetails, error.getErrors().get(0).getMessage()); return error; } profilesToPersist = JsonHandler.createArrayNode(); for (final ProfileModel pm : profileModelList) { setIdentifier(slaveMode, pm); final ObjectNode profileNode = (ObjectNode) JsonHandler.toJsonNode(pm); JsonNode jsonNode = profileNode.remove(VitamFieldsHelper.id()); if (jsonNode != null) { profileNode.set(_ID, jsonNode); } JsonNode hashTenant = profileNode.remove(VitamFieldsHelper.tenant()); if (hashTenant != null) { profileNode.set(_TENANT, hashTenant); } profilesToPersist.add(profileNode); } mongoAccess.insertDocuments(profilesToPersist, FunctionalAdminCollections.PROFILE).close(); functionalBackupService.saveCollectionAndSequence( eip, PROFILE_BACKUP_EVENT, FunctionalAdminCollections.PROFILE, eip.toString() ); } catch (final Exception exp) { LOGGER.error(exp); final String err = new StringBuilder("Import profiles error : ").append(exp.getMessage()).toString(); manager.logFatalError(PROFILES_IMPORT_EVENT, null, err); return getVitamError(VitamCode.PROFILE_FILE_IMPORT_ERROR.getItem(), err, StatusCode.KO).setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(PROFILES_IMPORT_EVENT, null, null); return new RequestResponseOK<ProfileModel>().addAllResults(profileModelList) .setHttpCode(Response.Status.CREATED.getStatusCode()); } ProfileServiceImpl(MongoDbAccessAdminImpl mongoAccess,
        VitamCounterService vitamCounterService, FunctionalBackupService functionalBackupService); @Override RequestResponse<ProfileModel> createProfiles(List<ProfileModel> profileModelList); @Override RequestResponse importProfileFile(String profileIdentifier,
        InputStream profileFile); @Override Response downloadProfileFile(String profileIdentifier); @Override RequestResponse<ProfileModel> updateProfile(String identifier, JsonNode jsonDsl); @Override RequestResponse<ProfileModel> updateProfile(ProfileModel profileModel, JsonNode jsonDsl); @Override ProfileModel findByIdentifier(String identifier); @Override RequestResponseOK<ProfileModel> findProfiles(JsonNode queryDsl); @Override void close(); static final String OP_PROFILE_STORAGE; static final String PROFILE_FORMAT_SHOULD_BE_XSD_OR_RNG; static final String PROFILE_IDENTIFIER_ALREADY_EXISTS_IN_DATABASE; static final String PROFILE_IDENTIFIER_MUST_BE_STRING; static final String PROFILE_BACKUP_EVENT; }