github.com/Iosif02/microgateway@v1.0.1/generate_test.go (about)

     1  package microgateway
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"os/exec"
     7  	"testing"
     8  
     9  	"github.com/Iosif02/core/engine"
    10  	_ "github.com/Iosif02/core/examples/action"
    11  )
    12  
    13  var testFlogoJSON = `{
    14    "name": "Test",
    15    "type": "flogo:app",
    16    "version": "1.0.0",
    17    "description": "This is a test application.",
    18  	"imports": [
    19  		"nil github.com/Iosif02/core@v0.9.5-0.20191107201139-c4e948d4cc8d",
    20  		"github.com/Iosif02/core/examples/action",
    21  		"github.com/Iosif02/core/examples/trigger",
    22  		"_ github.com/Iosif02/core/data/expression/script"
    23  	],
    24    "properties": [
    25  		{"name": "test0", "type": "string", "value": "test"},
    26  		{"name": "test1", "type": "int", "value": 1},
    27  		{"name": "test2", "type": "bool", "value": true}
    28  	],
    29    "channels": [
    30      "test0:1",
    31  		"test1:2",
    32  		"test2:3"
    33    ],
    34    "triggers": [
    35      {
    36        "name": "flogo-test0",
    37        "id": "test0",
    38        "ref": "#trigger",
    39        "settings": {
    40          "aSetting": 123
    41        },
    42        "handlers": [
    43          {
    44            "settings": {
    45              "aSetting": 123
    46            },
    47            "actions": [
    48              {
    49                "id": "action:Test0"
    50              }
    51            ]
    52          }
    53        ]
    54      },
    55  		{
    56        "name": "flogo-test1",
    57        "id": "test1",
    58        "ref": "#trigger",
    59        "settings": {
    60          "aSetting": 123
    61        },
    62        "handlers": [
    63          {
    64            "settings": {
    65              "aSetting": 123
    66            },
    67            "actions": [
    68              {
    69                "id": "action:Test1"
    70              }
    71            ]
    72          }
    73        ]
    74      },
    75  		{
    76        "name": "flogo-test1",
    77        "id": "test1",
    78        "ref": "github.com/Iosif02/core/examples/trigger",
    79        "settings": {
    80          "aSetting": 123
    81        },
    82        "handlers": [
    83          {
    84            "settings": {
    85              "aSetting": 123
    86            },
    87            "actions": [
    88              {
    89  							"if": "1 == 1",
    90                "id": "action:Test1",
    91  							"input": {
    92  								"test0": "=1",
    93  								"test1": "=2",
    94  								"test2": "=3"
    95  							},
    96  							"output": {
    97  								"test0": "=1",
    98  								"test1": "=2",
    99  								"test2": "=3"
   100  							}
   101              },
   102  						{
   103  							"ref": "github.com/Iosif02/core/examples/action",
   104  				      "settings": {
   105  				        "aSetting": "action:Test"
   106  				      }
   107  						}
   108            ]
   109          }
   110        ]
   111      }
   112    ],
   113    "resources": [
   114      {
   115        "id": "action:Test",
   116        "compressed": false,
   117        "data": {
   118  				"message": "hello world"
   119  			}
   120      }
   121    ],
   122    "actions": [
   123      {
   124        "ref": "github.com/Iosif02/core/examples/action",
   125        "settings": {
   126          "aSetting": "action:Test"
   127        },
   128        "id": "action:Test0",
   129        "metadata": null
   130      },
   131  		{
   132        "ref": "github.com/Iosif02/core/examples/action",
   133        "settings": {
   134          "aSetting": "action:Test"
   135        },
   136        "id": "action:Test1",
   137        "metadata": null
   138      }
   139    ]
   140  }`
   141  
   142  func TestGenerate(t *testing.T) {
   143  	app, err := engine.LoadAppConfig(testFlogoJSON, false)
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  	current, err := os.Getwd()
   148  	if err != nil {
   149  		t.Fatal(err)
   150  	}
   151  	defer func() {
   152  		err = os.Chdir(current)
   153  		if err != nil {
   154  			t.Fatal(err)
   155  		}
   156  	}()
   157  	tmp, err := ioutil.TempDir("", "generate")
   158  	if err != nil {
   159  		t.Fatal(err)
   160  	}
   161  	t.Log(tmp)
   162  	err = os.Chdir(tmp)
   163  	if err != nil {
   164  		t.Fatal(err)
   165  	}
   166  	Generate(app, "./test.go", "./go.mod")
   167  	cmd := exec.Command("go", "build")
   168  	err = cmd.Run()
   169  	if err != nil {
   170  		t.Fatal(err)
   171  	}
   172  	err = os.RemoveAll(tmp)
   173  	if err != nil {
   174  		t.Fatal(err)
   175  	}
   176  }