|
1 | 1 | package com.databricks.sdk.mixin; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
4 | 5 |
|
5 | 6 | import com.databricks.sdk.core.ApiClient; |
6 | 7 | import com.databricks.sdk.core.DatabricksConfig; |
@@ -118,4 +119,86 @@ void nullComparisonTest() { |
118 | 119 | String nodeType = clustersExt.selectNodeType(new NodeTypeSelector().withLocalDisk()); |
119 | 120 | assertEquals("testId1", nodeType); |
120 | 121 | } |
| 122 | + |
| 123 | + private GetSparkVersionsResponse testGetSparkVersionsWithSparkVersion() { |
| 124 | + Collection<SparkVersion> versions = new ArrayList<>(); |
| 125 | + // Mock realistic Databricks Runtime versions based on actual API response format |
| 126 | + versions.add(new SparkVersion() |
| 127 | + .setName("12.2 LTS (includes Apache Spark 3.3.2, Scala 2.12)") |
| 128 | + .setKey("12.2.x-scala2.12")); |
| 129 | + versions.add(new SparkVersion() |
| 130 | + .setName("13.3 LTS (includes Apache Spark 3.4.1, Scala 2.12)") |
| 131 | + .setKey("13.3.x-scala2.12")); |
| 132 | + versions.add(new SparkVersion() |
| 133 | + .setName("14.3 LTS (includes Apache Spark 3.5.0, Scala 2.12)") |
| 134 | + .setKey("14.3.x-scala2.12")); |
| 135 | + versions.add(new SparkVersion() |
| 136 | + .setName("14.2 ML (includes Apache Spark 3.5.0, Scala 2.12)") |
| 137 | + .setKey("14.2.x-cpu-ml-scala2.12")); |
| 138 | + // Add another version with same Spark version to create multiple matches |
| 139 | + versions.add(new SparkVersion() |
| 140 | + .setName("14.1 (includes Apache Spark 3.5.0, Scala 2.12)") |
| 141 | + .setKey("14.1.x-scala2.12")); |
| 142 | + return new GetSparkVersionsResponse().setVersions(versions); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void sparkVersionWithSparkVersionParameter() { |
| 147 | + ClustersExt clustersExt = new ClustersExt(clustersMock); |
| 148 | + Mockito.doReturn(testGetSparkVersionsWithSparkVersion()).when(clustersMock).sparkVersions(); |
| 149 | + |
| 150 | + // Test exact spark version match |
| 151 | + String sparkVersion = clustersExt.selectSparkVersion( |
| 152 | + new SparkVersionSelector().withSparkVersion("3.4.1")); |
| 153 | + assertEquals("13.3.x-scala2.12", sparkVersion); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + void sparkVersionWithSparkVersionParameterMultipleMatches() { |
| 158 | + ClustersExt clustersExt = new ClustersExt(clustersMock); |
| 159 | + Mockito.doReturn(testGetSparkVersionsWithSparkVersion()).when(clustersMock).sparkVersions(); |
| 160 | + |
| 161 | + // Test spark version with multiple matches - should return latest when latest=true is explicitly set |
| 162 | + String sparkVersion = clustersExt.selectSparkVersion( |
| 163 | + new SparkVersionSelector().withSparkVersion("3.5.0").withLatest()); |
| 164 | + // Should return the highest version (14.3.x) when latest=true and multiple 3.5.0 versions match |
| 165 | + assertEquals("14.3.x-scala2.12", sparkVersion); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + void sparkVersionWithSparkVersionParameterAndML() { |
| 170 | + ClustersExt clustersExt = new ClustersExt(clustersMock); |
| 171 | + Mockito.doReturn(testGetSparkVersionsWithSparkVersion()).when(clustersMock).sparkVersions(); |
| 172 | + |
| 173 | + // Test spark version combined with ML requirement |
| 174 | + String sparkVersion = clustersExt.selectSparkVersion( |
| 175 | + new SparkVersionSelector().withSparkVersion("3.5.0").withML()); |
| 176 | + assertEquals("14.2.x-cpu-ml-scala2.12", sparkVersion); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + void sparkVersionWithSparkVersionParameterNoMatch() { |
| 181 | + ClustersExt clustersExt = new ClustersExt(clustersMock); |
| 182 | + Mockito.doReturn(testGetSparkVersionsWithSparkVersion()).when(clustersMock).sparkVersions(); |
| 183 | + |
| 184 | + // Test spark version that doesn't exist |
| 185 | + assertThrows(IllegalArgumentException.class, () -> { |
| 186 | + clustersExt.selectSparkVersion( |
| 187 | + new SparkVersionSelector().withSparkVersion("2.4.5")); |
| 188 | + }); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + void sparkVersionWithSparkVersionParameterMultipleMatchesLatestFalse() { |
| 193 | + ClustersExt clustersExt = new ClustersExt(clustersMock); |
| 194 | + Mockito.doReturn(testGetSparkVersionsWithSparkVersion()).when(clustersMock).sparkVersions(); |
| 195 | + |
| 196 | + // Test spark version with multiple matches and latest=false (default) - should throw exception |
| 197 | + SparkVersionSelector selector = new SparkVersionSelector().withSparkVersion("3.5.0"); |
| 198 | + // latest defaults to false, so multiple matches should throw an exception |
| 199 | + |
| 200 | + assertThrows(IllegalArgumentException.class, () -> { |
| 201 | + clustersExt.selectSparkVersion(selector); |
| 202 | + }, "Expected exception when multiple versions match with latest=false (default)"); |
| 203 | + } |
121 | 204 | } |
0 commit comments