From 36df9b6be7e2e078e55da7955945e24f6f31802d Mon Sep 17 00:00:00 2001 From: Bram Boskamp Date: Tue, 20 Feb 2024 14:09:07 +0000 Subject: [PATCH 1/4] disallow twice write to the same 'samples' component (toml) --- .../api/Object_component_write.java | 4 ++++ .../api/CoderunIntegrationTest.java | 15 +++++++++++++++ api/src/test/resources/config-stdapi.yaml | 4 ++++ 3 files changed, 23 insertions(+) diff --git a/api/src/main/java/org/fairdatapipeline/api/Object_component_write.java b/api/src/main/java/org/fairdatapipeline/api/Object_component_write.java index 5204143..6518e6d 100644 --- a/api/src/main/java/org/fairdatapipeline/api/Object_component_write.java +++ b/api/src/main/java/org/fairdatapipeline/api/Object_component_write.java @@ -94,11 +94,15 @@ public void writeDistribution(Distribution distribution) { * @param samples a Samples object containing the samples */ public void writeSamples(Samples samples) { + if(this.been_used) { + throw(new RuntimeException("obj component already written")); + } try (CleanableFileChannel fileChannel = this.getFileChannel()) { this.dp.coderun.parameterDataWriter.write(fileChannel, this.component_name, samples); } catch (IOException e) { throw (new RuntimeException("writeSamples() -- IOException trying to write to file.", e)); } + this.been_used = true; } void register_me_in_code_run() { diff --git a/api/src/test/java/org/fairdatapipeline/api/CoderunIntegrationTest.java b/api/src/test/java/org/fairdatapipeline/api/CoderunIntegrationTest.java index 6c9bc30..bf19b10 100644 --- a/api/src/test/java/org/fairdatapipeline/api/CoderunIntegrationTest.java +++ b/api/src/test/java/org/fairdatapipeline/api/CoderunIntegrationTest.java @@ -2,6 +2,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertThrows; + import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -407,6 +409,19 @@ void testReadCategoricalDistribution() { check_last_coderun(List.of(new Triplet<>(dataProduct, component, hash)), null); } + @Test + @Order(7) + void testWriteSamplesFail() throws RuntimeException { + String dataProduct = "human/samples2"; + String component = "example-samples-w"; + try (var coderun = new Coderun(configPath, scriptPath, token)) { + Data_product_write dp = coderun.get_dp_for_write(dataProduct, "toml"); + Object_component_write oc = dp.getComponent(component); + oc.writeSamples(samples2); + assertThrows(RuntimeException.class, () -> oc.writeSamples(samples3)); + } + } + @Test @Order(7) void testWriteSamples() { diff --git a/api/src/test/resources/config-stdapi.yaml b/api/src/test/resources/config-stdapi.yaml index bcddb5f..19bfd0c 100644 --- a/api/src/test/resources/config-stdapi.yaml +++ b/api/src/test/resources/config-stdapi.yaml @@ -66,6 +66,10 @@ write: description: Coderun Integration test for samples use: version: 0.0.1 + - data_product: human/samples2 + description: Coderun Integration test for samples + use: + version: 0.0.1 - data_product: human/multicomp description: Coderun Integration test for samples multiple components use: From 718ccdc790593f06a2d36dc2b259a8559462b501 Mon Sep 17 00:00:00 2001 From: Bram Boskamp Date: Wed, 21 Feb 2024 14:12:58 +0000 Subject: [PATCH 2/4] disallow write twice to 'estimate' and 'distribution' component (toml) --- .../api/Object_component_write.java | 8 +++++++ .../api/CoderunIntegrationTest.java | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/api/src/main/java/org/fairdatapipeline/api/Object_component_write.java b/api/src/main/java/org/fairdatapipeline/api/Object_component_write.java index 6518e6d..7cd5838 100644 --- a/api/src/main/java/org/fairdatapipeline/api/Object_component_write.java +++ b/api/src/main/java/org/fairdatapipeline/api/Object_component_write.java @@ -64,6 +64,9 @@ public CleanableFileChannel writeFileChannel() throws IOException { * @param estimateNumber the number to write. */ public void writeEstimate(Number estimateNumber) { + if(this.been_used) { + throw(new RuntimeException("obj component already written")); + } var estimate = ImmutableEstimate.builder().internalValue(estimateNumber).rng(this.dp.coderun.rng).build(); @@ -72,6 +75,7 @@ public void writeEstimate(Number estimateNumber) { } catch (IOException e) { throw (new RuntimeException("writeEstimate() -- IOException trying to write to file.", e)); } + this.been_used = true; } /** @@ -80,12 +84,16 @@ public void writeEstimate(Number estimateNumber) { * @param distribution the Distribution to write */ public void writeDistribution(Distribution distribution) { + if(this.been_used) { + throw(new RuntimeException("obj component already written")); + } try (CleanableFileChannel fileChannel = this.getFileChannel()) { this.dp.coderun.parameterDataWriter.write(fileChannel, this.component_name, distribution); } catch (IOException e) { throw (new RuntimeException( "writeDistribution() -- IOException trying to write to file.", e)); } + this.been_used = true; } /** diff --git a/api/src/test/java/org/fairdatapipeline/api/CoderunIntegrationTest.java b/api/src/test/java/org/fairdatapipeline/api/CoderunIntegrationTest.java index bf19b10..93334eb 100644 --- a/api/src/test/java/org/fairdatapipeline/api/CoderunIntegrationTest.java +++ b/api/src/test/java/org/fairdatapipeline/api/CoderunIntegrationTest.java @@ -325,6 +325,18 @@ void check_last_coderun( } } + @Test + @Order(1) + void testWriteEstimateFail() { + String dataProduct = "human/population2"; + String component = "estimate-component"; + try (Coderun coderun = new Coderun(configPath, scriptPath, token)) { + Data_product_write dp = coderun.get_dp_for_write(dataProduct, "toml"); + Object_component_write oc = dp.getComponent(component); + oc.writeEstimate(estimate); + assertThrows(RuntimeException.class, () -> oc.writeEstimate(estimate)); + } + } @Test @Order(1) void testWriteEstimate() { @@ -381,6 +393,18 @@ void testReadDistribution() { check_last_coderun(List.of(new Triplet<>(dataProduct, component, hash)), null); } + @Test + @Order(5) + void testWriteCategoricalDistributionFail() { + String dataProduct = "human/cdistribution2"; + String component = "cdistribution-component"; + try (var coderun = new Coderun(configPath, scriptPath, token)) { + Data_product_write dp = coderun.get_dp_for_write(dataProduct, "toml"); + Object_component_write oc = dp.getComponent(component); + oc.writeDistribution(categoricalDistribution); + assertThrows(RuntimeException.class, () -> oc.writeDistribution(distribution)); + } + } @Test @Order(5) void testWriteCategoricalDistribution() { From 8c3e936f388cb2af8133b9703acf3fe1df9a1830 Mon Sep 17 00:00:00 2001 From: Bram Boskamp Date: Wed, 21 Feb 2024 14:27:07 +0000 Subject: [PATCH 3/4] spotlessApply --- .../fairdatapipeline/api/Object_component_write.java | 12 ++++++------ .../fairdatapipeline/api/CoderunIntegrationTest.java | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/org/fairdatapipeline/api/Object_component_write.java b/api/src/main/java/org/fairdatapipeline/api/Object_component_write.java index 7cd5838..327068f 100644 --- a/api/src/main/java/org/fairdatapipeline/api/Object_component_write.java +++ b/api/src/main/java/org/fairdatapipeline/api/Object_component_write.java @@ -64,8 +64,8 @@ public CleanableFileChannel writeFileChannel() throws IOException { * @param estimateNumber the number to write. */ public void writeEstimate(Number estimateNumber) { - if(this.been_used) { - throw(new RuntimeException("obj component already written")); + if (this.been_used) { + throw (new RuntimeException("obj component already written")); } var estimate = ImmutableEstimate.builder().internalValue(estimateNumber).rng(this.dp.coderun.rng).build(); @@ -84,8 +84,8 @@ public void writeEstimate(Number estimateNumber) { * @param distribution the Distribution to write */ public void writeDistribution(Distribution distribution) { - if(this.been_used) { - throw(new RuntimeException("obj component already written")); + if (this.been_used) { + throw (new RuntimeException("obj component already written")); } try (CleanableFileChannel fileChannel = this.getFileChannel()) { this.dp.coderun.parameterDataWriter.write(fileChannel, this.component_name, distribution); @@ -102,8 +102,8 @@ public void writeDistribution(Distribution distribution) { * @param samples a Samples object containing the samples */ public void writeSamples(Samples samples) { - if(this.been_used) { - throw(new RuntimeException("obj component already written")); + if (this.been_used) { + throw (new RuntimeException("obj component already written")); } try (CleanableFileChannel fileChannel = this.getFileChannel()) { this.dp.coderun.parameterDataWriter.write(fileChannel, this.component_name, samples); diff --git a/api/src/test/java/org/fairdatapipeline/api/CoderunIntegrationTest.java b/api/src/test/java/org/fairdatapipeline/api/CoderunIntegrationTest.java index 93334eb..06170b2 100644 --- a/api/src/test/java/org/fairdatapipeline/api/CoderunIntegrationTest.java +++ b/api/src/test/java/org/fairdatapipeline/api/CoderunIntegrationTest.java @@ -3,7 +3,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.assertThrows; - import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -337,6 +336,7 @@ void testWriteEstimateFail() { assertThrows(RuntimeException.class, () -> oc.writeEstimate(estimate)); } } + @Test @Order(1) void testWriteEstimate() { @@ -405,6 +405,7 @@ void testWriteCategoricalDistributionFail() { assertThrows(RuntimeException.class, () -> oc.writeDistribution(distribution)); } } + @Test @Order(5) void testWriteCategoricalDistribution() { From f5733345bcbd93b58ad9ccad7d4ddf1627f9469e Mon Sep 17 00:00:00 2001 From: Bram Boskamp Date: Wed, 21 Feb 2024 14:34:14 +0000 Subject: [PATCH 4/4] changed test yaml for new fail tests with twice write --- api/src/test/resources/config-stdapi.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/api/src/test/resources/config-stdapi.yaml b/api/src/test/resources/config-stdapi.yaml index 19bfd0c..0f44ab5 100644 --- a/api/src/test/resources/config-stdapi.yaml +++ b/api/src/test/resources/config-stdapi.yaml @@ -50,6 +50,10 @@ read: version: 0.0.1 write: + - data_product: human/population2 + description: Coderun Integration test + use: + version: 0.0.1 - data_product: human/population description: Coderun Integration test use: @@ -58,6 +62,10 @@ write: description: Coderun Integration test for distribution use: version: 0.0.1 + - data_product: human/cdistribution2 + description: Coderun Integration test for cdistribution + use: + version: 0.0.1 - data_product: human/cdistribution description: Coderun Integration test for cdistribution use: