srijeet0406 commented on code in PR #7396:
URL: https://github.com/apache/trafficcontrol/pull/7396#discussion_r1134355476


##########
traffic_ops/traffic_ops_golang/server/put_status_test.go:
##########
@@ -0,0 +1,76 @@
+package server
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+       "testing"
+       "time"
+
+       "github.com/jmoiron/sqlx"
+       "gopkg.in/DATA-DOG/go-sqlmock.v1"
+)
+
+func TestCheckExistingStatusInfo(t *testing.T) {
+       mockDB, mock, err := sqlmock.New()
+       if err != nil {
+               t.Fatalf("an error '%s' was not expected when opening a stub 
database connection", err)
+       }
+       defer mockDB.Close()
+
+       db := sqlx.NewDb(mockDB, "sqlmock")
+       defer db.Close()
+
+       lastUpdated := time.Now()
+       mock.ExpectBegin()
+       rows := sqlmock.NewRows([]string{"status", "status_last_updated"})
+       rows.AddRow(1, lastUpdated)
+       mock.ExpectQuery("SELECT").WithArgs(1).WillReturnRows(rows)
+
+       status, statusLastUpdated := checkExistingStatusInfo(1, 
db.MustBegin().Tx)
+       if status != 1 {
+               t.Errorf("Expected server status to be 1, got %v", status)
+       }
+
+       if statusLastUpdated != lastUpdated {
+               t.Errorf("Expected status time: %s, got: %s", lastUpdated, 
statusLastUpdated)
+       }
+}
+
+func TestUpdateServerStatusAndOfflineReason(t *testing.T) {
+       mockDB, mock, err := sqlmock.New()
+       if err != nil {
+               t.Fatalf("an error '%s' was not expected when opening a stub 
database connection", err)
+       }
+       defer mockDB.Close()
+
+       db := sqlx.NewDb(mockDB, "sqlmock")
+       defer db.Close()
+
+       lastUpdated := time.Now()
+       mock.ExpectBegin()
+       mock.ExpectExec("UPDATE").WithArgs(2, "no longer needed", lastUpdated, 
1).WillReturnResult(sqlmock.NewResult(1, 1))
+       mock.ExpectCommit()
+
+       reason := "no longer needed"

Review Comment:
   Instead of creating a new string here, you can just use `util.StrPtr("no 
longer needed")` directly in the next line.



##########
traffic_ops/traffic_ops_golang/server/servers_assignment_test.go:
##########
@@ -103,3 +105,29 @@ func TestAssignDsesToServer(t *testing.T) {
                t.Errorf("delivery services assigned: Expected %v.   Got  %v", 
newDses, result)
        }
 }
+
+func TestCheckForLastServerInActiveDeliveryServices(t *testing.T) {
+       mockDB, mock, err := sqlmock.New()
+       if err != nil {
+               t.Fatalf("an error '%s' was not expected when opening a stub 
database connection", err)
+       }
+       defer mockDB.Close()
+
+       db := sqlx.NewDb(mockDB, "sqlmock")
+       defer db.Close()
+
+       dsIDs := []int{1, 2, 3}
+       mock.ExpectBegin()
+       rows := sqlmock.NewRows([]string{"id", "multi_site_origin", "topology"})
+       rows.AddRow(1, false, util.Ptr("blah"))
+       mock.ExpectQuery("SELECT").WithArgs(1, tc.DSActiveStateActive, 
pq.Array(dsIDs), tc.CacheStatusOnline, tc.CacheStatusReported, 
"EDGE%").WillReturnRows(rows)
+       mock.ExpectCommit()
+
+       _, err = checkForLastServerInActiveDeliveryServices(1, "EDGE%", dsIDs, 
db.MustBegin().Tx)
+       //if vi[0] != 1 {
+       //      t.Errorf("mismatch violations, expected:%v, got: %v", 
violations, vi)
+       //}
+       if err != nil {
+               t.Errorf("unable to check server in active DS, got error:%s", 
err)

Review Comment:
   we should use `%v` here.



##########
traffic_ops/traffic_ops_golang/server/servers_server_capability_test.go:
##########
@@ -20,11 +20,76 @@ package server
  */
 
 import (
+       "net/http"
+       "strings"
        "testing"
+       "time"
 
+       "github.com/apache/trafficcontrol/lib/go-tc"
+       "github.com/apache/trafficcontrol/lib/go-util"
        "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+       "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/test"
+
+       "github.com/jmoiron/sqlx"
+       "github.com/lib/pq"
+       "gopkg.in/DATA-DOG/go-sqlmock.v1"
 )
 
+func getTestSSCs() []tc.ServerServerCapability {
+       sscs := []tc.ServerServerCapability{}
+       testSSC := tc.ServerServerCapability{
+               LastUpdated:      &tc.TimeNoMod{Time: time.Now()},
+               Server:           util.StrPtr("test"),
+               ServerID:         util.IntPtr(1),
+               ServerCapability: util.StrPtr("test"),
+       }
+       sscs = append(sscs, testSSC)
+
+       testSSC1 := testSSC
+       testSSC1.ServerCapability = util.Ptr("blah")
+       sscs = append(sscs, testSSC1)
+
+       return sscs
+}
+
+func TestReadSCs(t *testing.T) {
+       mockDB, mock, err := sqlmock.New()
+       if err != nil {
+               t.Fatalf("an error '%s' was not expected when opening a stub 
database connection", err)

Review Comment:
   We should use `%v` here.



##########
traffic_ops/traffic_ops_golang/server/servers_server_capability_test.go:
##########
@@ -42,3 +107,103 @@ func TestInterfaces(t *testing.T) {
                t.Errorf("ServerServerCapability must be Identifier")
        }
 }
+
+func TestFuncs(t *testing.T) {
+       if strings.Index(scSelectQuery(), "SELECT") != 0 {
+               t.Errorf("expected selectQuery to start with SELECT")
+       }
+       if strings.Index(scInsertQuery(), "INSERT") != 0 {
+               t.Errorf("expected insertQuery to start with INSERT")
+       }
+       if strings.Index(scDeleteQuery(), "DELETE") != 0 {
+               t.Errorf("expected deleteQuery to start with DELETE")
+       }
+}
+
+func TestValidate(t *testing.T) {
+       testSSC := tc.ServerServerCapability{
+               LastUpdated:      &tc.TimeNoMod{Time: time.Now()},
+               Server:           util.StrPtr("test1"),
+               ServerID:         util.IntPtr(1),
+               ServerCapability: util.StrPtr("abc"),
+       }
+       testTOSSC := TOServerServerCapability{
+               ServerServerCapability: testSSC,
+       }
+
+       err, _ := testTOSSC.Validate()
+       errs := test.SortErrors(test.SplitErrors(err))
+
+       if len(errs) > 0 {
+               t.Errorf(`expected no errors,  got %v`, errs)
+       }
+}
+
+func TestCheckExistingServer(t *testing.T) {
+       mockDB, mock, err := sqlmock.New()
+       if err != nil {
+               t.Fatalf("an error '%s' was not expected when opening a stub 
database connection", err)
+       }
+       defer mockDB.Close()
+
+       db := sqlx.NewDb(mockDB, "sqlmock")
+       defer db.Close()
+
+       mock.ExpectBegin()
+       rows := sqlmock.NewRows([]string{"host_name"})
+       rows.AddRow("test")
+       mock.ExpectQuery("SELECT host_name").WithArgs(1).WillReturnRows(rows)
+
+       rows1 := sqlmock.NewRows([]string{"name"})
+       rows1.AddRow("ALL")
+       mock.ExpectQuery("SELECT name").WithArgs(1).WillReturnRows(rows1)
+
+       rows2 := sqlmock.NewRows([]string{"username", "soft", 
"shared_usernames"})
+       rows2.AddRow("user1", false, []byte("{}"))
+       mock.ExpectQuery("SELECT c.username, 
c.soft").WithArgs("ALL").WillReturnRows(rows2)
+       mock.ExpectCommit()
+
+       testSCCs := getTestSSCs()
+       var sids []int64
+       sids = append(sids, int64(*testSCCs[0].ServerID))
+       code, usrErr, sysErr := checkExistingServer(db.MustBegin().Tx, sids, 
"user1")
+       if usrErr != nil || sysErr != nil {
+               t.Errorf("unable to check if server exists")

Review Comment:
   It will be helpful to know what these errors are, if any



##########
traffic_ops/traffic_ops_golang/server/servers_assignment_test.go:
##########
@@ -103,3 +105,29 @@ func TestAssignDsesToServer(t *testing.T) {
                t.Errorf("delivery services assigned: Expected %v.   Got  %v", 
newDses, result)
        }
 }
+
+func TestCheckForLastServerInActiveDeliveryServices(t *testing.T) {
+       mockDB, mock, err := sqlmock.New()
+       if err != nil {
+               t.Fatalf("an error '%s' was not expected when opening a stub 
database connection", err)
+       }
+       defer mockDB.Close()
+
+       db := sqlx.NewDb(mockDB, "sqlmock")
+       defer db.Close()
+
+       dsIDs := []int{1, 2, 3}
+       mock.ExpectBegin()
+       rows := sqlmock.NewRows([]string{"id", "multi_site_origin", "topology"})
+       rows.AddRow(1, false, util.Ptr("blah"))
+       mock.ExpectQuery("SELECT").WithArgs(1, tc.DSActiveStateActive, 
pq.Array(dsIDs), tc.CacheStatusOnline, tc.CacheStatusReported, 
"EDGE%").WillReturnRows(rows)
+       mock.ExpectCommit()
+
+       _, err = checkForLastServerInActiveDeliveryServices(1, "EDGE%", dsIDs, 
db.MustBegin().Tx)
+       //if vi[0] != 1 {

Review Comment:
   Could you pls remove the commented out code?



##########
traffic_ops/traffic_ops_golang/server/put_status_test.go:
##########
@@ -0,0 +1,76 @@
+package server
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+       "testing"
+       "time"
+
+       "github.com/jmoiron/sqlx"
+       "gopkg.in/DATA-DOG/go-sqlmock.v1"
+)
+
+func TestCheckExistingStatusInfo(t *testing.T) {
+       mockDB, mock, err := sqlmock.New()
+       if err != nil {
+               t.Fatalf("an error '%s' was not expected when opening a stub 
database connection", err)
+       }
+       defer mockDB.Close()
+
+       db := sqlx.NewDb(mockDB, "sqlmock")
+       defer db.Close()
+
+       lastUpdated := time.Now()
+       mock.ExpectBegin()
+       rows := sqlmock.NewRows([]string{"status", "status_last_updated"})
+       rows.AddRow(1, lastUpdated)
+       mock.ExpectQuery("SELECT").WithArgs(1).WillReturnRows(rows)
+
+       status, statusLastUpdated := checkExistingStatusInfo(1, 
db.MustBegin().Tx)
+       if status != 1 {
+               t.Errorf("Expected server status to be 1, got %v", status)
+       }
+
+       if statusLastUpdated != lastUpdated {
+               t.Errorf("Expected status time: %s, got: %s", lastUpdated, 
statusLastUpdated)
+       }
+}
+
+func TestUpdateServerStatusAndOfflineReason(t *testing.T) {
+       mockDB, mock, err := sqlmock.New()
+       if err != nil {
+               t.Fatalf("an error '%s' was not expected when opening a stub 
database connection", err)
+       }
+       defer mockDB.Close()
+
+       db := sqlx.NewDb(mockDB, "sqlmock")
+       defer db.Close()
+
+       lastUpdated := time.Now()
+       mock.ExpectBegin()
+       mock.ExpectExec("UPDATE").WithArgs(2, "no longer needed", lastUpdated, 
1).WillReturnResult(sqlmock.NewResult(1, 1))
+       mock.ExpectCommit()
+
+       reason := "no longer needed"
+       err = updateServerStatusAndOfflineReason(2, 2, 1, lastUpdated, &reason, 
db.MustBegin().Tx)
+       if err != nil {
+               t.Errorf("unable to change the status of the server, error: 
%s", err)

Review Comment:
   We should use `%v` here.



##########
traffic_ops/traffic_ops_golang/servercapability/servercapability_test.go:
##########
@@ -0,0 +1,145 @@
+package servercapability
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+       "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/test"

Review Comment:
   imports need re organizing



##########
traffic_ops/traffic_ops_golang/server/servers_server_capability_test.go:
##########
@@ -42,3 +107,103 @@ func TestInterfaces(t *testing.T) {
                t.Errorf("ServerServerCapability must be Identifier")
        }
 }
+
+func TestFuncs(t *testing.T) {
+       if strings.Index(scSelectQuery(), "SELECT") != 0 {
+               t.Errorf("expected selectQuery to start with SELECT")
+       }
+       if strings.Index(scInsertQuery(), "INSERT") != 0 {
+               t.Errorf("expected insertQuery to start with INSERT")
+       }
+       if strings.Index(scDeleteQuery(), "DELETE") != 0 {
+               t.Errorf("expected deleteQuery to start with DELETE")
+       }
+}
+
+func TestValidate(t *testing.T) {
+       testSSC := tc.ServerServerCapability{
+               LastUpdated:      &tc.TimeNoMod{Time: time.Now()},
+               Server:           util.StrPtr("test1"),
+               ServerID:         util.IntPtr(1),
+               ServerCapability: util.StrPtr("abc"),
+       }
+       testTOSSC := TOServerServerCapability{
+               ServerServerCapability: testSSC,
+       }
+
+       err, _ := testTOSSC.Validate()
+       errs := test.SortErrors(test.SplitErrors(err))
+
+       if len(errs) > 0 {
+               t.Errorf(`expected no errors,  got %v`, errs)
+       }
+}
+
+func TestCheckExistingServer(t *testing.T) {
+       mockDB, mock, err := sqlmock.New()
+       if err != nil {
+               t.Fatalf("an error '%s' was not expected when opening a stub 
database connection", err)
+       }
+       defer mockDB.Close()
+
+       db := sqlx.NewDb(mockDB, "sqlmock")
+       defer db.Close()
+
+       mock.ExpectBegin()
+       rows := sqlmock.NewRows([]string{"host_name"})
+       rows.AddRow("test")
+       mock.ExpectQuery("SELECT host_name").WithArgs(1).WillReturnRows(rows)
+
+       rows1 := sqlmock.NewRows([]string{"name"})
+       rows1.AddRow("ALL")
+       mock.ExpectQuery("SELECT name").WithArgs(1).WillReturnRows(rows1)
+
+       rows2 := sqlmock.NewRows([]string{"username", "soft", 
"shared_usernames"})
+       rows2.AddRow("user1", false, []byte("{}"))
+       mock.ExpectQuery("SELECT c.username, 
c.soft").WithArgs("ALL").WillReturnRows(rows2)
+       mock.ExpectCommit()
+
+       testSCCs := getTestSSCs()
+       var sids []int64
+       sids = append(sids, int64(*testSCCs[0].ServerID))
+       code, usrErr, sysErr := checkExistingServer(db.MustBegin().Tx, sids, 
"user1")
+       if usrErr != nil || sysErr != nil {
+               t.Errorf("unable to check if server exists")
+       }
+       if code != http.StatusOK {
+               t.Errorf("Failed")
+       }
+}
+
+func TestCheckServerType(t *testing.T) {
+       mockDB, mock, err := sqlmock.New()
+       if err != nil {
+               t.Fatalf("an error '%s' was not expected when opening a stub 
database connection", err)
+       }
+       defer mockDB.Close()
+
+       db := sqlx.NewDb(mockDB, "sqlmock")
+       defer db.Close()
+
+       testSCCs := getTestSSCs()
+       testSCCs[1].ServerID = util.Ptr(2)
+       testSCCs[1].Server = util.Ptr("foo")
+
+       mock.ExpectBegin()
+       rows := sqlmock.NewRows([]string{"array_agg"})
+       var sids []int64
+       for i, _ := range testSCCs {
+               sids = append(sids, int64(*testSCCs[i].ServerID))
+       }
+       rows.AddRow([]byte("{1,2}"))
+       mock.ExpectQuery("SELECT 
array_agg").WithArgs(pq.Array(sids)).WillReturnRows(rows)
+       mock.ExpectCommit()
+
+       code, usrErr, sysErr := checkServerType(db.MustBegin().Tx, sids)
+       if usrErr != nil || sysErr != nil {
+               t.Errorf("unable to check if server type exists")
+       }
+       if code != http.StatusOK {
+               t.Errorf("Failed")

Review Comment:
   Same comments here with respect to status code and error printing.



##########
traffic_ops/traffic_ops_golang/server/put_status_test.go:
##########
@@ -0,0 +1,76 @@
+package server
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+       "testing"
+       "time"
+
+       "github.com/jmoiron/sqlx"
+       "gopkg.in/DATA-DOG/go-sqlmock.v1"
+)
+
+func TestCheckExistingStatusInfo(t *testing.T) {
+       mockDB, mock, err := sqlmock.New()
+       if err != nil {
+               t.Fatalf("an error '%s' was not expected when opening a stub 
database connection", err)
+       }
+       defer mockDB.Close()
+
+       db := sqlx.NewDb(mockDB, "sqlmock")
+       defer db.Close()
+
+       lastUpdated := time.Now()
+       mock.ExpectBegin()
+       rows := sqlmock.NewRows([]string{"status", "status_last_updated"})
+       rows.AddRow(1, lastUpdated)
+       mock.ExpectQuery("SELECT").WithArgs(1).WillReturnRows(rows)
+
+       status, statusLastUpdated := checkExistingStatusInfo(1, 
db.MustBegin().Tx)
+       if status != 1 {
+               t.Errorf("Expected server status to be 1, got %v", status)
+       }
+
+       if statusLastUpdated != lastUpdated {
+               t.Errorf("Expected status time: %s, got: %s", lastUpdated, 
statusLastUpdated)
+       }
+}
+
+func TestUpdateServerStatusAndOfflineReason(t *testing.T) {
+       mockDB, mock, err := sqlmock.New()
+       if err != nil {
+               t.Fatalf("an error '%s' was not expected when opening a stub 
database connection", err)

Review Comment:
   We should use `%v` here.



##########
traffic_ops/traffic_ops_golang/server/servers_server_capability_test.go:
##########
@@ -42,3 +107,103 @@ func TestInterfaces(t *testing.T) {
                t.Errorf("ServerServerCapability must be Identifier")
        }
 }
+
+func TestFuncs(t *testing.T) {
+       if strings.Index(scSelectQuery(), "SELECT") != 0 {
+               t.Errorf("expected selectQuery to start with SELECT")
+       }
+       if strings.Index(scInsertQuery(), "INSERT") != 0 {
+               t.Errorf("expected insertQuery to start with INSERT")
+       }
+       if strings.Index(scDeleteQuery(), "DELETE") != 0 {
+               t.Errorf("expected deleteQuery to start with DELETE")
+       }
+}
+
+func TestValidate(t *testing.T) {
+       testSSC := tc.ServerServerCapability{
+               LastUpdated:      &tc.TimeNoMod{Time: time.Now()},
+               Server:           util.StrPtr("test1"),
+               ServerID:         util.IntPtr(1),
+               ServerCapability: util.StrPtr("abc"),
+       }
+       testTOSSC := TOServerServerCapability{
+               ServerServerCapability: testSSC,
+       }
+
+       err, _ := testTOSSC.Validate()
+       errs := test.SortErrors(test.SplitErrors(err))
+
+       if len(errs) > 0 {
+               t.Errorf(`expected no errors,  got %v`, errs)
+       }
+}
+
+func TestCheckExistingServer(t *testing.T) {
+       mockDB, mock, err := sqlmock.New()
+       if err != nil {
+               t.Fatalf("an error '%s' was not expected when opening a stub 
database connection", err)
+       }
+       defer mockDB.Close()
+
+       db := sqlx.NewDb(mockDB, "sqlmock")
+       defer db.Close()
+
+       mock.ExpectBegin()
+       rows := sqlmock.NewRows([]string{"host_name"})
+       rows.AddRow("test")
+       mock.ExpectQuery("SELECT host_name").WithArgs(1).WillReturnRows(rows)
+
+       rows1 := sqlmock.NewRows([]string{"name"})
+       rows1.AddRow("ALL")
+       mock.ExpectQuery("SELECT name").WithArgs(1).WillReturnRows(rows1)
+
+       rows2 := sqlmock.NewRows([]string{"username", "soft", 
"shared_usernames"})
+       rows2.AddRow("user1", false, []byte("{}"))
+       mock.ExpectQuery("SELECT c.username, 
c.soft").WithArgs("ALL").WillReturnRows(rows2)
+       mock.ExpectCommit()
+
+       testSCCs := getTestSSCs()
+       var sids []int64
+       sids = append(sids, int64(*testSCCs[0].ServerID))
+       code, usrErr, sysErr := checkExistingServer(db.MustBegin().Tx, sids, 
"user1")
+       if usrErr != nil || sysErr != nil {
+               t.Errorf("unable to check if server exists")
+       }
+       if code != http.StatusOK {
+               t.Errorf("Failed")

Review Comment:
   Same here, it would be helpful to know wat the actual status code was



##########
traffic_ops/traffic_ops_golang/servercapability/servercapability_test.go:
##########
@@ -0,0 +1,145 @@
+package servercapability
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+       "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/test"
+       "strings"
+       "testing"
+       "time"
+
+       "github.com/apache/trafficcontrol/lib/go-tc"
+       "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+       "github.com/jmoiron/sqlx"
+       "gopkg.in/DATA-DOG/go-sqlmock.v1"
+)
+
+func getTestSCs() []tc.ServerCapability {
+       scs := []tc.ServerCapability{}
+       testSC := tc.ServerCapability{
+               Name:        "test",
+               LastUpdated: &tc.TimeNoMod{Time: time.Now()},
+       }
+       scs = append(scs, testSC)
+
+       testSC1 := testSC
+       testSC1.Name = "blah"
+       scs = append(scs, testSC1)
+
+       return scs
+}
+
+func TestReadSCs(t *testing.T) {
+       mockDB, mock, err := sqlmock.New()
+       if err != nil {
+               t.Fatalf("an error '%s' was not expected when opening a stub 
database connection", err)
+       }
+       defer mockDB.Close()
+
+       db := sqlx.NewDb(mockDB, "sqlmock")
+       defer db.Close()
+
+       testSCs := getTestSCs()
+       rows := sqlmock.NewRows([]string{"name", "last_updated"})
+       for _, ts := range testSCs {
+               rows = rows.AddRow(
+                       ts.Name,
+                       ts.LastUpdated,
+               )
+       }
+       mock.ExpectBegin()
+       mock.ExpectQuery("SELECT").WillReturnRows(rows)
+       mock.ExpectCommit()
+
+       reqInfo := api.APIInfo{Tx: db.MustBegin(), Params: 
map[string]string{"name": "test"}}
+       obj := TOServerCapability{
+               APIInfoImpl:      api.APIInfoImpl{ReqInfo: &reqInfo},
+               ServerCapability: tc.ServerCapability{},
+       }
+
+       scs, userErr, sysErr, _, _ := obj.Read(nil, false)
+       if userErr != nil || sysErr != nil {
+               t.Errorf("Read expected: no errors, actual: %v %v", userErr, 
sysErr)
+       }
+
+       if len(scs) != 2 {
+               t.Errorf("Server Capability.Read expected: len(sc) == 1, 
actual: %v", len(scs))
+       }
+}
+
+func TestInterfaces(t *testing.T) {
+       var i interface{}
+       i = &TOServerCapability{}
+
+       if _, ok := i.(api.Creator); !ok {
+               t.Errorf("ServerServerCapability must be Creator")
+       }
+       if _, ok := i.(api.Reader); !ok {
+               t.Errorf("ServerServerCapability must be Reader")
+       }
+       if _, ok := i.(api.Deleter); !ok {
+               t.Errorf("ServerServerCapability must be Deleter")
+       }
+       if _, ok := i.(api.Identifier); !ok {
+               t.Errorf("ServerServerCapability must be Identifier")
+       }
+}
+
+func TestFuncs(t *testing.T) {
+       testTOSC := TOServerCapability{}
+       if strings.Index(testTOSC.SelectQuery(), "SELECT") != 0 {
+               t.Errorf("expected selectQuery to start with SELECT")
+       }
+       if strings.Index(testTOSC.InsertQuery(), "INSERT") != 0 {
+               t.Errorf("expected insertQuery to start with INSERT")
+       }
+       if strings.Index(testTOSC.updateQuery(), "UPDATE") != 0 {
+               t.Errorf("expected updateQuery to start with UPDATE")
+       }
+       if strings.Index(testTOSC.DeleteQuery(), "DELETE") != 0 {
+               t.Errorf("expected deleteQuery to start with DELETE")
+       }
+}
+
+func TestValidate(t *testing.T) {
+       var errs []error
+       // Negative test case
+       testSC := tc.ServerCapability{
+               Name: "",
+       }
+       testTOSC := TOServerCapability{}
+       testTOSC.ServerCapability = testSC
+       err, _ := testTOSC.Validate()
+       errs = test.SortErrors(test.SplitErrors(err))
+       if len(errs) < 0 {
+               t.Errorf(`expected errors: %v,  got no errors`, errs)
+       }
+
+       // Positive test case
+       testSCs := getTestSCs()
+       for _, val := range testSCs {
+               testTOSC.ServerCapability = val
+               err, _ := testTOSC.Validate()
+               errs = test.SortErrors(test.SplitErrors(err))
+       }
+       if len(errs) < 0 {

Review Comment:
   Shouldn't this be `> 0`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to