Fix registration of types for DMaaP Mediator 18/7518/1
authorelinuxhenrik <henrik.b.andersson@est.tech>
Mon, 10 Jan 2022 13:15:31 +0000 (14:15 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Mon, 10 Jan 2022 13:17:59 +0000 (14:17 +0100)
Issue-ID: NONRTRIC-702
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Change-Id: Iee9ac0cf831e216639f77d0a08aa0b7d54091aa5

dmaap-mediator-producer/Dockerfile
dmaap-mediator-producer/internal/config/registrator.go
dmaap-mediator-producer/internal/config/registrator_test.go
dmaap-mediator-producer/stub/ics/ics.go

index 1c7f45c..6d9b2b8 100644 (file)
@@ -30,7 +30,7 @@ RUN go build -o /dmaapmediatorproducer
 ##
 ## Deploy
 ##
-FROM gcr.io/distroless/base-debian10
+FROM gcr.io/distroless/base-debian11
 WORKDIR /
 ## Copy from "build" stage
 COPY --from=build /dmaapmediatorproducer .
index bac14e6..1dd0ad1 100644 (file)
@@ -73,7 +73,8 @@ func NewRegistratorImpl(infoCoordAddr string, client restclient.HTTPClient) *Reg
 
 func (r RegistratorImpl) RegisterTypes(jobTypes []TypeDefinition) error {
        for _, jobType := range jobTypes {
-               body := fmt.Sprintf(`{"info_job_data_schema": %v}`, jobType.TypeSchema)
+               s, _ := json.Marshal(jobType.TypeSchema)
+               body := fmt.Sprintf(`{"info_job_data_schema": %v}`, string(s))
                if error := restclient.Put(r.infoCoordinatorAddress+registerTypePath+url.PathEscape(jobType.Identity), []byte(body), r.httpClient); error != nil {
                        return error
                }
index d1b61d8..b2f10cc 100644 (file)
@@ -21,6 +21,7 @@
 package config
 
 import (
+       "encoding/json"
        "io/ioutil"
        "net/http"
        "testing"
@@ -39,9 +40,17 @@ func TestRegisterTypes(t *testing.T) {
                StatusCode: http.StatusCreated,
        }, nil)
 
+       schemaString := `{
+               "type": "object",
+               "properties": {},
+               "additionalProperties": false
+               }`
+       var schemaObj interface{}
+       json.Unmarshal([]byte(schemaString), &schemaObj)
+
        type1 := TypeDefinition{
                Identity:   "Type1",
-               TypeSchema: `{"type": "object","properties": {},"additionalProperties": false}`,
+               TypeSchema: schemaObj,
        }
        types := []TypeDefinition{type1}
 
@@ -60,7 +69,7 @@ func TestRegisterTypes(t *testing.T) {
        assertions.Equal("/data-producer/v1/info-types/Type1", actualRequest.URL.Path)
        assertions.Equal("application/json", actualRequest.Header.Get("Content-Type"))
        body, _ := ioutil.ReadAll(actualRequest.Body)
-       expectedBody := []byte(`{"info_job_data_schema": {"type": "object","properties": {},"additionalProperties": false}}`)
+       expectedBody := []byte(`{"info_job_data_schema": {"additionalProperties":false,"properties":{},"type":"object"}}`)
        assertions.Equal(expectedBody, body)
        clientMock.AssertNumberOfCalls(t, "Do", 1)
 }
index 0818d5e..87457c2 100644 (file)
@@ -23,6 +23,7 @@ package main
 import (
        "flag"
        "fmt"
+       "io/ioutil"
        "net/http"
 
        "github.com/gorilla/mux"
@@ -43,7 +44,7 @@ func handleTypeRegistration(w http.ResponseWriter, r *http.Request) {
        vars := mux.Vars(r)
        id, ok := vars["typeId"]
        if ok {
-               fmt.Println("Registered type ", id)
+               fmt.Printf("Registered type %v with schema: %v\n", id, readBody(r))
        }
 }
 
@@ -51,6 +52,14 @@ func handleProducerRegistration(w http.ResponseWriter, r *http.Request) {
        vars := mux.Vars(r)
        id, ok := vars["producerId"]
        if ok {
-               fmt.Println("Registered producer ", id)
+               fmt.Printf("Registered producer %v with data: %v\n", id, readBody(r))
        }
 }
+
+func readBody(r *http.Request) string {
+       b, readErr := ioutil.ReadAll(r.Body)
+       if readErr != nil {
+               return fmt.Sprintf("Unable to read body due to: %v", readErr)
+       }
+       return string(b)
+}