Files
SCM-Manager/scm-webapp/src/test/java/sonia/scm/plugin/PluginCenterDtoMapperTest.java

153 lines
5.9 KiB
Java
Raw Normal View History

/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.plugin;
2019-07-31 16:59:13 +02:00
2019-08-20 14:43:48 +02:00
import com.google.common.collect.ImmutableMap;
2019-08-20 07:53:17 +02:00
import com.google.common.collect.ImmutableSet;
2019-07-31 16:59:13 +02:00
import org.junit.jupiter.api.Test;
2019-08-20 12:29:59 +02:00
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
2019-07-31 16:59:13 +02:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
2019-08-20 12:29:59 +02:00
import static org.mockito.Mockito.when;
import static sonia.scm.plugin.PluginCenterDto.Plugin;
import static sonia.scm.plugin.PluginCenterDto.*;
2019-07-31 16:59:13 +02:00
2019-08-20 12:29:59 +02:00
@ExtendWith(MockitoExtension.class)
2019-07-31 16:59:13 +02:00
class PluginCenterDtoMapperTest {
2019-08-20 12:29:59 +02:00
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PluginCenterDto dto;
@InjectMocks
private PluginCenterDtoMapperImpl mapper;
2019-07-31 16:59:13 +02:00
@Test
void shouldMapSinglePlugin() {
Plugin plugin = new Plugin(
"scm-hitchhiker-plugin",
"2.0.0",
2019-07-31 16:59:13 +02:00
"SCM Hitchhiker Plugin",
"plugin for hitchhikers",
"Travel",
"trillian",
"http://avatar.url",
2019-07-31 16:59:13 +02:00
"555000444",
PluginInformation.PluginType.SCM,
new Condition(Collections.singletonList("linux"), "amd64","2.0.0"),
2019-08-20 07:53:17 +02:00
ImmutableSet.of("scm-review-plugin"),
ImmutableSet.of(),
2019-08-20 14:43:48 +02:00
ImmutableMap.of("download", new Link("http://download.hitchhiker.com"))
);
2019-07-31 16:59:13 +02:00
2019-08-20 12:29:59 +02:00
when(dto.getEmbedded().getPlugins()).thenReturn(Collections.singletonList(plugin));
AvailablePluginDescriptor descriptor = mapper.map(dto).iterator().next().getDescriptor();
PluginInformation information = descriptor.getInformation();
PluginCondition condition = descriptor.getCondition();
2019-07-31 16:59:13 +02:00
2019-08-20 14:43:48 +02:00
assertThat(descriptor.getUrl()).isEqualTo("http://download.hitchhiker.com");
assertThat(descriptor.getChecksum()).contains("555000444");
2019-08-20 12:29:59 +02:00
assertThat(information.getAuthor()).isEqualTo(plugin.getAuthor());
assertThat(information.getCategory()).isEqualTo(plugin.getCategory());
assertThat(information.getVersion()).isEqualTo(plugin.getVersion());
assertThat(condition.getArch()).isEqualTo(plugin.getConditions().getArch());
assertThat(condition.getMinVersion()).isEqualTo(plugin.getConditions().getMinVersion());
assertThat(condition.getOs().iterator().next()).isEqualTo(plugin.getConditions().getOs().iterator().next());
assertThat(information.getDescription()).isEqualTo(plugin.getDescription());
assertThat(information.getName()).isEqualTo(plugin.getName());
2019-07-31 16:59:13 +02:00
}
@Test
void shouldMapMultiplePlugins() {
Plugin plugin1 = new Plugin(
"scm-review-plugin",
"2.1.0",
2019-07-31 16:59:13 +02:00
"SCM Hitchhiker Plugin",
"plugin for hitchhikers",
"Travel",
"trillian",
"https://avatar.url",
"12345678aa",
PluginInformation.PluginType.SCM,
new Condition(Collections.singletonList("linux"), "amd64","2.0.0"),
2019-08-20 07:53:17 +02:00
ImmutableSet.of("scm-review-plugin"),
ImmutableSet.of(),
2019-08-20 14:43:48 +02:00
ImmutableMap.of("download", new Link("http://download.hitchhiker.com/review"))
);
2019-07-31 16:59:13 +02:00
Plugin plugin2 = new Plugin(
"scm-hitchhiker-plugin",
"2.0.0",
2019-07-31 16:59:13 +02:00
"SCM Hitchhiker Plugin",
"plugin for hitchhikers",
"Travel",
"dent",
"http://avatar.url",
"555000444",
PluginInformation.PluginType.CLOUDOGU,
new Condition(Collections.singletonList("linux"), "amd64","2.0.0"),
2019-08-20 07:53:17 +02:00
ImmutableSet.of("scm-review-plugin"),
ImmutableSet.of(),
2019-08-20 14:43:48 +02:00
ImmutableMap.of("download", new Link("http://download.hitchhiker.com/hitchhiker"))
);
2019-07-31 16:59:13 +02:00
2019-08-20 12:29:59 +02:00
when(dto.getEmbedded().getPlugins()).thenReturn(Arrays.asList(plugin1, plugin2));
Set<AvailablePlugin> resultSet = mapper.map(dto);
2019-07-31 16:59:13 +02:00
2019-08-21 16:10:17 +02:00
PluginInformation pluginInformation1 = findPlugin(resultSet, plugin1.getName());
PluginInformation pluginInformation2 = findPlugin(resultSet, plugin2.getName());
2019-07-31 16:59:13 +02:00
assertThat(pluginInformation1.getAuthor()).isEqualTo(plugin1.getAuthor());
assertThat(pluginInformation1.getVersion()).isEqualTo(plugin1.getVersion());
assertThat(pluginInformation2.getAuthor()).isEqualTo(plugin2.getAuthor());
assertThat(pluginInformation2.getVersion()).isEqualTo(plugin2.getVersion());
assertThat(pluginInformation1.getType()).isEqualTo(PluginInformation.PluginType.SCM);
assertThat(pluginInformation2.getType()).isEqualTo(PluginInformation.PluginType.CLOUDOGU);
2019-07-31 16:59:13 +02:00
assertThat(resultSet.size()).isEqualTo(2);
}
2019-08-21 16:10:17 +02:00
private PluginInformation findPlugin(Set<AvailablePlugin> resultSet, String name) {
return resultSet
.stream()
.filter(p -> name.equals(p.getDescriptor().getInformation().getName()))
.findFirst()
.orElseThrow(() -> new IllegalStateException("could not find plugin " + name))
.getDescriptor()
.getInformation();
}
2019-07-31 16:59:13 +02:00
}