dimanche 25 septembre 2016

Junit test in spring to check mapper

I am new to programming. I have written a jUnit test to test my mapper. But I have some errors. I am checking the same data in all the documents. The first one works fine. But the second and the third test function doesnt work well. My code is:

package com.my.reader.abc.mapping;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.prime.common.json.JacksonUtils;
import com.prime.json.modifier.AttributeRemover;

public class AbcToJsonMapperTest {

    private AbcToJsonMapper mapper;
    private List<String> removeList;
    private AttributeRemover remover;

    @Before
    public void setup() {
        mapper = new AbcToJsonMapper();
        removeList = new ArrayList<String>();
        // ignore this attributes
        removeList.add("systemInfo.upload.timestamp");
        remover = new AttributeRemover();
        remover.setAttributes(removeList);

    }

    @Test
    public void mappingTest1() throws JsonProcessingException, IOException {
        JsonNode pc_doc = readJsonFromFile("/mapping/abc-doc1.json");
        JsonNode processedDoc = pc_doc.get("wsTweetAPI").get("tweets").get(0);
        JsonNode mapped_doc = readJsonFromFile("/mapping/abc-doc1-mapped.json");
        Message<JsonNode> msg = MessageBuilder.withPayload(processedDoc).build();
        JsonNode result = mapper.process(msg);
        remover.process(result);
        removeList.add("systemInfo.upload.timestamp");
        remover.process(mapped_doc);
        System.out.println(result.toString());
        System.out.println(mapped_doc.toString());
        Assert.assertTrue(JacksonUtils.equals(result, mapped_doc));
    }

    @Test
    public void mappingTest2() throws JsonProcessingException, IOException {
        JsonNode pc_doc = readJsonFromFile("/mapping/abc-doc2.json");
        JsonNode processedDoc = pc_doc.get("wsTweetAPI").get("tweets").get(0);
        JsonNode mapped_doc = readJsonFromFile("/mapping/abc-doc2-mapped.json");
        Message<JsonNode> msg = MessageBuilder.withPayload(processedDoc).build();
        JsonNode result = mapper.process(msg);
        remover.process(result);
        removeList.add("systemInfo.upload.timestamp");
        remover.process(mapped_doc);
        System.out.println(result.toString());
        System.out.println(mapped_doc.toString());
        Assert.assertTrue(JacksonUtils.equals(result, mapped_doc));
    }

    @Test
    public void mappingTest3() throws JsonProcessingException, IOException {
        JsonNode pc_doc = readJsonFromFile("/mapping/abc-doc3.json");
        JsonNode processedDoc = pc_doc.get("wsTweetAPI").get("tweets").get(0);
        JsonNode mapped_doc = readJsonFromFile("/mapping/abc-doc3-mapped.json");
        Message<JsonNode> msg = MessageBuilder.withPayload(processedDoc).build();
        JsonNode result = mapper.process(msg);
        remover.process(result);
        removeList.add("systemInfo.upload.timestamp");
        remover.process(mapped_doc);
        System.out.println(result.toString());
        System.out.println(mapped_doc.toString());
        Assert.assertFalse(JacksonUtils.equals(result, mapped_doc));
    }

    private JsonNode readJsonFromFile(String jsonPath)
          throws JsonProcessingException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode node = objectMapper.readTree(getClass()
              .getResourceAsStream(jsonPath));
        return node;
    }

}

I am using the same data in document 1,2 and 3. However, i have errors only with the second and third function. Could it be that my function is wrong? I need to check three types of documents and only when all three documents yield correct result, then the testing should be positive. Initially the test was for one document and the code was:

package com.my.reader.abc.mapping;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.prime.common.json.JacksonUtils;
import com.prime.json.modifier.AttributeRemover;

public class AbcToJsonMapperTest {

  private AbcToJsonMapper mapper;
  private List<String> removeList;
  private AttributeRemover remover;

  @Before
  public void setup() {
    mapper = new AbcToJsonMapper();
    removeList = new ArrayList<String>();
    // ignore this attributes
    removeList.add("systemInfo.upload.timestamp");
    remover = new AttributeRemover();
    remover.setAttributes(removeList);

  }

  @Test
  public void mappingTest() throws JsonProcessingException, IOException {
    JsonNode pc_doc = readJsonFromFile("/mapping/abc-doc1.json");
    JsonNode mapped_doc = readJsonFromFile("/mapping/abc-doc1-mapped.json");
    JsonNode processedDoc = pc_doc.get("wsTweetAPI").get("tweets").get(0);
    assertOutput(processedDoc, mapped_doc);
  }

  private void assertOutput(JsonNode pc_doc, JsonNode mapped_doc) {
    Message<JsonNode> msg = MessageBuilder.withPayload(pc_doc).build();
    JsonNode result = mapper.process(msg);
    remover.process(result);
    removeList.add("systemInfo.upload.timestamp");
    remover.process(mapped_doc);
    System.out.println(result.toString());
    System.out.println(mapped_doc.toString());
    Assert.assertTrue(JacksonUtils.equals(result, mapped_doc));
  }

  private JsonNode readJsonFromFile(String jsonPath)
        throws JsonProcessingException, IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode result = objectMapper.readTree(getClass()
          .getResourceAsStream(jsonPath));
    return result;
  }

}

Aucun commentaire:

Enregistrer un commentaire