This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update attenuation-duration configuration spec (#431)
- Loading branch information
1 parent
20492aa
commit f778e04
Showing
13 changed files
with
324 additions
and
92 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
20 changes: 20 additions & 0 deletions
20
...src/main/proto/app/coronawarn/server/common/protocols/internal/attenuation_duration.proto
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,20 @@ | ||
syntax = "proto3"; | ||
package app.coronawarn.server.common.protocols.internal; | ||
option java_package = "app.coronawarn.server.common.protocols.internal"; | ||
option java_multiple_files = true; | ||
|
||
message AttenuationDuration { | ||
Thresholds thresholds = 1; | ||
Weights weights = 2; | ||
} | ||
|
||
message Thresholds { | ||
int32 lower = 1; | ||
int32 upper = 2; | ||
} | ||
|
||
message Weights { | ||
double low = 1; | ||
double mid = 2; | ||
double high = 3; | ||
} |
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
94 changes: 94 additions & 0 deletions
94
...ver/services/distribution/assembly/appconfig/validation/AttenuationDurationValidator.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,94 @@ | ||
/*- | ||
* ---license-start | ||
* Corona-Warn-App | ||
* --- | ||
* Copyright (C) 2020 SAP SE and all other contributors | ||
* --- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ---license-end | ||
*/ | ||
|
||
package app.coronawarn.server.services.distribution.assembly.appconfig.validation; | ||
|
||
import static app.coronawarn.server.services.distribution.assembly.appconfig.validation.GeneralValidationError.ErrorType.MIN_GREATER_THAN_MAX; | ||
import static app.coronawarn.server.services.distribution.assembly.appconfig.validation.GeneralValidationError.ErrorType.VALUE_OUT_OF_BOUNDS; | ||
import static app.coronawarn.server.services.distribution.assembly.appconfig.validation.ParameterSpec.ATTENUATION_DURATION_THRESHOLD_MAX; | ||
import static app.coronawarn.server.services.distribution.assembly.appconfig.validation.ParameterSpec.ATTENUATION_DURATION_THRESHOLD_MIN; | ||
import static app.coronawarn.server.services.distribution.assembly.appconfig.validation.ParameterSpec.ATTENUATION_DURATION_WEIGHT_MAX; | ||
import static app.coronawarn.server.services.distribution.assembly.appconfig.validation.ParameterSpec.ATTENUATION_DURATION_WEIGHT_MIN; | ||
import static app.coronawarn.server.services.distribution.assembly.appconfig.validation.WeightValidationError.ErrorType.OUT_OF_RANGE; | ||
import static app.coronawarn.server.services.distribution.assembly.appconfig.validation.WeightValidationError.ErrorType.TOO_MANY_DECIMAL_PLACES; | ||
|
||
import app.coronawarn.server.common.protocols.internal.AttenuationDuration; | ||
import java.math.BigDecimal; | ||
|
||
/** | ||
* The AttenuationDurationValidator validates the values of an associated {@link AttenuationDuration} instance. | ||
*/ | ||
public class AttenuationDurationValidator extends ConfigurationValidator { | ||
|
||
private final AttenuationDuration attenuationDuration; | ||
|
||
public AttenuationDurationValidator(AttenuationDuration attenuationDuration) { | ||
this.attenuationDuration = attenuationDuration; | ||
} | ||
|
||
@Override | ||
public ValidationResult validate() { | ||
errors = new ValidationResult(); | ||
|
||
validateThresholds(); | ||
validateWeights(); | ||
|
||
return errors; | ||
} | ||
|
||
private void validateThresholds() { | ||
int lower = attenuationDuration.getThresholds().getLower(); | ||
int upper = attenuationDuration.getThresholds().getUpper(); | ||
|
||
checkThresholdBound("lower", lower); | ||
checkThresholdBound("upper", upper); | ||
|
||
if (lower > upper) { | ||
String parameters = "attenuation-duration.thresholds.lower, attenuation-duration.thresholds.upper"; | ||
String values = lower + ", " + upper; | ||
this.errors.add(new GeneralValidationError(parameters, values, MIN_GREATER_THAN_MAX)); | ||
} | ||
} | ||
|
||
private void checkThresholdBound(String thresholdLabel, int thresholdValue) { | ||
if (thresholdValue < ATTENUATION_DURATION_THRESHOLD_MIN || thresholdValue > ATTENUATION_DURATION_THRESHOLD_MAX) { | ||
this.errors.add(new GeneralValidationError( | ||
"attenuation-duration.thresholds." + thresholdLabel, thresholdValue, VALUE_OUT_OF_BOUNDS)); | ||
} | ||
} | ||
|
||
private void validateWeights() { | ||
checkWeight("low", attenuationDuration.getWeights().getLow()); | ||
checkWeight("mid", attenuationDuration.getWeights().getMid()); | ||
checkWeight("high", attenuationDuration.getWeights().getHigh()); | ||
} | ||
|
||
private void checkWeight(String weightLabel, double weightValue) { | ||
if (weightValue < ATTENUATION_DURATION_WEIGHT_MIN || weightValue > ATTENUATION_DURATION_WEIGHT_MAX) { | ||
this.errors.add(new WeightValidationError( | ||
"attenuation-duration.weights." + weightLabel, weightValue, OUT_OF_RANGE)); | ||
} | ||
|
||
if (BigDecimal.valueOf(weightValue).scale() > ParameterSpec.ATTENUATION_DURATION_WEIGHT_MAX_DECIMALS) { | ||
this.errors.add(new WeightValidationError( | ||
"attenuation-duration.weights." + weightLabel, weightValue, TOO_MANY_DECIMAL_PLACES)); | ||
} | ||
} | ||
} |
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
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
15 changes: 15 additions & 0 deletions
15
services/distribution/src/main/resources/master-config/attenuation-duration.yaml
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,15 @@ | ||
# This is the attenuation and duration parameter thresholds. The lower and | ||
# upper threshold partitions the value range into 3 subsets: low, mid, high | ||
# | ||
# Each of the aforementioned partitions has a weight in the range of [0, 1] | ||
# assigned to it. The number of decimal places is restricted to 3. | ||
# | ||
# Change this file with caution! | ||
|
||
thresholds: | ||
lower: 50 | ||
upper: 70 | ||
weights: | ||
low: 1.0 | ||
mid: 0.5 | ||
high: 0.0 |
47 changes: 47 additions & 0 deletions
47
...rn/server/services/distribution/assembly/appconfig/AttenuationDurationMasterFileTest.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,47 @@ | ||
/*- | ||
* ---license-start | ||
* Corona-Warn-App | ||
* --- | ||
* Copyright (C) 2020 SAP SE and all other contributors | ||
* --- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ---license-end | ||
*/ | ||
|
||
package app.coronawarn.server.services.distribution.assembly.appconfig; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import app.coronawarn.server.common.protocols.internal.AttenuationDuration; | ||
import app.coronawarn.server.services.distribution.assembly.appconfig.validation.AttenuationDurationValidator; | ||
import app.coronawarn.server.services.distribution.assembly.appconfig.validation.ConfigurationValidator; | ||
import app.coronawarn.server.services.distribution.assembly.appconfig.validation.ValidationResult; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* This test will verify that the provided attenuation/duration parameters master file is syntactically correct and | ||
* according to spec. There should never be any deployment when this test is failing. | ||
*/ | ||
class AttenuationDurationMasterFileTest { | ||
|
||
private static final ValidationResult SUCCESS = new ValidationResult(); | ||
|
||
@Test | ||
void testMasterFile() throws UnableToLoadFileException { | ||
AttenuationDuration config = ApplicationConfigurationProvider.readMasterFile().getAttenuationDuration(); | ||
|
||
ConfigurationValidator validator = new AttenuationDurationValidator(config); | ||
|
||
assertThat(validator.validate()).isEqualTo(SUCCESS); | ||
} | ||
} |
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
Oops, something went wrong.