99
1010package org .ivoa .dm .proposal .prop ;
1111
12+ import static org .ivoa .dm .proposal .prop .BaseExample .makeList ;
1213import static org .junit .jupiter .api .Assertions .*;
1314
1415import java .util .List ;
2425import com .fasterxml .jackson .core .JsonProcessingException ;
2526import com .fasterxml .jackson .databind .ObjectMapper ;
2627
27- import org .ivoa .dm .proposal .management .ProposalCycle ;
28- import org .ivoa .dm .proposal .management .ProposalManagementModel ;
29- import org .ivoa .dm .proposal .management .SubmittedProposal ;
28+ import org .ivoa .dm .proposal .management .*;
3029import org .ivoa .dm .stc .coords .SpaceSys ;
3130import org .ivoa .vodml .ModelDescription ;
3231import org .junit .jupiter .api .*;
@@ -45,15 +44,16 @@ public abstract class AbstractProposalTest extends org.ivoa.vodml.testing.Abstra
4544 .getLogger (AbstractProposalTest .class );
4645
4746 protected ExampleGenerator ex ;
48- protected exampleFactory ef ;
47+ protected ExampleFactory ef ;
4948 protected EntityManager em ;
5049 protected Long proposalId ;
5150 protected ObservingProposal proposal ;
52- protected ModelDescription modelDescription ;
51+ private final ModelDescription modelDescription ;
5352 protected Long cycleId ;
5453 protected ProposalCycle cycle ;
54+ protected ProposalManagementModel model ;
5555
56- @ BeforeAll
56+ @ BeforeAll
5757 public void beforeAll () {
5858 em = setupH2Db (ProposalModel .pu_name (),modelDescription .allClassNames ());
5959 }
@@ -88,25 +88,50 @@ protected void retrieveCycle() {
8888 }
8989
9090
91- public interface exampleFactory {
91+
92+ public interface ExampleFactory {
9293 ExampleGenerator create ();
9394 }
9495
95- public AbstractProposalTest (ModelDescription md ,exampleFactory ef ) {
96- this .modelDescription = md ;
96+ public AbstractProposalTest (ExampleFactory ef ) {
9797 this .ef = ef ;
98+ model = new ProposalManagementModel ();
99+ this .modelDescription = model .management ().description ();
100+
98101 }
99102
100- protected abstract void doUrTest ();
103+
101104
102105
103106 @ Test
104107 @ Order (1 )
105108 void UrTest () { // this test has to be done first to make sure that the basic database save has been done
106- doUrTest ();
109+ ex = ef .create ();
110+
111+ em .getTransaction ().begin ();
112+ proposal = ex .getProposal ();
113+ cycle = ex .getCycle ();
114+ model .addContent (proposal );
115+ model .addContent (cycle );
116+ model .management ().persistRefs (em );
117+ em .persist (proposal );
118+ em .persist (cycle );
119+ em .getTransaction ().commit ();
120+ proposalId = proposal .getId ();
121+ cycleId = cycle .getId ();
122+
123+
107124 }
108125
109126
127+ /**
128+ * This test just tests basic XML serialization of a proposal as standalone
129+ * @throws JAXBException
130+ * @throws TransformerConfigurationException
131+ * @throws ParserConfigurationException
132+ * @throws TransformerFactoryConfigurationError
133+ * @throws TransformerException
134+ */
110135 @ org .junit .jupiter .api .Test
111136 @ Order (1000 )
112137 void proposalDmJaxbTest () throws JAXBException , TransformerConfigurationException ,
@@ -115,7 +140,7 @@ void proposalDmJaxbTest() throws JAXBException, TransformerConfigurationExceptio
115140 logger .debug ("starting test" );
116141
117142 ProposalModel model = new ProposalModel ();
118- model .addContent (proposal );
143+ model .addContent (ex . getProposal () );
119144 model .processReferences ();
120145 validateModel (model );
121146
@@ -125,6 +150,21 @@ void proposalDmJaxbTest() throws JAXBException, TransformerConfigurationExceptio
125150
126151 }
127152
153+ /**
154+ * This tests basic JSON serialization of the proposal as standalone
155+ * @throws JsonProcessingException
156+ */
157+ @ org .junit .jupiter .api .Test
158+ @ Order (1000 )
159+ void proposalDmJSONTest () throws JsonProcessingException {
160+ ProposalModel model = new ProposalModel ();
161+ model .addContent (ex .getProposal ());
162+ model .processReferences ();
163+ ProposalModel modelin = modelRoundTripJSONwithTest (model );
164+ List <ObservingProposal > props = modelin .getContent (ObservingProposal .class );
165+ assertEquals (1 , props .size ());
166+ }
167+
128168 @ org .junit .jupiter .api .Test
129169 @ Order (1010 )
130170 void proposalDmJPATest () {
@@ -139,16 +179,7 @@ void proposalDmJPATest() {
139179
140180
141181
142- @ org .junit .jupiter .api .Test
143- @ Order (1010 )
144- void proposalDmJSONTest () throws JsonProcessingException {
145- ProposalModel model = new ProposalModel ();
146- model .addContent (ex .getProposal ());
147- model .processReferences ();
148- ProposalModel modelin = modelRoundTripJSONwithTest (model );
149- List <ObservingProposal > props = modelin .getContent (ObservingProposal .class );
150- assertEquals (1 , props .size ());
151- }
182+
152183
153184 @ org .junit .jupiter .api .Test
154185 @ Order (1010 )
@@ -169,55 +200,126 @@ public void testObservations() {
169200 assertEquals ( 1 , proposal .observations .size (), "number of observations" );
170201 Observation obs = proposal .observations .get (0 );
171202 assertNotNull (obs .target );
172- assertTrue ( obs .target .get (0 ) instanceof CelestialTarget );
203+ assertInstanceOf ( CelestialTarget . class , obs .target .get (0 ));
173204 CelestialTarget target = (CelestialTarget )obs .target .get (0 );
174- SpaceSys cosys = ( SpaceSys ) target .sourceCoordinates .getCoordSys ();
205+ SpaceSys cosys = target .sourceCoordinates .getCoordSys ();
175206 assertNotNull (cosys );
176207
177208
178209 }
179- @ org .junit .jupiter .api .Test
180- @ Order (1020 )
181- public void testDeleteTarget () {
182210
183- Observation obs = proposal .observations .get (0 );
184- assertNotNull (obs .target );
185- em .getTransaction ().begin ();
186- em .remove (obs .target .get (0 )); // TODO perhaps really want to investigate list member deletion more...
187- em .getTransaction ().commit ();
188-
189-
211+
212+
213+ @ Test
214+ @ Order (1110 )
215+ public void testTACQuery () throws JsonProcessingException {
216+ TypedQuery <TAC > q = em .createQuery ("SELECT o FROM TAC o" , TAC .class );
217+ TAC tac = q .getSingleResult ();
218+ System .out .println (tac .getMembers ().get (0 ).getId ());
219+ String json = ProposalManagementModel .jsonMapper ().writeValueAsString (tac );
220+ System .out .println (json );
221+
190222 }
191- @ org .junit .jupiter .api .Test
192- @ Order (1020 )
193- public void testObservationTarget () {
194-
195- //copy obs
196- em .getTransaction ().begin ();
197- TypedQuery <ObservingProposal > q = em .createQuery ("SELECT o FROM ObservingProposal o" , ObservingProposal .class );
198- List <ObservingProposal > res = q .getResultList ();
199- ObservingProposal prop = res .get (0 );
200- prop .forceLoad ();
201- Observation obs = prop .observations .get (0 );
202- Observation obs2 = obs .copyMe ();
203- em .persist (obs2 );
204- prop .addToObservations (obs2 );
205- em .merge (prop );
206- em .getTransaction ().commit ();
207- //delete target
208- em .getTransaction ().begin ();
209- assertNotNull (obs .target );
210-
211- em .remove (obs .target .get (0 )); // TODO perhaps really want to investigate list member deletion more...
212- em .getTransaction ().commit ();
213-
214-
223+
224+ @ Test
225+ @ Order (1120 )
226+ void doTOCworkTest () throws JAXBException ,
227+ ParserConfigurationException ,
228+ TransformerFactoryConfigurationError , TransformerException , JsonProcessingException {
229+ logger .debug ("starting test" );
230+ model .createContext ();
231+ SubmittedProposal submittedProposal = ex .submitProposal (proposal , cycle );
232+ cycle .setSubmittedProposals (
233+ makeList (submittedProposal ));
234+
235+ AllocatedProposal allocatedProposal = ex .allocateProposal (submittedProposal );
236+ cycle .setAllocatedProposals (makeList (
237+ allocatedProposal
238+ ));
239+ cycle .updateClonedReferences ();
240+ //save out the work
241+ em .getTransaction ().begin ();
242+ em .persist (submittedProposal );
243+ em .persist (allocatedProposal );
244+ em .getTransaction ().commit ();
245+
246+ doSerialization (model );
247+
215248 }
216249
217- public static ObservingProposal cloneProposal (ObservingProposal p )
250+ public void doSerialization (ProposalManagementModel thisModel ) throws JsonProcessingException , JAXBException , ParserConfigurationException , TransformerException {
251+ validateModel (thisModel );
252+ ProposalManagementModel modelin = modelRoundTripXMLwithTest (thisModel );
253+ List <ProposalCycle > revs = modelin .getContent (ProposalCycle .class );
254+ assertEquals (1 , revs .size ());
255+
256+ ProposalManagementModel modelinj = modelRoundTripJSONwithTest (thisModel );
257+ List <ProposalCycle > revsj = modelinj .getContent (ProposalCycle .class );
258+ assertEquals (1 , revsj .size ());
259+
260+ }
261+
262+ @ org .junit .jupiter .api .Test
263+ @ Order (1210 )
264+ public void retrieveTest () throws JAXBException , ParserConfigurationException , JsonProcessingException , TransformerException {
265+ //flush any existing entities
266+ em .clear ();
267+ em .getEntityManagerFactory ().getCache ().evictAll ();
268+ retrieveProposal ();
269+ retrieveCycle ();
270+ ProposalManagementModel thismodel = new ProposalManagementModel ();
271+ thismodel .addContent (proposal );
272+ thismodel .addContent (cycle );
273+ assertEquals (1 , cycle .getSubmittedProposals ().size ());
274+ assertEquals (1 , cycle .getAllocatedProposals ().size ());
275+ doSerialization (thismodel );
276+ }
277+
278+
279+
280+ @ org .junit .jupiter .api .Test
281+ @ Order (1220 )
282+ public void testDeleteTarget () {
283+
284+ Observation obs = proposal .observations .get (0 );
285+ assertNotNull (obs .target );
286+ em .getTransaction ().begin ();
287+ em .remove (obs .target .get (0 )); // TODO perhaps really want to investigate list member deletion more...
288+ em .getTransaction ().commit ();
289+
290+
291+ }
292+
293+ @ org .junit .jupiter .api .Test
294+ @ Order (2110 )
295+ public void testObservationTarget () {
296+
297+ //copy obs
298+ em .getTransaction ().begin ();
299+ TypedQuery <ObservingProposal > q = em .createQuery ("SELECT o FROM ObservingProposal o" , ObservingProposal .class );
300+ List <ObservingProposal > res = q .getResultList ();
301+ ObservingProposal prop = res .get (0 );
302+ prop .forceLoad ();
303+ Observation obs = prop .observations .get (0 );
304+ Observation obs2 = obs .copyMe ();
305+ em .persist (obs2 );
306+ prop .addToObservations (obs2 );
307+ em .merge (prop );
308+ em .getTransaction ().commit ();
309+ //delete target
310+ em .getTransaction ().begin ();
311+ assertNotNull (obs .target );
312+
313+ em .remove (obs .target .get (0 )); // TODO perhaps really want to investigate list member deletion more...
314+ em .getTransaction ().commit ();
315+
316+
317+ }
318+
319+ public static ObservingProposal cloneProposal (ProposalManagementModel themodel , ObservingProposal p )
218320 {
219- ProposalModel pm = new ProposalModel ();
220- pm .createContext ();//FIXME this is an area where the API for https://github.com/ivoa/vo-dml/issues/42 is not great
321+
322+ themodel .createContext ();//FIXME this is an area where the API for https://github.com/ivoa/vo-dml/issues/42 is not great
221323 final ObservingProposal frozenProposal = new ObservingProposal (p ); // make a copy of the proposal to put into the submitted proposal
222324 frozenProposal .updateClonedReferences ();
223325 return frozenProposal ;
0 commit comments