|
| 1 | +/* |
| 2 | + * Copyright 2021 - 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.sbm.build.impl.inner; |
| 17 | + |
| 18 | +import org.intellij.lang.annotations.Language; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.openrewrite.maven.MavenParser; |
| 21 | +import org.openrewrite.xml.tree.Xml; |
| 22 | +import org.springframework.sbm.build.api.RepositoryDefinition; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 28 | + |
| 29 | +class PluginRepositoryHandlerTest { |
| 30 | + |
| 31 | + private final PluginRepositoryHandler sut = new PluginRepositoryHandler(); |
| 32 | + private final MavenParser mavenParser = MavenParser.builder().build(); |
| 33 | + |
| 34 | + @Test |
| 35 | + public void shouldConvertAllFieldsOfPluginRepo() { |
| 36 | + |
| 37 | + Xml.Document sourceFile = getSourceFile(""" |
| 38 | + <?xml version="1.0" encoding="UTF-8"?> |
| 39 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 40 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 41 | + <modelVersion>4.0.0</modelVersion> |
| 42 | + <groupId>com.example</groupId> |
| 43 | + <artifactId>boot-upgrade-27_30</artifactId> |
| 44 | + <version>0.0.1-SNAPSHOT</version> |
| 45 | + <name>boot-upgrade-27_30</name> |
| 46 | + <description>boot-upgrade-27_30</description> |
| 47 | +
|
| 48 | + <pluginRepositories> |
| 49 | + <pluginRepository> |
| 50 | + <id>spring-milestone</id> |
| 51 | + <url>https://repo.spring.io/milestone</url> |
| 52 | + <layout>default</layout> |
| 53 | + <snapshots> |
| 54 | + <checksumPolicy>fail</checksumPolicy> |
| 55 | + <enabled>true</enabled> |
| 56 | + <updatePolicy>daily</updatePolicy> |
| 57 | + </snapshots> |
| 58 | + <releases> |
| 59 | + <checksumPolicy>warn</checksumPolicy> |
| 60 | + <enabled>false</enabled> |
| 61 | + <updatePolicy>always</updatePolicy> |
| 62 | + </releases> |
| 63 | + </pluginRepository> |
| 64 | + </pluginRepositories> |
| 65 | + </project> |
| 66 | + """); |
| 67 | + List<RepositoryDefinition> output = sut.getRepositoryDefinitions(sourceFile); |
| 68 | + assertThat(output).hasSize(1); |
| 69 | + |
| 70 | + assertThat(output.get(0).getUrl()).isEqualTo("https://repo.spring.io/milestone"); |
| 71 | + assertThat(output.get(0).getId()).isEqualTo("spring-milestone"); |
| 72 | + assertThat(output.get(0).getLayout()).isEqualTo("default"); |
| 73 | + assertThat(output.get(0).getSnapshotsEnabled()).isTrue(); |
| 74 | + assertThat(output.get(0).getSnapshotsChecksumPolicy()).isEqualTo("fail"); |
| 75 | + assertThat(output.get(0).getSnapShotsUpdatePolicy()).isEqualTo("daily"); |
| 76 | + assertThat(output.get(0).getReleasesEnabled()).isFalse(); |
| 77 | + assertThat(output.get(0).getReleasesChecksumPolicy()).isEqualTo("warn"); |
| 78 | + assertThat(output.get(0).getReleasesUpdatePolicy()).isEqualTo("always"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void shouldThrowExceptionForMissingIdMandatoryAttribute() { |
| 83 | + |
| 84 | + Xml.Document sourceFile = getSourceFile(""" |
| 85 | + <?xml version="1.0" encoding="UTF-8"?> |
| 86 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 87 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 88 | + <modelVersion>4.0.0</modelVersion> |
| 89 | + <groupId>com.example</groupId> |
| 90 | + <artifactId>boot-upgrade-27_30</artifactId> |
| 91 | + <version>0.0.1-SNAPSHOT</version> |
| 92 | + <name>boot-upgrade-27_30</name> |
| 93 | + <description>boot-upgrade-27_30</description> |
| 94 | +
|
| 95 | + <pluginRepositories> |
| 96 | + <pluginRepository> |
| 97 | + <url>https://repo.spring.io/milestone</url> |
| 98 | + </pluginRepository> |
| 99 | + </pluginRepositories> |
| 100 | + </project> |
| 101 | +
|
| 102 | + """); |
| 103 | + RuntimeException runtimeException = assertThrows(RuntimeException.class, () -> sut.getRepositoryDefinitions(sourceFile)); |
| 104 | + assertThat(runtimeException).hasMessageContaining("id"); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void shouldThrowExceptionForMissingUrlMandatoryAttribute() { |
| 109 | + |
| 110 | + Xml.Document sourceFile = getSourceFile(""" |
| 111 | + <?xml version="1.0" encoding="UTF-8"?> |
| 112 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 113 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 114 | + <modelVersion>4.0.0</modelVersion> |
| 115 | + <groupId>com.example</groupId> |
| 116 | + <artifactId>boot-upgrade-27_30</artifactId> |
| 117 | + <version>0.0.1-SNAPSHOT</version> |
| 118 | + <name>boot-upgrade-27_30</name> |
| 119 | + <description>boot-upgrade-27_30</description> |
| 120 | +
|
| 121 | + <pluginRepositories> |
| 122 | + <pluginRepository> |
| 123 | + <id>someId</id> |
| 124 | + </pluginRepository> |
| 125 | + </pluginRepositories> |
| 126 | + </project> |
| 127 | +
|
| 128 | + """); |
| 129 | + RuntimeException runtimeException = assertThrows(RuntimeException.class, () -> sut.getRepositoryDefinitions(sourceFile)); |
| 130 | + assertThat(runtimeException).hasMessageContaining("url"); |
| 131 | + } |
| 132 | + |
| 133 | + private Xml.Document getSourceFile(@Language("xml") String xml) { |
| 134 | + |
| 135 | + return mavenParser.parse(xml).get(0); |
| 136 | + } |
| 137 | +} |
0 commit comments