5 Replies Latest reply on Dec 11, 2014 11:51 AM by Johnathan Ingram

    Request to Contribute Entity Manager MixIn

    Johnathan Ingram Newbie

      Hi

       

      I would like to contribute a MixIn for injecting an Entity Manager into test cases.

      I am already using this for test cases currently.

       

      The concept is as follows:

       

      A new MixIn jar switchyard-component-test-mixin-entitymanager project will be created under the components project.

       

      1) An entity manager delegate which is a dynamic proxy for the EntityManager interface is injected via CDI

        1.1) The delegate is able to utilise the transaction manager from the Transaction MixIn

        1.2) The delegate is able to lookup a datasource reference from its EntityManagerMixin

        1.3) The delegate processes requests and ensures there is an instance of the entity manager for the correct boundry

          1.3.1) Transaction boundary defined as being on both thread and an active transaction level within the thread

      2) An EntityManagerMixIn is created as the singleton to

         2.1) Register and create all instances of datasources

         2.2) Use the NamingMixIn to register the datasources for JNDI lookup

         2.3) Provide references required by the EntityManager delegate such as a reference to the transaction manager etc

      3) A CDI extension will be created to

         3.1) Ensure all @PersistenceContext annotations become CDI injectable

         3.2) The jar will contain a beans.xml that will automatically register the CDI extension

       

      If this is an acceptable contribution, please could I go ahead and create a JIRA ticket for this.

      I would also require some advice on the type of test cases to implement.

       

      A quick example of the MixIn (Note, the datasource creating will now become the responsibility of the MixIn given url, credentials etc)

       

      @RunWith(SwitchYardRunner.class)

      @SwitchYardTestCaseConfig(config = SwitchYardTestCaseConfig.SWITCHYARD_XML, include = "jpa", mixins = {CDIMixIn.class, TransactionMixIn.class, NamingMixIn.class, EntityManagerMixIn.class})

      public class MyTest  {

         private EntityManagerMixIn entityManagerMixIn;

       

         @BeforeDeploy

         public void beforeDeploy() throws NamingException {

            // Need persistence.xml with oracle-brm PU

            String url = "jdbc:h2:mem:oracle-brm-service";

            String user = "sa";

            String password = "sa";

            entityManagerMixIn.createEntityManager("oracle-brm", url, user, password);    // <-- will create datasource, register with JNDI using NamingMixIn and create entity manager accessible by delegate

         }

      }