Unit Testing Framework

IDE Setup

Maven Dependencies (pom.xml)

The following dependencies must be added to your pom.xml file in addition to those listed in the Samples section:

    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>2.28.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
      <scope>test</scope>
    </dependency>

A Simple Script Example

Consider our metadata update script. This simple script will retrieve our asset’s metadata fields, update one of the field values, then set the metadata back on the asset.

package com.ooyala.flex.example

import tv.nativ.mio.api.plugin.command.PluginCommand

class UpdateAssetMetadata extends PluginCommand {
    def execute() {
        def asset = context.asset
        def assetService = services.assetService
        def metadata = assetService.getAssetMetadata(asset.id)
        def directorField = metadata.getField('director')
        directorField.setValue('Michael Bay')
        assetService.setAssetMetadata(asset.id, directorField)
    }
}

A Simple Test Example

The unit test below will verify that our update metadata script is updating our metadata field correctly. Most unit tests for scripts which manipulate assets consist of similar steps:

  1. Create an asset object
  2. Add some information to the asset (in this case, create a metadata instance)
  3. Associate the information with the asset using a service mock
  4. Run the method under test
  5. Assert that the script took effect
package com.ooyala.flex.example

import tv.nativ.mio.api.context.MockContext
import tv.nativ.mio.api.model.asset.Asset
import tv.nativ.mio.api.model.internal.metadata.MockMetadata
import tv.nativ.mio.api.model.internal.metadata.MockSingleField
import tv.nativ.mio.api.services.asset.AssetService
import tv.nativ.mio.api.services.factory.MioServiceFactory

import static org.mockito.Matchers.eq
import static org.mockito.Mockito.doReturn
import static org.mockito.Mockito.mock


class UpdateAssetMetadataTest extends GroovyTestCase {

    MockContext context
    MioServiceFactory services

    @Override
    void setUp() {
        // Instantiate our context and service
        context = new MockContext()
        services = mock(MioServiceFactory.class)
    }

    void testExecute_DirectorExists() {
        // Set an asset object on the context
        def asset = new Asset(id: 123)
        context.setAsset(asset)
        // Create an initial metadata instance
        def metadata = new MockMetadata([
                new MockSingleField('director', '')
        ])
        // Tell the assetService mock to return our metadata instance
        def assetService = mock(AssetService.class)
        doReturn(metadata).when(assetService).getAssetMetadata(eq(asset.id))
        // Tell the services mock to return our assetService mock
        doReturn(assetService).when(services).getAssetService()
        // Run the method being tested
        def command = new UpdateAssetMetadata(context: context, services: services)
        command.execute()
        // Verify that our script changed the metadata field
        assertEquals('Michael Bay', metadata.getField('director').value)
    }
}