github.com/Axway/agent-sdk@v1.1.101/pkg/cmd/agentsync/agentsync_test.go (about)

     1  package agentsync
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/Axway/agent-sdk/pkg/cmd/properties"
     8  	"github.com/spf13/cobra"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  type customAgentSync struct {
    13  	AgentSync
    14  	syncCalled bool
    15  	err        string
    16  }
    17  
    18  func (c *customAgentSync) ProcessSynchronization() error {
    19  	c.syncCalled = true
    20  	if c.err != "" {
    21  		return fmt.Errorf(c.err)
    22  	}
    23  	return nil
    24  }
    25  
    26  func setupCustomAgentSync(err string) *customAgentSync {
    27  	// Create the custom agent sync object
    28  	cas := &customAgentSync{
    29  		syncCalled: false,
    30  		err:        err,
    31  	}
    32  
    33  	// Set the agent sync implementation to be our custom implementation
    34  	SetAgentSync(cas)
    35  
    36  	return cas
    37  }
    38  
    39  func TestDefaultAgentSync(t *testing.T) {
    40  	err := agentSync.ProcessSynchronization()
    41  
    42  	assert.Nil(t, err, " There was an unexpected error thrown by the default ProcessSynchroniization method")
    43  }
    44  
    45  func TestCustomAgentSync(t *testing.T) {
    46  	// Create the custom agent sync object
    47  	cas := setupCustomAgentSync("")
    48  
    49  	// Call the ProcessSynchronization
    50  	err := agentSync.ProcessSynchronization()
    51  
    52  	// Validate no err and that our method was called
    53  	assert.Nil(t, err, "There was an unexpected error thrown by the default ProcessSynchroniization method")
    54  	assert.True(t, cas.syncCalled, "The syncCalled attribute was not set properly, was our custom sync method used?")
    55  }
    56  
    57  func TestAddCmdProp(t *testing.T) {
    58  	// Create command properties
    59  	rootCmd := &cobra.Command{}
    60  	props := properties.NewProperties(rootCmd)
    61  
    62  	// Add the sync prop
    63  	AddSyncConfigProperties(props)
    64  
    65  	// Validate the property was added
    66  	val := props.BoolPropertyValue(syncFlag)
    67  
    68  	assert.False(t, val, "Validate that the default property value is false")
    69  }
    70  
    71  func SetSyncProperty(t *testing.T, sync bool) properties.Properties {
    72  	// reset syncmode
    73  	syncMode = false
    74  
    75  	// Create command properties
    76  	rootCmd := &cobra.Command{}
    77  	props := properties.NewProperties(rootCmd)
    78  
    79  	// Set the sync property to true
    80  	props.AddBoolProperty(syncFlag, sync, "")
    81  	SetSyncMode(props)
    82  
    83  	// Validate the property was added
    84  	val := props.BoolFlagValue(syncFlag)
    85  	assert.Equal(t, sync, val, "Validate that the sync property was set appropriately")
    86  
    87  	return props
    88  }
    89  
    90  func TestCheckSyncFlag(t *testing.T) {
    91  	// Create the custom agent sync
    92  	cas := setupCustomAgentSync("")
    93  
    94  	// Set the property to true for the next test
    95  	SetSyncProperty(t, true)
    96  
    97  	// Call the CheckSyncFlag
    98  	assert.False(t, cas.syncCalled, "The syncCalled attribute should be false prior to calling CheckSyncFlag")
    99  	exitcode := CheckSyncFlag()
   100  	assert.True(t, cas.syncCalled, "The syncCalled attribute was not set properly, was our custom sync method used?")
   101  	assert.Equal(t, 0, exitcode, "Expected the sync process to have a 0 exitcode")
   102  
   103  	// recreate the custom agent sync and props
   104  	cas = setupCustomAgentSync("")
   105  
   106  	// Set the property to false for the next test
   107  	SetSyncProperty(t, false)
   108  
   109  	// Call the CheckSyncFlag
   110  	assert.False(t, cas.syncCalled, "The syncCalled attribute should be false prior to calling CheckSyncFlag")
   111  	exitcode = CheckSyncFlag()
   112  	assert.False(t, cas.syncCalled, "The syncCalled attribute false as expected")
   113  	assert.Equal(t, -1, exitcode, "Expected the sync process to have a -1 exitcode")
   114  
   115  	// recreate the custom agent sync and props
   116  	cas = setupCustomAgentSync("failed")
   117  
   118  	// Set the property to true for the next test
   119  	SetSyncProperty(t, true)
   120  
   121  	// Call the CheckSyncFlag
   122  	assert.False(t, cas.syncCalled, "The syncCalled attribute should be false prior to calling CheckSyncFlag")
   123  	exitcode = CheckSyncFlag()
   124  	assert.True(t, cas.syncCalled, "The syncCalled attribute was not set properly, was our custom sync method used?")
   125  	assert.Equal(t, 1, exitcode, "Expected the sync process to have a 1 exitcode")
   126  
   127  }