Skip to content

Commit 1ef561a

Browse files
committed
Migrate "testng" package to Kotlin, add Java tests
1 parent cb092e8 commit 1ef561a

File tree

5 files changed

+126
-73
lines changed

5 files changed

+126
-73
lines changed

testsupport/testng/src/main/java/com/adobe/testing/s3mock/testng/package-info.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

testsupport/testng/src/main/kotlin/com/adobe/testing/s3mock/testng/S3Mock.kt

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,36 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
package com.adobe.testing.s3mock.testng
1617

17-
package com.adobe.testing.s3mock.testng;
18-
19-
import com.adobe.testing.s3mock.testsupport.common.S3MockStarter;
18+
import com.adobe.testing.s3mock.testsupport.common.S3MockStarter
2019

2120
/**
22-
* Singleton extending {@link com.adobe.testing.s3mock.testsupport.common.S3MockStarter}.
21+
* Singleton extending [S3MockStarter].
22+
*
2323
*
24-
* <p>Used in the {@link com.adobe.testing.s3mock.testng.S3MockListener} to start
25-
* {@link com.adobe.testing.s3mock.S3MockApplication} when TestNG starts running the suites and to
26-
* stop when TestNG has run all the suites</p>
24+
* Used in the [S3MockListener] to start
25+
* [com.adobe.testing.s3mock.S3MockApplication] when TestNG starts running the suites and to
26+
* stop when TestNG has run all the suites
2727
*/
28-
public class S3Mock extends S3MockStarter {
29-
30-
private static final S3Mock INSTANCE = new S3Mock();
31-
32-
private S3Mock() {
33-
super(null);
28+
class S3Mock private constructor() : S3MockStarter(null) {
29+
fun bootstrap() {
30+
start()
3431
}
3532

36-
/**
37-
* Returns an instance of S3Mock.
38-
*
39-
* @return an instance of S3Mock
40-
*/
41-
public static S3Mock getInstance() {
42-
return INSTANCE;
33+
fun terminate() {
34+
stop()
4335
}
4436

45-
void bootstrap() {
46-
start();
47-
}
37+
companion object {
38+
/**
39+
* Returns an instance of S3Mock.
40+
*
41+
* @return an instance of S3Mock
42+
*/
43+
private val _instance: S3Mock = S3Mock()
4844

49-
void terminate() {
50-
stop();
45+
@JvmStatic
46+
fun getInstance(): S3Mock = _instance
5147
}
5248
}
Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2021 Adobe.
2+
* Copyright 2017-2025 Adobe.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,46 +13,42 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
package com.adobe.testing.s3mock.testng
1617

17-
package com.adobe.testing.s3mock.testng;
18-
19-
import org.testng.IExecutionListener;
18+
import org.testng.IExecutionListener
2019

2120
/**
2221
* TestNG listener to start and stop the S3Mock Application. After the tests, the S3Mock is
2322
* stopped.
2423
*
2524
* <h2>Configuring through testng.xml file</h2>
2625
* <pre>
27-
* {@code <?xml version="1.0" encoding="UTF-8"?>
26+
* `<?xml version="1.0" encoding="UTF-8"?>
2827
* <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
2928
* <suite name="TestNG Listener Example">
30-
* <listeners>
31-
* <listener class-name="com.adobe.testing.s3mock.testng.S3ExecutionListener" />
32-
* </listeners>
29+
* <listeners>
30+
* <listener class-name="com.adobe.testing.s3mock.testng.S3ExecutionListener" />
31+
* </listeners>
3332
*
34-
* <test name="TestNG Sample Test" preserve-order="true">
35-
* <classes>
36-
* <class name="SampleS3MockTest">
37-
* <methods>
38-
* <include name="test1"/>
39-
* </methods>
40-
* </class>
41-
* </classes>
42-
* </test>
33+
* <test name="TestNG Sample Test" preserve-order="true">
34+
* <classes>
35+
* <class name="SampleS3MockTest">
36+
* <methods>
37+
* <include name="test1"/>
38+
* </methods>
39+
* </class>
40+
* </classes>
41+
* </test>
4342
* </suite>
44-
* }
45-
* </pre>
43+
` *
44+
</pre> *
4645
*/
47-
public class S3MockListener implements IExecutionListener {
48-
49-
@Override
50-
public void onExecutionStart() {
51-
S3Mock.getInstance().bootstrap();
46+
class S3MockListener : IExecutionListener {
47+
override fun onExecutionStart() {
48+
S3Mock.getInstance().bootstrap()
5249
}
5350

54-
@Override
55-
public void onExecutionFinish() {
56-
S3Mock.getInstance().terminate();
51+
override fun onExecutionFinish() {
52+
S3Mock.getInstance().terminate()
5753
}
5854
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2017-2025 Adobe.
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+
* http://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+
17+
package com.adobe.testing.s3mock.testng;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import com.adobe.testing.s3mock.util.DigestUtil;
22+
import java.io.File;
23+
import java.nio.file.Files;
24+
import org.testng.annotations.Test;
25+
import software.amazon.awssdk.core.sync.RequestBody;
26+
import software.amazon.awssdk.services.s3.S3Client;
27+
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
28+
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
29+
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
30+
31+
@Test
32+
public class S3MockListenerXmlConfigurationJavaTest {
33+
34+
private static final String BUCKET_NAME = "my-demo-test-bucket";
35+
private static final String UPLOAD_FILE_NAME = "src/test/resources/sampleFile.txt";
36+
37+
private final S3Client s3Client = S3Mock.getInstance().createS3ClientV2();
38+
39+
/**
40+
* Creates a bucket, stores a file, downloads the file again and compares checksums.
41+
*
42+
* @throws Exception if FileStreams can not be read
43+
*/
44+
@Test
45+
public void shouldUploadAndDownloadObject() throws Exception {
46+
var uploadFile = new File(UPLOAD_FILE_NAME);
47+
48+
s3Client.createBucket(CreateBucketRequest.builder().bucket(BUCKET_NAME).build());
49+
s3Client.putObject(
50+
PutObjectRequest
51+
.builder()
52+
.bucket(BUCKET_NAME)
53+
.key(uploadFile.getName())
54+
.build(),
55+
RequestBody.fromFile(uploadFile)
56+
);
57+
58+
var s3Object = s3Client.getObject(
59+
GetObjectRequest
60+
.builder()
61+
.bucket(BUCKET_NAME)
62+
.key(uploadFile.getName())
63+
.build()
64+
);
65+
66+
var uploadFileIs = Files.newInputStream(uploadFile.toPath());
67+
var uploadDigest = DigestUtil.hexDigest(uploadFileIs);
68+
var downloadedDigest = DigestUtil.hexDigest(s3Object);
69+
uploadFileIs.close();
70+
s3Object.close();
71+
72+
assertThat(uploadDigest)
73+
.as("Up- and downloaded Files should have equal digests")
74+
.isEqualTo(downloadedDigest);
75+
}
76+
}

testsupport/testng/src/test/resources/testng.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2017-2019 Adobe.
4+
Copyright 2017-2025 Adobe.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -29,6 +29,11 @@
2929
<include name="shouldUploadAndDownloadObject"/>
3030
</methods>
3131
</class>
32+
<class name="com.adobe.testing.s3mock.testng.S3MockListenerXmlConfigurationJavaTest">
33+
<methods>
34+
<include name="shouldUploadAndDownloadObject"/>
35+
</methods>
36+
</class>
3237
</classes>
3338
</test>
3439
</suite>

0 commit comments

Comments
 (0)