github.com/SAP/cloud-mta-build-tool@v1.2.27/internal/platform/process_test.go (about)

     1  package platform
     2  
     3  import (
     4  	. "github.com/onsi/ginkgo"
     5  	. "github.com/onsi/ginkgo/extensions/table"
     6  	. "github.com/onsi/gomega"
     7  
     8  	"github.com/SAP/cloud-mta/mta"
     9  )
    10  
    11  var _ = Describe("Process", func() {
    12  
    13  	var platforms = Platforms{[]Modules{
    14  		{Name: "cf",
    15  			Modules: []Properties{
    16  				{NativeType: "html5", PlatformType: "javascript.nodejs"},
    17  				{NativeType: "nodejs", PlatformType: "javascript.nodejs"},
    18  				{NativeType: "java", PlatformType: "java.tomcat"},
    19  				{NativeType: "hdb", PlatformType: "dbtype"},
    20  				{NativeType: "hdb", PlatformType: "dbtype2", Parameters: map[string]string{"value": "2"}},
    21  				{NativeType: "java", PlatformType: "java.tomee", Properties: map[string]string{"TARGET_RUNTIME": "tomee"}},
    22  			},
    23  		},
    24  		{Name: "neo",
    25  			Modules: []Properties{
    26  				{NativeType: "html5", PlatformType: "some.html"},
    27  				{NativeType: "java", PlatformType: "java.tomcat"},
    28  			},
    29  		},
    30  	}}
    31  
    32  	It("Unmarshal", func() {
    33  		var platformsCfg = []byte(`
    34  platform:
    35   - name: cf
    36     modules:
    37     - native-type: html5
    38       platform-type: "javascript.nodejs"
    39     - native-type: nodejs
    40       platform-type: "javascript.nodejs"
    41     - native-type: java
    42       platform-type: "java.tomcat"
    43     - native-type: hdb
    44       platform-type: "dbtype"
    45     - native-type: hdb
    46       platform-type: "dbtype2"
    47       parameters:
    48         value: 2
    49     - native-type: java
    50       platform-type: "java.tomee"
    51       properties:
    52         TARGET_RUNTIME: tomee
    53  
    54   - name: neo
    55     modules:
    56     - native-type: html5
    57       platform-type: "some.html"
    58     - native-type: java
    59       platform-type: "java.tomcat"
    60  `)
    61  		Ω(Unmarshal(platformsCfg)).Should(Equal(platforms))
    62  
    63  	})
    64  
    65  	It("Unmarshal - wrong elements", func() {
    66  		var platformsCfg = []byte(`
    67  platform:
    68   - name: cf
    69     modules:
    70     - native-type: html5
    71       platform-type: "javascript.nodejs"
    72     - native-type: nodejs
    73       platform-type: "javascript.nodejs"
    74     - native-type: java
    75       platform-type: "java.tomcat"
    76     - native-type: hdb
    77       platform-typex: "dbtype"
    78  `)
    79  		_, err := Unmarshal(platformsCfg)
    80  		Ω(err).Should(HaveOccurred())
    81  
    82  	})
    83  
    84  	var _ = DescribeTable("ConvertTypes", func(platform string) {
    85  		schemaVersion := "2.0.0"
    86  		mtaObj := mta.MTA{
    87  			SchemaVersion: &schemaVersion,
    88  			ID:            "mta_proj",
    89  			Version:       "1.0.0",
    90  			Modules: []*mta.Module{
    91  				{Name: "htmlapp", Type: "html5", Path: "app"},
    92  				{Name: "htmlapp2", Type: "html5", Path: "app2"},
    93  				{Name: "java", Type: "java", Path: "app3"},
    94  				{Name: "java2", Type: "java", Path: "app4", Properties: map[string]interface{}{"TARGET_RUNTIME": "tomee"}},
    95  				{Name: "hdb2", Type: "hdb", Path: "app5", Parameters: map[string]interface{}{"value": "2"}},
    96  			},
    97  		}
    98  		mtaObjMap := make(map[string]mta.MTA)
    99  		mtaObjMap["neo"] = mta.MTA{
   100  			SchemaVersion: &schemaVersion,
   101  			ID:            "mta_proj",
   102  			Version:       "1.0.0",
   103  			Modules: []*mta.Module{
   104  				{Name: "htmlapp", Type: "some.html", Path: "app"},
   105  				{Name: "htmlapp2", Type: "some.html", Path: "app2"},
   106  				{Name: "java", Type: "java.tomcat", Path: "app3"},
   107  				{Name: "java2", Type: "java.tomcat", Path: "app4", Properties: map[string]interface{}{"TARGET_RUNTIME": "tomee"}},
   108  				{Name: "hdb2", Type: "hdb", Path: "app5", Parameters: map[string]interface{}{"value": "2"}},
   109  			},
   110  		}
   111  		mtaObjMap["cf"] = mta.MTA{
   112  			SchemaVersion: &schemaVersion,
   113  			ID:            "mta_proj",
   114  			Version:       "1.0.0",
   115  			Modules: []*mta.Module{
   116  				{Name: "htmlapp", Type: "javascript.nodejs", Path: "app"},
   117  				{Name: "htmlapp2", Type: "javascript.nodejs", Path: "app2"},
   118  				{Name: "java", Type: "java.tomcat", Path: "app3"},
   119  				{Name: "java2", Type: "java.tomee", Path: "app4", Properties: map[string]interface{}{"TARGET_RUNTIME": "tomee"}},
   120  				{Name: "hdb2", Type: "dbtype2", Path: "app5", Parameters: map[string]interface{}{"value": "2"}},
   121  			},
   122  		}
   123  		ConvertTypes(mtaObj, platforms, platform)
   124  		Ω(mtaObj).Should(Equal(mtaObjMap[platform]))
   125  	},
   126  		Entry("Neo", "neo"),
   127  		Entry("CF", "cf"),
   128  	)
   129  
   130  	It("ConvertTypes returns the most accurate type when it appears before the less accurate type", func() {
   131  		schemaVersion := "2.0.0"
   132  		mtaObj := mta.MTA{
   133  			SchemaVersion: &schemaVersion,
   134  			ID:            "mta_proj",
   135  			Version:       "1.0.0",
   136  			Modules: []*mta.Module{
   137  				{Name: "java", Type: "java", Path: "app4", Properties: map[string]interface{}{"TARGET_RUNTIME": "tomee"}},
   138  			},
   139  		}
   140  		expected := mta.MTA{
   141  			SchemaVersion: &schemaVersion,
   142  			ID:            "mta_proj",
   143  			Version:       "1.0.0",
   144  			Modules: []*mta.Module{
   145  				{Name: "java", Type: "java.tomee", Path: "app4", Properties: map[string]interface{}{"TARGET_RUNTIME": "tomee"}},
   146  			},
   147  		}
   148  		platformsObj := Platforms{[]Modules{
   149  			{Name: "cf",
   150  				Modules: []Properties{
   151  					{NativeType: "java", PlatformType: "java.tomee", Properties: map[string]string{"TARGET_RUNTIME": "tomee"}},
   152  					{NativeType: "java", PlatformType: "java.tomcat"},
   153  				},
   154  			},
   155  		}}
   156  		ConvertTypes(mtaObj, platformsObj, "cf")
   157  		Ω(mtaObj).Should(Equal(expected))
   158  	})
   159  
   160  	It("platformConfig", func() {
   161  		expected := Modules{Name: "cf",
   162  			Modules: []Properties{
   163  				{NativeType: "html5", PlatformType: "javascript.nodejs"},
   164  				{NativeType: "nodejs", PlatformType: "javascript.nodejs"},
   165  				{NativeType: "java", PlatformType: "java.tomcat"},
   166  				{NativeType: "hdb", PlatformType: "dbtype"},
   167  				{NativeType: "hdb", PlatformType: "dbtype2", Parameters: map[string]string{"value": "2"}},
   168  				{NativeType: "java", PlatformType: "java.tomee", Properties: map[string]string{"TARGET_RUNTIME": "tomee"}},
   169  			},
   170  		}
   171  		Ω(platformConfig(platforms, "cf")).Should(Equal(expected))
   172  	})
   173  
   174  	Describe("satisfiesModuleConfig", func() {
   175  		It("returns ok=false when module type is mismatched", func() {
   176  			m := mta.Module{Name: "htmlapp", Type: "javascript.nodejs", Path: "app"}
   177  			config := Properties{NativeType: "abcd", PlatformType: "abcd"}
   178  			ok, acc := satisfiesModuleConfig(&m, &config)
   179  			Ω(ok).Should(BeFalse())
   180  			Ω(acc).Should(BeNumerically("<", 0))
   181  		})
   182  
   183  		It("returns ok=true when module type matches and no other conditions", func() {
   184  			m := mta.Module{Type: "htmlapp"}
   185  			config := Properties{NativeType: "htmlapp", PlatformType: "abcd"}
   186  			ok, acc := satisfiesModuleConfig(&m, &config)
   187  			Ω(ok).Should(BeTrue())
   188  			Ω(acc).Should(BeNumerically(">=", 0))
   189  		})
   190  
   191  		DescribeTable("returns ok=false when module type matches and property or parameter doesn't match", func(c Properties) {
   192  			m := mta.Module{
   193  				Type:       "a",
   194  				Properties: map[string]interface{}{"a": "b"},
   195  				Parameters: map[string]interface{}{"a": "b"},
   196  			}
   197  			c.NativeType = "a"
   198  			ok, acc := satisfiesModuleConfig(&m, &c)
   199  			Ω(ok).Should(BeFalse())
   200  			Ω(acc).Should(BeNumerically("<", 0))
   201  		},
   202  			Entry("property doesn't match", Properties{Properties: map[string]string{"a": "a"}}),
   203  			Entry("property doesn't exist", Properties{Properties: map[string]string{"b": "a"}}),
   204  			Entry("parameter doesn't match", Properties{Parameters: map[string]string{"a": "a"}}),
   205  			Entry("parameter doesn't exist", Properties{Parameters: map[string]string{"b": "a"}}),
   206  			Entry("One parameter matches and one doesn't", Properties{Parameters: map[string]string{"a": "b", "b": "a"}}),
   207  			Entry("One property matches and one doesn't", Properties{Properties: map[string]string{"a": "b", "b": "a"}}),
   208  			Entry("Parameter matches and property doesn't", Properties{Parameters: map[string]string{"a": "b"}, Properties: map[string]string{"a": "a"}}),
   209  			Entry("Property matches and parameter doesn't", Properties{Parameters: map[string]string{"b": "b"}, Properties: map[string]string{"a": "b"}}),
   210  		)
   211  
   212  		It("returns ok=true and accuracy is higher the more conditions there are", func() {
   213  			configs := []struct {
   214  				desc   string
   215  				config Properties
   216  			}{
   217  				{"no properties or parameters", Properties{}},
   218  				{"1 property", Properties{Properties: map[string]string{"a": "a"}}},
   219  				{"2 properties", Properties{Properties: map[string]string{"a": "a", "b": "b"}}},
   220  				{"3 parameters", Properties{Parameters: map[string]string{"c": "c", "d": "d", "e": "e"}}},
   221  				{"3 parameters and a property", Properties{Parameters: map[string]string{"c": "c", "d": "d", "e": "e"}, Properties: map[string]string{"c": "c"}}},
   222  			}
   223  			m := mta.Module{
   224  				Type:       "a",
   225  				Properties: map[string]interface{}{"a": "a", "b": "b", "c": "c"},
   226  				Parameters: map[string]interface{}{"c": "c", "d": "d", "e": "e"},
   227  			}
   228  			acc := -1
   229  			prevDesc := "initial value"
   230  			for _, c := range configs {
   231  				c.config.NativeType = "a"
   232  				ok, moduleAcc := satisfiesModuleConfig(&m, &c.config)
   233  				Ω(ok).Should(BeTrue(), "module did not satisfy config with "+c.desc)
   234  				Ω(moduleAcc).Should(BeNumerically(">", acc), c.desc+" has lower accuracy than "+prevDesc)
   235  				acc = moduleAcc
   236  				prevDesc = c.desc
   237  			}
   238  		})
   239  	})
   240  })