Update getnextpendingoperation method with saving response time

APiTesting
Akeela Azhar 2 years ago
parent 35c92c3ed6
commit f90ec52ba8

@ -72,7 +72,28 @@ import java.util.*;
public class DeviceAgentServiceImpl implements DeviceAgentService {
static final Log log = LogFactory.getLog(DeviceAgentServiceImpl.class);
private static final String POLICY_MONITOR = "POLICY_MONITOR";
private static List<ComplianceFeature> getComplianceFeatures(Object compliancePayload) throws
PolicyComplianceException {
String compliancePayloadString = new Gson().toJson(compliancePayload);
if (compliancePayload == null) {
return null;
}
// Parsing json string to get compliance features.
JsonElement jsonElement = new JsonParser().parse(compliancePayloadString);
JsonArray jsonArray = jsonElement.getAsJsonArray();
Gson gson = new Gson();
ComplianceFeature complianceFeature;
List<ComplianceFeature> complianceFeatures = new ArrayList<>(jsonArray.size());
for (JsonElement element : jsonArray) {
complianceFeature = gson.fromJson(element, ComplianceFeature.class);
complianceFeatures.add(complianceFeature);
}
return complianceFeatures;
}
@POST
@Path("/enroll")
@Override
@ -102,26 +123,26 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
DeviceMgtAPIUtils.getPolicyManagementService().getEffectivePolicy(deviceId);
pap.publishChanges();
// Save the response time to Google Spreadsheet
long startTime = System.currentTimeMillis(); // Record the start time
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
// Create the values to be written
List<List<Object>> values = Collections.singletonList(
Arrays.asList(device.getDeviceIdentifier(), device.getType(), String.valueOf(System.currentTimeMillis() - startTime))
);
// Create the value range
ValueRange body = new ValueRange().setValues(values);
// Write the values to the spreadsheet
sheetsService.spreadsheets().values()
.append(spreadsheetId, "Sheet1", body)
.setValueInputOption("USER_ENTERED")
.execute();
return Response.status(Response.Status.OK).entity(status).build();
} catch (DeviceManagementException e) {
String msg = "Error occurred while enrolling the device, which carries the id '" +
@ -141,7 +162,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
}
}
@DELETE
@Path("/enroll/{type}/{id}")
@Override
@ -153,19 +174,19 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
result = DeviceMgtAPIUtils.getDeviceManagementService().disenrollDevice(deviceIdentifier);
long endTime = System.currentTimeMillis(); // Record the end time
long responseTime = endTime - startTime; // Calculate the response time
// Save the response time to Google Spreadsheet
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
// Create the values to be written
List<List<Object>> values = Collections.singletonList(
Arrays.asList(id, type, String.valueOf(responseTime))
);
// Create the value range
ValueRange body = new ValueRange().setValues(values);
// Write the values to the spreadsheet
sheetsService.spreadsheets().values()
.append(spreadsheetId, "Sheet1", body)
@ -188,7 +209,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
}
}
@PUT
@Path("/enroll/{type}/{id}")
@Override
@ -205,7 +226,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
if (updateDevice == null) {
String errorMessage = "The payload of the device enrollment is incorrect.";
log.error(errorMessage);
@ -257,20 +278,20 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
try {
device.setType(type);
result = DeviceMgtAPIUtils.getDeviceManagementService().modifyEnrollment(device);
// Save the response time to Google Spreadsheet
long startTime = System.currentTimeMillis(); // Record the start time
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
// Create the values to be written
List<List<Object>> values = Collections.singletonList(
Arrays.asList(id, type, String.valueOf(System.currentTimeMillis() - startTime))
);
// Create the value range
ValueRange body = new ValueRange().setValues(values);
// Write the values to the spreadsheet
sheetsService.spreadsheets().values()
.append(spreadsheetId, "Sheet1", body)
@ -286,20 +307,20 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
id + "'";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}catch (IOException e) {
} catch (IOException e) {
// Handle any errors occurred while writing to the spreadsheet
String errorMessage = "Error writing response time to Google Spreadsheet";
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
}
}
@POST
@Path("/events/publish/{type}/{deviceId}")
@Override
public Response publishEvents(@Valid Map<String, Object> payload, @PathParam("type") String type
, @PathParam("deviceId") String deviceId) {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
EventStreamAdminServiceStub eventStreamAdminServiceStub = null;
try {
@ -330,7 +351,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
for (EventStreamAttributeDto eventStreamAttributeDto : eventStreamAttributeDtos) {
attributes.add(new Attribute(eventStreamAttributeDto.getAttributeName()
, AttributeType.valueOf(eventStreamAttributeDto.getAttributeType().toUpperCase())));
}
if (payload.size() != attributes.size()) {
String msg = "Payload does not match with the stream definition";
@ -393,13 +414,13 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
}
}
}
@POST
@Path("/events/publish/data/{type}/{deviceId}")
@Override
public Response publishEvents(@Valid List<Object> payload, @PathParam("type") String type
, @PathParam("deviceId") String deviceId) {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
EventStreamAdminServiceStub eventStreamAdminServiceStub = null;
try {
@ -430,7 +451,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
for (EventStreamAttributeDto eventStreamAttributeDto : eventStreamAttributeDtos) {
attributes.add(new Attribute(eventStreamAttributeDto.getAttributeName()
, AttributeType.valueOf(eventStreamAttributeDto.getAttributeType().toUpperCase())));
}
if (payload.size() != attributes.size()) {
String msg = "Payload does not match with the stream definition";
@ -494,7 +515,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
}
}
}
@GET
@Path("/pending/operations/{type}/{id}")
public Response getPendingOperations(@PathParam("type") String type, @PathParam("id") String deviceId) {
@ -511,28 +532,28 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
return Response.status(Response.Status.NO_CONTENT).entity(msg).build();
}
long startTime = System.currentTimeMillis(); // Record the start time
List<? extends Operation> operations = DeviceMgtAPIUtils.getDeviceManagementService().getPendingOperations(
deviceIdentifier);
OperationList operationsList = new OperationList();
operationsList.setList(operations);
operationsList.setCount(operations.size());
long endTime = System.currentTimeMillis(); // Record the end time
long responseTime = endTime - startTime; // Calculate the response time
// Save the response time to Google Spreadsheet
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; //Update with Spreadsheet ID
// Create the values to be written
List<List<Object>> values = Collections.singletonList(
Arrays.asList(deviceId, String.valueOf(responseTime))
);
// Create the value range
ValueRange body = new ValueRange().setValues(values);
// Write the values to the spreadsheet
sheetsService.spreadsheets().values()
.append(spreadsheetId, "Sheet1", body)
@ -554,7 +575,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
}
}
private Sheets createSheetsService() throws IOException {
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/apicall-382608-48aa6a62800d.json"))
.createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS));
@ -566,7 +587,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
throw new RuntimeException(e);
}
}
@GET
@Path("/next-pending/operation/{type}/{id}")
public Response getNextPendingOperation(@PathParam("type") String type, @PathParam("id") String deviceId) {
@ -586,23 +607,30 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
deviceIdentifier);
// Save the response time to Google Spreadsheet
long startTime = System.currentTimeMillis(); // Record the start time
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
// Create Sheets service using loaded credentials
Sheets sheetsService = createSheetsService();
// Spreadsheet ID
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
// Calculate the response time
long endTime = System.currentTimeMillis(); // Record the end time
long responseTime = endTime - startTime; // Calculate the response time
// Create the values to be written
List<List<Object>> values = Collections.singletonList(
Arrays.asList(deviceId, type, String.valueOf(System.currentTimeMillis() - startTime))
Arrays.asList(deviceId, type, String.valueOf(responseTime))
);
// Create the value range
ValueRange body = new ValueRange().setValues(values);
// Write the values to the spreadsheet
sheetsService.spreadsheets().values()
.append(spreadsheetId, "Sheet1", body)
.setValueInputOption("USER_ENTERED")
.execute();
return Response.status(Response.Status.OK).entity(operation).build();
} catch (OperationManagementException e) {
String errorMessage = "Issue in retrieving operation management service instance";
@ -612,14 +640,14 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
String errorMessage = "Issue in retrieving deivce management service instance";
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
}catch (IOException e) {
} catch (IOException e) {
// Handle any errors occurred while writing to the spreadsheet
String errorMessage = "Error writing response time to Google Spreadsheet";
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
}
}
@PUT
@Path("/operations/{type}/{id}")
public Response updateOperation(@PathParam("type") String type, @PathParam("id") String deviceId, @Valid Operation operation) {
@ -666,7 +694,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
}
}
@Override
@PUT
@Path("/properties/{type}/{id}")
@ -690,20 +718,20 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
long startTime = System.currentTimeMillis(); // Record the start time
if (DeviceMgtAPIUtils.getDeviceManagementService().updateProperties(deviceIdentifier, properties)) {
// Save the response time to Google Spreadsheet
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
// Create the values to be written
List<List<Object>> values = Collections.singletonList(
Arrays.asList(deviceId, type, String.valueOf(System.currentTimeMillis() - startTime))
);
// Create the value range
ValueRange body = new ValueRange().setValues(values);
// Write the values to the spreadsheet
sheetsService.spreadsheets().values()
.append(spreadsheetId, "Sheet1", body)
@ -717,14 +745,14 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
String errorMessage = "Issue in retrieving device management service instance";
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
}catch (IOException e) {
} catch (IOException e) {
// Handle any errors occurred while writing to the spreadsheet
String errorMessage = "Error writing response time to Google Spreadsheet";
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
}
}
@GET
@Path("/status/operations/{type}/{id}")
public Response getOperationsByDeviceAndStatus(@PathParam("type") String type, @PathParam("id") String deviceId,
@ -735,8 +763,8 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
return Response.status(Response.Status.BAD_REQUEST).build();
}
long startTime = System.currentTimeMillis(); // Record the start time
try {
if (!DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(type)) {
String errorMessage = "Invalid Device Type";
@ -748,22 +776,22 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
OperationList operationsList = new OperationList();
operationsList.setList(operations);
operationsList.setCount(operations.size());
long endTime = System.currentTimeMillis(); // Record the end time
long responseTime = endTime - startTime; // Calculate the response time
// Save the response time to Google Spreadsheet
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
// Create the values to be written
List<List<Object>> values = Collections.singletonList(
Arrays.asList(deviceId, type, status.toString(), String.valueOf(responseTime))
);
// Create the value range
ValueRange body = new ValueRange().setValues(values);
// Write the values to the spreadsheet
sheetsService.spreadsheets().values()
.append(spreadsheetId, "Sheet1", body)
@ -778,32 +806,11 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
String errorMessage = "Issue in retrieving device management service";
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
}catch (IOException e) {
} catch (IOException e) {
// Handle any errors occurred while writing to the spreadsheet
String errorMessage = "Error writing response time to Google Spreadsheet";
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
}
}
private static List<ComplianceFeature> getComplianceFeatures(Object compliancePayload) throws
PolicyComplianceException {
String compliancePayloadString = new Gson().toJson(compliancePayload);
if (compliancePayload == null) {
return null;
}
// Parsing json string to get compliance features.
JsonElement jsonElement = new JsonParser().parse(compliancePayloadString);
JsonArray jsonArray = jsonElement.getAsJsonArray();
Gson gson = new Gson();
ComplianceFeature complianceFeature;
List<ComplianceFeature> complianceFeatures = new ArrayList<>(jsonArray.size());
for (JsonElement element : jsonArray) {
complianceFeature = gson.fromJson(element, ComplianceFeature.class);
complianceFeatures.add(complianceFeature);
}
return complianceFeatures;
}
}

Loading…
Cancel
Save