Skip to content

Commit

Permalink
Removed TryIfModifiedSinceQuery from servicecategories.go and used th…
Browse files Browse the repository at this point in the history
…e one from ims.go
  • Loading branch information
jagan-parthiban committed Jul 9, 2023
1 parent 8a08e3b commit cfc12bd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 123 deletions.
66 changes: 10 additions & 56 deletions traffic_ops/traffic_ops_golang/servicecategory/servicecategories.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import (
"time"

"github.com/apache/trafficcontrol/lib/go-log"
"github.com/apache/trafficcontrol/lib/go-rfc"
"github.com/apache/trafficcontrol/lib/go-tc"
"github.com/apache/trafficcontrol/lib/go-tc/tovalidate"
"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/dbhelpers"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/util/ims"

validation "github.com/go-ozzo/ozzo-validation"
"github.com/jmoiron/sqlx"
Expand Down Expand Up @@ -264,7 +264,7 @@ func GetServiceCategory(tx *sqlx.Tx, params map[string]string, useIMS bool, head
}

if useIMS {
runSecond, maxTime = TryIfModifiedSinceQuery(header, tx, where, queryValues)
runSecond, maxTime = ims.TryIfModifiedSinceQuery(tx, header, queryValues, SelectMaxLastUpdatedQuery(where))
if !runSecond {
log.Debugln("IMS HIT")
return scList, maxTime, http.StatusNotModified, nil, nil
Expand All @@ -291,60 +291,6 @@ func GetServiceCategory(tx *sqlx.Tx, params map[string]string, useIMS bool, head
return scList, maxTime, http.StatusOK, nil, nil
}

// TryIfModifiedSinceQuery [Version : V5] function receives transactions and header from GetServiceCategory function and returns bool value if status is not modified.
func TryIfModifiedSinceQuery(header http.Header, tx *sqlx.Tx, where string, queryValues map[string]interface{}) (bool, time.Time) {
var max time.Time
var imsDate time.Time
var ok bool
imsDateHeader := []string{}
runSecond := true
dontRunSecond := false

if header == nil {
return runSecond, max
}

imsDateHeader = header[rfc.IfModifiedSince]
if len(imsDateHeader) == 0 {
return runSecond, max
}

if imsDate, ok = rfc.ParseHTTPDate(imsDateHeader[0]); !ok {
log.Warnf("IMS request header date '%s' not parsable", imsDateHeader[0])
return runSecond, max
}

imsQuery := `SELECT max(last_updated) as t from service_category sc`
query := imsQuery + where
rows, err := tx.NamedQuery(query, queryValues)

if errors.Is(err, sql.ErrNoRows) {
return dontRunSecond, max
}

if err != nil {
log.Warnf("Couldn't get the max last updated time: %v", err)
return runSecond, max
}

defer rows.Close()
// This should only ever contain one row
if rows.Next() {
v := time.Time{}
if err = rows.Scan(&v); err != nil {
log.Warnf("Failed to parse the max time stamp into a struct %v", err)
return runSecond, max
}

max = v
// The request IMS time is later than the max of (lastUpdated, deleted_time)
if imsDate.After(v) {
return dontRunSecond, max
}
}
return runSecond, max
}

// CreateServiceCategory [Version : V5] function creates the service category with the passed name.
func CreateServiceCategory(w http.ResponseWriter, r *http.Request) {
inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
Expand Down Expand Up @@ -506,3 +452,11 @@ func readAndValidateJsonStruct(r *http.Request) (tc.ServiceCategoryV5, error) {
}
return sc, nil
}

// SelectMaxLastUpdatedQuery used for TryIfModifiedSinceQuery()
func SelectMaxLastUpdatedQuery(where string) string {
return `SELECT max(t) from (
SELECT max(last_updated) as t from service_category sc ` + where +
` UNION ALL
select max(last_updated) as t from last_deleted l where l.table_name='service_category') as res`
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,73 +30,6 @@ import (
"gopkg.in/DATA-DOG/go-sqlmock.v1"
)

func TestTryIfModifiedSinceQuery(t *testing.T) {

type testStruct struct {
ifModifiedSince time.Time
setHeader bool
setImsDateHeader bool
expected bool
}

var testData = []testStruct{

// When header is not set, runSecond must be true
{time.Time{}, false, false, true},

// When header set but header[If-Modified-Since] is not set, runSecond must be true
{time.Time{}, true, false, true},

// When header set and header[If-Modified-Since] is set, but incorrect time is given then runSecond must be true
{time.Time{}, true, true, true},

// When header set and header[If-Modified-Since] is set with correct time, and If-Modified_since < Max(last_Updated) then runSecond must be false
{time.Now().AddDate(0, 00, 01), true, true, false},

// When header set and header[If-Modified-Since] is set with correct time, and If-Modified_since > Max(last_Updated) then runSecond must be true
{time.Now().AddDate(0, 00, -01), true, true, true},
}

var header http.Header
lastUpdated := time.Now()
for i, _ := range testData {

mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%v' was not expected when opening a stub database connection", err)
}
defer mockDB.Close()

db := sqlx.NewDb(mockDB, "sqlmock")
defer db.Close()

if testData[i].setHeader {
header = make(http.Header)
}

if testData[i].setImsDateHeader {
timeValue := testData[i].ifModifiedSince.Format("Mon, 02 Jan 2006 15:04:05 MST")
header.Set(rfc.IfModifiedSince, timeValue)
}

mock.ExpectBegin()
rows := sqlmock.NewRows([]string{"t"})
rows.AddRow(lastUpdated)
mock.ExpectQuery("SELECT").WithArgs().WillReturnRows(rows)

where := ""
queryValues := map[string]interface{}{}

runSecond, _ := TryIfModifiedSinceQuery(header, db.MustBegin(), where, queryValues)

if testData[i].expected != runSecond {
t.Errorf("Expected runSecond result doesn't match, got: %t; expected: %t", runSecond, testData[i].expected)
}

}

}

func TestGetServiceCategory(t *testing.T) {

type testStruct struct {
Expand Down

0 comments on commit cfc12bd

Please sign in to comment.