create admin info resource // fix rss feed parsing

This commit is contained in:
Eduard Heimbuch
2020-09-22 13:41:40 +02:00
parent c784c97acf
commit 851c5f9287
15 changed files with 423 additions and 62 deletions

View File

@@ -34,20 +34,20 @@ import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.net.ahc.AdvancedHttpClient;
import java.io.IOException;
import java.time.Instant;
import java.util.Date;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class ReleaseFeedReaderTest {
class ReleaseFeedParserTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
AdvancedHttpClient client;
@InjectMocks
ReleaseFeedReader releaseFeedReader;
ReleaseFeedParser releaseFeedParser;
@Test
void shouldFindLatestRelease() throws IOException {
@@ -55,7 +55,7 @@ class ReleaseFeedReaderTest {
when(client.get(url).request().contentFromXml(ReleaseFeedDto.class)).thenReturn(createReleaseFeedDto());
Optional<ReleaseInfo> release = releaseFeedReader.findLatestRelease(url);
Optional<ReleaseInfo> release = releaseFeedParser.findLatestRelease(url);
assertThat(release).isPresent();
assertThat(release.get().getTitle()).isEqualTo("3");
@@ -63,16 +63,14 @@ class ReleaseFeedReaderTest {
}
private ReleaseFeedDto createReleaseFeedDto() {
ReleaseFeedDto.Release release1 = createRelease("1", "download-1", 1000000000L);
ReleaseFeedDto.Release release2 = createRelease("2", "download-2", 2000000000L);
ReleaseFeedDto.Release release3 = createRelease("3", "download-3", 3000000000L);
ReleaseFeedDto.Channel channel = new ReleaseFeedDto.Channel("scm", "scm releases", "scm-download", "gatsby", Instant.now(), ImmutableList.of(release1, release2, release3));
ReleaseFeedDto.RSS rss = new ReleaseFeedDto.RSS(channel);
return new ReleaseFeedDto(rss);
ReleaseFeedDto.Release release1 = createRelease("1", "download-1", new Date(1000000000L));
ReleaseFeedDto.Release release2 = createRelease("2", "download-2", new Date(2000000000L));
ReleaseFeedDto.Release release3 = createRelease("3", "download-3", new Date(3000000000L));
ReleaseFeedDto.Channel channel = new ReleaseFeedDto.Channel("scm", "scm releases", "scm-download", "gatsby", new Date(1L), ImmutableList.of(release1, release2, release3));
return new ReleaseFeedDto(channel);
}
private ReleaseFeedDto.Release createRelease(String version, String link, long date) {
return new ReleaseFeedDto.Release(version, version, link, version, Instant.ofEpochMilli(date));
private ReleaseFeedDto.Release createRelease(String version, String link, Date date) {
return new ReleaseFeedDto.Release(version, version, link, version, date);
}
}

View File

@@ -0,0 +1,48 @@
/*
* 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.admin;
import org.junit.jupiter.api.Test;
import sonia.scm.api.v2.resources.ReleaseInfoDto;
import java.time.Instant;
import static org.assertj.core.api.Assertions.assertThat;
class ReleaseInfoMapperTest {
ReleaseInfoMapper mapper = new ReleaseInfoMapperImpl();
@Test
void shouldMapToDto() {
Instant releaseDate = Instant.now();
ReleaseInfo releaseInfo = new ReleaseInfo("1.2.3", "download-link", releaseDate);
ReleaseInfoDto dto = mapper.map(releaseInfo);
assertThat(dto.getLink()).isEqualTo(releaseInfo.getLink());
assertThat(dto.getReleaseDate()).isEqualTo(releaseDate);
assertThat(dto.getTitle()).isEqualTo(releaseInfo.getTitle());
}
}

View File

@@ -24,8 +24,77 @@
package sonia.scm.admin;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.SCMContextProvider;
import sonia.scm.cache.Cache;
import sonia.scm.cache.CacheManager;
import sonia.scm.cache.MapCacheManager;
import sonia.scm.config.ScmConfiguration;
import java.io.IOException;
import java.time.Instant;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class ReleaseVersionCheckerTest {
private ReleaseFeedParser feedReader;
private ScmConfiguration scmConfiguration;
private SCMContextProvider contextProvider;
private ReleaseVersionChecker checker;
@BeforeEach
void setup() {
feedReader = mock(ReleaseFeedParser.class);
scmConfiguration = mock(ScmConfiguration.class);
contextProvider = mock(SCMContextProvider.class);
CacheManager cacheManager = new MapCacheManager();
checker = new ReleaseVersionChecker(feedReader, scmConfiguration, contextProvider, cacheManager);
}
@Test
void shouldReturnEmptyOptional() throws IOException {
when(scmConfiguration.getReleaseFeedUrl()).thenReturn("releaseFeed");
when(feedReader.findLatestRelease("releaseFeed")).thenReturn(Optional.empty());
Optional<ReleaseInfo> releaseInfo = checker.checkForNewerVersion();
assertThat(releaseInfo).isNotPresent();
}
@Test
void shouldReturnReleaseInfoFromCache() {
ReleaseInfo cachedReleaseInfo = new ReleaseInfo("1.42.9", "download-link", Instant.now());
Cache<String, ReleaseInfo> cache = new MapCacheManager().getCache("sonia.cache.releaseInfo");
cache.put("latest", cachedReleaseInfo);
checker.setCache(cache);
Optional<ReleaseInfo> releaseInfo = checker.checkForNewerVersion();
assertThat(releaseInfo).isPresent();
assertThat(releaseInfo.get().getTitle()).isEqualTo("1.42.9");
assertThat(releaseInfo.get().getLink()).isEqualTo("download-link");
}
@Test
void shouldReturnReleaseInfo() throws IOException {
ReleaseInfo releaseInfo = new ReleaseInfo("2.0.0", "download-link", Instant.now());
when(scmConfiguration.getReleaseFeedUrl()).thenReturn("releaseFeed");
when(feedReader.findLatestRelease("releaseFeed")).thenReturn(Optional.of(releaseInfo));
when(contextProvider.getVersion()).thenReturn("1.9.0");
Optional<ReleaseInfo> latestRelease = checker.checkForNewerVersion();
assertThat(latestRelease).isPresent();
assertThat(latestRelease.get().getTitle()).isEqualTo("2.0.0");
assertThat(latestRelease.get().getLink()).isEqualTo("download-link");
}
}

View File

@@ -79,6 +79,7 @@ public class ResourceLinksMock {
lenient().when(resourceLinks.namespace()).thenReturn(new ResourceLinks.NamespaceLinks(pathInfo));
lenient().when(resourceLinks.namespaceCollection()).thenReturn(new ResourceLinks.NamespaceCollectionLinks(pathInfo));
lenient().when(resourceLinks.namespacePermission()).thenReturn(new ResourceLinks.NamespacePermissionLinks(pathInfo));
lenient().when(resourceLinks.adminInfo()).thenReturn(new ResourceLinks.AdminInfoLinks(pathInfo));
return resourceLinks;
}