@Override
    public ObjectNode encode(Group group, CodecContext context) {
        checkNotNull(group, "Group cannot be null");
        ObjectNode result = context.mapper().createObjectNode()
                // a Group id should be an unsigned integer
                .put(ID, Integer.toUnsignedLong(group.id().id()))
                .put(STATE, group.state().toString())
                .put(LIFE, group.life())
                .put(PACKETS, group.packets())
                .put(BYTES, group.bytes())
                .put(REFERENCE_COUNT, group.referenceCount())
                .put(TYPE, group.type().toString())
                .put(DEVICE_ID, group.deviceId().toString());

        if (group.appId() != null) {
            result.put(APP_ID, group.appId().name());
        }

        if (group.appCookie() != null) {
            result.put(APP_COOKIE, group.appCookie().toString());
        }

        if (group.givenGroupId() != null) {
            // a given Group id should be an unsigned integer
            result.put(GIVEN_GROUP_ID, Integer.toUnsignedLong(group.givenGroupId()));
        }

        ArrayNode buckets = context.mapper().createArrayNode();
        group.buckets().buckets().forEach(bucket -> {
            ObjectNode bucketJson = context.codec(GroupBucket.class).encode(bucket, context);
            buckets.add(bucketJson);
        });
        result.set(BUCKETS, buckets);
        return result;
    }
@Test
    public void codecEncodeTest() {
        GroupBucket bucket1 = DefaultGroupBucket.createAllGroupBucket(DefaultTrafficTreatment.emptyTreatment());
        GroupBucket bucket2 = DefaultGroupBucket.createAllGroupBucket(DefaultTrafficTreatment.emptyTreatment());
        GroupBucket bucket3 = DefaultGroupBucket.createIndirectGroupBucket(DefaultTrafficTreatment.emptyTreatment());
        GroupBuckets allBuckets = new GroupBuckets(ImmutableList.of(bucket1, bucket2));
        GroupBuckets indirectBuckets = new GroupBuckets(ImmutableList.of(bucket3));

        DefaultGroup group = new DefaultGroup(
                new GroupId(1),
                NetTestTools.did("d1"),
                ALL,
                allBuckets);
        DefaultGroup group1 = new DefaultGroup(
                new GroupId(2),
                NetTestTools.did("d2"),
                INDIRECT,
                indirectBuckets);

        MockCodecContext context = new MockCodecContext();
        GroupCodec codec = new GroupCodec();
        ObjectNode groupJson = codec.encode(group, context);

        ObjectNode groupJsonIndirect = codec.encode(group1, context);

        assertThat(groupJson, matchesGroup(group));
        assertThat(groupJsonIndirect, matchesGroup(group1));
    }