-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SyncTest for local testing and update rest template sslcontext
- Loading branch information
Showing
6 changed files
with
266 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...sdk/dpppt-backend-sdk-interops/src/test/java/org/dpppt/backend/sdk/interops/SyncTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package org.dpppt.backend.sdk.interops; | ||
|
||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
import java.security.SecureRandom; | ||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneOffset; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.apache.commons.codec.binary.Base64; | ||
import org.bouncycastle.cms.CMSException; | ||
import org.bouncycastle.operator.OperatorCreationException; | ||
import org.dpppt.backend.sdk.data.gaen.GaenDataService; | ||
import org.dpppt.backend.sdk.data.interops.SyncLogDataService; | ||
import org.dpppt.backend.sdk.interops.config.FlyWayConfig; | ||
import org.dpppt.backend.sdk.interops.config.GaenDataServiceConfig; | ||
import org.dpppt.backend.sdk.interops.config.StandaloneDataConfig; | ||
import org.dpppt.backend.sdk.interops.config.SyncLogDataServiceConfig; | ||
import org.dpppt.backend.sdk.interops.model.EfgsGatewayConfig; | ||
import org.dpppt.backend.sdk.interops.syncer.EfgsHubSyncer; | ||
import org.dpppt.backend.sdk.interops.syncer.efgs.EfgsClient; | ||
import org.dpppt.backend.sdk.model.gaen.GaenKey; | ||
import org.dpppt.backend.sdk.model.gaen.GaenKeyWithOrigin; | ||
import org.dpppt.backend.sdk.utils.UTCInstant; | ||
import org.junit.Assert; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.test.context.support.AnnotationConfigContextLoader; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration( | ||
loader = AnnotationConfigContextLoader.class, | ||
classes = { | ||
StandaloneDataConfig.class, | ||
FlyWayConfig.class, | ||
GaenDataServiceConfig.class, | ||
SyncLogDataServiceConfig.class | ||
}) | ||
public class SyncTest { | ||
|
||
@Autowired private GaenDataService gaenDataService; | ||
|
||
@Autowired private SyncLogDataService syncLogDataService; | ||
|
||
private static final SecureRandom SECURE_RANDOM = new SecureRandom(); | ||
|
||
@Test | ||
@Ignore("for local testing") | ||
public void testEfgsClientUpload() | ||
throws GeneralSecurityException, OperatorCreationException, CMSException, IOException { | ||
EfgsClient efgsClient = new EfgsClient(getEfgsGatewayConfig()); | ||
String batchTag = getBatchTag(); | ||
List<GaenKeyWithOrigin> keysToUpload = createMockedKeys(10); | ||
List<GaenKeyWithOrigin> uploadedKeys = efgsClient.upload(keysToUpload, batchTag); | ||
Assert.assertEquals(keysToUpload.size(), uploadedKeys.size()); | ||
} | ||
|
||
@Test | ||
// @Ignore("for local testing") | ||
public void testEfgsClientDownload() throws GeneralSecurityException { | ||
EfgsHubSyncer syncer = | ||
new EfgsHubSyncer( | ||
new EfgsClient(getEfgsGatewayConfig()), | ||
Duration.ofDays(14), | ||
gaenDataService, | ||
syncLogDataService); | ||
syncer.download(UTCInstant.today().getLocalDate()); | ||
} | ||
|
||
private EfgsGatewayConfig getEfgsGatewayConfig() { | ||
EfgsGatewayConfig efgsGatewayConfig = new EfgsGatewayConfig(); | ||
efgsGatewayConfig.setId("efgs-gateway"); | ||
efgsGatewayConfig.setBaseUrl("https://api.ch-hub-r.bit.admin.ch"); | ||
efgsGatewayConfig.setAuthClientCert( | ||
"base64:/*"); | ||
efgsGatewayConfig.setAuthClientCertPassword("*"); | ||
efgsGatewayConfig.setSignClientCert( | ||
"-----BEGIN CERTIFICATE-----\n*\n-----END CERTIFICATE-----\n"); | ||
efgsGatewayConfig.setSignClientCertPrivateKey( | ||
"-----BEGIN PRIVATE KEY-----\n*\n-----END PRIVATE KEY-----\n"); | ||
efgsGatewayConfig.setSignAlgorithmName("sha256WithRSAEncryption"); | ||
efgsGatewayConfig.setVisitedCountries(List.of("CH", "DE")); | ||
return efgsGatewayConfig; | ||
} | ||
|
||
private List<GaenKeyWithOrigin> createMockedKeys(int numOfKeysToCreate) { | ||
List<GaenKeyWithOrigin> keys = new ArrayList<>(); | ||
for (int i = 0; i < numOfKeysToCreate; i++) { | ||
byte[] bytes = new byte[16]; | ||
SECURE_RANDOM.nextBytes(bytes); | ||
GaenKeyWithOrigin keyWithOrigin = new GaenKeyWithOrigin(); | ||
keyWithOrigin.setGaenKey(new GaenKey()); | ||
keyWithOrigin.setKeyData(java.util.Base64.getEncoder().encodeToString(bytes)); | ||
keyWithOrigin.setRollingStartNumber( | ||
(int) UTCInstant.now().atStartOfDay().minusDays(1).get10MinutesSince1970()); | ||
keyWithOrigin.setRollingPeriod(144); | ||
keyWithOrigin.setTransmissionRiskLevel(0); | ||
keyWithOrigin.setFake(0); | ||
keyWithOrigin.setOrigin("CH"); | ||
keyWithOrigin.setId(i); | ||
keys.add(keyWithOrigin); | ||
} | ||
return keys; | ||
} | ||
|
||
private String getBatchTag() { | ||
byte[] hash = new byte[4]; | ||
SECURE_RANDOM.nextBytes(hash); | ||
var now = LocalDateTime.now(ZoneOffset.UTC); | ||
return String.format( | ||
"%d-%d-%d-%s-%d", | ||
now.getYear(), | ||
now.getMonth().getValue(), | ||
now.getDayOfMonth(), | ||
Base64.encodeBase64String(hash), | ||
0); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ackend-sdk-interops/src/test/java/org/dpppt/backend/sdk/interops/config/FlyWayConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
package org.dpppt.backend.sdk.interops.config; | ||
|
||
import javax.sql.DataSource; | ||
import org.flywaydb.core.Flyway; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class FlyWayConfig { | ||
|
||
@Autowired DataSource dataSource; | ||
|
||
@Bean | ||
public Flyway flyway() { | ||
Flyway flyWay = | ||
Flyway.configure() | ||
.dataSource(dataSource) | ||
.locations("classpath:/db/migration/hsqldb") | ||
.load(); | ||
flyWay.migrate(); | ||
return flyWay; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...k-interops/src/test/java/org/dpppt/backend/sdk/interops/config/GaenDataServiceConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
package org.dpppt.backend.sdk.interops.config; | ||
|
||
import java.time.Duration; | ||
import javax.sql.DataSource; | ||
import org.dpppt.backend.sdk.data.gaen.GaenDataService; | ||
import org.dpppt.backend.sdk.data.gaen.JdbcGaenDataServiceImpl; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
@Configuration | ||
public class GaenDataServiceConfig { | ||
|
||
@Value("${ws.exposedlist.releaseBucketDuration: 7200000}") | ||
long releaseBucketDuration; | ||
|
||
final String originCountry = "CH"; | ||
|
||
final Duration timeSkew = Duration.ofHours(2); | ||
|
||
@Autowired DataSource dataSource; | ||
|
||
@Autowired String dbType; | ||
|
||
@Bean | ||
public GaenDataService gaenDataService() { | ||
return new JdbcGaenDataServiceImpl( | ||
dbType, dataSource, Duration.ofMillis(releaseBucketDuration), timeSkew, originCountry); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...dk-interops/src/test/java/org/dpppt/backend/sdk/interops/config/StandaloneDataConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
package org.dpppt.backend.sdk.interops.config; | ||
|
||
import javax.sql.DataSource; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; | ||
|
||
@Configuration | ||
public class StandaloneDataConfig { | ||
|
||
@Bean | ||
public DataSource dataSource() { | ||
return new EmbeddedDatabaseBuilder() | ||
.generateUniqueName(true) | ||
.setType(EmbeddedDatabaseType.HSQL) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public String dbType() { | ||
return "hsqldb"; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...nterops/src/test/java/org/dpppt/backend/sdk/interops/config/SyncLogDataServiceConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
package org.dpppt.backend.sdk.interops.config; | ||
|
||
import javax.sql.DataSource; | ||
import org.dpppt.backend.sdk.data.interops.JdbcSyncLogDataServiceImpl; | ||
import org.dpppt.backend.sdk.data.interops.SyncLogDataService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
@Configuration | ||
public class SyncLogDataServiceConfig { | ||
|
||
@Autowired DataSource dataSource; | ||
|
||
@Autowired String dbType; | ||
|
||
@Bean | ||
public SyncLogDataService syncLogDataService() { | ||
return new JdbcSyncLogDataServiceImpl(dbType, dataSource); | ||
} | ||
} |