PeppolSBDHDocumentWriter { @Nonnull public StandardBusinessDocument createStandardBusinessDocument (@Nonnull final PeppolSBDHDocument aData) { ValueEnforcer.notNull (aData, "Data"); if (!aData.areAllFieldsSet ()) throw new IllegalArgumentException ("Not all data fields are set!"); final StandardBusinessDocument aSBD = new StandardBusinessDocument (); { final StandardBusinessDocumentHeader aSBDH = new StandardBusinessDocumentHeader (); aSBDH.setHeaderVersion (m_sHeaderVersion); { final Partner aSender = new Partner (); final PartnerIdentification aSenderID = new PartnerIdentification (); aSenderID.setAuthority (aData.getSenderScheme ()); aSenderID.setValue (aData.getSenderValue ()); aSender.setIdentifier (aSenderID); aSBDH.addSender (aSender); } { final Partner aReceiver = new Partner (); final PartnerIdentification aReceiverID = new PartnerIdentification (); aReceiverID.setAuthority (aData.getReceiverScheme ()); aReceiverID.setValue (aData.getReceiverValue ()); aReceiver.setIdentifier (aReceiverID); aSBDH.addReceiver (aReceiver); } { final DocumentIdentification aDI = new DocumentIdentification (); aDI.setStandard (aData.getStandard ()); aDI.setTypeVersion (aData.getTypeVersion ()); aDI.setType (aData.getType ()); aDI.setInstanceIdentifier (aData.getInstanceIdentifier ()); aDI.setCreationDateAndTime (aData.getCreationDateAndTime ()); aSBDH.setDocumentIdentification (aDI); } { final BusinessScope aBusinessScope = new BusinessScope (); { final Scope aScope = new Scope (); aScope.setType (CPeppolSBDH.SCOPE_DOCUMENT_TYPE_ID); aScope.setInstanceIdentifier (aData.getDocumentTypeValue ()); aScope.setIdentifier (aData.getDocumentTypeScheme ()); aBusinessScope.addScope (aScope); } { final Scope aScope = new Scope (); aScope.setType (CPeppolSBDH.SCOPE_PROCESS_ID); aScope.setInstanceIdentifier (aData.getProcessValue ()); aScope.setIdentifier (aData.getProcessScheme ()); aBusinessScope.addScope (aScope); } for (final Map.Entry <String, String> aEntry : aData.additionalAttributes ().entrySet ()) { final Scope aScope = new Scope (); aScope.setType (aEntry.getKey ()); aScope.setInstanceIdentifier (StringHelper.getNotNull (aEntry.getValue ())); aBusinessScope.addScope (aScope); } aSBDH.setBusinessScope (aBusinessScope); } aSBD.setStandardBusinessDocumentHeader (aSBDH); } aSBD.setAny (aData.getBusinessMessage ()); return aSBD; } PeppolSBDHDocumentWriter(); @Nonnull final String getHeaderVersion(); @Nonnull final PeppolSBDHDocumentWriter setHeaderVersion(@Nonnull final String sHeaderVersion); @Nonnull StandardBusinessDocument createStandardBusinessDocument(@Nonnull final PeppolSBDHDocument aData);  }
@Test public void testCreateSBDH () throws PeppolSBDHDocumentReadException { final Document aDoc = DOMReader.readXMLDOM ("<root xmlns='urn:foobar'><child>a</child></root>"); final PeppolSBDHDocument aData = PeppolSBDHDocument.create (aDoc.getDocumentElement (), PeppolIdentifierFactory.INSTANCE) .setSenderWithDefaultScheme ("0088:sender") .setReceiverWithDefaultScheme ("0099:receiver") .setDocumentTypeWithDefaultScheme ("doctypeid") .setProcessWithDefaultScheme ("procid"); assertTrue (aData.areAllFieldsSet ()); assertTrue (aData.areAllAdditionalAttributesValid ()); assertEquals (PeppolIdentifierHelper.DEFAULT_DOCUMENT_TYPE_SCHEME, aData.getDocumentTypeScheme ()); assertEquals ("doctypeid", aData.getDocumentTypeValue ()); assertEquals (PeppolIdentifierHelper.DEFAULT_PROCESS_SCHEME, aData.getProcessScheme ()); assertEquals ("procid", aData.getProcessValue ()); assertEquals (0, aData.additionalAttributes ().size ()); final StandardBusinessDocument aSBD = new PeppolSBDHDocumentWriter ().createStandardBusinessDocument (aData); assertNotNull (aSBD); final String sXML = new SBDMarshaller ().getAsString (aSBD); assertNotNull (sXML); if (false) LOGGER.info (sXML); final PeppolSBDHDocument aDataRead = new PeppolSBDHDocumentReader (SimpleIdentifierFactory.INSTANCE).extractData (aSBD); assertNotNull (aDataRead); assertEquals (PeppolIdentifierHelper.DEFAULT_PARTICIPANT_SCHEME, aDataRead.getSenderScheme ()); assertEquals ("0088:sender", aDataRead.getSenderValue ()); assertEquals (PeppolIdentifierHelper.DEFAULT_PARTICIPANT_SCHEME, aDataRead.getReceiverScheme ()); assertEquals ("0099:receiver", aDataRead.getReceiverValue ()); assertEquals ("urn:foobar", aDataRead.getStandard ()); assertEquals ("2.1", aDataRead.getTypeVersion ()); assertEquals ("root", aDataRead.getType ()); assertEquals (aData.getInstanceIdentifier (), aDataRead.getInstanceIdentifier ()); assertEquals (aData.getCreationDateAndTime (), aDataRead.getCreationDateAndTime ()); assertEquals (aData.getCreationDateAndTime ().toString (), aDataRead.getCreationDateAndTime ().toString ()); assertEquals (PeppolIdentifierHelper.DEFAULT_DOCUMENT_TYPE_SCHEME, aDataRead.getDocumentTypeScheme ()); assertEquals ("doctypeid", aDataRead.getDocumentTypeValue ()); assertEquals (PeppolIdentifierHelper.DEFAULT_PROCESS_SCHEME, aDataRead.getProcessScheme ()); assertEquals ("procid", aDataRead.getProcessValue ()); assertTrue (aDataRead.hasBusinessMessage ()); assertEquals ("root", aDataRead.getBusinessMessage ().getLocalName ()); assertEquals (0, aDataRead.additionalAttributes ().size ()); CommonsTestHelper.testDefaultImplementationWithEqualContentObject (aData, aDataRead); }
@Test public void testCreateSBDHWithAdditionalAttributes () throws PeppolSBDHDocumentReadException { final Document aDoc = DOMReader.readXMLDOM ("<root xmlns='urn:foobar'><child>a</child></root>"); final PeppolSBDHDocument aData = PeppolSBDHDocument.create (aDoc.getDocumentElement (), PeppolIdentifierFactory.INSTANCE) .setSenderWithDefaultScheme ("0088:sender") .setReceiverWithDefaultScheme ("0099:receiver") .setDocumentTypeWithDefaultScheme ("doctypeid") .setProcessWithDefaultScheme ("procid"); assertTrue (aData.areAllFieldsSet ()); assertTrue (aData.areAllAdditionalAttributesValid ()); assertEquals (PeppolIdentifierHelper.DEFAULT_DOCUMENT_TYPE_SCHEME, aData.getDocumentTypeScheme ()); assertEquals ("doctypeid", aData.getDocumentTypeValue ()); assertEquals (PeppolIdentifierHelper.DEFAULT_PROCESS_SCHEME, aData.getProcessScheme ()); assertEquals ("procid", aData.getProcessValue ()); assertEquals (0, aData.additionalAttributes ().size ()); aData.additionalAttributes ().add ("Attr1", "Value1"); aData.additionalAttributes ().add ("Attr2", (String) null); aData.additionalAttributes ().add ("Attr3", ""); assertTrue (aData.areAllAdditionalAttributesValid ()); final StandardBusinessDocument aSBD = new PeppolSBDHDocumentWriter ().createStandardBusinessDocument (aData); assertNotNull (aSBD); final String sXML = new SBDMarshaller ().getAsString (aSBD); assertNotNull (sXML); if (false) LOGGER.info (sXML); final PeppolSBDHDocument aDataRead = new PeppolSBDHDocumentReader (SimpleIdentifierFactory.INSTANCE).extractData (aSBD); assertNotNull (aDataRead); assertEquals (PeppolIdentifierHelper.DEFAULT_PARTICIPANT_SCHEME, aDataRead.getSenderScheme ()); assertEquals ("0088:sender", aDataRead.getSenderValue ()); assertEquals (PeppolIdentifierHelper.DEFAULT_PARTICIPANT_SCHEME, aDataRead.getReceiverScheme ()); assertEquals ("0099:receiver", aDataRead.getReceiverValue ()); assertEquals ("urn:foobar", aDataRead.getStandard ()); assertEquals ("2.1", aDataRead.getTypeVersion ()); assertEquals ("root", aDataRead.getType ()); assertEquals (aData.getInstanceIdentifier (), aDataRead.getInstanceIdentifier ()); assertEquals (aData.getCreationDateAndTime ().toString (), aDataRead.getCreationDateAndTime ().toString ()); assertEquals (PeppolIdentifierHelper.DEFAULT_DOCUMENT_TYPE_SCHEME, aDataRead.getDocumentTypeScheme ()); assertEquals ("doctypeid", aDataRead.getDocumentTypeValue ()); assertEquals (PeppolIdentifierHelper.DEFAULT_PROCESS_SCHEME, aDataRead.getProcessScheme ()); assertEquals ("procid", aDataRead.getProcessValue ()); assertTrue (aDataRead.hasBusinessMessage ()); assertEquals ("root", aDataRead.getBusinessMessage ().getLocalName ()); assertEquals (3, aDataRead.additionalAttributes ().size ()); assertEquals ("Value1", aDataRead.additionalAttributes ().get ("Attr1")); assertTrue (aDataRead.additionalAttributes ().containsKey ("Attr2")); assertNull (aDataRead.additionalAttributes ().get ("Attr2")); assertTrue (aDataRead.additionalAttributes ().containsKey ("Attr3")); assertNull (aDataRead.additionalAttributes ().get ("Attr3")); CommonsTestHelper.testDefaultImplementationWithDifferentContentObject (aData, aDataRead); aDataRead.additionalAttributes ().put ("Attr3", ""); CommonsTestHelper.testDefaultImplementationWithEqualContentObject (aData, aDataRead); }
@Test public void testBadCase () { final IIdentifierFactory aIF = SimpleIdentifierFactory.INSTANCE; final PeppolSBDHDocument aData = new PeppolSBDHDocument (aIF); try { new PeppolSBDHDocumentWriter ().createStandardBusinessDocument (aData); fail (); } catch (final IllegalArgumentException ex) { } }