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..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,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; } /** @@ -94,11 +102,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..06170b2 100644 --- a/api/src/test/java/org/fairdatapipeline/api/CoderunIntegrationTest.java +++ b/api/src/test/java/org/fairdatapipeline/api/CoderunIntegrationTest.java @@ -2,6 +2,7 @@ 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; @@ -323,6 +324,19 @@ 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() { @@ -379,6 +393,19 @@ 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() { @@ -407,6 +434,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..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: @@ -66,6 +74,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: