package org.onap.cps.ncmp.impl.data.policyexecutor;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.onap.cps.ncmp.api.data.models.OperationType;
+import org.onap.cps.ncmp.api.exceptions.NcmpException;
 import org.onap.cps.ncmp.api.exceptions.PolicyExecutorException;
 import org.onap.cps.ncmp.api.exceptions.ServerNcmpException;
 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle;
     @Qualifier("policyExecutorWebClient")
     private final WebClient policyExecutorWebClient;
 
+    private final ObjectMapper objectMapper;
+
     /**
      * Use the Policy Executor to check permission for a cm write operation.
      * Wil throw an exception when the operation is not permitted (work in progress)
         data.put("resourceIdentifier", resourceIdentifier);
         data.put("targetIdentifier", yangModelCmHandle.getAlternateId());
         if (!OperationType.DELETE.equals(operationType)) {
-            data.put("cmChangeRequest", changeRequestAsJson);
+            try {
+                final Object changeRequestAsObject = objectMapper.readValue(changeRequestAsJson, Object.class);
+                data.put("cmChangeRequest", changeRequestAsObject);
+            } catch (final JsonProcessingException e) {
+                throw new NcmpException("Cannot convert Change Request data to Object",
+                    "Invalid Json: " + changeRequestAsJson);
+            }
         }
 
         final Map<String, Object> request = new HashMap<>(2);
 
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.impl.data
+package org.onap.cps.ncmp.impl.data.policyexecutor
 
 import ch.qos.logback.classic.Level
 import ch.qos.logback.classic.Logger
 import ch.qos.logback.core.read.ListAppender
 import com.fasterxml.jackson.databind.JsonNode
 import com.fasterxml.jackson.databind.ObjectMapper
+import org.onap.cps.ncmp.api.exceptions.NcmpException
 import org.onap.cps.ncmp.api.exceptions.PolicyExecutorException
 import org.onap.cps.ncmp.api.exceptions.ServerNcmpException
-import org.onap.cps.ncmp.impl.data.policyexecutor.PolicyExecutor
 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle
 import org.slf4j.LoggerFactory
 import org.springframework.http.HttpStatus
     def mockWebClient = Mock(WebClient)
     def mockRequestBodyUriSpec = Mock(WebClient.RequestBodyUriSpec)
     def mockResponseSpec = Mock(WebClient.ResponseSpec)
+    def spiedObjectMapper = Spy(ObjectMapper)
 
-    PolicyExecutor objectUnderTest = new PolicyExecutor(mockWebClient)
+    PolicyExecutor objectUnderTest = new PolicyExecutor(mockWebClient, spiedObjectMapper)
 
     def logAppender = Spy(ListAppender<ILoggingEvent>)
 
-    ObjectMapper objectMapper = new ObjectMapper()
+    def someValidJson = '{"Hello":"World"}'
 
     def setup() {
         setupLogger()
         given: 'allow response'
             mockResponse([decision:'allow'], HttpStatus.OK)
         when: 'permission is checked for an operation'
-            objectUnderTest.checkPermission(new YangModelCmHandle(), operationType, 'my credentials','my resource','my change')
+            objectUnderTest.checkPermission(new YangModelCmHandle(), operationType, 'my credentials','my resource',someValidJson)
         then: 'system logs the operation is allowed'
             assert getLogEntry(2) == 'Policy Executor allows the operation'
         and: 'no exception occurs'
         given: 'other response'
             mockResponse([decision:'other', decisionId:123, message:'I dont like Mondays' ], HttpStatus.OK)
         when: 'permission is checked for an operation'
-            objectUnderTest.checkPermission(new YangModelCmHandle(), PATCH, 'my credentials','my resource','my change')
+            objectUnderTest.checkPermission(new YangModelCmHandle(), PATCH, 'my credentials','my resource',someValidJson)
         then: 'Policy Executor exception is thrown'
             def thrownException = thrown(PolicyExecutorException)
             assert thrownException.message == 'Policy Executor did not allow request. Decision #123 : other'
         given: 'other response'
             mockResponse([], HttpStatus.I_AM_A_TEAPOT)
         when: 'permission is checked for an operation'
-            objectUnderTest.checkPermission(new YangModelCmHandle(), PATCH, 'my credentials','my resource','my change')
+            objectUnderTest.checkPermission(new YangModelCmHandle(), PATCH, 'my credentials','my resource',someValidJson)
         then: 'Server Ncmp exception is thrown'
             def thrownException = thrown(ServerNcmpException)
             assert thrownException.message == 'Policy Executor invocation failed'
         given: 'invalid response from Policy executor'
             mockResponseSpec.toEntity(*_) >> invalidResponse
         when: 'permission is checked for an operation'
-            objectUnderTest.checkPermission(new YangModelCmHandle(), CREATE, 'my credentials','my resource','my change')
+            objectUnderTest.checkPermission(new YangModelCmHandle(), CREATE, 'my credentials','my resource',someValidJson)
         then: 'system logs the expected message'
             assert getLogEntry(1) == expectedMessage
         where: 'following invalid responses are received'
             Mono.just(new ResponseEntity<>(null, HttpStatus.OK))   || 'No valid response body from policy, ignored'
     }
 
+    def 'Permission check with an invalid change request json.'() {
+        when: 'permission is checked for an invalid change request'
+            objectUnderTest.checkPermission(new YangModelCmHandle(), CREATE, 'my credentials', 'my resource', 'invalid json string')
+        then: 'an ncmp exception thrown'
+            def ncmpException = thrown(NcmpException)
+            ncmpException.message == 'Cannot convert Change Request data to Object'
+            ncmpException.details.contains('invalid json string')
+    }
+
     def 'Permission check feature disabled.'() {
         given: 'feature is disabled'
             objectUnderTest.enabled = false
         when: 'permission is checked for an operation'
-            objectUnderTest.checkPermission(new YangModelCmHandle(), PATCH, 'my credentials','my resource','my change')
+            objectUnderTest.checkPermission(new YangModelCmHandle(), PATCH, 'my credentials','my resource',someValidJson)
         then: 'system logs that the feature not enabled'
             assert getLogEntry(0) == 'Policy Executor Enabled: false'
     }
 
     def mockResponse(mockResponseAsMap, httpStatus) {
-        JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(mockResponseAsMap))
+        JsonNode jsonNode = spiedObjectMapper.readTree(spiedObjectMapper.writeValueAsString(mockResponseAsMap))
         def mono = Mono.just(new ResponseEntity<>(jsonNode, httpStatus))
         mockResponseSpec.toEntity(*_) >> mono
     }