Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(artifact): moved artifacts (groovy) unit tests to artifacts (java) #1258

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spinnaker.igor.build.model.GenericBuild;
import com.netflix.spinnaker.kork.annotations.VisibleForTesting;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.netflix.spinnaker.kork.artifacts.parsing.JinjaArtifactExtractor;
import java.util.ArrayList;
Expand Down Expand Up @@ -91,7 +92,8 @@ private JinjaTemplate getTemplateFromProperty(GenericBuild build) {
return jinjaTemplateService.getTemplate(messageFormat, templateType);
}

private boolean parseCustomFormat(Object customFormat) {
@VisibleForTesting
boolean parseCustomFormat(Object customFormat) {
if (customFormat == null) {
return false;
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package com.netflix.spinnaker.igor.artifacts;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.netflix.spinnaker.igor.Main;
import com.netflix.spinnaker.igor.build.model.GenericBuild;
import com.netflix.spinnaker.igor.build.model.GenericGitRevision;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.netflix.spinnaker.kork.artifacts.parsing.DefaultJinjavaFactory;
import com.netflix.spinnaker.kork.artifacts.parsing.JinjaArtifactExtractor;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
Expand All @@ -27,16 +35,33 @@ public class ArtifactExtractorTest {

@Autowired private ArtifactExtractor artifactExtractor;

@Test
public void should_be_able_to_serialize_jsr310_dates() {
GenericBuild build = new GenericBuild();
build.setProperties(
@Autowired private JinjaTemplateService jinjaTemplateService;

private JinjaArtifactExtractor.Factory jinjaArtifactExtractorFactory;
private ObjectMapper objectMapper;
private GenericBuild build;

@BeforeEach
public void setUp() {
objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
jinjaArtifactExtractorFactory = new JinjaArtifactExtractor.Factory(new DefaultJinjavaFactory());
artifactExtractor =
new ArtifactExtractor(objectMapper, jinjaTemplateService, jinjaArtifactExtractorFactory);

build = new GenericBuild();
Map<String, Object> properties =
Map.of(
"group", "test.group",
"artifact", "test-artifact",
"version", "1.0",
"messageFormat", "JAR",
"customFormat", "false"));
"customFormat", "false");
build.setProperties(properties);
}

@Test
public void should_be_able_to_serialize_jsr310_dates() {
build.setGenericGitRevisions(
List.of(
GenericGitRevision.builder()
Expand All @@ -51,4 +76,20 @@ public void should_be_able_to_serialize_jsr310_dates() {
assertThat(artifact.getName()).isEqualTo("test-artifact-1.0");
assertThat(artifact.getVersion()).isEqualTo("1.0");
}

@Test
public void testParsesAnArtifactReturnsItsArtifacts() {
List<Artifact> artifacts = artifactExtractor.extractArtifacts(build);

assertEquals(1, artifacts.size());
assertEquals("test-artifact-1.0", artifacts.get(0).getName());
}

@ParameterizedTest
@CsvSource({"true, true", "false, false", "'true', true", "'false', false"})
public void testParseCustomFormatCorrectlyParsesBooleansAndStrings(
Object customFormat, boolean expectedResult) {
boolean result = artifactExtractor.parseCustomFormat(customFormat);
assertThat(result).isEqualTo(expectedResult);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
*
* Copyright 2019 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.netflix.spinnaker.igor.artifacts;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.netflix.spinnaker.kork.web.exceptions.NotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class ArtifactServiceTest {

private ArtifactServices artifactServices;

@BeforeEach
public void setUp() {
artifactServices = new ArtifactServices();
Map<String, ArtifactService> services = new HashMap<>();
services.put("artifactory", new TestArtifactService());
artifactServices.addServices(services);
}

@Test
public void findsMatchingService() {
ArtifactService service = artifactServices.getService("artifactory");
assertThat(service).isNotNull();
}

@Test
public void doesNotFindNonMatchingService() {
ArtifactService service = artifactServices.getService("what");
assertThat(service).isNull();
}

@Test
public void serviceFindsArtifactVersions() {
ArtifactService service = artifactServices.getService("artifactory");
List<String> versions = service.getArtifactVersions("deb", "test", (List<String>) null);

assertThat(versions).isNotNull();
assertThat(versions).isNotEmpty();
assertThat(versions.size()).isGreaterThan(0);
}

@Test
public void serviceFindsOnlySnapshotArtifacts() {
ArtifactService service = artifactServices.getService("artifactory");
List<String> versions = service.getArtifactVersions("deb", "test", "snapshot");

assertThat(versions).isNotNull();
assertThat(versions).isNotEmpty();
assertThat(versions.size()).isEqualTo(1);
}

@Test
public void serviceFindsArtifact() {
ArtifactService service = artifactServices.getService("artifactory");
Artifact artifact = service.getArtifact("deb", "test", "v0.4.0");

assertThat(artifact).isNotNull();
assertThat(artifact.getName()).isEqualTo("test");
assertThat(artifact.getVersion()).isEqualTo("v0.4.0");
}

@Test
public void versionsListIsEmptyWhenNoVersionsFound() {
ArtifactService service = artifactServices.getService("artifactory");
List<String> versions = service.getArtifactVersions("deb", "blah", "");

assertThat(versions).isNotNull();
assertThat(versions).isEmpty();
}

@Test
public void notFoundExceptionIsThrownWhenArtifactNotFound() {
ArtifactService service = artifactServices.getService("artifactory");
assertThrows(NotFoundException.class, () -> service.getArtifact("deb", "blah", "v0.0.1"));
}
}