From c8c4993ac9c40c298382be6fdd7c04858fa1b1fb Mon Sep 17 00:00:00 2001
From: philippe PICHET <p.pichet@matawan-mobility.com>
Date: Tue, 13 Aug 2024 10:37:42 +0200
Subject: [PATCH] GPS: revert the attributes and use the headers instead.

---
 pkg/gps/GpsMessage.php            | 21 ++-------------------
 pkg/gps/GpsProducer.php           |  4 ++--
 pkg/gps/Tests/GpsMessageTest.php  | 19 ++++---------------
 pkg/gps/Tests/GpsProducerTest.php |  8 ++++----
 4 files changed, 12 insertions(+), 40 deletions(-)

diff --git a/pkg/gps/GpsMessage.php b/pkg/gps/GpsMessage.php
index 684a7ba7b..b7e2bf484 100644
--- a/pkg/gps/GpsMessage.php
+++ b/pkg/gps/GpsMessage.php
@@ -24,11 +24,6 @@ class GpsMessage implements Message, \JsonSerializable
      */
     private $headers;
 
-    /**
-     * @var array
-     */
-    private $attributes;
-
     /**
      * @var bool
      */
@@ -39,12 +34,11 @@ class GpsMessage implements Message, \JsonSerializable
      */
     private $nativeMessage;
 
-    public function __construct(string $body = '', array $properties = [], array $headers = [], array $attributes = [])
+    public function __construct(string $body = '', array $properties = [], array $headers = [])
     {
         $this->body = $body;
         $this->properties = $properties;
         $this->headers = $headers;
-        $this->attributes = $attributes;
 
         $this->redelivered = false;
     }
@@ -157,7 +151,6 @@ public function jsonSerialize(): array
             'body' => $this->getBody(),
             'properties' => $this->getProperties(),
             'headers' => $this->getHeaders(),
-            'attributes' => $this->getAttributes(),
         ];
     }
 
@@ -168,7 +161,7 @@ public static function jsonUnserialize(string $json): self
             throw new \InvalidArgumentException(sprintf('The malformed json given. Error %s and message %s', json_last_error(), json_last_error_msg()));
         }
 
-        return new self($data['body'] ?? $json, $data['properties'] ?? [], $data['headers'] ?? [], $data['attributes'] ?? []);
+        return new self($data['body'] ?? $json, $data['properties'] ?? [], $data['headers'] ?? []);
     }
 
     public function getNativeMessage(): ?GoogleMessage
@@ -180,14 +173,4 @@ public function setNativeMessage(?GoogleMessage $message = null): void
     {
         $this->nativeMessage = $message;
     }
-
-    public function setAttributes(array $attributes): void
-    {
-        $this->attributes = $attributes;
-    }
-
-    public function getAttributes(): array
-    {
-        return $this->attributes;
-    }
 }
diff --git a/pkg/gps/GpsProducer.php b/pkg/gps/GpsProducer.php
index b36fad19d..7e307636f 100644
--- a/pkg/gps/GpsProducer.php
+++ b/pkg/gps/GpsProducer.php
@@ -40,8 +40,8 @@ public function send(Destination $destination, Message $message): void
 
         $params = ['data' => json_encode($message)];
 
-        if (count($message->getAttributes()) > 0) {
-            $params['attributes'] = $message->getAttributes();
+        if (count($message->getHeaders()) > 0) {
+            $params['attributes'] = $message->getHeaders();
         }
 
         $topic->publish($params);
diff --git a/pkg/gps/Tests/GpsMessageTest.php b/pkg/gps/Tests/GpsMessageTest.php
index ee9e652cd..8182333cc 100644
--- a/pkg/gps/Tests/GpsMessageTest.php
+++ b/pkg/gps/Tests/GpsMessageTest.php
@@ -18,14 +18,14 @@ public function testCouldSetGetNativeMessage()
 
     public function testColdBeSerializedToJson()
     {
-        $message = new GpsMessage('theBody', ['thePropFoo' => 'thePropFooVal'], ['theHeaderFoo' => 'theHeaderFooVal'], ['theAttributeFoo' => 'theAttributeFooVal']);
+        $message = new GpsMessage('theBody', ['thePropFoo' => 'thePropFooVal'], ['theHeaderFoo' => 'theHeaderFooVal']);
 
-        $this->assertEquals('{"body":"theBody","properties":{"thePropFoo":"thePropFooVal"},"headers":{"theHeaderFoo":"theHeaderFooVal"},"attributes":{"theAttributeFoo":"theAttributeFooVal"}}', json_encode($message));
+        $this->assertEquals('{"body":"theBody","properties":{"thePropFoo":"thePropFooVal"},"headers":{"theHeaderFoo":"theHeaderFooVal"}}', json_encode($message));
     }
 
     public function testCouldBeUnserializedFromJson()
     {
-        $message = new GpsMessage('theBody', ['thePropFoo' => 'thePropFooVal'], ['theHeaderFoo' => 'theHeaderFooVal'], ['theAttributeFoo' => 'theAttributeFooVal']);
+        $message = new GpsMessage('theBody', ['thePropFoo' => 'thePropFooVal'], ['theHeaderFoo' => 'theHeaderFooVal']);
 
         $json = json_encode($message);
 
@@ -40,7 +40,7 @@ public function testCouldBeUnserializedFromJson()
 
     public function testMessageEntityCouldBeUnserializedFromJson()
     {
-        $json = '{"body":"theBody","properties":{"thePropFoo":"thePropFooVal"},"headers":{"theHeaderFoo":"theHeaderFooVal"},"attributes":{"theAttributeFoo":"theAttributeFooVal"}}';
+        $json = '{"body":"theBody","properties":{"thePropFoo":"thePropFooVal"},"headers":{"theHeaderFoo":"theHeaderFooVal"}}';
 
         $unserializedMessage = GpsMessage::jsonUnserialize($json);
 
@@ -49,7 +49,6 @@ public function testMessageEntityCouldBeUnserializedFromJson()
         $this->assertEquals($decoded['body'], $unserializedMessage->getBody());
         $this->assertEquals($decoded['properties'], $unserializedMessage->getProperties());
         $this->assertEquals($decoded['headers'], $unserializedMessage->getHeaders());
-        $this->assertEquals($decoded['attributes'], $unserializedMessage->getAttributes());
     }
 
     public function testMessagePayloadCouldBeUnserializedFromJson()
@@ -62,7 +61,6 @@ public function testMessagePayloadCouldBeUnserializedFromJson()
         $this->assertEquals($json, $unserializedMessage->getBody());
         $this->assertEquals([], $unserializedMessage->getProperties());
         $this->assertEquals([], $unserializedMessage->getHeaders());
-        $this->assertEquals([], $unserializedMessage->getAttributes());
     }
 
     public function testThrowIfMalformedJsonGivenOnUnsterilizedFromJson()
@@ -72,13 +70,4 @@ public function testThrowIfMalformedJsonGivenOnUnsterilizedFromJson()
 
         GpsMessage::jsonUnserialize('{]');
     }
-
-    public function testGetAttributes()
-    {
-        $message = new GpsMessage('the body', [], [], ['key1' => 'value1']);
-
-        $attributes = $message->getAttributes();
-
-        $this->assertSame(['key1' => 'value1'], $attributes);
-    }
 }
diff --git a/pkg/gps/Tests/GpsProducerTest.php b/pkg/gps/Tests/GpsProducerTest.php
index 34dc2504f..3079d3c7c 100644
--- a/pkg/gps/Tests/GpsProducerTest.php
+++ b/pkg/gps/Tests/GpsProducerTest.php
@@ -34,7 +34,7 @@ public function testShouldSendMessage()
             ->expects($this->once())
             ->method('publish')
             ->with($this->identicalTo([
-                'data' => '{"body":"","properties":[],"headers":[],"attributes":[]}',
+                'data' => '{"body":"","properties":[],"headers":[]}',
             ]));
 
         $client = $this->createPubSubClientMock();
@@ -56,16 +56,16 @@ public function testShouldSendMessage()
         $producer->send($topic, $message);
     }
 
-    public function testShouldSendMessageWithAttributes()
+    public function testShouldSendMessageWithHeaders()
     {
         $topic = new GpsTopic('topic-name');
-        $message = new GpsMessage('', [], [], ['key1' => 'value1']);
+        $message = new GpsMessage('', [], ['key1' => 'value1']);
 
         $gtopic = $this->createGTopicMock();
         $gtopic
             ->expects($this->once())
             ->method('publish')
-            ->with($this->identicalTo(['data' => '{"body":"","properties":[],"headers":[],"attributes":{"key1":"value1"}}', 'attributes' => ['key1' => 'value1']]))
+            ->with($this->identicalTo(['data' => '{"body":"","properties":[],"headers":{"key1":"value1"}}', 'attributes' => ['key1' => 'value1']]))
         ;
 
         $client = $this->createPubSubClientMock();