/
MongoDBArtifactStoreTest.java
148 lines (118 loc) · 6.63 KB
/
MongoDBArtifactStoreTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.artifact.repository;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import org.eclipse.hawkbit.artifact.TestConfiguration;
import org.eclipse.hawkbit.artifact.repository.model.AbstractDbArtifact;
import org.eclipse.hawkbit.artifact.repository.model.DbArtifactHash;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Description;
import org.springframework.test.context.junit4.SpringRunner;
import com.google.common.io.BaseEncoding;
import io.qameta.allure.Feature;
import io.qameta.allure.Step;
import io.qameta.allure.Story;
@Feature("Component Tests - Repository")
@Story("Artifact Store MongoDB")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MongoDBArtifactStoreAutoConfiguration.class, TestConfiguration.class}, properties = {
"spring.mongodb.embedded.version=3.5.5",
"spring.mongodb.embedded.features=sync_delay,no_http_interface_arg"})
public class MongoDBArtifactStoreTest {
private static final String TENANT = "test_tenant";
private static final String TENANT2 = "test_tenant2";
@Autowired
private MongoDBArtifactStore artifactStoreUnderTest;
@Test
@Description("Ensures that search by SHA1 hash (which is used by hawkBit as artifact ID) finds the expected results.")
public void findArtifactBySHA1Hash() throws NoSuchAlgorithmException, IOException {
final String sha1 = storeRandomArtifactAndVerify(TENANT);
final String sha2 = storeRandomArtifactAndVerify(TENANT2);
assertThat(artifactStoreUnderTest.getArtifactBySha1(TENANT2, sha1)).isNull();
assertThat(artifactStoreUnderTest.getArtifactBySha1(TENANT, sha2)).isNull();
}
@Test
@Description("Deletes file from repository identified by SHA1 hash as filename.")
public void deleteArtifactBySHA1Hash() throws NoSuchAlgorithmException, IOException {
final String sha1 = storeRandomArtifactAndVerify(TENANT);
artifactStoreUnderTest.deleteBySha1(TENANT, sha1);
assertThat(artifactStoreUnderTest.getArtifactBySha1(TENANT, sha1)).isNull();
}
@Test
@Description("Verfies that all data of a tenant is erased if repository is asked to do so. "
+ "Data of other tenants is not affected.")
public void deleteTenant() throws NoSuchAlgorithmException, IOException {
final String shaDeleted = storeRandomArtifactAndVerify(TENANT);
final String shaUndeleted = storeRandomArtifactAndVerify("another_tenant");
artifactStoreUnderTest.deleteByTenant("tenant_that_does_not_exist");
artifactStoreUnderTest.deleteByTenant(TENANT);
assertThat(artifactStoreUnderTest.getArtifactBySha1(TENANT, shaDeleted)).isNull();
assertThat(artifactStoreUnderTest.getArtifactBySha1("another_tenant", shaUndeleted)).isNotNull();
}
@Test
@Description("Verfies that artifacts with equal binary content are only stored once.")
public void storeSameArtifactMultipleTimes() throws NoSuchAlgorithmException, IOException {
final byte[] bytes = new byte[128];
new Random().nextBytes(bytes);
final MessageDigest mdSHA1 = MessageDigest.getInstance("SHA1");
final MessageDigest mdSHA256 = MessageDigest.getInstance("SHA-256");
final MessageDigest mdMD5 = MessageDigest.getInstance("MD5");
final DbArtifactHash hash = new DbArtifactHash(BaseEncoding.base16().lowerCase().encode(mdSHA1.digest(bytes)),
BaseEncoding.base16().lowerCase().encode(mdMD5.digest(bytes)),
BaseEncoding.base16().lowerCase().encode(mdSHA256.digest(bytes)));
final AbstractDbArtifact artifact1 = storeArtifact(TENANT, "file1.txt", new ByteArrayInputStream(bytes), mdSHA1,
mdMD5, hash);
final AbstractDbArtifact artifact2 = storeArtifact(TENANT, "file2.bla", new ByteArrayInputStream(bytes), mdSHA1,
mdMD5, hash);
assertThat(artifact1.getArtifactId()).isEqualTo(artifact2.getArtifactId());
}
@Step
private String storeRandomArtifactAndVerify(final String tenant) throws NoSuchAlgorithmException, IOException {
final int filelengthBytes = 128;
final String filename = "testfile.json";
final MessageDigest mdSHA1 = MessageDigest.getInstance("SHA1");
final MessageDigest mdMD5 = MessageDigest.getInstance("MD5");
storeArtifact(tenant, filename, generateInputStream(filelengthBytes), mdSHA1, mdMD5, null);
final String sha1Hash16 = BaseEncoding.base16().lowerCase().encode(mdSHA1.digest());
final String md5Hash16 = BaseEncoding.base16().lowerCase().encode(mdMD5.digest());
final AbstractDbArtifact loaded = artifactStoreUnderTest.getArtifactBySha1(tenant, sha1Hash16);
assertThat(loaded).isNotNull();
assertThat(loaded.getContentType()).isEqualTo("application/json");
assertThat(loaded.getHashes().getSha1()).isEqualTo(sha1Hash16);
assertThat(loaded.getHashes().getMd5()).isNull();
assertThat(loaded.getSize()).isEqualTo(filelengthBytes);
return sha1Hash16;
}
private AbstractDbArtifact storeArtifact(final String tenant, final String filename, final InputStream content,
final MessageDigest sha1, final MessageDigest md5, final DbArtifactHash hash)
throws NoSuchAlgorithmException, IOException {
try (final DigestInputStream digestInputStream = wrapInDigestInputStream(content, sha1, md5)) {
return artifactStoreUnderTest.store(tenant, digestInputStream, filename, "application/json", hash);
}
}
private static ByteArrayInputStream generateInputStream(final int length) {
final byte[] bytes = new byte[length];
new Random().nextBytes(bytes);
return new ByteArrayInputStream(bytes);
}
private static DigestInputStream wrapInDigestInputStream(final InputStream input, final MessageDigest mdSHA1,
final MessageDigest mdMD5) {
return new DigestInputStream(new DigestInputStream(input, mdMD5), mdSHA1);
}
}