<commit_before>/*
 * Copyright 2014-2017 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.netflix.atlas.webapi

import com.netflix.atlas.core.model.Datapoint
import org.scalatest.FunSuite


class PublishApiJsonSuite extends FunSuite {

  test("encode and decode datapoint") {
    val original = Datapoint(Map("name" -> "foo", "id" -> "bar"), 42L, 1024.0)
    val decoded = PublishApi.decodeDatapoint(PublishApi.encodeDatapoint(original))
    assert(original === decoded)
  }

  test("encode and decode batch") {
    val commonTags = Map("id" -> "bar")
    val original = List(Datapoint(Map("name" -> "foo"), 42L, 1024.0))
    val decoded = PublishApi.decodeBatch(PublishApi.encodeBatch(commonTags, original))
    assert(original.map(d => d.copy(tags = d.tags ++ commonTags)) === decoded)
  }

  test("decode batch empty") {
    val decoded = PublishApi.decodeBatch("{}")
    assert(decoded.size === 0)
  }

  test("decode with legacy array value") {
    val expected = Datapoint(Map("name" -> "foo"), 42L, 1024.0)
    val decoded = PublishApi.decodeDatapoint(
      """{"tags":{"name":"foo"},"timestamp":42,"values":[1024.0]}""")
    assert(expected === decoded)
  }

  test("decode legacy batch empty") {
    val decoded = PublishApi.decodeBatch("""
      {
        "tags": {},
        "metrics": []
      }
      """)
    assert(decoded.size === 0)
  }

  test("decode legacy batch no tags") {
    val decoded = PublishApi.decodeBatch("""
      {
        "metrics": []
      }
      """)
    assert(decoded.size === 0)
  }

  test("decode legacy batch with tags before") {
    val decoded = PublishApi.decodeBatch("""
      {
        "tags": {
          "foo": "bar"
        },
        "metrics": [
          {
            "tags": {"name": "test"},
            "start": 123456789,
            "values": [1.0]
          }
        ]
      }
      """)
    assert(decoded.size === 1)
    assert(decoded.head.tags === Map("name" -> "test", "foo" -> "bar"))
  }

  test("decode legacy batch with tags after") {
    val decoded = PublishApi.decodeBatch("""
      {
        "metrics": [
          {
            "tags": {"name": "test"},
            "start": 123456789,
            "values": [1.0]
          }
        ],
        "tags": {
          "foo": "bar"
        }
      }
      """)
    assert(decoded.size === 1)
    assert(decoded.head.tags === Map("name" -> "test", "foo" -> "bar"))
  }

  test("decode legacy batch no tags metric") {
    val decoded = PublishApi.decodeBatch("""
      {
        "metrics": [
          {
            "tags": {"name": "test"},
            "start": 123456789,
            "values": [1.0]
          }
        ]
      }
      """)
    assert(decoded.size === 1)
  }

  test("decode list empty") {
    val decoded = PublishApi.decodeList("""
      []
      """)
    assert(decoded.size === 0)
  }

  test("decode list") {
    val decoded = PublishApi.decodeList("""
      [
        {
          "tags": {"name": "test"},
          "timestamp": 123456789,
          "values": 1.0
        }
      ]
      """)
    assert(decoded.size === 1)
  }

  test("decode list with unknown key") {
    val decoded = PublishApi.decodeList("""
      [
        {
          "tags": {"name": "test"},
          "timestamp": 123456789,
          "unknown": {},
          "values": 1.0
        },
        {
          "tags": {"name": "test"},
          "timestamp": 123456789,
          "unknown": {"a":{"b":"c"},"b":[1,2,3]},
          "values": 1.0
        },
        {
          "tags": {"name": "test"},
          "timestamp": 123456789,
          "unknown": [1,2,3],
          "values": 1.0
        },
        {
          "tags": {"name": "test"},
          "timestamp": 123456789,
          "unknown": "foo",
          "values": 1.0
        }
      ]
      """)
    assert(decoded.size === 4)
  }

  test("decode batch bad object") {
    intercept[IllegalArgumentException] {
      PublishApi.decodeBatch("""{"foo":"bar"}""")
    }
  }
}
<commit_msg>add test case for decoding datapoint (#608)<commit_after>/*
 * Copyright 2014-2017 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.netflix.atlas.webapi

import com.netflix.atlas.core.model.Datapoint
import com.netflix.atlas.json.Json
import org.scalatest.FunSuite


class PublishApiJsonSuite extends FunSuite {

  test("encode and decode datapoint") {
    val original = Datapoint(Map("name" -> "foo", "id" -> "bar"), 42L, 1024.0)
    val decoded = PublishApi.decodeDatapoint(PublishApi.encodeDatapoint(original))
    assert(original === decoded)
  }

  test("encode and decode batch") {
    val commonTags = Map("id" -> "bar")
    val original = List(Datapoint(Map("name" -> "foo"), 42L, 1024.0))
    val decoded = PublishApi.decodeBatch(PublishApi.encodeBatch(commonTags, original))
    assert(original.map(d => d.copy(tags = d.tags ++ commonTags)) === decoded)
  }

  test("decode batch empty") {
    val decoded = PublishApi.decodeBatch("{}")
    assert(decoded.size === 0)
  }

  test("decode with legacy array value") {
    val expected = Datapoint(Map("name" -> "foo"), 42L, 1024.0)
    val decoded = PublishApi.decodeDatapoint(
      """{"tags":{"name":"foo"},"timestamp":42,"values":[1024.0]}""")
    assert(expected === decoded)
  }

  test("decode legacy batch empty") {
    val decoded = PublishApi.decodeBatch("""
      {
        "tags": {},
        "metrics": []
      }
      """)
    assert(decoded.size === 0)
  }

  test("decode legacy batch no tags") {
    val decoded = PublishApi.decodeBatch("""
      {
        "metrics": []
      }
      """)
    assert(decoded.size === 0)
  }

  test("decode legacy batch with tags before") {
    val decoded = PublishApi.decodeBatch("""
      {
        "tags": {
          "foo": "bar"
        },
        "metrics": [
          {
            "tags": {"name": "test"},
            "start": 123456789,
            "values": [1.0]
          }
        ]
      }
      """)
    assert(decoded.size === 1)
    assert(decoded.head.tags === Map("name" -> "test", "foo" -> "bar"))
  }

  test("decode legacy batch with tags after") {
    val decoded = PublishApi.decodeBatch("""
      {
        "metrics": [
          {
            "tags": {"name": "test"},
            "start": 123456789,
            "values": [1.0]
          }
        ],
        "tags": {
          "foo": "bar"
        }
      }
      """)
    assert(decoded.size === 1)
    assert(decoded.head.tags === Map("name" -> "test", "foo" -> "bar"))
  }

  test("decode legacy batch no tags metric") {
    val decoded = PublishApi.decodeBatch("""
      {
        "metrics": [
          {
            "tags": {"name": "test"},
            "start": 123456789,
            "values": [1.0]
          }
        ]
      }
      """)
    assert(decoded.size === 1)
  }

  test("decode list empty") {
    val decoded = PublishApi.decodeList("""
      []
      """)
    assert(decoded.size === 0)
  }

  test("decode list") {
    val decoded = PublishApi.decodeList("""
      [
        {
          "tags": {"name": "test"},
          "timestamp": 123456789,
          "values": 1.0
        }
      ]
      """)
    assert(decoded.size === 1)
  }

  test("decode list with unknown key") {
    val decoded = PublishApi.decodeList("""
      [
        {
          "tags": {"name": "test"},
          "timestamp": 123456789,
          "unknown": {},
          "values": 1.0
        },
        {
          "tags": {"name": "test"},
          "timestamp": 123456789,
          "unknown": {"a":{"b":"c"},"b":[1,2,3]},
          "values": 1.0
        },
        {
          "tags": {"name": "test"},
          "timestamp": 123456789,
          "unknown": [1,2,3],
          "values": 1.0
        },
        {
          "tags": {"name": "test"},
          "timestamp": 123456789,
          "unknown": "foo",
          "values": 1.0
        }
      ]
      """)
    assert(decoded.size === 4)
  }

  test("decode batch bad object") {
    intercept[IllegalArgumentException] {
      PublishApi.decodeBatch("""{"foo":"bar"}""")
    }
  }

  test("decode list from encoded datapoint") {
    val vs = List(Datapoint(Map("a" -> "b"), 0L, 42.0))
    val decoded = PublishApi.decodeList(Json.encode(vs))
    assert(decoded.size === 1)
  }

  test("decode list from PublishApi.encoded datapoint") {
    val vs = "[" + PublishApi.encodeDatapoint(Datapoint(Map("a" -> "b"), 0L, 42.0)) + "]"
    val decoded = PublishApi.decodeList(vs)
    assert(decoded.size === 1)
  }
}

{'fraction_non_alphanumeric': 0.17047248237398716, 'fraction_numerical': 0.03146374829001368, 'mean_word_length': 2.1222076215505914, 'pattern_counts': {'":': 108, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '682178', 'n_tokens_mistral': 3206, 'n_tokens_neox': 2951, 'n_words': 692}