github.com/Comcast/plax@v0.8.32/dsl/spec_test.go (about)

     1  /*
     2   * Copyright 2021 Comcast Cable Communications Management, LLC
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * SPDX-License-Identifier: Apache-2.0
    17   */
    18  
    19  package dsl
    20  
    21  import (
    22  	"encoding/json"
    23  	"fmt"
    24  	"testing"
    25  	"time"
    26  )
    27  
    28  var dejson = MustParseJSON
    29  
    30  func addMock(t *testing.T, ctx *Ctx, p *Phase) {
    31  	p.AddStep(ctx, &Step{
    32  		Pub: &Pub{
    33  			// Chan defaults to mother.
    34  			Payload: dejson(`{"make":{"name":"mock1","type":"mock"}}`),
    35  		},
    36  	})
    37  
    38  	p.AddStep(ctx, &Step{
    39  		Recv: &Recv{
    40  			// Default chan is now mock1, which is
    41  			// not what we want here.
    42  			Chan:    "mother",
    43  			Pattern: dejson(`{"success":true}`),
    44  		},
    45  	})
    46  
    47  }
    48  
    49  func newTest(t *testing.T) (*Ctx, *Spec, *Test) {
    50  	var (
    51  		ctx = NewCtx(nil)
    52  		s   = NewSpec()
    53  		tst = NewTest(ctx, "", s)
    54  	)
    55  
    56  	return ctx, s, tst
    57  }
    58  
    59  func run(t *testing.T, ctx *Ctx, tst *Test) {
    60  	if err := tst.Init(ctx); err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	if err := tst.Run(ctx); err != nil {
    65  		// We are ignoring errors from final phases.  ToDo:
    66  		// Not that.
    67  		t.Fatal(err)
    68  	}
    69  }
    70  
    71  func TestBasic(t *testing.T) {
    72  
    73  	ctx, s, tst := newTest(t)
    74  	ctx.LogLevel = "debug"
    75  
    76  	{
    77  		p := &Phase{
    78  			Doc: "Create a mock channel",
    79  		}
    80  		s.Phases["phase1"] = p
    81  
    82  		addMock(t, ctx, p)
    83  
    84  		p.AddStep(ctx, &Step{
    85  			Goto: "mock-test",
    86  		})
    87  	}
    88  
    89  	{
    90  		p := &Phase{
    91  			Doc: "Test our mock channel",
    92  		}
    93  		s.Phases["mock-test"] = p
    94  
    95  		p.AddStep(ctx, &Step{
    96  			Pub: &Pub{
    97  				Payload: `{"want":"tacos"}`,
    98  			},
    99  		})
   100  
   101  		p.AddStep(ctx, &Step{
   102  			Recv: &Recv{
   103  				Pattern: `{"want":"?*x"}`,
   104  				Timeout: time.Second,
   105  			},
   106  		})
   107  
   108  		p.AddStep(ctx, &Step{
   109  			Pub: &Pub{
   110  				Payload: `{"want":"chips"}`,
   111  			},
   112  		})
   113  
   114  		p.AddStep(ctx, &Step{
   115  			Recv: &Recv{
   116  				Pattern: `{"want":"?*x"}`,
   117  				Timeout: time.Second,
   118  			},
   119  		})
   120  
   121  	}
   122  
   123  	run(t, ctx, tst)
   124  
   125  }
   126  
   127  func runTest(t *testing.T, ctx *Ctx, tst *Test) error {
   128  	if err := tst.Init(ctx); err != nil {
   129  		t.Fatal(err)
   130  	}
   131  
   132  	return tst.Run(ctx)
   133  }
   134  
   135  func MustParseJSON(js string) interface{} {
   136  	var x interface{}
   137  	if err := json.Unmarshal([]byte(js), &x); err != nil {
   138  		panic(fmt.Errorf("failed to parse %s: %s", js, err))
   139  	}
   140  	return x
   141  }
   142  
   143  func TestFails(t *testing.T) {
   144  
   145  	ctx, s, tst := newTest(t)
   146  
   147  	{
   148  		p := &Phase{}
   149  
   150  		s.Phases["phase1"] = p
   151  
   152  		addMock(t, ctx, p)
   153  
   154  		p.AddStep(ctx, &Step{
   155  			Goto: "mock-test",
   156  		})
   157  	}
   158  
   159  	{
   160  		p := &Phase{}
   161  
   162  		s.Phases["mock-test"] = p
   163  
   164  		p.AddStep(ctx, &Step{
   165  			Pub: &Pub{
   166  				Payload: `{"want":"tacos"}`,
   167  			},
   168  		})
   169  
   170  		p.AddStep(ctx, &Step{
   171  			Recv: &Recv{
   172  				Pattern: `{"need":"?*x"}`,
   173  				Timeout: time.Second,
   174  			},
   175  			Fails: true,
   176  		})
   177  	}
   178  
   179  	run(t, ctx, tst)
   180  
   181  }
   182  
   183  func TestValidateSchema(t *testing.T) {
   184  	ctx := NewCtx(nil)
   185  	schema := "file://../demos/order.json"
   186  	t.Run("happy", func(t *testing.T) {
   187  		msg := `{"want":"chips"}`
   188  		if err := validateSchema(ctx, schema, msg); err != nil {
   189  			t.Fatal(err)
   190  		}
   191  	})
   192  	t.Run("sad", func(t *testing.T) {
   193  		msg := `{"need":"queso"}`
   194  		if err := validateSchema(ctx, schema, msg); err == nil {
   195  			t.Fatal("'want' was required")
   196  		}
   197  	})
   198  }