Skip to content

Commit

Permalink
feat: fake deployment for oasis service (#142)
Browse files Browse the repository at this point in the history
* feat: fake deployment for oasis service
issue: #133

* fix: adjust names.
issue: #133
  • Loading branch information
CodeBear801 authored Feb 3, 2020
1 parent 037a545 commit dc103fa
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
2 changes: 1 addition & 1 deletion integration/cmd/oasis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func main() {
mux := http.NewServeMux()

oasisService := oasis.New(flags.osrmBackendEndpoint)
mux.Handle("/oasis", oasisService)
mux.Handle("/oasis/v1/earliest/", oasisService)

// listen
listening := ":" + strconv.Itoa(flags.listenPort)
Expand Down
45 changes: 43 additions & 2 deletions integration/oasis/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package oasis

import (
"encoding/json"
"fmt"
"net/http"

"github.com/Telenav/osrm-backend/integration/pkg/api/oasis"
"github.com/Telenav/osrm-backend/integration/pkg/api/search/nearbychargestation"
"github.com/golang/glog"
)

type Handler struct {
Expand All @@ -19,8 +21,18 @@ func New(osrmBackend string) *Handler {
}

func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200)
json.NewEncoder(w).Encode(generateFakeOasisResponse())
glog.Infof("Handle incoming request %s from remote addr %s", req.RequestURI, req.RemoteAddr)
w.WriteHeader(http.StatusOK)

oasisRequest, err := oasis.ParseRequestURL(req.URL)
if err != nil || len(oasisRequest.Coordinates) != 2 {
glog.Warning(err)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "%v", err)
return
}

json.NewEncoder(w).Encode(generateFakeOasisResponseBasedOnRequest(oasisRequest))
}

func generateFakeOasisResponse() *oasis.Response {
Expand Down Expand Up @@ -63,3 +75,32 @@ func generateFakeOasisResponse() *oasis.Response {

return r
}

func generateFakeOasisResponseWithSingleChargeStation(req *oasis.Request) *oasis.Response {
fakeSolution1 := new(oasis.Solution)
fakeSolution1.Distance = 90000.0
fakeSolution1.Duration = 30000.0
fakeSolution1.Weight = 3000.0
fakeSolution1.RemainingRage = 100000.0
fakeSolution1.WeightName = "duration"

fakeStation1 := new(oasis.ChargeStation)
address1 := new(nearbychargestation.Address)
latMedian := (req.Coordinates[0].Lat + req.Coordinates[1].Lat) / 2
lonMedian := (req.Coordinates[0].Lon + req.Coordinates[1].Lon) / 2
address1.GeoCoordinate = nearbychargestation.Coordinate{Latitude: latMedian, Longitude: lonMedian}
address1.NavCoordinates = append(address1.NavCoordinates, &nearbychargestation.Coordinate{Latitude: latMedian, Longitude: lonMedian})
fakeStation1.Address = append(fakeStation1.Address, address1)

fakeStation1.WaitTime = 0.0
fakeStation1.ChargeTime = 7200.0
fakeStation1.ChargeRange = req.MaxRange
fakeStation1.DetailURL = "url"
fakeSolution1.ChargeStations = append(fakeSolution1.ChargeStations, fakeStation1)

r := new(oasis.Response)
r.Code = "200"
r.Message = "Success."
r.Solutions = append(r.Solutions, fakeSolution1)
return r
}
12 changes: 6 additions & 6 deletions integration/pkg/api/oasis/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ func (r *Request) validate() error {
return errors.New(options.KeyCurrRange + " must be smaller or equal to " + options.KeyMaxRange + ".")
}

// CurrRange must be smaller or equal to MaxRange
if r.PreferLevel > r.MaxRange {
return errors.New(options.KeyPreferLevel + " must be smaller or equal to " + options.KeyMaxRange + ".")
// PreferLevel must be smaller to MaxRange
if r.PreferLevel >= r.MaxRange {
return errors.New(options.KeyPreferLevel + " must be smaller to " + options.KeyMaxRange + ".")
}

// CurrRange must be smaller or equal to MaxRange
if r.SafeLevel > r.MaxRange {
return errors.New(options.KeySafeLevel + " must be smaller or equal to " + options.KeyMaxRange + ".")
// SafeLevel must be smaller to MaxRange
if r.SafeLevel >= r.MaxRange {
return errors.New(options.KeySafeLevel + " must be smaller to " + options.KeyMaxRange + ".")
}

return nil
Expand Down

0 comments on commit dc103fa

Please sign in to comment.