package org.onap.cps.policyexecutor.stub.controller;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.RequiredArgsConstructor;
public class PolicyExecutorStubController implements OperationPermissionApi {
private final Sleeper sleeper;
+ private final ObjectMapper objectMapper;
private static final Pattern PATTERN_SIMULATION = Pattern.compile("policySimulation=(\\w+_\\w+)");
private static final Pattern PATTERN_HTTP_ERROR = Pattern.compile("httpError_(\\d{3})");
final String accept,
final String authorization) {
log.info("Stub Policy Executor Invoked");
+ log.info("Permission Request: {}", formatPermissionRequest(permissionRequest));
if (permissionRequest.getOperations().isEmpty()) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return ResponseEntity.ok(new PermissionResponse(id, permissionResult, message));
}
+ private String formatPermissionRequest(final PermissionRequest permissionRequest) {
+ try {
+ return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(permissionRequest);
+ } catch (final JsonProcessingException jsonProcessingException) {
+ log.error("Error while formatting permission request", jsonProcessingException);
+ return "invalid json";
+ }
+ }
+
}
package org.onap.cps.policyexecutor.stub.controller
+import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.ObjectMapper
+import com.fasterxml.jackson.databind.ObjectWriter
import org.onap.cps.policyexecutor.stub.model.Operation
import org.onap.cps.policyexecutor.stub.model.PermissionRequest
import org.onap.cps.policyexecutor.stub.model.PermissionResponse
@Autowired
MockMvc mockMvc
- @Autowired
- ObjectMapper objectMapper
+ @SpringBean
+ ObjectMapper spiedObjectMapper = Spy(new ObjectMapper())
@SpringBean
- Sleeper sleeper = Spy()
+ Sleeper spiedSleeper = Spy()
def url = '/operation-permission/v1/permissions'
assert response.status == HttpStatus.OK.value()
and: 'the response body has the expected decision details'
def responseBody = response.contentAsString
- def permissionResponse = objectMapper.readValue(responseBody, PermissionResponse.class)
+ def permissionResponse = spiedObjectMapper.readValue(responseBody, PermissionResponse.class)
assert permissionResponse.id == expectedId
assert permissionResponse.permissionResult == expectedResult
assert permissionResponse.message == expectedMessage
'prefix/policySimulation=slowResponse_1' || '2' | 'allow' | 'all good'
'prefix/policySimulation=policyResponse_deny' || '3' | 'deny' | 'Stub is mocking a policy response: deny'
'prefix/policySimulation=policyResponse_other' || '4' | 'other' | 'Stub is mocking a policy response: other'
-
}
def 'Permission request with a HTTP error code.'() {
def 'Permission request with no operations.'() {
given: 'a permission request with no operations'
def permissionRequest = new PermissionRequest('some decision type', [])
- def requestBody = objectMapper.writeValueAsString(permissionRequest)
+ def requestBody = spiedObjectMapper.writeValueAsString(permissionRequest)
when: 'request is posted'
def response = mockMvc.perform(post(url)
.header('Authorization','some string')
assert response.status == HttpStatus.BAD_REQUEST.value()
}
+ def 'Exception thrown while printing pretty json.'() {
+ given: 'object writer for pretty json throws an exception'
+ def mockObjectWriter = Mock(ObjectWriter)
+ spiedObjectMapper.writerWithDefaultPrettyPrinter() >> mockObjectWriter
+ mockObjectWriter.writeValueAsString(_) >> { throw new JsonProcessingException('test') }
+ when: 'request is posted'
+ def response = mockMvc.perform(post(url)
+ .header('Authorization','some string')
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(createRequestBody('some fdn')))
+ .andReturn().response
+ then: 'response status is OK (exception is ignored)'
+ assert response.status == HttpStatus.OK.value()
+ }
+
+
def 'Permission request with interrupted exception during slow response.'() {
given: 'a permission request with a target fdn to simulate a slow response'
def requestBody = createRequestBody('policySimulation=slowResponse_5')
- sleeper.haveALittleRest(_) >> { throw new InterruptedException() }
+ spiedSleeper.haveALittleRest(_) >> { throw new InterruptedException() }
when: 'request is posted'
mockMvc.perform(post(url)
.header('Authorization','some string')
operation.setChangeRequest(changeRequest)
operation.setResourceIdentifier(resourceIdentifier)
def permissionRequest = new PermissionRequest('cm-legacy', [operation])
- return objectMapper.writeValueAsString(permissionRequest)
+ return spiedObjectMapper.writeValueAsString(permissionRequest)
}
}