Skip to content

Commit

Permalink
refactor the examples a bit more
Browse files Browse the repository at this point in the history
Add a FullExample which will be a container for all the examples
  • Loading branch information
pahjbo committed Oct 23, 2024
1 parent 13fb873 commit 6e6ed04
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down
24 changes: 23 additions & 1 deletion src/test/java/org/ivoa/dm/proposal/prop/EmerlinExampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ObservingMode> retval = mapper.readValue(json,new TypeReference<List<ObservingMode>>(){} );
assertNotNull(retval);

retval = mapper.readValue(this.getClass().getResourceAsStream("/observingmodes.json"),new TypeReference<List<ObservingMode>>(){} );
}



Expand Down
82 changes: 82 additions & 0 deletions src/test/java/org/ivoa/dm/proposal/prop/FullExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Created on 22 Oct 2024
* Copyright 2024 Paul Harrison ([email protected])
*
* 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 ([email protected])
* @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;
}

}


105 changes: 105 additions & 0 deletions src/test/java/org/ivoa/dm/proposal/prop/FullExampleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Created on 22 Oct 2024
* Copyright 2024 Paul Harrison ([email protected])
*
* 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 ([email protected])
*/
class FullExampleTest extends AutoDBRoundTripTest<ProposalManagementModel, Long,ProposalCycle> {

/**
* @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();
}

}


2 changes: 1 addition & 1 deletion src/test/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
</appender>
</appender>
<logger name="org.hibernate.SQL" level="INFO" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
Expand Down

0 comments on commit 6e6ed04

Please sign in to comment.