summaryrefslogtreecommitdiffstats
path: root/msb2pilot/src/msb2pilot/models/conversion_test.go
blob: 212e994fd9011041de20cd5cef26c8522652d6e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * Copyright (c) 2018 ZTE Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     ZTE - initial Project
 */
package models

import (
	"reflect"
	"testing"
)

func TestConvertBaseInfo(t *testing.T) {
	cases := []struct{ in, want string }{
		{
			in:   `{"enable_ssl":"true", "version":"v1","protocol":"REST","publish_port":"28012|28013","url":"/api/itm-pmadaptor/v1","is_manual":"false","visualRange":"0","appversion":"v1.18.20.04"}`,
			want: `v1`,
		},
		{
			in:   `{"enable_ssl":"true", "version":"v2","protocol":"UI", "status":"1", "url":"/api/itm-pmadaptor/v1","is_manual":"false"}`,
			want: `v2`,
		},
		{
			in:   `{"others":"other"}`,
			want: ``,
		},
		{
			in:   ``,
			want: ``,
		},
	}

	for _, cas := range cases {
		got, _ := convertBaseInfo(cas.in)
		if got.Version != cas.want {
			t.Errorf("convertBaseInfo(%s) => got %s, want %s", cas.in, got.AppVersion, cas.want)
		}
	}

}

func TestConvertNameSpace(t *testing.T) {
	cases := []struct{ in, want string }{
		{
			in:   `{"namespace":"test"}`,
			want: `test`,
		},
		{
			in:   `{"namespace":"testwithother", "others":"other"}`,
			want: `testwithother`,
		},
		{
			in:   `{"others":"other"}`,
			want: ``,
		},
		{
			in:   ``,
			want: ``,
		},
	}

	for _, cas := range cases {
		got, _ := convertNameSpace(cas.in)
		if got.NameSpace != cas.want {
			t.Errorf("convertNameSpace(%s) => got %s, want %s", cas.in, got.NameSpace, cas.want)
		}
	}

}

func TestConvertMsbLabel(t *testing.T) {
	cases := []struct{ label, in, want string }{
		{
			label: "base",
			in:    `"base":{"enable_ssl":"true", "version":"v1","protocol":"REST","publish_port":"28012|28013","url":"/api/itm-pmadaptor/v1","is_manual":"false","visualRange":"0","appversion":"v1.18.20.04"}`,
			want:  `*models.BaseInfo`,
		},
		{
			label: "ns",
			in:    `"ns":{"namespace":"test"}`,
			want:  `*models.NameSpace`,
		},
		{
			label: "others",
			in:    `{"others":"other"}`,
			want:  ``,
		},
		{
			label: "",
			in:    ``,
			want:  ``,
		},
	}

	for _, cas := range cases {
		got := convertMsbLabel(cas.label, cas.in)

		if got == nil {
			if cas.want != "" {
				t.Errorf("convertMsbLabel(%s, %s) => got nil, want %s", cas.label, cas.in, cas.want)
			}
		} else {
			if reflect.TypeOf(got).String() != cas.want {
				t.Errorf("convertMsbLabel(%s, %s) => got %v, want %s", cas.label, cas.in, reflect.TypeOf(got), cas.want)
			}
		}
	}

}