github.com/mmatczuk/gohan@v0.0.0-20170206152520-30e45d9bdb69/cli/client/client_test_utils.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 client
    17  
    18  import (
    19  	"encoding/json"
    20  	"sort"
    21  
    22  	g "github.com/onsi/gomega"
    23  
    24  	"github.com/cloudwan/gohan/schema"
    25  )
    26  
    27  func getAuthRequest() interface{} {
    28  	return map[string]interface{}{
    29  		"auth": map[string]interface{}{
    30  			"tenantName": "admin",
    31  			"passwordCredentials": map[string]interface{}{
    32  				"username": "admin",
    33  				"password": "password",
    34  			},
    35  		},
    36  	}
    37  }
    38  
    39  func getAuthResponse(gohanEndpointURL string) interface{} {
    40  	return map[string]interface{}{
    41  		"access": map[string]interface{}{
    42  			"token": map[string]interface{}{
    43  				"expires":   "2014-01-31T15:30:58Z",
    44  				"id":        "admin_token",
    45  				"issued_at": "2014-01-30T15:30:58.819584",
    46  				"tenant": map[string]interface{}{
    47  					"description": nil,
    48  					"enabled":     true,
    49  					"id":          "fc394f2ab2df4114bde39905f800dc57",
    50  					"name":        "admin",
    51  				},
    52  			},
    53  			"serviceCatalog": []map[string]interface{}{
    54  				map[string]interface{}{
    55  					"endpoints": []map[string]interface{}{
    56  						map[string]interface{}{
    57  							"adminURL":    gohanEndpointURL,
    58  							"internalURL": gohanEndpointURL,
    59  							"publicURL":   gohanEndpointURL,
    60  							"region":      "RegionOne",
    61  							"id":          "2dad48f09e2a447a9bf852bcd93548ef",
    62  						},
    63  					},
    64  					"endpoints_links": []interface{}{},
    65  					"type":            "gohan",
    66  					"name":            "gohan",
    67  				},
    68  			},
    69  		},
    70  	}
    71  }
    72  
    73  func getCastleSchema() map[string]interface{} {
    74  	return map[string]interface{}{
    75  		"description": "Castle",
    76  		"id":          "castle",
    77  		"singular":    "castle",
    78  		"title":       "castle",
    79  		"parent":      "",
    80  		"plural":      "castles",
    81  		"prefix":      "/v2.0",
    82  		"url":         "/v2.0/castles",
    83  		"schema": map[string]interface{}{
    84  			"properties": map[string]interface{}{
    85  				"id": map[string]interface{}{
    86  					"format": "uuid",
    87  					"permission": []interface{}{
    88  						"create",
    89  					},
    90  					"type": "string",
    91  				},
    92  				"name": map[string]interface{}{
    93  					"permission": []interface{}{
    94  						"create",
    95  						"update",
    96  					},
    97  					"type": "string",
    98  				},
    99  			},
   100  			"propertiesOrder": []interface{}{
   101  				"id",
   102  				"name",
   103  			},
   104  		},
   105  		"actions": map[string]interface{}{
   106  			"open_gates": map[string]interface{}{
   107  				"method": "GET",
   108  				"path":   "/:id/open_gates",
   109  				"input": map[string]interface{}{
   110  					"type": "object",
   111  					"properties": map[string]interface{}{
   112  						"gate_id": map[string]interface{}{
   113  							"type": "number",
   114  						},
   115  					},
   116  				},
   117  				"output": map[string]interface{}{
   118  					"type": "string",
   119  				},
   120  			},
   121  			"close_gates": map[string]interface{}{
   122  				"method": "GET",
   123  				"path":   "/close_all_gates",
   124  				"output": map[string]interface{}{
   125  					"type": "string",
   126  				},
   127  			},
   128  		},
   129  	}
   130  }
   131  
   132  func getTowerSchema() map[string]interface{} {
   133  	return map[string]interface{}{
   134  		"description": "Tower",
   135  		"id":          "tower",
   136  		"title":       "tower",
   137  		"singular":    "tower",
   138  		"parent":      "castle",
   139  		"plural":      "towers",
   140  		"prefix":      "/v2.0",
   141  		"url":         "/v2.0/towers",
   142  		"schema": map[string]interface{}{
   143  			"properties": map[string]interface{}{
   144  				"id": map[string]interface{}{
   145  					"format": "uuid",
   146  					"permission": []interface{}{
   147  						"create",
   148  					},
   149  					"type": "string",
   150  				},
   151  				"isMain": map[string]interface{}{
   152  					"permission": []interface{}{
   153  						"create",
   154  						"update",
   155  					},
   156  					"type": "boolean",
   157  				},
   158  				"sister_id": map[string]interface{}{
   159  					"permission": []interface{}{
   160  						"create",
   161  						"update",
   162  					},
   163  					"type":     "string",
   164  					"relation": "tower",
   165  				},
   166  			},
   167  			"propertiesOrder": []interface{}{
   168  				"id",
   169  				"isMain",
   170  				"sister",
   171  			},
   172  		},
   173  	}
   174  }
   175  
   176  func getChamberSchema() map[string]interface{} {
   177  	return map[string]interface{}{
   178  		"description": "Chamber",
   179  		"id":          "chamber",
   180  		"title":       "chamber",
   181  		"singular":    "chamber",
   182  		"parent":      "tower",
   183  		"plural":      "chambers",
   184  		"prefix":      "/v2.0",
   185  		"url":         "/v2.0/chambers",
   186  		"schema": map[string]interface{}{
   187  			"properties": map[string]interface{}{
   188  				"name": map[string]interface{}{
   189  					"permission": []interface{}{
   190  						"create",
   191  					},
   192  					"type": []string{"string", "null"},
   193  				},
   194  				"isPrincessIn": map[string]interface{}{
   195  					"permission": []interface{}{
   196  						"create",
   197  						"update",
   198  					},
   199  					"type": "boolean",
   200  				},
   201  				"windows": map[string]interface{}{
   202  					"permission": []interface{}{
   203  						"create",
   204  						"update",
   205  					},
   206  					"type": "integer",
   207  				},
   208  				"chest": map[string]interface{}{
   209  					"permission": []interface{}{
   210  						"create",
   211  						"update",
   212  					},
   213  					"type": "array",
   214  					"items": map[string]interface{}{
   215  						"type": "string",
   216  					},
   217  				},
   218  				"weapon": map[string]interface{}{
   219  					"permission": []interface{}{
   220  						"create",
   221  						"update",
   222  					},
   223  					"type": "object",
   224  					"properties": map[string]interface{}{
   225  						"name": map[string]interface{}{
   226  							"type": "string",
   227  						},
   228  					},
   229  				},
   230  			},
   231  			"propertiesOrder": []interface{}{
   232  				"name",
   233  				"isPrincessIn",
   234  				"windows",
   235  			},
   236  		},
   237  	}
   238  }
   239  
   240  func getSchemasResponse() interface{} {
   241  	return map[string]interface{}{
   242  		"schemas": []map[string]interface{}{
   243  			getCastleSchema(),
   244  			getTowerSchema(),
   245  		},
   246  	}
   247  }
   248  
   249  func getSchemas() []*schema.Schema {
   250  	rawSchemasResponse := getSchemasResponse()
   251  	rawSchemas := rawSchemasResponse.(map[string]interface{})["schemas"].([]map[string]interface{})
   252  	schemas := []*schema.Schema{}
   253  	for _, s := range rawSchemas {
   254  		schema, _ := schema.NewSchemaFromObj(s)
   255  		schemas = append(schemas, schema)
   256  	}
   257  	return schemas
   258  }
   259  
   260  var (
   261  	castleID       = "de305d54-75b4-431b-adb2-eb6b9e546013"
   262  	icyTowerID     = "de305d54-75b4-431b-adb2-eb6b9e546014"
   263  	icyTowerName   = "Icy Tower"
   264  	babylonTowerID = "de305d54-75b4-431b-adb2-eb6b9e546015"
   265  )
   266  
   267  func openGates() string {
   268  	return "gates opened"
   269  }
   270  
   271  func closeGates() string {
   272  	return "gates closed"
   273  }
   274  
   275  func getIcyTower() map[string]interface{} {
   276  	return map[string]interface{}{
   277  		"id":   icyTowerID,
   278  		"name": icyTowerName,
   279  	}
   280  }
   281  
   282  func getBabylonTower() map[string]interface{} {
   283  	return map[string]interface{}{
   284  		"id":   babylonTowerID,
   285  		"name": "Babylon Tower",
   286  	}
   287  }
   288  
   289  func getIcyTowerListResponse() map[string]interface{} {
   290  	return map[string]interface{}{
   291  		"towers": []map[string]interface{}{
   292  			getIcyTower(),
   293  		},
   294  	}
   295  }
   296  
   297  func getTowerListResponse() map[string]interface{} {
   298  	return map[string]interface{}{
   299  		"towers": []map[string]interface{}{
   300  			getIcyTower(),
   301  			getBabylonTower(),
   302  		},
   303  	}
   304  }
   305  
   306  func getTowerListJSONResponse() []byte {
   307  	towersJSON, _ := json.Marshal(getTowerListResponse())
   308  	return towersJSON
   309  }
   310  
   311  func compareSchemas(actual, expected []*schema.Schema) {
   312  	g.Expect(actual).To(g.HaveLen(len(expected)))
   313  	for i, s := range actual {
   314  		sortProperties(s)
   315  		sortProperties(expected[i])
   316  		sortActions(s)
   317  		sortActions(expected[i])
   318  		g.Expect(s).To(g.Equal(expected[i]))
   319  	}
   320  }
   321  
   322  type properties []schema.Property
   323  
   324  func (p properties) Len() int           { return len(p) }
   325  func (p properties) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
   326  func (p properties) Less(i, j int) bool { return p[i].ID < p[j].ID }
   327  
   328  func sortProperties(schema *schema.Schema) {
   329  	sort.Sort(properties(schema.Properties))
   330  }
   331  
   332  type actions []schema.Action
   333  
   334  func (a actions) Len() int           { return len(a) }
   335  func (a actions) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
   336  func (a actions) Less(i, j int) bool { return a[i].ID < a[j].ID }
   337  
   338  func sortActions(schema *schema.Schema) {
   339  	sort.Sort(actions(schema.Actions))
   340  }
   341  
   342  //ByName is alias type for handling gohanCommands
   343  type ByName []gohanCommand
   344  
   345  func (a ByName) Len() int {
   346  	return len(a)
   347  }
   348  func (a ByName) Swap(i, j int) {
   349  	a[i], a[j] = a[j], a[i]
   350  }
   351  func (a ByName) Less(i, j int) bool { return a[i].Name < a[j].Name }