From 6e6ed04f77ecab15f5b7cad3863314ea08d6a38e Mon Sep 17 00:00:00 2001 From: Paul Harrison Date: Wed, 23 Oct 2024 15:49:29 +0100 Subject: [PATCH] refactor the examples a bit more Add a FullExample which will be a container for all the examples --- .../proposal/prop/AbstractProposalTest.java | 4 +- .../dm/proposal/prop/EmerlinExampleTest.java | 24 +++- .../ivoa/dm/proposal/prop/FullExample.java | 82 ++++++++++++++ .../dm/proposal/prop/FullExampleTest.java | 105 ++++++++++++++++++ src/test/resources/logback.xml | 2 +- 5 files changed, 213 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/ivoa/dm/proposal/prop/FullExample.java create mode 100644 src/test/java/org/ivoa/dm/proposal/prop/FullExampleTest.java diff --git a/src/test/java/org/ivoa/dm/proposal/prop/AbstractProposalTest.java b/src/test/java/org/ivoa/dm/proposal/prop/AbstractProposalTest.java index 3b5f6ec..230ba4a 100644 --- a/src/test/java/org/ivoa/dm/proposal/prop/AbstractProposalTest.java +++ b/src/test/java/org/ivoa/dm/proposal/prop/AbstractProposalTest.java @@ -266,7 +266,7 @@ public void testObservationTarget() { } - protected ObservingProposal cloneProposal(ObservingProposal p) + public static ObservingProposal cloneProposal(ObservingProposal p) { ProposalModel pm = new ProposalModel(); pm.createContext();//FIXME this is an area where the API for https://github.com/ivoa/vo-dml/issues/42 is not great @@ -278,7 +278,7 @@ protected ObservingProposal cloneProposal(ObservingProposal p) private ProposalCycle doTacWork() { final ProposalCycle cycle = ex.getCycle(); ObservingProposal prop = ex.getProposal(); - SubmittedProposal sprop = ex.submitProposal(prop); + SubmittedProposal sprop = ex.submitProposal(cloneProposal(prop)); ex.allocateProposal(sprop); return cycle; diff --git a/src/test/java/org/ivoa/dm/proposal/prop/EmerlinExampleTest.java b/src/test/java/org/ivoa/dm/proposal/prop/EmerlinExampleTest.java index 4ca048f..62cd66e 100644 --- a/src/test/java/org/ivoa/dm/proposal/prop/EmerlinExampleTest.java +++ b/src/test/java/org/ivoa/dm/proposal/prop/EmerlinExampleTest.java @@ -11,13 +11,17 @@ import static org.junit.jupiter.api.Assertions.*; +import java.io.IOException; import java.util.Arrays; import java.util.List; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import org.ivoa.dm.ivoa.RealQuantity; import org.ivoa.dm.proposal.management.AllocatedProposal; +import org.ivoa.dm.proposal.management.ObservingMode; import org.ivoa.dm.proposal.management.ProposalCycle; import org.ivoa.dm.proposal.management.ProposalManagementModel; import org.ivoa.dm.proposal.management.SubmittedProposal; @@ -278,7 +282,25 @@ public void testCopy() { assertNotNull(tobs2); } - + @org.junit.jupiter.api.Test + public void TestModes() throws IOException { + ProposalManagementModel model = new ProposalManagementModel(); + final ProposalCycle cycle = ex.getCycle(); + model.addContent(cycle); + jakarta.persistence.EntityManager em = setupH2Db(ProposalManagementModel.pu_name()); + em.getTransaction().begin(); + cycle.persistRefs(em); + em.persist(cycle); + em.getTransaction().commit(); + ObjectMapper mapper = model.management().jsonMapper(); + String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(cycle.getObservingModes()); + System.out.println("JSON output"); + System.out.println(json); + List retval = mapper.readValue(json,new TypeReference>(){} ); + assertNotNull(retval); + + retval = mapper.readValue(this.getClass().getResourceAsStream("/observingmodes.json"),new TypeReference>(){} ); + } diff --git a/src/test/java/org/ivoa/dm/proposal/prop/FullExample.java b/src/test/java/org/ivoa/dm/proposal/prop/FullExample.java new file mode 100644 index 0000000..e7dae1c --- /dev/null +++ b/src/test/java/org/ivoa/dm/proposal/prop/FullExample.java @@ -0,0 +1,82 @@ +/* + * Created on 22 Oct 2024 + * Copyright 2024 Paul Harrison (paul.harrison@manchester.ac.uk) + * + * 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 in file LICENSE + */ + +package org.ivoa.dm.proposal.prop; + +import org.ivoa.dm.proposal.management.ProposalCycle; +import org.ivoa.dm.proposal.management.ProposalManagementModel; +import org.ivoa.dm.proposal.management.SubmittedProposal; + +import jakarta.persistence.EntityManager; + +/** + * Complete example with all example observatories . + * @author Paul Harrison (paul.harrison@manchester.ac.uk) + * @since 22 Oct 2024 + */ +public class FullExample { + + + private ObservingProposal proposal; + private ProposalCycle cycle; + private ProposalManagementModel model = new ProposalManagementModel(); + + /** + * create full example. + */ + public FullExample() { + EmerlinExample emerlin = new EmerlinExample(); + proposal = new ExampleProposal().getProposal(); + cycle = emerlin.getCycle(); + ObservingProposal clonedProposal = AbstractProposalTest.cloneProposal(proposal); + clonedProposal.setSubmitted(true); + SubmittedProposal submittedProposal = emerlin.submitProposal(clonedProposal); + emerlin.allocateProposal(submittedProposal); + + } + + /** + * Save to an RDB. + * @param em the entity manager for the RDB. + */ + public void saveTodB(EntityManager em) { + + + cycle.persistRefs(em); + em.persist(cycle); + proposal.persistRefs(em); + em.persist(proposal); + + + + } + + /** + * get the management model. + * @return the model. + */ + public ProposalManagementModel getManagementModel() { + model.addContent(cycle); + return model; + } + + + /** + * get the proposal model. + * @return the model. + */ + public ProposalModel getProposalModel() { + ProposalModel pmodel = new ProposalModel(); + pmodel.addContent(proposal); + return pmodel; + } + +} + + diff --git a/src/test/java/org/ivoa/dm/proposal/prop/FullExampleTest.java b/src/test/java/org/ivoa/dm/proposal/prop/FullExampleTest.java new file mode 100644 index 0000000..1c52430 --- /dev/null +++ b/src/test/java/org/ivoa/dm/proposal/prop/FullExampleTest.java @@ -0,0 +1,105 @@ +/* + * Created on 22 Oct 2024 + * Copyright 2024 Paul Harrison (paul.harrison@manchester.ac.uk) + * + * 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 in file LICENSE + */ + +package org.ivoa.dm.proposal.prop; + +import static org.junit.jupiter.api.Assertions.*; + +import org.ivoa.dm.proposal.management.ProposalCycle; +import org.ivoa.dm.proposal.management.ProposalManagementModel; +import org.ivoa.vodml.testing.AutoDBRoundTripTest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Test the FullExample . + * @author Paul Harrison (paul.harrison@manchester.ac.uk) + */ +class FullExampleTest extends AutoDBRoundTripTest { + + /** + * @throws java.lang.Exception + */ + @BeforeAll + static void setUpBeforeClass() throws Exception { + + } + + private FullExample example; + + /** + * @throws java.lang.Exception + */ + @BeforeEach + void setUp() throws Exception { + + example = new FullExample(); + } + + /** + * @throws java.lang.Exception + */ + @AfterEach + void tearDown() throws Exception { + } + + /** + * {@inheritDoc} + * overrides @see org.ivoa.vodml.testing.AutoDBRoundTripTest#entityForDb() + */ + @Override + public ProposalCycle entityForDb() { + return example.getManagementModel().getContent(ProposalCycle.class).get(0); + } + + /** + * {@inheritDoc} + * overrides @see org.ivoa.vodml.testing.AutoDBRoundTripTest#testEntity(org.ivoa.vodml.jpa.JPAManipulationsForObjectType) + */ + @Override + public void testEntity(ProposalCycle e) { + //TODO some testing + } + + /** + * {@inheritDoc} + * overrides @see org.ivoa.vodml.testing.AutoRoundTripTest#createModel() + */ + @Override + public ProposalManagementModel createModel() { + return example.getManagementModel(); + } + + /** + * {@inheritDoc} + * overrides @see org.ivoa.vodml.testing.AutoRoundTripTest#testModel(org.ivoa.vodml.VodmlModel) + */ + @Override + public void testModel(ProposalManagementModel m) { + //TODO some testing + } + + + /** + * test the full DB save - note the transaction needs to be around the + */ + @Test + public void testDBSave() { + jakarta.persistence.EntityManager em = setupH2Db(example.getManagementModel().management().pu_name()); + + em.getTransaction().begin(); + example.saveTodB(em); + em.getTransaction().commit(); + } + +} + + diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml index 190a693..7dd6133 100644 --- a/src/test/resources/logback.xml +++ b/src/test/resources/logback.xml @@ -6,7 +6,7 @@ %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n - +