66import com .dotcms .contenttype .transform .contenttype .StructureTransformer ;
77import com .dotcms .datagen .*;
88import com .dotcms .languagevariable .business .LanguageVariableAPI ;
9+ import com .dotcms .publisher .bundle .bean .Bundle ;
10+ import com .dotcms .publisher .bundle .business .BundleFactoryImpl ;
11+ import com .dotcms .publisher .business .DotPublisherException ;
12+ import com .dotcms .publisher .business .PublishAuditAPI ;
13+ import com .dotcms .publisher .business .PublishAuditHistory ;
14+ import com .dotcms .publisher .business .PublishAuditStatus ;
15+ import com .dotcms .publisher .endpoint .bean .impl .PushPublishingEndPoint ;
16+ import com .dotcms .publisher .environment .bean .Environment ;
917import com .dotcms .publisher .pusher .PushPublisher ;
1018import com .dotcms .publisher .pusher .PushPublisherConfig ;
11- import com .dotcms .publisher .receiver .BundlePublisher ;
12- import com .dotcms .publishing .output .BundleOutput ;
13- import com .dotcms .publishing .output .DirectoryBundleOutput ;
14- import com .dotcms .publishing .output .TarGzipBundleOutput ;
1519import com .dotcms .test .util .FileTestUtil ;
1620import com .dotcms .util .IntegrationTestInitService ;
1721import com .dotmarketing .beans .Host ;
4044import com .liferay .portal .model .User ;
4145import com .liferay .util .FileUtil ;
4246import com .liferay .util .StringPool ;
47+ import com .sun .net .httpserver .HttpExchange ;
48+ import com .sun .net .httpserver .HttpHandler ;
49+ import com .sun .net .httpserver .HttpServer ;
4350import com .tngtech .java .junit .dataprovider .DataProvider ;
4451import com .tngtech .java .junit .dataprovider .DataProviderRunner ;
4552import com .tngtech .java .junit .dataprovider .UseDataProvider ;
4653import java .io .BufferedInputStream ;
4754import java .io .FileInputStream ;
55+ import java .io .InputStream ;
4856import java .io .OutputStream ;
57+ import java .net .HttpURLConnection ;
58+ import java .net .InetSocketAddress ;
4959import java .nio .file .Files ;
5060import java .nio .file .Paths ;
5161import org .apache .commons .compress .archivers .tar .TarArchiveEntry ;
5262import org .apache .commons .compress .archivers .tar .TarArchiveInputStream ;
5363import org .apache .commons .compress .compressors .gzip .GzipCompressorInputStream ;
54- import org .elasticsearch .index .fielddata .FieldData ;
64+ import org .apache .commons .io .FileUtils ;
65+
5566import org .jetbrains .annotations .Nullable ;
5667import org .junit .AfterClass ;
5768import org .junit .BeforeClass ;
6475import java .util .stream .Collectors ;
6576
6677import static com .dotcms .util .CollectionsUtils .*;
67- import static org .jgroups .util .Util .assertEquals ;
78+ import static org .junit .Assert .assertEquals ;
79+ import static org .junit .Assert .assertTrue ;
6880
6981@ RunWith (DataProviderRunner .class )
7082public class PublisherAPIImplTest {
7183
7284 private static Contentlet languageVariableCreated ;
7385
74- public static class PushPublisherMock extends PushPublisher {
75- @ Override
76- public PublisherConfig process ( final PublishStatus status ) throws DotPublishingException {
77- return this .config ;
78- }
79- }
80-
8186 public static void prepare () throws Exception {
8287
8388 //Setting web app environment
@@ -125,7 +130,7 @@ public static void createLanguageVariableIfNeeded() throws DotSecurityException,
125130 public static Object [] publishers () throws Exception {
126131 prepare ();
127132
128- final List < TestAsset > assets = list (
133+ return new TestAsset []{
129134 getContentTypeWithHost (),
130135 getTemplateWithDependencies (),
131136 getContainerWithDependencies (),
@@ -136,21 +141,7 @@ public static Object[] publishers() throws Exception {
136141 getLanguageWithDependencies (),
137142 getRuleWithDependencies (),
138143 getContentWithSeveralVersions ()
139- );
140- final List <Class <? extends Publisher >> publishers = list (
141- GenerateBundlePublisher .class ,
142- PushPublisherMock .class
143- );
144-
145- final List <TestCase > cases = new ArrayList <>();
146-
147- for (final Class <? extends Publisher > publisher : publishers ) {
148- for (TestAsset asset : assets ) {
149- cases .add (new TestCase (publisher , asset ));
150- }
151- }
152-
153- return cases .toArray ();
144+ };
154145 }
155146
156147 private static TestAsset getContentWithSeveralVersions () throws DotDataException , DotSecurityException {
@@ -385,20 +376,19 @@ private static TestAsset getContainerWithDependencies() throws DotDataException,
385376 }
386377
387378 /**
388- * Method to Test: {@link PublisherAPIImpl#publish(PublisherConfig, BundleOutput )}
389- * When: Add different assets into a bundle
379+ * Method to Test: {@link PublisherAPIImpl#publish(PublisherConfig)}
380+ * When: Add different assets into a bundle, and generate it
390381 * Should: Create all the files
391382 */
392383 @ Test
393384 @ UseDataProvider ("publishers" )
394- public void publish (final TestCase testCase ) throws DotPublishingException , DotSecurityException , IOException , DotDataException {
395- final Class <? extends Publisher > publisher = testCase .publisher ;
396- final TestAsset testAsset = testCase .asset ;
385+ public void generateBundle (final TestAsset testAsset ) throws DotPublishingException , DotSecurityException , IOException , DotDataException {
386+ final Class <? extends Publisher > publisher = GenerateBundlePublisher .class ;
397387 final Collection <Object > dependencies = new HashSet <>();
398388 dependencies .addAll (testAsset .expectedInBundle );
399389
400390 createLanguageVariableIfNeeded ();
401- addLanguageVariableDependencies (dependencies , testCase . asset .addLanguageVariableDependencies );
391+ addLanguageVariableDependencies (dependencies , testAsset .addLanguageVariableDependencies );
402392
403393 final FilterDescriptor filterDescriptor = new FilterDescriptorDataGen ().nextPersisted ();
404394
@@ -420,13 +410,92 @@ public void publish(final TestCase testCase) throws DotPublishingException, DotS
420410 final PublishStatus publish = publisherAPI .publish (config );
421411 File bundleRoot = publish .getOutputFiles ().get (0 );
422412
423- if (GenerateBundlePublisher .class .equals (publisher )) {
413+ final File extractHere = new File (bundleRoot .getParent () + File .separator + config .getName ());
414+ extractTarArchive (bundleRoot , extractHere );
415+ assertBundle (testAsset , dependencies , extractHere );
416+ }
417+
418+ /**
419+ * Method to Test: {@link PublisherAPIImpl#publish(PublisherConfig)}
420+ * When: Add different assets into a bundle, and send it
421+ * Should: Create all the files
422+ */
423+ @ Test
424+ @ UseDataProvider ("publishers" )
425+ public void sendPushPublishBundle (final TestAsset testAsset )
426+ throws DotPublishingException , DotSecurityException , IOException , DotDataException , DotPublisherException {
427+ final Class <? extends Publisher > publisher = PushPublisher .class ;
428+
429+ final Environment environment = new EnvironmentDataGen ().nextPersisted ();
430+
431+ final PushPublishingEndPoint publishingEndPoint = new PushPublishingEndPointDataGen ()
432+ .environment (environment )
433+ .nextPersisted ();
434+
435+ final FilterDescriptor filterDescriptor = new FilterDescriptorDataGen ().nextPersisted ();
436+
437+ final PushPublisherConfig config = new PushPublisherConfig ();
438+ config .setPublishers (list (publisher ));
439+ config .setOperation (PublisherConfig .Operation .PUBLISH );
440+ config .setLuceneQueries (list ());
441+ config .setId ("sendPushPublishBundle_" + System .currentTimeMillis ());
442+
443+ final Bundle bundle = new BundleDataGen ()
444+ .pushPublisherConfig (config )
445+ .addAssets (list (testAsset .asset ))
446+ .filter (filterDescriptor )
447+ .nextPersisted ();
448+
449+ final BundleFactoryImpl bundleFactory = new BundleFactoryImpl ();
450+ bundleFactory .saveBundleEnvironment (bundle , environment );
451+
452+ final Collection <Object > dependencies = new HashSet <>();
453+ dependencies .addAll (testAsset .expectedInBundle );
454+
455+ createLanguageVariableIfNeeded ();
456+ addLanguageVariableDependencies (dependencies , testAsset .addLanguageVariableDependencies );
457+
458+ final PublisherAPIImpl publisherAPI = new PublisherAPIImpl ();
459+
460+ final PublishAuditStatus publishAuditStatus = new PublishAuditStatus (bundle .getId ());
461+
462+ final PublishAuditHistory publishAuditHistory = new PublishAuditHistory ();
463+ publishAuditStatus .setStatusPojo (publishAuditHistory );
464+
465+ PublishAuditAPI .getInstance ().insertPublishAuditStatus (publishAuditStatus );
466+
467+ final File tempFile = com .dotmarketing .util .FileUtil
468+ .createTemporaryFile ("sendPushPublishBundle_" );
469+
470+ HttpServer httpServer = createHttpServer (tempFile );
471+
472+ try {
473+ httpServer .start ();
474+ final PublishStatus publish = publisherAPI .publish (config );
475+ File bundleRoot = publish .getOutputFiles ().get (0 );
476+
477+ assertTrue (tempFile .exists ());
478+ assertTrue (tempFile .length () > 0 );
479+
424480 final File extractHere = new File (bundleRoot .getParent () + File .separator + config .getName ());
425- extractTarArchive (bundleRoot , extractHere );
426- bundleRoot = extractHere ;
481+ extractTarArchive (tempFile , extractHere );
482+ assertBundle (testAsset , dependencies , extractHere );
483+ } finally {
484+ httpServer .stop (0 );
427485 }
486+ }
428487
429- assertBundle (testAsset , dependencies , bundleRoot );
488+ private HttpServer createHttpServer (File tempFile ) throws IOException {
489+ final HttpServer httpServer = HttpServer .create (new InetSocketAddress ("127.0.0.1" , 8080 ), 0 );
490+
491+ httpServer .createContext ("/api/bundlePublisher/publish" , exchange -> {
492+ final InputStream responseBody = exchange .getRequestBody ();
493+ FileUtils .copyInputStreamToFile (responseBody , tempFile );
494+ exchange .sendResponseHeaders ( HttpURLConnection .HTTP_OK , 0 );
495+ exchange .close ();
496+ });
497+
498+ return httpServer ;
430499 }
431500
432501 private void assertBundle (TestAsset testAsset , Collection <Object > dependencies , File bundleRoot )
@@ -487,18 +556,6 @@ private List<String> getDifferences(long numberFilesExpected, int numberFiles, L
487556 return differences ;
488557 }
489558
490- private static class TestCase {
491- Class <? extends Publisher > publisher ;
492- TestAsset asset ;
493-
494- public TestCase (
495- final Class <? extends Publisher > publisher ,
496- TestAsset asset ) {
497-
498- this .publisher = publisher ;
499- this .asset = asset ;
500- }
501- }
502559
503560 public static List <Contentlet > getLanguageVariables () throws DotDataException , DotSecurityException {
504561 final User systemUser = APILocator .systemUser ();
0 commit comments