BackendUtils { public static TArtifactTemplate synchronizeReferences(IRepository repository, ArtifactTemplateId id) throws IOException { TArtifactTemplate template = repository.getElement(id); List<TArtifactReference> toRemove = new ArrayList<>(); List<RepositoryFileReference> toAdd = new ArrayList<>(); TArtifactTemplate.ArtifactReferences artifactReferences = template.getArtifactReferences(); DirectoryId fileDir = new ArtifactTemplateFilesDirectoryId(id); SortedSet<RepositoryFileReference> files = repository.getContainedFiles(fileDir); if (artifactReferences == null) { artifactReferences = new TArtifactTemplate.ArtifactReferences(); template.setArtifactReferences(artifactReferences); } List<TArtifactReference> artRefList = artifactReferences.getArtifactReference(); determineChanges(artRefList, files, toRemove, toAdd); if (toAdd.size() > 0 || toRemove.size() > 0) { toRemove.forEach(artRefList::remove); artRefList.addAll(toAdd.stream().map(fileRef -> { String path = Util.getUrlPath(fileRef); TArtifactReference artRef = new TArtifactReference(); artRef.setReference(path); return artRef; }).collect(Collectors.toList())); BackendUtils.persist(repository, id, template); } return template; }  static boolean isFileNewerThanModifiedDate(long millis, String modified); static T getDefinitionsChildId(Class<T> idClass, String qnameStr); static T getDefinitionsChildId(Class<T> idClass, QName qname); static T getDefinitionsChildId(Class<T> idClass, String namespace, String id, boolean URLencoded); static Namespace getNamespace(ToscaElementId id); static String getDefinitionsAsXMLString(TDefinitions definitions); static String getName(DefinitionsChildId instanceId); static String getPathInsideRepo(GenericId id); static String getPathInsideRepo(RepositoryFileReference ref); static String getFilenameAndSubDirectory(RepositoryFileReference ref); static RepositoryFileReference getRefOfDefinitions(DefinitionsChildId id); static String getFileNameOfDefinitions(DefinitionsChildId id); static String getFileNameOfDefinitions(Class<T> id); static String getTypeForAdminId(Class<? extends AdminId> idClass); static RepositoryFileReference getRefOfConfiguration(GenericId id); static RepositoryFileReference getRefOfJsonConfiguration(GenericId id); static List<TNodeTemplate> getAllNestedNodeTemplates(TServiceTemplate serviceTemplate); static Collection<QName> getArtifactTemplatesOfReferencedDeploymentArtifacts(TNodeTemplate nodeTemplate); static Collection<QName> getArtifactTemplatesOfReferencedImplementationArtifacts(TNodeTemplate nodeTemplate); static Definitions updateWrapperDefinitions(DefinitionsChildId tcId, Definitions defs); static TTopologyTemplate clone(TTopologyTemplate topologyTemplate); static TNodeTemplate clone(TNodeTemplate nodeTemplate); static TRelationshipTemplate clone(TRelationshipTemplate relationshipTemplate); static Definitions createWrapperDefinitions(DefinitionsChildId tcId); static Definitions createWrapperDefinitionsAndInitialEmptyElement(IRepository repository, DefinitionsChildId id); static void initializeProperties(IRepository repository, TEntityTemplate entityTemplate); static void persist(IRepository repository, DefinitionsChildId id, TExtensibleElements element); static void persist(DefinitionsChildId id, Definitions definitions); static void persist(Object o, RepositoryFileReference ref, MediaType mediaType); static Optional<XSModel> getXSModel(final RepositoryFileReference ref); static void deriveWPD(TEntityType ci, List<String> errors); SortedSet<T> getAllTOSCAElementIds(Class<T> idClass); static Collection<QName> convertDefinitionsChildIdCollectionToQNameCollection(Collection<? extends DefinitionsChildId> col); static MediaType getMimeType(BufferedInputStream bis, String fn); static MediaType getFixedMimeType(BufferedInputStream is, String fileName, MediaType mediaType); static void copyIdToFields(HasIdInIdOrNameField element, DefinitionsChildId id); static GitInfo getGitInformation(DirectoryId directoryId); static TArtifactTemplate getTArtifactTemplate(DirectoryId directoryId); static DirectoryId getYamlArtifactsDirectoryOfNodeTemplate(ServiceTemplateId serviceTemplateId, String nodeTemplateId); static DirectoryId getYamlArtifactDirectoryOfNodeTemplate(ServiceTemplateId serviceTemplateId,
                                                                     String nodeTemplateId, String yamlArtifactId); static boolean isGlobMatch(String glob, Path path); static boolean injectArtifactTemplateIntoDeploymentArtifact(ServiceTemplateId serviceTemplate, String nodeTemplateId, String deploymentArtifactId, ArtifactTemplateId artifactTemplate); static String getImportLocationForWinerysPropertiesDefinitionXSD(EntityTypeId tcId, URI uri, String wrapperElementLocalName); static TArtifactTemplate synchronizeReferences(IRepository repository, ArtifactTemplateId id); static void synchronizeReferences(ServiceTemplateId id); static String getXMLAsString(Object obj); static String getXMLAsString(T obj, boolean includeProcessingInstruction); static String getXMLAsString(Class<T> clazz, T obj, boolean includeProcessingInstruction); static ErrorHandler getErrorHandler(StringBuilder sb); static RepositoryFileReference getRepositoryFileReference(Path rootPath, Path path, DirectoryId directoryId); static void importDirectory(Path rootPath, IRepository repository, DirectoryId dir); static Definitions getDefinitionsHavingCorrectImports(IRepository repository, DefinitionsChildId id); static void mergeTopologyTemplateAinTopologyTemplateB(ServiceTemplateId serviceTemplateIdA, ServiceTemplateId serviceTemplateIdB); static Map<String, String> mergeTopologyTemplateAinTopologyTemplateB(TTopologyTemplate topologyTemplateA, TTopologyTemplate topologyTemplateB); static Map<String, String> mergeTopologyTemplateAinTopologyTemplateB(TTopologyTemplate topologyTemplateA, TTopologyTemplate topologyTemplateB, List<TEntityTemplate> stayingElements); static TTopologyTemplate updateVersionOfNodeTemplate(TTopologyTemplate topologyTemplate, String nodeTemplateId, String newComponentType); static SortedSet<? extends DefinitionsChildId> getOtherVersionDefinitionsFromDefinition(DefinitionsChildId id); static List<WineryVersion> getAllVersionsOfOneDefinition(DefinitionsChildId id); static WineryVersion getCurrentVersionWithAllFlags(DefinitionsChildId id); static WineryVersion getPredecessor(DefinitionsChildId id); static ToscaDiff compare(DefinitionsChildId id, WineryVersion versionToCompareTo); static void commit(DefinitionsChildId componentToCommit, String commitMessagePrefix);  }
@Test public void synchronizeReferencesDoesNotRemoveUrls() throws Exception { ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId("http: IRepository repository = mock(IRepository.class); ArtifactTemplateFilesDirectoryId artifactTemplateFilesDirectoryId = new ArtifactTemplateFilesDirectoryId(artifactTemplateId); when(repository.getContainedFiles(artifactTemplateFilesDirectoryId)).thenReturn(Collections.emptySortedSet()); TArtifactTemplate artifactTemplate = createArtifactTemplateWithSingleReferenceToAnUrl(); when(repository.getElement(artifactTemplateId)).thenReturn(artifactTemplate); TArtifactTemplate synchronizhedArtifactTemplate = BackendUtils.synchronizeReferences(repository, artifactTemplateId); assertEquals(createArtifactTemplateWithSingleReferenceToAnUrl(), synchronizhedArtifactTemplate); }
@Test public void synchronizeReferencesRemovesNonExistantFileAndDoesNotRemoveUrls() throws Exception { ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId("http: IRepository repository = mock(IRepository.class); ArtifactTemplateFilesDirectoryId artifactTemplateFilesDirectoryId = new ArtifactTemplateFilesDirectoryId(artifactTemplateId); when(repository.getContainedFiles(artifactTemplateFilesDirectoryId)).thenReturn(Collections.emptySortedSet()); TArtifactTemplate artifactTemplate = createArtifactTemplateWithReferenceToAnUrlAndANonExistentFile(); when(repository.getElement(artifactTemplateId)).thenReturn(artifactTemplate); TArtifactTemplate synchronizhedArtifactTemplate = BackendUtils.synchronizeReferences(repository, artifactTemplateId); assertEquals(createArtifactTemplateWithSingleReferenceToAnUrl(), synchronizhedArtifactTemplate); }
@Test public void synchronizeReferencesDoesNontRemoveExistantFileAndDoesNotRemoveUrls() throws Exception { ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId("http: IRepository repository = mock(IRepository.class); ArtifactTemplateFilesDirectoryId artifactTemplateFilesDirectoryId = new ArtifactTemplateFilesDirectoryId(artifactTemplateId); SortedSet<RepositoryFileReference> containedReferences = new TreeSet<>(); RepositoryFileReference repositoryFileReference = new RepositoryFileReference(artifactTemplateId, "exists.txt"); containedReferences.add(repositoryFileReference); when(repository.getContainedFiles(artifactTemplateFilesDirectoryId)).thenReturn(containedReferences); TArtifactTemplate artifactTemplate = createArtifactTemplateWithReferenceToAnUrlAndExistentFile(); when(repository.getElement(artifactTemplateId)).thenReturn(artifactTemplate); TArtifactTemplate synchronizhedArtifactTemplate = BackendUtils.synchronizeReferences(repository, artifactTemplateId); assertEquals(createArtifactTemplateWithReferenceToAnUrlAndExistentFile(), synchronizhedArtifactTemplate); }