Skip to content

Commit

Permalink
Add registry properties support to unit tests
Browse files Browse the repository at this point in the history
Add functionality to include registry properties in synapse unit tests.
Fixes wso2/micro-integrator/issues/3606
  • Loading branch information
GDLMadushanka committed Oct 7, 2024
1 parent 66fc629 commit 9ded7c3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
package org.wso2.integrationstudio.esb.synapse.unit.test.component;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -777,6 +780,7 @@ private void addRegistryElements(IContainer registry) {
QName registryItemFile = new QName("", Constants.REGISTRY_FILE, "");
QName registryItemPath = new QName("", Constants.REGISTRY_FILE_PATH, "");
QName registryItemMediaType = new QName("", Constants.REGISTRY_FILE_MEDIA_TYPE, "");
QName registryProperties = new QName("", Constants.REGISTRY_PROPERTIES, "");

OMElement registryItemNode = artifact.getFirstChildWithName(registryItem);
if (registryItemNode != null) {
Expand All @@ -797,7 +801,7 @@ private void addRegistryElements(IContainer registry) {
OMElement registryItemFileTypeNode = registryItemNode.getFirstChildWithName(registryItemMediaType);
String fileType = registryItemFileTypeNode.getText();
resource.setMediaType(fileType);

// add MMM parent to the registry resource
String grandParentPath = "";
try {
Expand All @@ -813,6 +817,44 @@ private void addRegistryElements(IContainer registry) {
}
String absolutePath = grandParentPath + Constants.PATH_PREFIX + registry.getProject().getName() + Constants.PATH_PREFIX + originalFileName;
resource.setAbsolutePath(absolutePath);

OMElement regProperties = registryItemNode.getFirstChildWithName(registryProperties);
if (regProperties != null) {
// If registry resource has properties, create a new file called filename.properties in the same
// location
String artifactPath = file.getAbsolutePath();
String propFileName = fileName + Constants.PROPPERTIES_FILE_SUFFIX;
java.nio.file.Path propertiesFilePath = Paths.get(
artifactPath.substring(0,
artifactPath.length() - Constants.REGISTRY_RESOURCE_FILE.length()),
propFileName);
if (Files.notExists(propertiesFilePath)) {
Files.createFile(propertiesFilePath);
}
StringBuilder str = new StringBuilder();
Iterator<?> propertyIterator = regProperties.getChildElements();
while (propertyIterator.hasNext()) {
OMElement property = (OMElement) propertyIterator.next();
String key = property.getAttributeValue(new QName(Constants.REGISTRY_PROPERTY_KEY));
String value = property.getAttributeValue(new QName(Constants.REGISTRY_PROPERTY_VALUE));
str.append(key).append("=").append(value).append("\n");
}
if (str.length() > 0) {
try (FileWriter writer = new FileWriter(propertiesFilePath.toString(), false)) {
writer.write(str.toString());
} catch (IOException e) {
log.error("Error occurred while writing the proeprties file : " + propertiesFilePath,
e);
}
}
// add dependency
RegistryResource propResource = new RegistryResource();
propResource.setFileName(propFileName);
propResource.setMediaType(fileType);
propResource.setFilePath(filePath);
propResource.setAbsolutePath(absolutePath + Constants.PROPPERTIES_FILE_SUFFIX);
registryElements.put(propFileName, propResource);
}
}

registryElements.put(resource.getFileName(), resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ private Constants() {
public static final String REGISTRY_FILE = "file";
public static final String REGISTRY_FILE_PATH = "path";
public static final String REGISTRY_FILE_MEDIA_TYPE = "mediaType";
public static final String REGISTRY_PROPERTIES = "properties";
public static final String REGISTRY_PROPERTY = "property";
public static final String REGISTRY_PROPERTY_KEY = "key";
public static final String REGISTRY_PROPERTY_VALUE = "value";
public static final String PROPPERTIES_FILE_SUFFIX = ".properties";

public static final String PATH_PREFIX = "/";
public static final String SPACE_STRING = " ";
Expand Down

0 comments on commit 9ded7c3

Please sign in to comment.