Search in sources :

Example 1 with DynamicEntity

use of org.eclipse.persistence.dynamic.DynamicEntity in project aai-aai-common by onap.

the class StoreNotificationEvent method storeDynamicEvent.

/**
 * Store dynamic event.
 *
 * @param notificationJaxbContext
 *        the notification jaxb context
 * @param notificationVersion
 *        the notification version
 * @param eventHeader
 *        the event header
 * @param obj
 *        the obj
 * @throws AAIException
 *         the AAI exception
 */
public void storeDynamicEvent(DynamicJAXBContext notificationJaxbContext, String notificationVersion, DynamicEntity eventHeader, DynamicEntity obj) throws AAIException {
    if (obj == null) {
        throw new AAIException("AAI_7350");
    }
    DynamicEntity notificationEvent = notificationJaxbContext.getDynamicType("inventory.aai.onap.org." + notificationVersion + ".NotificationEvent").newDynamicEntity();
    if (eventHeader.get("id") == null) {
        eventHeader.set("id", genDate2() + "-" + UUID.randomUUID().toString());
    }
    if (eventHeader.get("timestamp") == null) {
        eventHeader.set("timestamp", genDate());
    }
    if (eventHeader.get("entityLink") == null) {
        eventHeader.set("entityLink", "UNK");
    }
    if (eventHeader.get("action") == null) {
        eventHeader.set("action", "UNK");
    }
    if (eventHeader.get("eventType") == null) {
        eventHeader.set("eventType", AAIConfig.get("aai.notificationEvent.default.eventType", "UNK"));
    }
    if (eventHeader.get("domain") == null) {
        eventHeader.set("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
    }
    if (eventHeader.get("sourceName") == null) {
        eventHeader.set("sourceName", AAIConfig.get("aai.notificationEvent.default.sourceName", "UNK"));
    }
    if (eventHeader.get("sequenceNumber") == null) {
        eventHeader.set("sequenceNumber", AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
    }
    if (eventHeader.get("severity") == null) {
        eventHeader.set("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
    }
    if (eventHeader.get("version") == null) {
        eventHeader.set("version", AAIConfig.get("aai.notificationEvent.default.version", "UNK"));
    }
    if (notificationEvent.get("cambriaPartition") == null) {
        notificationEvent.set("cambriaPartition", AAIConstants.UEB_PUB_PARTITION_AAI);
    }
    notificationEvent.set("eventHeader", eventHeader);
    notificationEvent.set("entity", obj);
    try {
        StringWriter result = new StringWriter();
        Marshaller marshaller = notificationJaxbContext.createMarshaller();
        marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.MEDIA_TYPE, "application/json");
        marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_INCLUDE_ROOT, false);
        marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, false);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
        marshaller.marshal(notificationEvent, result);
        this.sendToDmaapJmsQueue(result.toString());
    } catch (Exception e) {
        throw new AAIException("AAI_7350", e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) AAIException(org.onap.aai.exceptions.AAIException) JSONException(org.json.JSONException) AAIUnknownObjectException(org.onap.aai.introspection.exceptions.AAIUnknownObjectException) AAIException(org.onap.aai.exceptions.AAIException)

Example 2 with DynamicEntity

use of org.eclipse.persistence.dynamic.DynamicEntity in project aai-aai-common by onap.

the class PojoUtilsTest method testGetJsonFromDynamicObject.

@Test
public void testGetJsonFromDynamicObject() throws Exception {
    DynamicEntity dynamicEntity = Mockito.mock(DynamicEntity.class);
    JAXBContext jaxbContext = Mockito.mock(JAXBContext.class);
    JAXBMarshaller marshaller = Mockito.mock(JAXBMarshaller.class);
    Mockito.when(jaxbContext.createMarshaller()).thenReturn(marshaller);
    String output = pojoUtils.getJsonFromDynamicObject(dynamicEntity, jaxbContext, true);
    assertEquals("", output);
}
Also used : DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) JAXBContext(org.eclipse.persistence.jaxb.JAXBContext) JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller) Test(org.junit.Test)

Example 3 with DynamicEntity

use of org.eclipse.persistence.dynamic.DynamicEntity in project aai-aai-common by onap.

the class StoreNotificationEventTest method testStoreDynamicEventAAIException.

@Test(expected = Exception.class)
public void testStoreDynamicEventAAIException() throws Exception {
    DynamicJAXBContext notificationJaxbContext = nodeIngestor.getContextForVersion(schemaVersions.getEdgeLabelVersion());
    DynamicEntity obj = Mockito.mock(DynamicEntity.class);
    DynamicEntity eventHeader = Mockito.mock(DynamicEntity.class);
    sne.storeDynamicEvent(notificationJaxbContext, "v12", eventHeader, obj);
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) Test(org.junit.Test)

Example 4 with DynamicEntity

use of org.eclipse.persistence.dynamic.DynamicEntity in project aai-aai-common by onap.

the class NodeIngestorTest method testGetContextForVersion.

@Test
public void testGetContextForVersion() {
    DynamicJAXBContext ctx10 = nodeIngestor.getContextForVersion(new SchemaVersion("v10"));
    // should work bc Foo is valid in test_network_v10 schema
    DynamicEntity foo10 = ctx10.newDynamicEntity("Foo");
    foo10.set("fooId", "bar");
    assertEquals("bar", foo10.get("fooId"));
    // should work bc Bar is valid in test_business_v10 schema
    DynamicEntity bar10 = ctx10.newDynamicEntity("Bar");
    bar10.set("barId", "bar2");
    assertEquals("bar2", bar10.get("barId"));
    XSDOutputResolver outputResolver10 = new XSDOutputResolver();
    ctx10.generateSchema(outputResolver10);
    DynamicJAXBContext ctx11 = nodeIngestor.getContextForVersion(new SchemaVersion("v11"));
    // should work bc Foo.quantity is valid in test_network_v11 schema
    DynamicEntity foo11 = ctx11.newDynamicEntity("Foo");
    foo11.set("quantity", "12");
    assertEquals("12", foo11.get("quantity"));
    DynamicEntity quux11 = ctx11.newDynamicEntity("Quux");
    quux11.set("qManagerName", "some guy");
    assertEquals("some guy", quux11.get("qManagerName"));
    XSDOutputResolver outputResolver11 = new XSDOutputResolver();
    ctx11.generateSchema(outputResolver11);
    thrown.expect(IllegalArgumentException.class);
    // should fail bc Quux not in v10 test schema
    ctx10.newDynamicEntity("Quux");
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) SchemaVersion(org.onap.aai.setup.SchemaVersion) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with DynamicEntity

use of org.eclipse.persistence.dynamic.DynamicEntity in project aai-aai-common by onap.

the class MoxyLoader method unmarshal.

/**
 * {@inheritDoc}
 */
@Override
public Introspector unmarshal(String type, String json, MediaType mediaType) throws AAIUnmarshallingException {
    try {
        final Object clazz = objectFromName(type);
        final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        if (mediaType.equals(MediaType.APPLICATION_JSON_TYPE)) {
            unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
            unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
            unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
        }
        final DynamicEntity entity = (DynamicEntity) unmarshaller.unmarshal(new StreamSource(new StringReader(json)), clazz.getClass()).getValue();
        return IntrospectorFactory.newInstance(ModelType.MOXY, entity);
    } catch (JAXBException e) {
        AAIException ex = new AAIException("AAI_4007", e);
        ErrorLogHelper.logException(ex);
        throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage(), ex);
    } catch (AAIUnknownObjectException e) {
        throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage(), e);
    }
}
Also used : AAIUnknownObjectException(org.onap.aai.introspection.exceptions.AAIUnknownObjectException) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) AAIUnmarshallingException(org.onap.aai.introspection.exceptions.AAIUnmarshallingException) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(javax.xml.bind.JAXBException) AAIException(org.onap.aai.exceptions.AAIException) Unmarshaller(javax.xml.bind.Unmarshaller)

Aggregations

DynamicEntity (org.eclipse.persistence.dynamic.DynamicEntity)318 Test (org.junit.Test)116 Document (org.w3c.dom.Document)74 InputStream (java.io.InputStream)62 HashMap (java.util.HashMap)61 DynamicType (org.eclipse.persistence.dynamic.DynamicType)54 EntityManager (jakarta.persistence.EntityManager)39 Node (org.w3c.dom.Node)39 ArrayList (java.util.ArrayList)36 StreamSource (javax.xml.transform.stream.StreamSource)35 LinkAdapterString (org.eclipse.persistence.testing.jaxb.dynamic.util.LinkAdapterString)35 DynamicJAXBContext (org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext)32 JAXBElement (jakarta.xml.bind.JAXBElement)26 Date (java.util.Date)25 QName (javax.xml.namespace.QName)23 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)23 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)23 StringReader (java.io.StringReader)21 ReportQuery (org.eclipse.persistence.queries.ReportQuery)21 Unmarshaller (javax.xml.bind.Unmarshaller)15