Skip to content

Commit

Permalink
Merge pull request #30 from spyrkob/issue_29
Browse files Browse the repository at this point in the history
[#29] Add missing channel names when generating metadata
  • Loading branch information
spyrkob authored Jun 6, 2024
2 parents 09779bb + 662764e commit 8bd66a6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class ProsperoMetadataUtils {

Expand Down Expand Up @@ -119,8 +122,30 @@ public static void writeChannelsConfiguration(Path channelPath, List<Channel> ch
throw new IllegalArgumentException(String.format("The target path %s does not exist.", channelPath.getParent()));
}

final Set<String> definedChannelNames = channels.stream().map(Channel::getName).filter(Objects::nonNull).filter(s -> !s.isEmpty()).collect(Collectors.toSet());
final AtomicInteger counter = new AtomicInteger(0);
final List<Channel> namedChannels = channels.stream().map(c -> {
final String name;
if (c.getName() == null || c.getName().isEmpty()) {
name = nextAvailableName(c, counter, definedChannelNames);
} else {
name = c.getName();
}
return new Channel(c.getSchemaVersion(), name, c.getDescription(), c.getVendor(), c.getRepositories(), c.getManifestCoordinate(),
c.getBlocklistCoordinate(), c.getNoStreamStrategy());
}).collect(Collectors.toList());


writeToFile(channelPath, ChannelMapper.toYaml(namedChannels));
}

writeToFile(channelPath, ChannelMapper.toYaml(channels));
private static String nextAvailableName(Channel c, AtomicInteger counter, Set<String> definedChannelNames) {
String name;
do {
name = (c.getName() == null || c.getName().isEmpty()) ? "channel-" + counter.getAndIncrement() : c.getName();
} while (definedChannelNames.contains(name));
definedChannelNames.add(name);
return name;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.nio.file.Path;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
Expand Down Expand Up @@ -128,7 +129,7 @@ public void recordProvisioningConfigurationIfExists() throws Exception {

ProsperoMetadataUtils.generate(server, List.of(A_CHANNEL), A_MANIFEST, null);

Assertions.assertThat(server.resolve(METADATA_DIR).resolve(PROVISIONING_RECORD_XML))
assertThat(server.resolve(METADATA_DIR).resolve(PROVISIONING_RECORD_XML))
.hasContent("<provisioning></provisioning>");
}

Expand All @@ -141,10 +142,32 @@ public void overrideProvisioningConfigurationIfContentNotEquals() throws Excepti

ProsperoMetadataUtils.generate(server, List.of(A_CHANNEL), A_MANIFEST, null);

Assertions.assertThat(server.resolve(METADATA_DIR).resolve(PROVISIONING_RECORD_XML))
assertThat(server.resolve(METADATA_DIR).resolve(PROVISIONING_RECORD_XML))
.hasContent("<provisioning></provisioning>");
}

@Test
public void generateChannelNamesIfNotProvided() throws Exception {
Files.createDirectory(server.resolve(Constants.PROVISIONED_STATE_DIR));
Files.writeString(server.resolve(Constants.PROVISIONED_STATE_DIR).resolve(Constants.PROVISIONING_XML), "<provisioning></provisioning>");
Files.createDirectory(server.resolve(METADATA_DIR));
Files.writeString(server.resolve(METADATA_DIR).resolve(PROVISIONING_RECORD_XML), "<provisioning>content</provisioning>");

final Channel channelNoName = new Channel(null, null, null,
List.of(new Repository("test-repo", "http://test.te")),
new ChannelManifestCoordinate("foo", "bar"), null, null);
final Channel channelZero = new Channel("channel-0", null, null,
List.of(new Repository("test-repo", "http://test.te")),
new ChannelManifestCoordinate("foo", "bar"), null, null);

ProsperoMetadataUtils.generate(server, List.of(channelNoName, channelZero), A_MANIFEST, null);

final List<Channel> channels = ChannelMapper.fromString(Files.readString(server.resolve(METADATA_DIR).resolve(INSTALLER_CHANNELS_FILE_NAME)));
assertThat(channels)
.map(Channel::getName)
.containsExactlyInAnyOrder("channel-0", "channel-1");
}

private void assertMetadataWritten() throws MalformedURLException {
final Channel channel = ChannelMapper.from(channelPath.toUri().toURL());
final ChannelManifest manifest = ChannelManifestMapper.from(manifestPath.toUri().toURL());
Expand Down

0 comments on commit 8bd66a6

Please sign in to comment.