Search code examples
continuous-integrationrefactoringintegration-testingcode-reusecitrus-framework

How to reuse a scenario in citrus framework?


I set this testing scenario with citrus framework. Now I'm trying to reuse it in other scenarios. I'm creating a behavior for each step. My behaviors are mainly http requests

public class NoProductDocumentValidationScenarioIT {

    private @CitrusResource TestContext parentContext;

    @CitrusEndpoint(name = "todoBasicAuthClient")
    private HttpClient cmsAuthClient;

    @CitrusEndpoint(name = "vdmBasicAuthClient")
    private HttpClient vdmAuthClient;

    @CitrusEndpoint(name = "gvHttpClient")
    private HttpClient gvHttpClient;

    @Test
    @CitrusTest
    public String NoProductDocumentValidation(@CitrusResource TestRunner runner, @CitrusResource TestContext context)
            throws BadNewsMLG2Exception {
        String pdtIdentifier = "EDIT-FR-SVID2-YM9N001479";
        String videoDocument = VideoDocument.setUpVideoDocument("fr", "v1_afptv_sport_broadcast_photos");
        int jobPublicationID = 5;
        // CMS Authentification
        TestBehavior authenticateCMS = new ProductAuthenticationBehavior(cmsAuthClient);
        ApplyTestBehaviorAction authenticateActionCMS = new ApplyTestBehaviorAction(runner, authenticateCMS);
        authenticateActionCMS.doExecute(context);

        // Document Creation
        CreateVideoDocumentBehavior createDoc = new CreateVideoDocumentBehavior(cmsAuthClient, pdtIdentifier,
                videoDocument);
        ApplyTestBehaviorAction createDocAction = new ApplyTestBehaviorAction(runner, createDoc);
        createDocAction.doExecute(context);

        // get document data
        videoDocument = createDoc.getVideoDocument();
        G2VideoDocument g2VideoDocument = ((G2VideoDocument) G2ObjectFactory.parse(videoDocument));
        g2VideoDocument.getProducts();
        String linkToVideoDocument = g2VideoDocument.getLinkToSelf().toString();
        String linkToProject = g2VideoDocument.getLinkToVideoProject().toString();
        String projectID = IrisStringTools.extractIdFromUri(linkToProject);
        String documentID = IrisStringTools.extractIdFromUri(linkToVideoDocument);
        String etag = g2VideoDocument.getEditorialTag();

        // Lock document Metadata
        EditVideoDocumentMetaBehavior lockDocMeta = new EditVideoDocumentMetaBehavior(cmsAuthClient, pdtIdentifier,
                videoDocument, documentID);
        ApplyTestBehaviorAction lockDocMetaAction = new ApplyTestBehaviorAction(runner, lockDocMeta);
        lockDocMetaAction.doExecute(context);
}
}

I run this in eclipse as JUnit test.

I thought about using a super class but it didn't work.

public class ProductDocumentValidationScenarioIT extends NoProductDocumentValidationScenarioIT {

    public String ProductDocumentValidation(@CitrusResource TestRunner runner, @CitrusResource TestContext context)
            throws BadNewsMLG2Exception {
                return something;

    }
}

Solution

  • what we finally did is to create a behavior runner (a java class) where all the behaviors are instanciated and then in the scenario we call the behavior runner with the behavior constant corresponding to the behavior I need:

    public class BehaviorRunner {
    private void doExecute(TestRunner runner, TestContext context, TestBehavior testBehavior) {
        ApplyTestBehaviorAction behaviorAction = new ApplyTestBehaviorAction(runner,testBehavior);
        behaviorAction.doExecute(context);
    }
    
    public void execute( String behaviorLabel, @CitrusResource TestRunner runner, @CitrusResource TestContext context) {
        try {
            switch (behaviorLabel) {
                case BehaviorConstants.CREATE_VIDEO_DOCUMENT :
                    CreateVideoDocumentBehavior createVideoDocumentBehavior = new CreateVideoDocumentBehavior(cmsAuthClient, pdtIdentifier, VideoDocument.setUpVideoDocument2(LanguageConstants.EN, "v1_afptv_sport_broadcast_photos"));
                    doExecute(runner, context, createVideoDocumentBehavior);
                    break;
                case BehaviorConstants.MOVIEDRAFT :
                    MovieDraftDocumentBehavior movieDraftDocumentBehavior = new MovieDraftDocumentBehavior(cmsAuthClient, pdtIdentifier, 1, g2VideoDoc);
                    doExecute(runner, context, movieDraftDocumentBehavior);     
                    break;
                case BehaviorConstants.PUBLICATION_PROGRESSION_STATUS:
                    GetPublicationProgressionStatusBehavior publicationProgressionStatusBehavior = new GetPublicationProgressionStatusBehavior(vdmAuthClient, pdtIdentifier , g2VideoDoc);
                    doExecute(runner, context, publicationProgressionStatusBehavior);   
                    break;
                case BehaviorConstants.VALIDATE :
                    ValidateDocumentBehavior validateDocumentBehavior = new ValidateDocumentBehavior(cmsAuthClient, pdtIdentifier, g2VideoDoc);
                    doExecute(runner, context, validateDocumentBehavior);   
                    break;
                default:
                        break;
            }   
    

    we ended up with a scenario like this:

    @Test
    @CitrusTest
    public void NoProductDocumentValidation(@CitrusResource TestRunner runner, @CitrusResource TestContext context) throws BadNewsMLG2Exception {
        slf4jLogger.info("Montage analysis scenario START");
        // execute scenario
        // Document Creation 
        behaviorRunner.execute(BehaviorConstants.CREATE_VIDEO_DOCUMENT, runner, context);
        // Lock document Metadata
        behaviorRunner.execute(BehaviorConstants.EDITOR, runner, context);
        // Lock Document Binary 
        behaviorRunner.execute(BehaviorConstants.BINARY_EDITOR, runner, context);
    

    That saved us a lot of code lines since we're using different combinations of behaviors in different scenarios.

    I hope this would help someone!