package net.javacomm.protocol;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import com.google.gson.JsonParseException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;



public class TestCBlock {

  private final static Logger log = LogManager.getLogger(TestCBlock.class);
  CBlock cblock;

  @BeforeEach
  public void setUp() {
    cblock = new CBlock();
  }



  @Test
  @DisplayName(value = "SERIALIZE")
  public void serialize() {
    cblock.setPseudoPassword("7e53c5fc-1a38-448f-bb16-1efc98c6ff06");
    long timestamp = 1776800055553l;
    cblock.setTimestamp(timestamp);
    String encoded = "{\"PPASSWORD\":\"7e53c5fc-1a38-448f-bb16-1efc98c6ff06\",\"TIMESTAMP\":1776800055553}";
    String result = cblock.toString();
    assertEquals(encoded, result);
  }



  @Test
  @DisplayName(value = "SERIALIZE | TIMESTAMP | FEHLT")
  public void serialize_TIMESTAMP_FEHLT() {
    cblock.setPseudoPassword("7e53c5fc-1a38-448f-bb16-1efc98c6ff06");
    try {
      cblock.toString();
      fail("Exception nicht geworfen, TIMESTAMP fehlt");
    }
    catch (Exception e) {
      assertTrue(true);
    }
  }



  @Test
  @DisplayName(value = "SERIALIZE | PPASSWORD | FEHLT")
  public void serialize_PPASSWORD_FEHLT() {
    long timestamp = 1776800055553l;
    cblock.setTimestamp(timestamp);

    try {
      cblock.toString();
      fail("Exception nicht geworfen, PPASSWORD fehlt");
    }
    catch (Exception e) {
      assertTrue(true);
    }

  }



  @Test
  @DisplayName(value = "DESERIALIZE")
  public void deserialize() {
    String encoded = "{\"PPASSWORD\":\"7e53c5fc-1a38-448f-bb16-1efc98c6ff06\",\"TIMESTAMP\":1776800055553}";

    cblock = CBlock.toCBlock(encoded);
    assertEquals("7e53c5fc-1a38-448f-bb16-1efc98c6ff06", cblock.getPseudoPassword(), "PPASSWORD");
    assertEquals(1776800055553l, cblock.getTimestamp(), "TIMESTAMP");

  }



  @Test
  @DisplayName(value = "DESERIALIZE | TIMESTAMP | FEHLT")
  public void deserialize_TIMESTAMP_FEHLT() {
    String encoded = "{\"PPASSWORD\":\"7e53c5fc-1a38-448f-bb16-1efc98c6ff06\"}";

    try {
      cblock = CBlock.toCBlock(encoded);
      fail("JsonParseException nicht geworfen, TIMESTAMP fehlt");
    }
    catch (JsonParseException e) {
      assertTrue(true);
    }

  }



  @Test
  @DisplayName(value = "DESERIALIZE | PPASSWORD | FEHLT")
  public void deserialize_PPASSWORD_FEHLT() {
    String encoded = "{\"TIMESTAMP\":1776800055553}";

    try {
      cblock = CBlock.toCBlock(encoded);
      fail("JsonParseException nicht geworfen, PPASSWORD fehlt");
    }
    catch (JsonParseException e) {
      assertTrue(true);
    }

  }

}
