package com.edisonren.nmm.model;

import com.edisonren.nmm.v1.Scenario;
import com.edisonren.nmm.v1.ScenarioInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.Serializable;

/**
 * Created by edison on 9/13/17.
 */
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize
public class NmmModel  implements Serializable {
    private static final long serialVersionUID = 22L;

    private Scenario scenario;
    private ScenarioInfo scenarioInfo;
    private Integer mockVer;
    private JsonNode response;

    public static class NmmModelBuilder {
        private Scenario scenario;
        private ScenarioInfo scenarioInfo;
        private Integer mockVer;
        private JsonNode response;

        public NmmModelBuilder() {}

        public NmmModelBuilder setScenario(Scenario scenario) {
            this.scenario = scenario;
            return this;
        }

        public NmmModelBuilder setScenarioInfo(ScenarioInfo scenarioInfo) {
            this.scenarioInfo = scenarioInfo;
            return this;
        }

        public NmmModelBuilder setMockVer(Integer mockVer) {
            this.mockVer = mockVer;
            return this;
        }

        public NmmModelBuilder setResponse(JsonNode response) {
            this.response = response;
            return this;
        }

        public NmmModel build() {
            assertField(scenario, "Scenario");
            assertField(scenario.getServiceName(), "Scenario.serviceName");

            assertField(scenarioInfo, "ScenarioInfo");
            assertField(scenarioInfo, "ScenarioInfo.ScenarioId");

            assertField(mockVer, "MockVer");
            assertField(response, "Response");

            return new NmmModel(this);
        }

        private void assertField(Object field, String name) {
            if (field == null) {
                throw new IllegalArgumentException(String.format("%s is required by NmmModel.", name));
            }
        }
    }

    public NmmModel() {}

    public NmmModel(NmmModelBuilder builder) {
        this.scenario = builder.scenario;
        this.scenarioInfo = builder.scenarioInfo;
        this.mockVer = builder.mockVer;
        this.response = builder.response;
    }

    public Scenario getScenario() {
        return scenario;
    }

    public void setScenario(Scenario scenario) {
        this.scenario = scenario;
    }

    public ScenarioInfo getScenarioInfo() {
        return scenarioInfo;
    }

    public void setScenarioInfo(ScenarioInfo scenarioInfo) {
        this.scenarioInfo = scenarioInfo;
    }

    public Integer getMockVer() {
        return mockVer;
    }

    public void setMockVer(Integer mockVer) {
        this.mockVer = mockVer;
    }

    public JsonNode getResponse() {
        return response;
    }

    public void setResponse(JsonNode response) {
        this.response = response;
    }
}

@@ -202,91 +202,371 @@
 son.
-databind.JsonNode;%0Aimport com.fasterxml.jackson.databind.annotation.JsonSerialize;%0A
+core.JsonProcessingException;%0Aimport com.fasterxml.jackson.databind.JsonNode;%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0Aimport com.fasterxml.jackson.databind.ObjectReader;%0Aimport com.fasterxml.jackson.databind.ObjectWriter;%0Aimport com.fasterxml.jackson.databind.annotation.JsonSerialize;%0A%0Aimport java.io.ByteArrayInputStream;%0Aimport java.io.IOException;
 %0Aimp
@@ -796,16 +796,212 @@
 = 22L;%0A%0A
+    private static final ObjectMapper mapper = new ObjectMapper();%0A    private static final ObjectWriter writer = mapper.writer();%0A    private static final ObjectReader reader = mapper.reader();%0A%0A
     priv
@@ -1099,32 +1099,30 @@
     private 
-JsonNode
+byte%5B%5D
  response;%0A%0A
@@ -2780,32 +2780,28 @@
         
-this.r
+setR
 esponse
- = 
+(
 builder.
@@ -2800,32 +2800,33 @@
 builder.response
+)
 ;%0A    %7D%0A%0A    pub
@@ -3364,114 +3364,518 @@
     
-return response;%0A    %7D%0A%0A    public void setResponse(JsonNode response) %7B%0A        this.response = response;
+try %7B%0A            return reader.readTree(new ByteArrayInputStream(this.response));%0A        %7D catch (IOException exception) %7B%0A            throw new IllegalStateException(%22Failed to convert byte array to JsonNode.%22);%0A        %7D%0A    %7D%0A%0A    public void setResponse(JsonNode response) %7B%0A        try %7B%0A            this.response = writer.writeValueAsBytes(response);%0A        %7D catch (JsonProcessingException exception) %7B%0A            throw new IllegalStateException(%22Failed to parse JsonNode to byte array.%22);%0A        %7D
 %0A   

change NmmModel.response from JsonNode to byte array