github.com/jbking/gohan@v0.0.0-20151217002006-b41ccf1c2a96/extension/extension_test.go (about)

     1  // Copyright (C) 2015 NTT Innovation Institute, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package extension_test
    17  
    18  import (
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  
    22  	"github.com/cloudwan/gohan/extension"
    23  	"github.com/cloudwan/gohan/extension/otto"
    24  	"github.com/cloudwan/gohan/server/middleware"
    25  )
    26  
    27  var _ = Describe("Environment manager", func() {
    28  	const (
    29  		schemaID1 = "Wormtongue"
    30  		schemaID2 = "Dumbledore"
    31  	)
    32  	var (
    33  		env1    extension.Environment
    34  		env2    extension.Environment
    35  		manager *extension.Manager
    36  	)
    37  
    38  	BeforeEach(func() {
    39  		env1 = otto.NewEnvironment(testDB1, &middleware.FakeIdentity{})
    40  		env2 = otto.NewEnvironment(testDB2, &middleware.FakeIdentity{})
    41  	})
    42  
    43  	JustBeforeEach(func() {
    44  		manager = extension.GetManager()
    45  		Expect(manager.RegisterEnvironment(schemaID1, env1)).To(Succeed())
    46  	})
    47  
    48  	AfterEach(func() {
    49  		extension.ClearManager()
    50  	})
    51  
    52  	Describe("Registering environments", func() {
    53  		Context("When it isn't registered", func() {
    54  			It("Should register it", func() {
    55  				Expect(manager.RegisterEnvironment(schemaID2, env2)).To(Succeed())
    56  			})
    57  		})
    58  
    59  		Context("When it is registered", func() {
    60  			It("Should return an error", func() {
    61  				err := manager.RegisterEnvironment(schemaID1, env2)
    62  				Expect(err).To(MatchError("Environment already registered for this schema"))
    63  			})
    64  		})
    65  	})
    66  
    67  	Describe("Getting an environment", func() {
    68  		Context("When it is registered", func() {
    69  			It("Should return it", func() {
    70  				env, ok := manager.GetEnvironment(schemaID1)
    71  				Expect(ok).To(BeTrue())
    72  				Expect(env).NotTo(BeNil())
    73  			})
    74  		})
    75  
    76  		Context("When it isn't registered", func() {
    77  			It("Should return a false ok", func() {
    78  				_, ok := manager.GetEnvironment(schemaID2)
    79  				Expect(ok).To(BeFalse())
    80  			})
    81  		})
    82  	})
    83  
    84  	Describe("Unregistering an environment", func() {
    85  		Context("When it is registered", func() {
    86  			It("Should unregister it", func() {
    87  				Expect(manager.UnRegisterEnvironment(schemaID1)).To(Succeed())
    88  			})
    89  		})
    90  
    91  		Context("When it isn't registered", func() {
    92  			It("Should return an error", func() {
    93  				err := manager.UnRegisterEnvironment(schemaID2)
    94  				Expect(err).To(MatchError("No environment registered for this schema"))
    95  			})
    96  		})
    97  	})
    98  
    99  	Describe("Executing a sequence of operations", func() {
   100  		Context("A typical sequence", func() {
   101  			It("Should do what expected", func() {
   102  				By("Getting a registered environment")
   103  				env, ok := manager.GetEnvironment(schemaID1)
   104  				Expect(ok).To(BeTrue())
   105  				Expect(env).NotTo(BeNil())
   106  
   107  				By("Successfully unregistering it")
   108  				Expect(manager.UnRegisterEnvironment(schemaID1)).To(Succeed())
   109  
   110  				By("No longer returning it")
   111  				env, ok = manager.GetEnvironment(schemaID1)
   112  				Expect(ok).To(BeFalse())
   113  
   114  				By("Registering another environment")
   115  				Expect(manager.RegisterEnvironment(schemaID2, env2)).To(Succeed())
   116  
   117  				By("Getting it when requested")
   118  				env, ok = manager.GetEnvironment(schemaID2)
   119  				Expect(ok).To(BeTrue())
   120  
   121  				By("Clearing the whole manager")
   122  				extension.ClearManager()
   123  				manager = extension.GetManager()
   124  
   125  				By("No longer returning the environment")
   126  				env, ok = manager.GetEnvironment(schemaID2)
   127  				Expect(ok).To(BeFalse())
   128  			})
   129  		})
   130  	})
   131  })