From 756cdaa150f84c85612057cb8c2af05c7beddbb4 Mon Sep 17 00:00:00 2001 From: Hareesha Guntupalli Date: Thu, 25 Jan 2024 10:06:57 -0600 Subject: [PATCH] Pushing 9.0.0 changes Pushing 9.0.0 changes --- pom.xml | 14 +- .../alerting/AlertValidationErrorHandler.java | 73 +++ .../apiadapters/alerting/AlertsParameter.java | 177 ++++++ .../apiadapters/alerting/AlertsService.java | 97 ++++ .../BatchAlertsOperationsAdapter.java | 270 +++++++++ .../SingleAlertOperationsAdapter.java | 174 ++++++ .../alerting/calendar/CalendarParameter.java | 47 ++ .../alerting/calendar/CalendarType.java | 34 ++ .../SingleCalendarOperationsAdapter.java | 106 ++++ .../jaxrs/client/core/Session.java | 6 + .../dto/alerting/calender/AnnualCalendar.java | 105 ++++ .../dto/alerting/calender/BaseCalendar.java | 41 ++ .../dto/alerting/calender/Calendar.java | 103 ++++ .../dto/alerting/calender/CronCalendar.java | 80 +++ .../dto/alerting/calender/DailyCalendar.java | 110 ++++ .../alerting/calender/HolidayCalendar.java | 105 ++++ .../alerting/calender/MonthlyCalendar.java | 90 +++ .../dto/alerting/calender/WeeklyCalendar.java | 89 +++ ...ingleCalendarOperationsAdapterTestNew.java | 362 ++++++++++++ .../alertingNew/AlertsServiceTest.java | 287 ++++++++++ .../BatchAlertsOperationsAdapterTest.java | 87 +++ .../SingleAlertOperationsAdapterTest.java | 534 ++++++++++++++++++ .../trigger/CalendarTriggerTest.java | 51 ++ .../trigger/SimpleTriggerTest.java | 49 ++ .../jaxrs/client/core/SessionTest.java | 11 +- 25 files changed, 3099 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/AlertValidationErrorHandler.java create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/AlertsParameter.java create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/AlertsService.java create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/BatchAlertsOperationsAdapter.java create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/SingleAlertOperationsAdapter.java create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/CalendarParameter.java create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/CalendarType.java create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/SingleCalendarOperationsAdapter.java create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/AnnualCalendar.java create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/BaseCalendar.java create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/Calendar.java create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/CronCalendar.java create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/DailyCalendar.java create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/HolidayCalendar.java create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/MonthlyCalendar.java create mode 100644 src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/WeeklyCalendar.java create mode 100644 src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/SingleCalendarOperationsAdapterTestNew.java create mode 100644 src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/AlertsServiceTest.java create mode 100644 src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/BatchAlertsOperationsAdapterTest.java create mode 100644 src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/SingleAlertOperationsAdapterTest.java create mode 100644 src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/trigger/CalendarTriggerTest.java create mode 100644 src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/trigger/SimpleTriggerTest.java diff --git a/pom.xml b/pom.xml index 4a1a0acd..3b69e51d 100644 --- a/pom.xml +++ b/pom.xml @@ -7,13 +7,13 @@ com.jaspersoft jrs-rest-java-client - 8.2.0 + 9.0.0 REST client for JasperReports server http://github.com/Jaspersoft/jrs-rest-java-client UTF-8 - 8.2.0 + 9.0.0 2.0.9 2.39 2.13.5 @@ -26,6 +26,11 @@ + + mvn-central + Maven Central + https://repo.maven.apache.org/maven2 + jaspersoft-clients-snapshots Jaspersoft clients snapshots @@ -36,6 +41,11 @@ jaspersoft-releases https://jaspersoft.jfrog.io/jaspersoft/jrs-ce-releases + + branch-central + jaspersoft-releases + https://jaspersoft.jfrog.io/jaspersoft/jrs-ce-snapshots + diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/AlertValidationErrorHandler.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/AlertValidationErrorHandler.java new file mode 100644 index 00000000..9fef77e1 --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/AlertValidationErrorHandler.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ +package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting; + +import com.jaspersoft.jasperserver.dto.common.ErrorDescriptor; +import com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.ValidationException; +import com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.handling.DefaultErrorHandler; +import com.jaspersoft.jasperserver.jaxrs.client.dto.common.ValidationError; +import com.jaspersoft.jasperserver.jaxrs.client.dto.common.ValidationErrorsListWrapper; + +import javax.ws.rs.core.Response; +import java.util.ArrayList; +import java.util.List; + +public class AlertValidationErrorHandler extends DefaultErrorHandler { + + @Override + protected void handleBodyError(Response response) { + List errorDescriptors; + if (response.getHeaderString("Content-Type").contains("xml") || response.getHeaderString("Content-Type").contains("alert+json")) { + ValidationErrorsListWrapper validationErrors = readBody(response, ValidationErrorsListWrapper.class); + if (validationErrors == null) { + super.handleBodyError(response); + return; + } + errorDescriptors = toErrorDescriptorList(validationErrors); + throw new ValidationException(generateErrorMessage(errorDescriptors), errorDescriptors); + } + super.handleBodyError(response); + } + + protected List toErrorDescriptorList(ValidationErrorsListWrapper validationErrors) { + List errorDescriptors = new ArrayList(); + List errors = validationErrors.getErrors(); + for (ValidationError error : errors) { + ErrorDescriptor errorDescriptor = new ErrorDescriptor(); + errorDescriptor.setMessage(error.toString() + " (field: " + error.getField() + ")"); + errorDescriptor.setErrorCode(error.getErrorCode()); + errorDescriptor.addParameters(error.getErrorArguments()); + errorDescriptors.add(errorDescriptor); + } + return errorDescriptors; + } + + private String generateErrorMessage(List errorDescriptors) { + StringBuilder sb = new StringBuilder(); + if (errorDescriptors != null) { + for (ErrorDescriptor errorDescriptor : errorDescriptors) { + String message = errorDescriptor.getMessage(); + sb.append("\n\t\t").append(message != null ? message : errorDescriptor.getErrorCode()); + } + } + return sb.toString(); + } +} diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/AlertsParameter.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/AlertsParameter.java new file mode 100644 index 00000000..1d695e5f --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/AlertsParameter.java @@ -0,0 +1,177 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ + +package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting; + +public enum AlertsParameter { + + /** + * URI of thumbnail inFolder schedule. Report + * options URI can also be used + * here + */ + SEARCH_RESOURCE_URI("resourceURI"), + + /** + * Name|Organization of the user, + * who scheduled the job + */ + SEARCH_OWNER("owner"), + + /** + * Label of the jobs inFolder find. + */ + SEARCH_LABEL("label"), + + /** + * This argument defines the current state of a job. + * The parameter accepts multiple values: + * NORMAL + * EXECUTING + * PAUSED + * COMPLETE + * ERROR + */ + SEARCH_STATE("state"), + + /** + * Reserved, not implemented + */ + SEARCH_PREVIOUS_FIRE_TIME("previousFireTime"), + + /** + * Reserved, not implemented + */ + SEARCH_NEXT_FIRE_TIME("nextFireTime"), + + /** + * Pagination, start index + */ + SEARCH_START_INDEX("startIndex"), + + /** + * Pagination, results count in a + * page + */ + SEARCH_NUMBER_OF_ROWS("numberOfRows"), + + /** + * Field name inFolder sort by. + * Supported values: + * NONE + * SORTBY_JOBID + * SORTBY_JOBNAME + * SORTBY_REPORTURI + * SORTBY_REPORTNAME + * SORTBY_REPORTFOLDER + * SORTBY_OWNER + * SORTBY_STATUS + * SORTBY_LASTRUN + * SORTBY_NEXTRUN + * SORTBY_RESOURCELABEL + */ + SEARCH_SORT_TYPE("sortType"), + + /** + * Sorting direction. Supported values: + * true - ascending; + * false - descending + */ + SEARCH_IS_ASCENDING("isAscending"), + + /** + * This argument defines the job id; jobID should be integer/long + */ + SEARCH_JOB_ID("jobID"), + + /** + * This argument defines the start of a range of time that matches if the job was previously triggered during this time. + * Specify the date and time in the following pattern: `yyyy-MM-dd'T'HH:mm:ss.SSSZ`. + */ + SEARCH_PREVIOUS_FIRE_TIME_FROM("previousFireTimeFrom"), + + /** + * This argument defines the end of a range of time that matches if the job was previously triggered during this time. + * Specify the date and time in the following pattern: `yyyy-MM-dd'T'HH:mm:ss.SSSZ`. + */ + SEARCH_PREVIOUS_FIRE_TIME_TO("previousFireTimeTo"), + + /** + * This argument defines the start of a range of time that matches if the job that is currently running was triggered during this time. + * Specify the date and time in the following pattern: `yyyy-MM-dd'T'HH:mm:ss.SSSZ`. + */ + SEARCH_NEST_FIRE_TIME_FROM("nextFireTimeFrom"), + + /** + * This argument defines the end of a range of time that matches if the job that is currently running was triggered during this time. + * Specify the date and time in the following pattern: `yyyy-MM-dd'T'HH:mm:ss.SSSZ`. + */ + SEARCH_NEST_FIRE_TIME_TO("nextFireTimeTo"), + + /** + * This argument defines the job label. + */ + SEARCH_JOB_LABEL("jobLabel"), + + /** + * This argument defines the Resource label. + */ + SEARCH_JOB_RESOURCE_LABEL("resourceLabel"), + + /** + * This argument defines the job description. + */ + SEARCH_JOB_DESCRIPTION("description"), + + /** + * Can be used multiple times inFolder createInFolder a list of jobIDs inFolder update + */ + JOB_ID("id"), + + /** + * When true, the trigger is replaced from the content being sent and the trigger + * type is ignored. When false or omitted, the trigger is updated automatically + * by the scheduler. + */ + UPDATE_REPLACE_TRIGGER_IGNORE_TYPE("replaceTriggerIgnoreType"), + /** + * Pagination. Start index for requested pate. + */ + OFFSET("offset"), + + /** + * Pagination. Resources count per page + */ + LIMIT("limit") + ; + + private String name; + + private AlertsParameter(String name) { + this.name = name; + } + + + public String getName() { + return name; + } + +} diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/AlertsService.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/AlertsService.java new file mode 100644 index 00000000..adfd68dd --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/AlertsService.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ + +package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting; + +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertCalendar; +import com.jaspersoft.jasperserver.dto.alerting.ClientReportAlert; +import com.jaspersoft.jasperserver.dto.alerting.wrappers.ClientCalendarNameListWrapper; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.AbstractAdapter; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.calendar.SingleCalendarOperationsAdapter; +import com.jaspersoft.jasperserver.jaxrs.client.core.*; +import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.OperationResult; + +import static com.jaspersoft.jasperserver.jaxrs.client.core.JerseyRequest.buildRequest; + +public class AlertsService extends AbstractAdapter { + + public static final String SERVICE_URI = "alerts"; + public static final String CALENDARS = "calendars"; + + public AlertsService(SessionStorage sessionStorage) { + super(sessionStorage); + } + + public BatchAlertsOperationsAdapter alerts() { + return new BatchAlertsOperationsAdapter(sessionStorage); + } + + public BatchAlertsOperationsAdapter alerts(Long... ids) { + return new BatchAlertsOperationsAdapter(sessionStorage, ids); + } + + public SingleAlertOperationsAdapter alert(long id) { + return new SingleAlertOperationsAdapter(sessionStorage, String.valueOf(id)); + } + + public SingleAlertOperationsAdapter alert(ClientReportAlert reportAlert) { + return new SingleAlertOperationsAdapter(sessionStorage, reportAlert); + } + + public OperationResult allCalendars() { + return calendar((ClientAlertCalendar.Type) null); + } + + public RequestExecution asyncCalendar(final Callback, R> callback) { + return asyncCalendar(null, callback); + } + + public OperationResult calendar(ClientAlertCalendar.Type type) { + JerseyRequest request = buildRequest(sessionStorage, ClientCalendarNameListWrapper.class, new String[]{SERVICE_URI, CALENDARS}); + if (type != null) { + request.addParam("calendarType", type.name().toLowerCase()); + } + return request.get(); + } + + public RequestExecution asyncCalendar(final ClientAlertCalendar.Type type, final Callback, R> callback) { + final JerseyRequest request = buildRequest(sessionStorage, ClientCalendarNameListWrapper.class, new String[]{SERVICE_URI, CALENDARS}); + if (type != null) { + request.addParam("calendarType", type.name().toLowerCase()); + } + RequestExecution task = new RequestExecution(new Runnable() { + @Override + public void run() { + callback.execute(request.get()); + } + }); + ThreadPoolUtil.runAsynchronously(task); + return task; + } + + public SingleCalendarOperationsAdapter calendar(String calendarName) { + if ("".equals(calendarName) || "/".equals(calendarName)) { + throw new IllegalArgumentException("'calendarName' mustn't be an empty string"); + } + return new SingleCalendarOperationsAdapter(sessionStorage, calendarName); + } + +} diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/BatchAlertsOperationsAdapter.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/BatchAlertsOperationsAdapter.java new file mode 100644 index 00000000..391821e2 --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/BatchAlertsOperationsAdapter.java @@ -0,0 +1,270 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ +package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.AnnotationIntrospector; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.type.TypeFactory; +import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector; +import com.jaspersoft.jasperserver.dto.alerting.ClientReportAlert; +import com.jaspersoft.jasperserver.dto.alerting.model.ClientReportAlertModel; +import com.jaspersoft.jasperserver.dto.alerting.wrappers.ClientAlertIdListWrapper; +import com.jaspersoft.jasperserver.dto.alerting.wrappers.ClientAlertSummariesListWrapper; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.AbstractAdapter; +import com.jaspersoft.jasperserver.jaxrs.client.core.*; +import com.jaspersoft.jasperserver.jaxrs.client.core.enums.MimeType; +import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.OperationResult; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import java.io.IOException; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; + +import static com.jaspersoft.jasperserver.jaxrs.client.core.JerseyRequest.buildRequest; + +public class BatchAlertsOperationsAdapter extends AbstractAdapter { + + private static final Logger log = LogManager.getLogger(BatchAlertsOperationsAdapter.class); + public static final String SERVICE_URI = "alerts"; + public static final String PAUSE = "pause"; + public static final String RESUME = "resume"; + public static final String RESTART = "restart"; + + private MultivaluedMap params; + private Long[] ids; + + + public BatchAlertsOperationsAdapter(SessionStorage sessionStorage) { + super(sessionStorage); + params = new MultivaluedHashMap<>(); + } + + public BatchAlertsOperationsAdapter(SessionStorage sessionStorage, Long... ids) { + super(sessionStorage); + this.ids = ids; + } + + public BatchAlertsOperationsAdapter parameter(AlertsParameter parameter, String value) { + params.add(parameter.getName(), UrlUtils.encode(value)); + return this; + } + + public BatchAlertsOperationsAdapter parameters(AlertsParameter parameter, String... values) { + for (String value : values) { + params.add(parameter.getName(), UrlUtils.encode(value)); + } + return this; + } + + public BatchAlertsOperationsAdapter parameters(AlertsParameter parameter, int... values) { + for (int value : values) { + params.add(parameter.getName(), String.valueOf(value)); + } + return this; + } + + public OperationResult searchAlerts() { + return searchAlerts(null); + } + + public OperationResult searchAlerts(ClientReportAlert searchCriteria) { + JerseyRequest request = prepareSearchRequest(searchCriteria); + return request.get(); + } + + public RequestExecution asyncSearchAlerts(final ClientReportAlert searchCriteria, final Callback, R> callback) { + final JerseyRequest request = prepareSearchRequest(searchCriteria); + RequestExecution task = new RequestExecution(new Runnable() { + @Override + public void run() { + callback.execute(request.get()); + } + }); + ThreadPoolUtil.runAsynchronously(task); + return task; + } + + private JerseyRequest prepareSearchRequest(ClientReportAlert searchCriteria) { + JerseyRequest request= buildRequest(sessionStorage, ClientAlertSummariesListWrapper.class, new String[]{SERVICE_URI}); + request.addParams(params); + if (searchCriteria != null) { + String criteriaJson = buildJson(searchCriteria); + request.addParam("example", UrlUtils.encode(criteriaJson)); + } + return request; + } + + private String buildJson(Object object) { + ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()); + mapper.setAnnotationIntrospector(introspector); + + + try { + return mapper.writeValueAsString(object); + } catch (IOException e) { + log.warn("Can't marshal search criteria."); + throw new RuntimeException("Failed inFolder build criteria json.", e); + } + } + + private String buildXml(ClientReportAlertModel reportAlertModel) { + try { + StringWriter writer = new StringWriter(); + JAXBContext jaxbContext = JAXBContext.newInstance(ClientReportAlertModel.class); + Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); + jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + jaxbMarshaller.marshal(reportAlertModel, writer); + return writer.toString(); + } catch (JAXBException e) { + log.warn("Can't marshal report alert model."); + throw new RuntimeException("Failed inFolder build report alert model xml.", e); + } + } + + /** + * Updates all alert alerts which ids were specified. Updates only set fields, other fields are ignored. + */ + public OperationResult update(ClientReportAlertModel alertModel) { + JerseyRequest request = buildRequest(sessionStorage, ClientAlertIdListWrapper.class, new String[]{SERVICE_URI}); + addIdsToQueryParams(); + request.addParams(params); + String content; + + if (sessionStorage.getConfiguration().getContentMimeType() == MimeType.JSON) { + content = buildJson(alertModel); + } else { + content = buildXml(alertModel); + } + return request.post(content); + } + + public RequestExecution asyncUpdate(final ClientReportAlertModel alertModel, final Callback, R> callback) { + final JerseyRequest request = buildRequest(sessionStorage, ClientAlertIdListWrapper.class, new String[]{SERVICE_URI}); + request.addParams(params); + final String alertJson = buildJson(alertModel); + RequestExecution task = new RequestExecution(new Runnable() { + @Override + public void run() { + callback.execute(request.post(alertJson)); + } + }); + ThreadPoolUtil.runAsynchronously(task); + return task; + } + + private List getIds() { + List ids = new ArrayList(); + if (this.ids != null && this.ids.length > 0) { + for (Long id : ids) { + ids.add(id); + } + } + + List idsTemp = params.get(AlertsParameter.JOB_ID.getName()); + + if (idsTemp != null) { + for (String id : idsTemp) { + ids.add(Long.parseLong(id)); + } + } + return ids; + } + + public OperationResult pauseAlerts() { + ClientAlertIdListWrapper alertIdListWrapper = new ClientAlertIdListWrapper(getIds()); + return buildRequest(sessionStorage, ClientAlertIdListWrapper.class, new String[]{SERVICE_URI, PAUSE}).post(alertIdListWrapper); + } + + public RequestExecution asyncPauseAlerts(final Callback, R> callback) { + final ClientAlertIdListWrapper alertIdListWrapper = new ClientAlertIdListWrapper(getIds()); + final JerseyRequest request = buildRequest(sessionStorage, ClientAlertIdListWrapper.class, new String[]{SERVICE_URI, PAUSE}); + RequestExecution task = new RequestExecution(new Runnable() { + @Override + public void run() { + callback.execute(request.post(alertIdListWrapper)); + } + }); + ThreadPoolUtil.runAsynchronously(task); + return task; + } + + public OperationResult resumeAlerts() { + ClientAlertIdListWrapper alertIdListWrapper = new ClientAlertIdListWrapper(getIds()); + return buildRequest(sessionStorage, ClientAlertIdListWrapper.class, new String[]{SERVICE_URI, RESUME}).post(alertIdListWrapper); + } + + public RequestExecution asyncResumeAlerts(final Callback, R> callback) { + final ClientAlertIdListWrapper alertIdListWrapper = new ClientAlertIdListWrapper(getIds()); + final JerseyRequest request = buildRequest(sessionStorage, ClientAlertIdListWrapper.class, new String[]{SERVICE_URI, RESUME}); + RequestExecution task = new RequestExecution(new Runnable() { + @Override + public void run() { + callback.execute(request.post(alertIdListWrapper)); + } + }); + ThreadPoolUtil.runAsynchronously(task); + return task; + } + + public OperationResult restartAlerts() { + ClientAlertIdListWrapper alertIdListWrapper = new ClientAlertIdListWrapper(getIds()); + return buildRequest(sessionStorage, ClientAlertIdListWrapper.class, new String[]{SERVICE_URI, RESTART}).post(alertIdListWrapper); + } + + public RequestExecution asyncRestartAlerts(final Callback, R> callback) { + final ClientAlertIdListWrapper alertIdListWrapper = new ClientAlertIdListWrapper(getIds()); + final JerseyRequest request = buildRequest(sessionStorage, ClientAlertIdListWrapper.class, new String[]{SERVICE_URI, RESTART}); + RequestExecution task = new RequestExecution(new Runnable() { + @Override + public void run() { + callback.execute(request.post(alertIdListWrapper)); + } + }); + ThreadPoolUtil.runAsynchronously(task); + return task; + } + + public OperationResult delete() { + JerseyRequest jerseyRequest = buildRequest(sessionStorage, ClientAlertIdListWrapper.class, new String[]{SERVICE_URI}); + addIdsToQueryParams(); + jerseyRequest.addParams(params); + return jerseyRequest.delete(); + } + + protected void addIdsToQueryParams() { + if (ids != null && ids.length > 0) { + for (Long id : ids) { + this.params.add(AlertsParameter.JOB_ID.getName(), id.toString()); + } + } + } + +} diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/SingleAlertOperationsAdapter.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/SingleAlertOperationsAdapter.java new file mode 100644 index 00000000..38f6ebcf --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/SingleAlertOperationsAdapter.java @@ -0,0 +1,174 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ + +package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting; + +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertingState; +import com.jaspersoft.jasperserver.dto.alerting.ClientReportAlert; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.AbstractAdapter; +import com.jaspersoft.jasperserver.jaxrs.client.core.Callback; +import com.jaspersoft.jasperserver.jaxrs.client.core.JerseyRequest; +import com.jaspersoft.jasperserver.jaxrs.client.core.MimeTypeUtil; +import com.jaspersoft.jasperserver.jaxrs.client.core.RequestExecution; +import com.jaspersoft.jasperserver.jaxrs.client.core.SessionStorage; +import com.jaspersoft.jasperserver.jaxrs.client.core.ThreadPoolUtil; +import com.jaspersoft.jasperserver.jaxrs.client.core.enums.JRSVersion; +import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.OperationResult; + +import static com.jaspersoft.jasperserver.jaxrs.client.core.JerseyRequest.buildRequest; + +public class SingleAlertOperationsAdapter extends AbstractAdapter { + public static final String SERVICE_URI = "alerts"; + public static final String STATE = "state"; + private String jobId; + private ClientReportAlert reportAlert; + + public SingleAlertOperationsAdapter(SessionStorage sessionStorage, String jobId) { + super(sessionStorage); + this.jobId = jobId; + } + + public SingleAlertOperationsAdapter(SessionStorage sessionStorage, ClientReportAlert reportAlert) { + super(sessionStorage); + this.reportAlert = reportAlert; + } + + public OperationResult getJob() { + JerseyRequest request = buildRequest(sessionStorage, ClientReportAlert.class, new String[]{SERVICE_URI, jobId}); + if (sessionStorage.getConfiguration().getJrsVersion().compareTo(JRSVersion.v5_5_0) > 0) { + request.setAccept(MimeTypeUtil.toCorrectAcceptMime(sessionStorage.getConfiguration(), "application/alert+{mime}")); + } else { + request.setAccept("application/alert+json"); + } + return request.get(); + } + + public RequestExecution asyncGetJob(final Callback, R> callback) { + final JerseyRequest request = buildRequest(sessionStorage, ClientReportAlert.class, new String[]{SERVICE_URI, jobId}); + if (sessionStorage.getConfiguration().getJrsVersion().compareTo(JRSVersion.v5_5_0) > 0) { + request.setAccept(MimeTypeUtil.toCorrectAcceptMime(sessionStorage.getConfiguration(), "application/alert+{mime}")); + } else { + request.setAccept("application/alert+json"); + } + RequestExecution task = new RequestExecution(new Runnable() { + @Override + public void run() { + callback.execute(request.get()); + } + }); + ThreadPoolUtil.runAsynchronously(task); + return task; + } + + public OperationResult create() { + JerseyRequest request = buildRequest(sessionStorage, ClientReportAlert.class, new String[]{SERVICE_URI}, new AlertValidationErrorHandler()); + if (sessionStorage.getConfiguration().getJrsVersion().compareTo(JRSVersion.v5_5_0) > 0) { + request.setContentType(MimeTypeUtil.toCorrectContentMime(sessionStorage.getConfiguration(), "application/alert+{mime}")); + request.setAccept(MimeTypeUtil.toCorrectAcceptMime(sessionStorage.getConfiguration(), "application/alert+{mime}")); + } else { + request.setContentType("application/alert+json"); + request.setAccept("application/alert+json"); + } + return request.put(reportAlert); + } + + public RequestExecution asyncCreate(final Callback, R> callback) { + final JerseyRequest request = buildRequest(sessionStorage, ClientReportAlert.class, new String[]{SERVICE_URI}, new AlertValidationErrorHandler()); + if (sessionStorage.getConfiguration().getJrsVersion().compareTo(JRSVersion.v5_5_0) > 0) { + request.setContentType(MimeTypeUtil.toCorrectContentMime(sessionStorage.getConfiguration(), "application/alert+{mime}")); + request.setAccept(MimeTypeUtil.toCorrectAcceptMime(sessionStorage.getConfiguration(), "application/alert+{mime}")); + } else { + request.setContentType("application/alert+json"); + request.setAccept("application/alert+json"); + } + RequestExecution task = new RequestExecution(new Runnable() { + @Override + public void run() { + callback.execute(request.put(reportAlert)); + } + }); + ThreadPoolUtil.runAsynchronously(task); + return task; + } + + public OperationResult jobState() { + return buildRequest(sessionStorage, ClientAlertingState.class, new String[]{SERVICE_URI, jobId, STATE}).get(); + } + + public RequestExecution asyncJobState(final Callback, R> callback) { + final JerseyRequest request = buildRequest(sessionStorage, ClientAlertingState.class, new String[]{SERVICE_URI, jobId, STATE}); + RequestExecution task = new RequestExecution(new Runnable() { + @Override + public void run() { + callback.execute(request.get()); + } + }); + ThreadPoolUtil.runAsynchronously(task); + return task; + } + + public OperationResult update(ClientReportAlert job) { + JerseyRequest request = buildRequest(sessionStorage, ClientReportAlert.class, new String[]{SERVICE_URI, jobId}, new AlertValidationErrorHandler()); + if (sessionStorage.getConfiguration().getJrsVersion().compareTo(JRSVersion.v5_5_0) > 0) { + request.setContentType(MimeTypeUtil.toCorrectContentMime(sessionStorage.getConfiguration(), "application/alert+{mime}")); + request.setAccept(MimeTypeUtil.toCorrectAcceptMime(sessionStorage.getConfiguration(), "application/alert+{mime}")); + } else { + request.setContentType("application/alert+json"); + request.setAccept("application/alert+json"); + } + return request.post(job); + } + + public RequestExecution asyncUpdate(final ClientReportAlert job, final Callback, R> callback) { + final JerseyRequest request = buildRequest(sessionStorage, ClientReportAlert.class, new String[]{SERVICE_URI, jobId}, new AlertValidationErrorHandler()); + if (sessionStorage.getConfiguration().getJrsVersion().compareTo(JRSVersion.v5_5_0) > 0) { + request.setContentType(MimeTypeUtil.toCorrectContentMime(sessionStorage.getConfiguration(), "application/alert+{mime}")); + request.setAccept(MimeTypeUtil.toCorrectAcceptMime(sessionStorage.getConfiguration(), "application/alert+{mime}")); + } else { + request.setContentType("application/alert+json"); + request.setAccept("application/alert+json"); + } + RequestExecution task = new RequestExecution(new Runnable() { + @Override + public void run() { + callback.execute(request.post(job)); + } + }); + ThreadPoolUtil.runAsynchronously(task); + return task; + } + + public OperationResult delete() { + return buildRequest(sessionStorage, Object.class, new String[]{SERVICE_URI, jobId}).delete(); + } + + public RequestExecution asyncDelete(final Callback callback) { + final JerseyRequest request = buildRequest(sessionStorage, Object.class, new String[]{SERVICE_URI, jobId}); + RequestExecution task = new RequestExecution(new Runnable() { + @Override + public void run() { + callback.execute(request.delete()); + } + }); + ThreadPoolUtil.runAsynchronously(task); + return task; + } +} diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/CalendarParameter.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/CalendarParameter.java new file mode 100644 index 00000000..a380370b --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/CalendarParameter.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ + +package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.calendar; + +public enum CalendarParameter { + + /** + * If true, any calendar existing in the JobStore with the same name should be + * over-written. + */ + REPLACE("replace"), + + /** + * whether or not inFolder update existing triggers that referenced the already + * existing calendar so that they are 'correct' based on the new trigger. + */ + UPDATE_TRIGGERS("updateTriggers"); + + private String name; + + private CalendarParameter(String name){ + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/CalendarType.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/CalendarType.java new file mode 100644 index 00000000..7742b959 --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/CalendarType.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ + +package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.calendar; + +public enum CalendarType { + + annual, + base, + cron, + daily, + holiday, + monthly, + weekly + +} diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/SingleCalendarOperationsAdapter.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/SingleCalendarOperationsAdapter.java new file mode 100644 index 00000000..7f9e1abd --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/SingleCalendarOperationsAdapter.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ +package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.calendar; + +//import com.jaspersoft.jasperserver.dto.job.ClientJobCalendar; + +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertCalendar; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.AbstractAdapter; +import com.jaspersoft.jasperserver.jaxrs.client.core.*; +import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.OperationResult; + +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; + +import static com.jaspersoft.jasperserver.jaxrs.client.core.JerseyRequest.buildRequest; + +public class SingleCalendarOperationsAdapter extends AbstractAdapter { + + public static final String JOBS = "alerts"; + public static final String CALENDARS = "calendars"; + private final String calendarName; + private final MultivaluedMap params; + + public SingleCalendarOperationsAdapter(SessionStorage sessionStorage, String calendarName) { + super(sessionStorage); + this.calendarName = calendarName; + params = new MultivaluedHashMap<>(); + } + + public SingleCalendarOperationsAdapter parameter(CalendarParameter parameter, String value) { + params.add(parameter.getName(), UrlUtils.encode(value)); + return this; + } + + public OperationResult getCalendar() { + //OperationResult result = buildRequest(sessionStorage, ClientAlertCalendar.class, new String[]{JOBS, CALENDARS, calendarName}).get(); + OperationResult result = buildRequest(sessionStorage, ClientAlertCalendar.class, new String[]{JOBS, CALENDARS, calendarName}).get(); + return result; + } + + public RequestExecution asyncGetCalendar(final Callback, R> callback) { + final JerseyRequest request = buildRequest(sessionStorage, ClientAlertCalendar.class, new String[]{JOBS, CALENDARS, calendarName}); + RequestExecution task = new RequestExecution(new Runnable() { + @Override + public void run() { + callback.execute(request.get()); + } + }); + ThreadPoolUtil.runAsynchronously(task); + return task; + } + + public OperationResult delete() { + return buildRequest(sessionStorage, Object.class, new String[]{JOBS, CALENDARS, calendarName}).delete(); + } + + public RequestExecution asyncDelete(final Callback callback) { + final JerseyRequest request = buildRequest(sessionStorage, Object.class, new String[]{JOBS, CALENDARS, calendarName}); + RequestExecution task = new RequestExecution(new Runnable() { + @Override + public void run() { + callback.execute(request.delete()); + } + }); + ThreadPoolUtil.runAsynchronously(task); + return task; + } + + public OperationResult createNewCalendar(ClientAlertCalendar calendarDescriptor) { + JerseyRequest request = buildRequest(sessionStorage, ClientAlertCalendar.class, new String[]{JOBS, CALENDARS, calendarName}); + request.addParams(params); + return request.put(calendarDescriptor); + } + + public RequestExecution asyncCreateNewCalendar(final ClientAlertCalendar calendarDescriptor, final Callback, R> callback) { + final JerseyRequest request = buildRequest(sessionStorage, ClientAlertCalendar.class, new String[]{JOBS, CALENDARS, calendarName}); + request.addParams(params); + RequestExecution task = new RequestExecution(new Runnable() { + @Override + public void run() { + callback.execute(request.put(calendarDescriptor)); + } + }); + ThreadPoolUtil.runAsynchronously(task); + return task; + } + +} diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/Session.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/Session.java index 16e09e8b..70315eb3 100644 --- a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/Session.java +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/Session.java @@ -21,6 +21,7 @@ package com.jaspersoft.jasperserver.jaxrs.client.core; import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.adhoc.queryexecution.QueryExecutionService; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.AlertsService; import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.attributes.AttributesService; import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.authority.organizations.OrganizationsService; import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.authority.roles.RolesService; @@ -156,6 +157,11 @@ public FavoritesService favoritesService() { return getService(FavoritesService.class); } + public AlertsService alertsService() { + return getService(AlertsService.class); + } + + public DashboardsService dashboardsService() { return getService(DashboardsService.class); } diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/AnnualCalendar.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/AnnualCalendar.java new file mode 100644 index 00000000..1840d426 --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/AnnualCalendar.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ + +package com.jaspersoft.jasperserver.jaxrs.client.dto.alerting.calender; + +//import com.jaspersoft.jasperserver.dto.job.ClientJobCalendar; +//import com.jaspersoft.jasperserver.dto.job.adapters.ExcludeDaysXmlAdapter; + +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertCalendar; +import com.jaspersoft.jasperserver.dto.alerting.adapters.ExcludeDaysXmlAdapter; + +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; +import java.util.ArrayList; + +@XmlRootElement(name = "reportJobCalendar") +public class AnnualCalendar extends Calendar { + + private ArrayList excludeDays = new ArrayList(); + // true, if excludeDays is sorted + private Boolean dataSorted; + + public AnnualCalendar() { + super(); + this.calendarType = ClientAlertCalendar.Type.annual; + } + + public AnnualCalendar(AnnualCalendar other) { + super(other); + this.excludeDays = new ArrayList(); + if (other.getExcludeDays() != null) { + for (java.util.Calendar calendar : other.getExcludeDays()) { + this.excludeDays.add((java.util.Calendar) calendar.clone()); + } + } + this.dataSorted = other.dataSorted; + } + + @XmlJavaTypeAdapter(ExcludeDaysXmlAdapter.class) + public ArrayList getExcludeDays() { + return excludeDays; + } + + public AnnualCalendar setExcludeDays(ArrayList excludeDays) { + this.excludeDays = excludeDays; + return this; + } + + public Boolean getDataSorted() { + return dataSorted; + } + + public AnnualCalendar setDataSorted(Boolean dataSorted) { + this.dataSorted = dataSorted; + return this; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + + AnnualCalendar that = (AnnualCalendar) o; + + if (dataSorted != null ? !dataSorted.equals(that.dataSorted) : that.dataSorted != null) return false; + if (excludeDays != null ? !excludeDays.equals(that.excludeDays) : that.excludeDays != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (excludeDays != null ? excludeDays.hashCode() : 0); + result = 31 * result + (dataSorted != null ? dataSorted.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "AnnualCalendar{" + + "excludeDays=" + excludeDays + + ", dataSorted=" + dataSorted + + '}'; + } +} diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/BaseCalendar.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/BaseCalendar.java new file mode 100644 index 00000000..712f9be0 --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/BaseCalendar.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ + +package com.jaspersoft.jasperserver.jaxrs.client.dto.alerting.calender; + +//import com.jaspersoft.jasperserver.dto.job.ClientJobCalendar; +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertCalendar; + +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "reportJobCalendar") +public class BaseCalendar extends Calendar { + + public BaseCalendar(){ + this.calendarType = ClientAlertCalendar.Type.base; + } + + BaseCalendar(BaseCalendar other) { + super(other); + } + + +} diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/Calendar.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/Calendar.java new file mode 100644 index 00000000..c5aa3d51 --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/Calendar.java @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ + +package com.jaspersoft.jasperserver.jaxrs.client.dto.alerting.calender; + +//import com.jaspersoft.jasperserver.dto.job.ClientJobCalendar; +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertCalendar; + +import java.util.TimeZone; + +public abstract class Calendar { + + protected ClientAlertCalendar.Type calendarType; + protected String description; + protected TimeZone timeZone; + + public Calendar() { + } + + protected Calendar(Calendar other){ + this.calendarType = other.calendarType; + this.description = other.description; + this.timeZone = (other.timeZone != null) ? (TimeZone) other.timeZone.clone() : null; + } + + public ClientAlertCalendar.Type getCalendarType() { + return calendarType; + } + + public Calendar setCalendarType(ClientAlertCalendar.Type calendarType) { + this.calendarType = calendarType; + return this; + } + + public String getDescription() { + return description; + } + + public Calendar setDescription(String description) { + this.description = description; + return this; + } + + public TimeZone getTimeZone() { + return timeZone; + } + + public Calendar setTimeZone(TimeZone timeZone) { + this.timeZone = timeZone; + return this; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Calendar calendar = (Calendar) o; + + if (calendarType != null ? !calendarType.equals(calendar.calendarType) : calendar.calendarType != null) + return false; + if (description != null ? !description.equals(calendar.description) : calendar.description != null) + return false; + if (timeZone != null ? !timeZone.equals(calendar.timeZone) : calendar.timeZone != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = calendarType != null ? calendarType.hashCode() : 0; + result = 31 * result + (description != null ? description.hashCode() : 0); + result = 31 * result + (timeZone != null ? timeZone.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "Calendar{" + + "calendarType='" + calendarType + '\'' + + ", description='" + description + '\'' + + ", timeZone='" + timeZone + '\'' + + '}'; + } +} diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/CronCalendar.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/CronCalendar.java new file mode 100644 index 00000000..3688803b --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/CronCalendar.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ + +package com.jaspersoft.jasperserver.jaxrs.client.dto.alerting.calender; + +//import com.jaspersoft.jasperserver.dto.job.ClientJobCalendar; +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertCalendar; + +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "reportJobCalendar") +public class CronCalendar extends Calendar { + + private String cronExpression; + + public CronCalendar() { + super(); + this.calendarType = ClientAlertCalendar.Type.cron; + } + + public CronCalendar(CronCalendar other) { + super(other); + this.cronExpression = other.cronExpression; + } + + public String getCronExpression() { + return cronExpression; + } + + public CronCalendar setCronExpression(String cronExpression) { + this.cronExpression = cronExpression; + return this; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + + CronCalendar that = (CronCalendar) o; + + if (cronExpression != null ? !cronExpression.equals(that.cronExpression) : that.cronExpression != null) + return false; + + return true; + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (cronExpression != null ? cronExpression.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "CronCalendar{" + + "cronExpression='" + cronExpression + '\'' + + "} " + super.toString(); + } +} diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/DailyCalendar.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/DailyCalendar.java new file mode 100644 index 00000000..9bf3066f --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/DailyCalendar.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ + +package com.jaspersoft.jasperserver.jaxrs.client.dto.alerting.calender; + +//import com.jaspersoft.jasperserver.dto.job.ClientJobCalendar; +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertCalendar; + +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "reportJobCalendar") +public class DailyCalendar extends Calendar { + + private java.util.Calendar rangeStartingCalendar; + private java.util.Calendar rangeEndingCalendar; + private Boolean invertTimeRange; + + public DailyCalendar() { + super(); + calendarType = ClientAlertCalendar.Type.daily; + } + + public DailyCalendar(DailyCalendar other) { + super(other); + this.invertTimeRange = other.invertTimeRange; + this.rangeEndingCalendar = (other.rangeEndingCalendar != null) ? (java.util.Calendar) other.rangeEndingCalendar.clone() : null; + this.rangeStartingCalendar = (other.rangeStartingCalendar != null) ? (java.util.Calendar) other.rangeStartingCalendar.clone() : null; + } + + public java.util.Calendar getRangeStartingCalendar() { + return rangeStartingCalendar; + } + + public DailyCalendar setRangeStartingCalendar(java.util.Calendar rangeStartingCalendar) { + this.rangeStartingCalendar = rangeStartingCalendar; + return this; + } + + public java.util.Calendar getRangeEndingCalendar() { + return rangeEndingCalendar; + } + + public DailyCalendar setRangeEndingCalendar(java.util.Calendar rangeEndingCalendar) { + this.rangeEndingCalendar = rangeEndingCalendar; + return this; + } + + public Boolean getInvertTimeRange() { + return invertTimeRange; + } + + public DailyCalendar setInvertTimeRange(Boolean invertTimeRange) { + this.invertTimeRange = invertTimeRange; + return this; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + + DailyCalendar that = (DailyCalendar) o; + + if (invertTimeRange != null ? !invertTimeRange.equals(that.invertTimeRange) : that.invertTimeRange != null) + return false; + if (rangeEndingCalendar != null ? !rangeEndingCalendar.equals(that.rangeEndingCalendar) : that.rangeEndingCalendar != null) + return false; + if (rangeStartingCalendar != null ? !rangeStartingCalendar.equals(that.rangeStartingCalendar) : that.rangeStartingCalendar != null) + return false; + + return true; + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (rangeStartingCalendar != null ? rangeStartingCalendar.hashCode() : 0); + result = 31 * result + (rangeEndingCalendar != null ? rangeEndingCalendar.hashCode() : 0); + result = 31 * result + (invertTimeRange != null ? invertTimeRange.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "DailyCalendar{" + + "rangeStartingCalendar=" + rangeStartingCalendar + + ", rangeEndingCalendar=" + rangeEndingCalendar + + ", invertTimeRange=" + invertTimeRange + + "} " + super.toString(); + } +} diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/HolidayCalendar.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/HolidayCalendar.java new file mode 100644 index 00000000..fb358d96 --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/HolidayCalendar.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ + +package com.jaspersoft.jasperserver.jaxrs.client.dto.alerting.calender; + +//import com.jaspersoft.jasperserver.dto.job.ClientJobCalendar; +//import com.jaspersoft.jasperserver.dto.job.adapters.ExcludeDaysXmlAdapter; +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertCalendar; +import com.jaspersoft.jasperserver.dto.alerting.adapters.ExcludeDaysXmlAdapter; + +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; +import java.util.ArrayList; + + +@XmlRootElement(name = "reportJobCalendar") +public class HolidayCalendar extends Calendar { + + public HolidayCalendar() { + super(); + this.calendarType = ClientAlertCalendar.Type.holiday; + } + + public HolidayCalendar(HolidayCalendar other) { + super(other); + this.dataSorted = other.dataSorted; + this.excludeDays = new ArrayList(); + if (other.getExcludeDays() != null) { + for (java.util.Calendar excludeDay : other.getExcludeDays()) { + this.excludeDays.add(excludeDay); + } + } + } + + private ArrayList excludeDays = new ArrayList(); + // true, if excludeDays is sorted + private Boolean dataSorted; + + @XmlJavaTypeAdapter(ExcludeDaysXmlAdapter.class) + public ArrayList getExcludeDays() { + return excludeDays; + } + + public HolidayCalendar setExcludeDays(ArrayList excludeDays) { + this.excludeDays = excludeDays; + return this; + } + + public Boolean getDataSorted() { + return dataSorted; + } + + public HolidayCalendar setDataSorted(Boolean dataSorted) { + this.dataSorted = dataSorted; + return this; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + + HolidayCalendar that = (HolidayCalendar) o; + + if (dataSorted != null ? !dataSorted.equals(that.dataSorted) : that.dataSorted != null) return false; + if (excludeDays != null ? !excludeDays.equals(that.excludeDays) : that.excludeDays != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (excludeDays != null ? excludeDays.hashCode() : 0); + result = 31 * result + (dataSorted != null ? dataSorted.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "HolidayCalendar{" + + "excludeDays=" + excludeDays + + ", dataSorted=" + dataSorted + + "} " + super.toString(); + } +} diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/MonthlyCalendar.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/MonthlyCalendar.java new file mode 100644 index 00000000..cab17793 --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/MonthlyCalendar.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ + +package com.jaspersoft.jasperserver.jaxrs.client.dto.alerting.calender; + + +//import com.jaspersoft.jasperserver.dto.job.ClientJobCalendar; +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertCalendar; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.Arrays; + +@XmlRootElement(name = "reportJobCalendar") +public class MonthlyCalendar extends Calendar { + + private boolean[] excludeDaysFlags; + + public MonthlyCalendar() { + super(); + this.calendarType = ClientAlertCalendar.Type.monthly; + } + + public MonthlyCalendar(MonthlyCalendar other) { + super(other); + if (other.excludeDaysFlags != null) { + this.excludeDaysFlags = new boolean[other.excludeDaysFlags.length]; + for (int i = 0; i < other.excludeDaysFlags.length; i++) { + this.excludeDaysFlags[i] = other.excludeDaysFlags[i]; + } + } + } + + @XmlElementWrapper(name = "excludeDaysFlags") + @XmlElement(name = "excludeDayFlag") + public boolean[] getExcludeDaysFlags() { + return excludeDaysFlags; + } + + public MonthlyCalendar setExcludeDaysFlags(boolean[] excludeDaysFlags) { + this.excludeDaysFlags = excludeDaysFlags; + return this; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + + MonthlyCalendar that = (MonthlyCalendar) o; + + if (!Arrays.equals(excludeDaysFlags, that.excludeDaysFlags)) return false; + + return true; + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (excludeDaysFlags != null ? Arrays.hashCode(excludeDaysFlags) : 0); + return result; + } + + @Override + public String toString() { + return "MonthlyCalendar{" + + "excludeDaysFlags=" + excludeDaysFlags + + "} " + super.toString(); + } +} diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/WeeklyCalendar.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/WeeklyCalendar.java new file mode 100644 index 00000000..63e59269 --- /dev/null +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/alerting/calender/WeeklyCalendar.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2005 - 2014 Jaspersoft Corporation. All rights reserved. + * http://www.jaspersoft.com. + * + * Unless you have purchased a commercial license agreement from Jaspersoft, + * the following license terms apply: + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program.  If not, see . + */ + +package com.jaspersoft.jasperserver.jaxrs.client.dto.alerting.calender; + +//import com.jaspersoft.jasperserver.dto.job.ClientJobCalendar; +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertCalendar; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.Arrays; + +@XmlRootElement(name = "reportJobCalendar") +public class WeeklyCalendar extends Calendar { + + private boolean[] excludeDaysFlags; + + public WeeklyCalendar() { + super(); + this.calendarType = ClientAlertCalendar.Type.weekly; + } + + public WeeklyCalendar(WeeklyCalendar other) { + super(other); + if (other.excludeDaysFlags != null) { + this.excludeDaysFlags = new boolean[other.excludeDaysFlags.length]; + for (int i = 0; i < other.excludeDaysFlags.length; i++) { + this.excludeDaysFlags[i] = other.excludeDaysFlags[i]; + } + } + } + + @XmlElementWrapper(name = "excludeDaysFlags") + @XmlElement(name = "excludeDayFlag") + public boolean[] getExcludeDaysFlags() { + return excludeDaysFlags; + } + + public WeeklyCalendar setExcludeDaysFlags(boolean[] excludeDaysFlags) { + this.excludeDaysFlags = excludeDaysFlags; + return this; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + + WeeklyCalendar calendar = (WeeklyCalendar) o; + + if (!Arrays.equals(excludeDaysFlags, calendar.excludeDaysFlags)) return false; + + return true; + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (excludeDaysFlags != null ? Arrays.hashCode(excludeDaysFlags) : 0); + return result; + } + + @Override + public String toString() { + return "WeeklyCalendar{" + + "excludeDaysFlags=" + excludeDaysFlags + + "} " + super.toString(); + } +} diff --git a/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/SingleCalendarOperationsAdapterTestNew.java b/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/SingleCalendarOperationsAdapterTestNew.java new file mode 100644 index 00000000..b8bb01d2 --- /dev/null +++ b/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alerting/calendar/SingleCalendarOperationsAdapterTestNew.java @@ -0,0 +1,362 @@ +package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.calendar; + +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertCalendar; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.calendar.CalendarParameter; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.calendar.SingleCalendarOperationsAdapter; +import com.jaspersoft.jasperserver.jaxrs.client.core.*; +import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.OperationResult; +import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.WithEntityOperationResult; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.testng.PowerMockTestCase; +import org.powermock.reflect.Whitebox; +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; +import java.util.concurrent.atomic.AtomicInteger; + +import static com.jaspersoft.jasperserver.jaxrs.client.core.JerseyRequest.buildRequest; +import static org.hamcrest.core.IsInstanceOf.instanceOf; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; +import static org.mockito.MockitoAnnotations.initMocks; +import static org.powermock.api.mockito.PowerMockito.doReturn; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.spy; +import static org.powermock.api.mockito.PowerMockito.when; +import static org.powermock.api.mockito.PowerMockito.*; +import static org.testng.AssertJUnit.*; + + +/** + * Unit tests for {@link SingleCalendarOperationsAdapter} + */ +@PrepareForTest({JerseyRequest.class, SingleCalendarOperationsAdapter.class, MultivaluedHashMap.class}) +public class SingleCalendarOperationsAdapterTestNew extends PowerMockTestCase { + + @Mock + private SessionStorage sessionStorageMock; + + @Mock + private JerseyRequest requestMock; + + @Mock + private JerseyRequest objRequestMock; + + @Mock + private OperationResult delResultMock; + + @Mock + private OperationResult operationResultMock; + + @Mock + private ClientAlertCalendar reportAlertCalendarMock; + + @Mock + private Response responseMock; + + @Mock + private RequestBuilder builderMock; + + @Mock + private WithEntityOperationResult withEntityOperationResultMock; + + @Mock + private ClientAlertCalendar calendarEntityMock; + + @Mock + private Callback, Object> callbackMock; + + @Mock + private Callback, Object> callbackMock3; + + @Mock + private Callback resultObjectCallbackMock; + + private MultivaluedHashMap paramsSpy; + + @BeforeMethod + public void before() { + initMocks(this); + paramsSpy = spy(new MultivaluedHashMap()); + } + + @Test + public void should_pass_proper_params_to_super_class() { + + // When + SingleCalendarOperationsAdapter calendarOperationsAdapter = + new SingleCalendarOperationsAdapter(sessionStorageMock, "testCalendarName"); + + // Then + assertSame(calendarOperationsAdapter.getSessionStorage(), sessionStorageMock); + Object calendarName = Whitebox.getInternalState(calendarOperationsAdapter, "calendarName"); + Object params = Whitebox.getInternalState(calendarOperationsAdapter, "params"); + + assertNotNull(params); + assertSame(calendarName, "testCalendarName"); + + assertTrue(instanceOf(String.class).matches(calendarName)); + assertTrue(instanceOf(MultivaluedHashMap.class).matches(params)); + + + } + + @Test + @SuppressWarnings("unchecked") + public void should_set_parameter_field_of_adapter() { + + // Given + SingleCalendarOperationsAdapter adapter = new SingleCalendarOperationsAdapter(sessionStorageMock, "MyCal"); + + // When + adapter.parameter(CalendarParameter.REPLACE, "true"); + MultivaluedMap params = (MultivaluedMap) Whitebox.getInternalState(adapter, "params"); + + // Then + Assert.assertTrue(Boolean.valueOf(params.get("replace").get(0))); + } + + @Test(enabled = true) + public void parameter() throws Exception { + + // Given + PowerMockito.whenNew(MultivaluedHashMap.class).withNoArguments().thenReturn(paramsSpy); + SingleCalendarOperationsAdapter adapterSpy = Mockito.spy(new SingleCalendarOperationsAdapter(sessionStorageMock, "testCalendarName")); + + // When + SingleCalendarOperationsAdapter retrieved = adapterSpy.parameter(CalendarParameter.UPDATE_TRIGGERS, "testValue"); + + // Then + verify(adapterSpy, times(1)).parameter(CalendarParameter.UPDATE_TRIGGERS, "testValue"); + verify(paramsSpy, times(1)).add(CalendarParameter.UPDATE_TRIGGERS.getName(), "testValue"); + verify(paramsSpy, never()).getFirst(anyString()); + + Assert.assertSame(retrieved, adapterSpy); + Assert.assertTrue(paramsSpy.size() == 1); + } + + @Test + public void get() throws Exception { + + // Given + SingleCalendarOperationsAdapter adapterSpy = spy(new SingleCalendarOperationsAdapter(sessionStorageMock, + "testCalendarName")); + mockStatic(JerseyRequest.class); + when(buildRequest(eq(sessionStorageMock), eq(ClientAlertCalendar.class), + eq(new String[]{"alerts", "calendars", "testCalendarName"}))).thenReturn(requestMock); + doReturn(operationResultMock).when(requestMock).get(); + + // When + OperationResult retrieved = adapterSpy.getCalendar(); + + // Then + verifyStatic(JerseyRequest.class, times(1)); + buildRequest(eq(sessionStorageMock), eq(ClientAlertCalendar.class), + eq(new String[]{"alerts", "calendars", "testCalendarName"})); + assertSame(retrieved, operationResultMock); + } + + @Test + public void delete() throws Exception { + + // Given + mockStatic(JerseyRequest.class); + when(buildRequest(eq(sessionStorageMock), + eq(Object.class), + eq(new String[]{"alerts", "calendars", "testCalendarName"}))).thenReturn(objRequestMock); + doReturn(delResultMock).when(objRequestMock).delete(); + SingleCalendarOperationsAdapter adapterSpy = spy(new SingleCalendarOperationsAdapter(sessionStorageMock, "testCalendarName")); + + // When + OperationResult retrieved = adapterSpy.delete(); + + // Then + verifyStatic(JerseyRequest.class, times(1)); + buildRequest(eq(sessionStorageMock), eq(Object.class), eq(new String[]{"alerts", "calendars", "testCalendarName"})); + assertSame(retrieved, delResultMock); + } + + @Test + @SuppressWarnings("unchecked") + public void createNew() { + + // Given + mockStatic(JerseyRequest.class); + when(buildRequest(eq(sessionStorageMock), + eq(ClientAlertCalendar.class), + eq(new String[]{"alerts", "calendars", "testCalendarName"}))).thenReturn(requestMock); + PowerMockito.when(requestMock.put(calendarEntityMock)).thenReturn(operationResultMock); + SingleCalendarOperationsAdapter adapterSpy = spy(new SingleCalendarOperationsAdapter(sessionStorageMock, "testCalendarName")); + + // When + OperationResult retrieved = adapterSpy.createNewCalendar(calendarEntityMock); + + // Then + verifyStatic(JerseyRequest.class, times(1)); + buildRequest(eq(sessionStorageMock), + eq(ClientAlertCalendar.class), + eq(new String[]{"alerts", "calendars", "testCalendarName"})); + assertSame(retrieved, operationResultMock); + + MultivaluedMap retrievedParams = (MultivaluedMap) Whitebox.getInternalState(adapterSpy, "params"); + Mockito.verify(requestMock).addParams(retrievedParams); + Mockito.verify(requestMock).put(calendarEntityMock); + } + + @Test + public void asyncGet() throws Exception { + + // Given + final AtomicInteger newThreadId = new AtomicInteger(); + final int currentThreadId = (int) Thread.currentThread().getId(); + + PowerMockito.mockStatic(JerseyRequest.class); + PowerMockito.when( + buildRequest( + eq(sessionStorageMock), + eq(ClientAlertCalendar.class), + eq(new String[]{"alerts", "calendars", "testCalendarName"}))) + .thenReturn(requestMock); + + SingleCalendarOperationsAdapter adapterSpy = spy(new SingleCalendarOperationsAdapter(sessionStorageMock, "testCalendarName")); + + Callback, Void> callback = PowerMockito.spy(new Callback, Void>() { + public Void execute(OperationResult data) { + newThreadId.set((int) Thread.currentThread().getId()); + synchronized (this) { + this.notify(); + } + return null; + } + }); + + PowerMockito.doReturn(operationResultMock).when(requestMock).get(); + PowerMockito.doNothing().when(callback).execute(operationResultMock); + + // When + RequestExecution retrieved = adapterSpy.asyncGetCalendar(callback); + + // Wait + synchronized (callback) { + callback.wait(500); + } + + // Then + Assert.assertNotNull(retrieved); + Assert.assertNotSame(currentThreadId, newThreadId.get()); + } + + @Test(enabled = true) + public void asyncDelete() throws Exception { + + PowerMockito.mockStatic(JerseyRequest.class); + PowerMockito.when( + buildRequest( + eq(sessionStorageMock), + eq(Object.class), + eq(new String[]{"alerts", "calendars", "testCalendarName"}))) + .thenReturn(objRequestMock); + + SingleCalendarOperationsAdapter adapterSpy = spy(new SingleCalendarOperationsAdapter(sessionStorageMock, "testCalendarName")); + PowerMockito.doReturn(delResultMock).when(objRequestMock).delete(); + PowerMockito.doReturn(new Object()).when(resultObjectCallbackMock).execute(delResultMock); + + // When + adapterSpy.asyncDelete(resultObjectCallbackMock); + + // Then + verifyStatic(JerseyRequest.class, times(1)); + JerseyRequest.buildRequest( + eq(sessionStorageMock), + eq(Object.class), + eq(new String[]{"alerts", "calendars", "testCalendarName"})); + + } + + @Test + public void asyncCreateNew() throws Exception { + + PowerMockito.mockStatic(JerseyRequest.class); + PowerMockito.whenNew(MultivaluedHashMap.class).withNoArguments().thenReturn(paramsSpy); + PowerMockito.when( + buildRequest( + eq(sessionStorageMock), + eq(ClientAlertCalendar.class), + eq(new String[]{"alerts", "calendars", "testCalendarName"}))) + .thenReturn(requestMock); + + SingleCalendarOperationsAdapter adapterSpy = PowerMockito.spy(new SingleCalendarOperationsAdapter(sessionStorageMock, "testCalendarName")); + + PowerMockito.doReturn(operationResultMock).when(requestMock).put(calendarEntityMock); + PowerMockito.doReturn(new Object()).when(callbackMock3).execute(operationResultMock); + PowerMockito.doReturn(builderMock).when(requestMock).addParams(paramsSpy); + // When + adapterSpy.asyncCreateNewCalendar(calendarEntityMock, callbackMock3); + + // Then + verifyStatic(JerseyRequest.class, times(1)); + JerseyRequest.buildRequest( + eq(sessionStorageMock), + eq(ClientAlertCalendar.class), + eq(new String[]{"alerts", "calendars", "testCalendarName"})); + } + + @Test + public void should_delete_calendar_asynchronously() throws InterruptedException { + + /* Given */ + PowerMockito.mockStatic(JerseyRequest.class); + PowerMockito.when(buildRequest(eq(sessionStorageMock), + eq(Object.class), + eq(new String[]{"alerts", "calendars", "testCalendarName"}))).thenReturn(objRequestMock); + + PowerMockito.doReturn(delResultMock).when(objRequestMock).delete(); + + final AtomicInteger newThreadId = new AtomicInteger(); + final int currentThreadId = (int) Thread.currentThread().getId(); + + final Callback callback = PowerMockito.spy(new Callback() { + @Override + public Void execute(OperationResult data) { + newThreadId.set((int) Thread.currentThread().getId()); + synchronized (this) { + this.notify(); + } + return null; + } + }); + + SingleCalendarOperationsAdapter adapterSpy = PowerMockito.spy(new SingleCalendarOperationsAdapter(sessionStorageMock, "testCalendarName")); + + /* When */ + RequestExecution retrieved = adapterSpy.asyncDelete(callback); + + /* Wait */ + synchronized (callback) { + callback.wait(500); + } + + /* Then */ + Assert.assertNotNull(retrieved); + Assert.assertNotSame(currentThreadId, newThreadId.get()); + + Mockito.verify(objRequestMock).delete(); + Mockito.verify(callback).execute(delResultMock); + } + + @AfterMethod + public void after() { + reset(sessionStorageMock, requestMock, objRequestMock, delResultMock, + operationResultMock, reportAlertCalendarMock, responseMock, withEntityOperationResultMock, + calendarEntityMock); + } +} \ No newline at end of file diff --git a/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/AlertsServiceTest.java b/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/AlertsServiceTest.java new file mode 100644 index 00000000..7c0f9829 --- /dev/null +++ b/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/AlertsServiceTest.java @@ -0,0 +1,287 @@ +package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alertingNew; + +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertCalendar; +import com.jaspersoft.jasperserver.dto.alerting.ClientReportAlert; +import com.jaspersoft.jasperserver.dto.alerting.wrappers.ClientCalendarNameListWrapper; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.AlertsService; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.BatchAlertsOperationsAdapter; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.SingleAlertOperationsAdapter; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.calendar.CalendarType; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.calendar.SingleCalendarOperationsAdapter; +import com.jaspersoft.jasperserver.jaxrs.client.core.*; +import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.OperationResult; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.testng.PowerMockTestCase; +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.concurrent.atomic.AtomicInteger; + +import static com.jaspersoft.jasperserver.jaxrs.client.core.JerseyRequest.buildRequest; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; +import static org.mockito.MockitoAnnotations.initMocks; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.*; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertSame; + +/** + * Unit tests for {@link AlertsService} + */ +@PrepareForTest({JerseyRequest.class, AlertsService.class}) +public class AlertsServiceTest extends PowerMockTestCase { + + @Mock + private SessionStorage sessionStorageMock; + + @Mock + private BatchAlertsOperationsAdapter expectedAdapterMock; + + @Mock + private SingleAlertOperationsAdapter expectedAlertOperationsAdapter; + + @Mock + private JerseyRequest alertRequestMock; + + @Mock + private JerseyRequest wrapperRequestMock; + + @Mock + private OperationResult expectedAlertOperationResultMock; + + @Mock + private OperationResult expectedWrapperOperationResultMock; + + @Mock + private ClientReportAlert reportMock; + + @Mock + private RestClientConfiguration configurationMock; + + @Mock + private Callback, Object> callbackMock; + + @Mock + private RequestExecution executionMock; + + @Mock + private SingleCalendarOperationsAdapter expectedCalendarOperationsAdapterMock; + + @BeforeMethod + public void before() { + initMocks(this); + } + + @Test(testName = "alerts") + public void should_return_proper_adapter() throws Exception { + + // Given + AlertsService serviceSpy = spy(new AlertsService(sessionStorageMock)); + whenNew(BatchAlertsOperationsAdapter.class) + .withParameterTypes(SessionStorage.class) + .withArguments(sessionStorageMock) + .thenReturn(expectedAdapterMock); + + // When + BatchAlertsOperationsAdapter retrieved = serviceSpy.alerts(); + + // Then + assertSame(retrieved, expectedAdapterMock); + } + + @Test(testName = "alert") + public void should_return_proper_SingleJobOperationsAdapter() throws Exception { + + // Given + AlertsService serviceSpy = spy(new AlertsService(sessionStorageMock)); + whenNew(SingleAlertOperationsAdapter.class) + .withParameterTypes(SessionStorage.class, String.class) + .withArguments(sessionStorageMock, "9056") + .thenReturn(expectedAlertOperationsAdapter); + + // When + SingleAlertOperationsAdapter retrieved = serviceSpy.alert(9056); + + // Then + verify(serviceSpy, times(1)).alert(9056); + assertSame(retrieved, expectedAlertOperationsAdapter); + } + + @Test(testName = "allCalendars") + public void should_return_proper_op_result_object() { + + // Given + AlertsService serviceSpy = spy(new AlertsService(sessionStorageMock)); + doReturn(expectedWrapperOperationResultMock).when(serviceSpy).calendar((ClientAlertCalendar.Type) null); + + // When + OperationResult retrieved = serviceSpy.allCalendars(); + + // Then + verify(serviceSpy, times(1)).allCalendars(); + verify(serviceSpy, times(1)).calendar(((ClientAlertCalendar.Type) null)); + verify(serviceSpy, never()).calendar(ClientAlertCalendar.Type.holiday); + verifyNoMoreInteractions(serviceSpy); + + assertSame(retrieved, expectedWrapperOperationResultMock); + } + + @Test(testName = "asyncCalendar") + public void should_return_RequestExecution_with_CalendarNameListWrapper_instance() { + + // Given + AlertsService serviceSpy = spy(new AlertsService(sessionStorageMock)); + doReturn(executionMock).when(serviceSpy).asyncCalendar(null, callbackMock); + + // When + RequestExecution retrieved = serviceSpy.asyncCalendar(callbackMock); + + // Then + verify(serviceSpy, times(1)).asyncCalendar(null, callbackMock); + verify(serviceSpy, never()).asyncCalendar(ClientAlertCalendar.Type.holiday, callbackMock); + + assertSame(retrieved, executionMock); + } + + @Test(testName = "calendars_with_param") + public void should_return_proper_op_result_object_when_param_is_not_null() { + + // Given + mockStatic(JerseyRequest.class); + when(JerseyRequest.buildRequest(eq(sessionStorageMock), + eq(ClientCalendarNameListWrapper.class), + eq(new String[]{"alerts", "calendars"}))).thenReturn(wrapperRequestMock); + when(wrapperRequestMock.get()).thenReturn(expectedWrapperOperationResultMock); + AlertsService serviceSpy = spy(new AlertsService(sessionStorageMock)); + + // When + OperationResult retrieved = serviceSpy.calendar(ClientAlertCalendar.Type.daily); + + // Then + assertNotNull(retrieved); + assertSame(retrieved, expectedWrapperOperationResultMock); + + verifyStatic(JerseyRequest.class, times(1)); + JerseyRequest.buildRequest(eq(sessionStorageMock), + eq(ClientCalendarNameListWrapper.class), + eq(new String[]{"alerts", "calendars"})); + + verify(wrapperRequestMock, times(1)).get(); + verify(wrapperRequestMock, times(1)).addParam("calendarType", CalendarType.daily.toString().toLowerCase()); + } + + + @Test(testName = "calendars_with_param") + public void should_return_proper_op_result_object_when_param_is_null() { + + // Given + mockStatic(JerseyRequest.class); + when(JerseyRequest.buildRequest(eq(sessionStorageMock), + eq(ClientCalendarNameListWrapper.class), + eq(new String[]{"alerts", "calendars"}))).thenReturn(wrapperRequestMock); + when(wrapperRequestMock.get()).thenReturn(expectedWrapperOperationResultMock); + AlertsService serviceSpy = spy(new AlertsService(sessionStorageMock)); + + // When + OperationResult retrieved = serviceSpy.calendar((ClientAlertCalendar.Type) null); + + // Then + assertNotNull(retrieved); + assertSame(retrieved, expectedWrapperOperationResultMock); + + verifyStatic(JerseyRequest.class, times(1)); + JerseyRequest.buildRequest(eq(sessionStorageMock), + eq(ClientCalendarNameListWrapper.class), + eq(new String[]{"alerts", "calendars"})); + + verify(wrapperRequestMock, times(1)).get(); + verify(wrapperRequestMock, never()).addParam("calendarType", CalendarType.daily.toString().toLowerCase()); + } + + + @Test(testName = "calendar") + public void should_return_an_calendar_adapter() throws Exception { + + // Given + final String calendarName = "testCalendar"; + AlertsService serviceSpy = spy(new AlertsService(sessionStorageMock)); + whenNew(SingleCalendarOperationsAdapter.class) + .withParameterTypes(SessionStorage.class, String.class) + .withArguments(sessionStorageMock, calendarName) + .thenReturn(expectedCalendarOperationsAdapterMock); + + // When + SingleCalendarOperationsAdapter retrieved = serviceSpy.calendar(calendarName); + + // Then + assertNotNull(retrieved); + assertSame(retrieved, expectedCalendarOperationsAdapterMock); + } + + @Test(testName = "calendar", expectedExceptions = IllegalArgumentException.class) + public void should_throw_an_exception_when_invalid_calendar_name() throws Exception { + AlertsService serviceSpy = spy(new AlertsService(sessionStorageMock)); + serviceSpy.calendar(""); + } + + @Test + public void should_get_calendars_asynchronously() throws InterruptedException { + + /* Given */ + PowerMockito.mockStatic(JerseyRequest.class); + PowerMockito.when(buildRequest(eq(sessionStorageMock), + eq(ClientCalendarNameListWrapper.class), + eq(new String[]{"alerts", "calendars"}))).thenReturn(wrapperRequestMock); + + PowerMockito.doReturn(expectedWrapperOperationResultMock).when(wrapperRequestMock).get(); + AlertsService serviceSpy = PowerMockito.spy(new AlertsService(sessionStorageMock)); + + final AtomicInteger newThreadId = new AtomicInteger(); + final int currentThreadId = (int) Thread.currentThread().getId(); + + final Callback, Void> callback = PowerMockito.spy(new Callback, Void>() { + @Override + public Void execute(OperationResult data) { + newThreadId.set((int) Thread.currentThread().getId()); + synchronized (this) { + this.notify(); + } + return null; + } + }); + + PowerMockito.doNothing().when(callback).execute(expectedWrapperOperationResultMock); + + /* When */ + RequestExecution retrieved = serviceSpy.asyncCalendar(ClientAlertCalendar.Type.annual, callback); + + /* Wait */ + synchronized (callback) { + callback.wait(1000); + } + + /* Then */ + Assert.assertNotNull(retrieved); + Assert.assertNotSame(currentThreadId, newThreadId.get()); + + Mockito.verify(wrapperRequestMock).get(); + Mockito.verify(callback).execute(expectedWrapperOperationResultMock); + Mockito.verify(wrapperRequestMock).addParam("calendarType", "annual"); + } + + @AfterMethod + public void after() { + reset(sessionStorageMock, expectedAdapterMock, expectedAlertOperationsAdapter, + alertRequestMock, expectedAlertOperationResultMock, reportMock, configurationMock); + } +} \ No newline at end of file diff --git a/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/BatchAlertsOperationsAdapterTest.java b/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/BatchAlertsOperationsAdapterTest.java new file mode 100644 index 00000000..8d11a54b --- /dev/null +++ b/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/BatchAlertsOperationsAdapterTest.java @@ -0,0 +1,87 @@ +package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alertingNew; + +import com.jaspersoft.jasperserver.dto.alerting.ClientReportAlert; +import com.jaspersoft.jasperserver.dto.alerting.wrappers.ClientAlertSummariesListWrapper; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.BatchAlertsOperationsAdapter; +import com.jaspersoft.jasperserver.jaxrs.client.core.JerseyRequest; +import com.jaspersoft.jasperserver.jaxrs.client.core.RestClientConfiguration; +import com.jaspersoft.jasperserver.jaxrs.client.core.SessionStorage; +import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.OperationResult; +import org.mockito.Mock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.testng.PowerMockTestCase; +import org.powermock.reflect.Whitebox; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import javax.ws.rs.core.MultivaluedHashMap; + +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; +import static org.mockito.MockitoAnnotations.initMocks; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.verifyStatic; +import static org.testng.Assert.*; + +/** + * @author Tetiana Iefimenko + */ +@PrepareForTest({JerseyRequest.class}) +public class BatchAlertsOperationsAdapterTest extends PowerMockTestCase { + + @Mock + private SessionStorage sessionStorageMock; + + @Mock + private JerseyRequest alertSummaryListWrapperJerseyRequest; + + @Mock + private OperationResult alertSummaryListWrapperOperationResult; + + @Mock + private RestClientConfiguration configurationMock; + + @Mock + private ClientReportAlert alertMock; + + @BeforeMethod + public void before() { + initMocks(this); + } + + @Test(testName = "constructor") + public void should_create_proper_SingleAlertOperationsAdapter_object() throws IllegalAccessException { + + // When + BatchAlertsOperationsAdapter adapter = new BatchAlertsOperationsAdapter(sessionStorageMock); + + // Then + assertEquals(Whitebox.getInternalState(adapter, "sessionStorage"), sessionStorageMock); + assertEquals(Whitebox.getInternalState(adapter, "params"), new MultivaluedHashMap()); + } + + @Test + public void should_return_all_jobs() { + + // Given + mockStatic(JerseyRequest.class); + when(JerseyRequest.buildRequest(eq(sessionStorageMock), + eq(ClientAlertSummariesListWrapper.class), + eq(new String[]{"alerts"}))).thenReturn(alertSummaryListWrapperJerseyRequest); + when(alertSummaryListWrapperJerseyRequest.get()).thenReturn(alertSummaryListWrapperOperationResult); + when(sessionStorageMock.getConfiguration()).thenReturn(configurationMock); + + // When + BatchAlertsOperationsAdapter adapter = spy(new BatchAlertsOperationsAdapter(sessionStorageMock)); + OperationResult retrieved = adapter.searchAlerts(); + + // Then + verifyStatic(JerseyRequest.class, times(1)); + JerseyRequest.buildRequest(eq(sessionStorageMock), eq(ClientAlertSummariesListWrapper.class), eq(new String[]{"alerts"})); + verify(alertSummaryListWrapperJerseyRequest, times(1)).get(); + assertNotNull(retrieved); + assertSame(retrieved, alertSummaryListWrapperOperationResult); + + } + +} diff --git a/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/SingleAlertOperationsAdapterTest.java b/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/SingleAlertOperationsAdapterTest.java new file mode 100644 index 00000000..48b86e8c --- /dev/null +++ b/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/SingleAlertOperationsAdapterTest.java @@ -0,0 +1,534 @@ +package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alertingNew; + +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertingState; +import com.jaspersoft.jasperserver.dto.alerting.ClientReportAlert; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.AlertValidationErrorHandler; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.SingleAlertOperationsAdapter; +import com.jaspersoft.jasperserver.jaxrs.client.core.Callback; +import com.jaspersoft.jasperserver.jaxrs.client.core.JerseyRequest; +import com.jaspersoft.jasperserver.jaxrs.client.core.RequestExecution; +import com.jaspersoft.jasperserver.jaxrs.client.core.RestClientConfiguration; +import com.jaspersoft.jasperserver.jaxrs.client.core.SessionStorage; +import com.jaspersoft.jasperserver.jaxrs.client.core.enums.JRSVersion; +import com.jaspersoft.jasperserver.jaxrs.client.core.enums.MimeType; +import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.OperationResult; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.testng.PowerMockTestCase; +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.lang.reflect.Field; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.verifyStatic; +import static org.powermock.api.support.membermodification.MemberMatcher.field; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertSame; + + +/** + * Unit tests for {@link SingleAlertOperationsAdapter} + */ +@PrepareForTest({JerseyRequest.class}) +public class SingleAlertOperationsAdapterTest extends PowerMockTestCase { + + @Mock + private SessionStorage sessionStorageMock; + + @Mock + private JerseyRequest alertRequestMock; + + @Mock + private JerseyRequest objectJerseyRequestMock; + + @Mock + private JerseyRequest alertStateJerseyRequestMock; + + @Mock + private OperationResult alertStateOperationResultMock; + + @Mock + private OperationResult alertOperationResultMock; + + @Mock + private OperationResult operationResultMock; + + @Mock + private RestClientConfiguration configurationMock; + + @Mock + private ClientReportAlert alertMock; + + private final String expectedAlertId = "3819"; + + @BeforeMethod + public void before() { + initMocks(this); + } + + @Test(testName = "constructor") + public void should_create_proper_SingleAlertOperationsAdapter_object() throws IllegalAccessException { + + // When + SingleAlertOperationsAdapter adapter = spy(new SingleAlertOperationsAdapter(sessionStorageMock, expectedAlertId)); + + // Then + assertSame(adapter.getSessionStorage(), sessionStorageMock); + Field field = field(SingleAlertOperationsAdapter.class, "jobId"); + String retrievedAlertId = (String) field.get(adapter); + assertEquals(retrievedAlertId, expectedAlertId); + } + + @Test(testName = "group") + public void should_return_proper_Alert_operation_result_when_JRS_version_is_v5_6_1() { + + // Given + mockStatic(JerseyRequest.class); + when(JerseyRequest.buildRequest(eq(sessionStorageMock), eq(ClientReportAlert.class), eq(new String[]{"alerts", expectedAlertId}))).thenReturn(alertRequestMock); + when(alertRequestMock.get()).thenReturn(alertOperationResultMock); + when(sessionStorageMock.getConfiguration()).thenReturn(configurationMock); + when(configurationMock.getJrsVersion()).thenReturn(JRSVersion.v5_6_1); + when(configurationMock.getContentMimeType()).thenReturn(MimeType.XML); + + // When + SingleAlertOperationsAdapter adapter = spy(new SingleAlertOperationsAdapter(sessionStorageMock, expectedAlertId)); + OperationResult retrieved = adapter.getJob(); + + // Then + verifyStatic(JerseyRequest.class, times(1)); + JerseyRequest.buildRequest(eq(sessionStorageMock), eq(ClientReportAlert.class), eq(new String[]{"alerts", expectedAlertId})); + verify(alertRequestMock, times(1)).get(); + verify(alertRequestMock, times(1)).setAccept("application/alert+xml"); + + assertNotNull(retrieved); + assertSame(retrieved, alertOperationResultMock); + } + + @Test + public void should_return_proper_alert_operation_result_when_JRS_version_is_v4_7_0() { + + // Given + mockStatic(JerseyRequest.class); + when(JerseyRequest.buildRequest(eq(sessionStorageMock), eq(ClientReportAlert.class), eq(new String[]{"alerts", expectedAlertId}))).thenReturn(alertRequestMock); + when(alertRequestMock.get()).thenReturn(alertOperationResultMock); + when(sessionStorageMock.getConfiguration()).thenReturn(configurationMock); + when(configurationMock.getJrsVersion()).thenReturn(JRSVersion.v4_7_0); + when(configurationMock.getContentMimeType()).thenReturn(MimeType.JSON); + + // When + SingleAlertOperationsAdapter adapter = spy(new SingleAlertOperationsAdapter(sessionStorageMock, expectedAlertId)); + OperationResult retrieved = adapter.getJob(); + + // Then + verifyStatic(JerseyRequest.class, times(1)); + JerseyRequest.buildRequest(eq(sessionStorageMock), eq(ClientReportAlert.class), eq(new String[]{"alerts", expectedAlertId})); + verify(alertRequestMock, times(1)).get(); + verify(alertRequestMock, times(1)).setAccept("application/alert+json"); + + assertNotNull(retrieved); + assertSame(retrieved, alertOperationResultMock); + } + + @Test + public void should_proper_op_result_instance() { + + // Given + mockStatic(JerseyRequest.class); + when(JerseyRequest.buildRequest(eq(sessionStorageMock), eq(ClientAlertingState.class), + eq(new String[]{"alerts", expectedAlertId, "state"}))).thenReturn(alertStateJerseyRequestMock); + when(alertStateJerseyRequestMock.get()).thenReturn(alertStateOperationResultMock); + + // When + SingleAlertOperationsAdapter adapter = spy(new SingleAlertOperationsAdapter(sessionStorageMock, expectedAlertId)); + OperationResult retrieved = adapter.jobState(); + + // Then + verifyStatic(JerseyRequest.class, times(1)); + JerseyRequest.buildRequest(eq(sessionStorageMock), eq(ClientAlertingState.class), + eq(new String[]{"alerts", expectedAlertId, "state"})); + verify(alertStateJerseyRequestMock, times(1)).get(); + assertSame(retrieved, alertStateOperationResultMock); + } + + @Test(testName = "update") + public void should_return_prepared_op_result_when_jrs_version_is_5_6_1() { + + // Given + mockStatic(JerseyRequest.class); + when(JerseyRequest.buildRequest(eq(sessionStorageMock), eq(ClientReportAlert.class), eq(new String[]{"alerts", expectedAlertId}), any(AlertValidationErrorHandler.class))).thenReturn(alertRequestMock); + when(alertRequestMock.post(alertMock)).thenReturn(alertOperationResultMock); + when(sessionStorageMock.getConfiguration()).thenReturn(configurationMock); + when(configurationMock.getJrsVersion()).thenReturn(JRSVersion.v5_6_1); + when(configurationMock.getContentMimeType()).thenReturn(MimeType.XML); + when(configurationMock.getAcceptMimeType()).thenReturn(MimeType.XML); + + // When + SingleAlertOperationsAdapter adapter = spy(new SingleAlertOperationsAdapter(sessionStorageMock, expectedAlertId)); + OperationResult retrieved = adapter.update(alertMock); + + // Then + verifyStatic(JerseyRequest.class, times(1)); + JerseyRequest.buildRequest(eq(sessionStorageMock), eq(ClientReportAlert.class), eq(new String[]{"alerts", expectedAlertId}), any(AlertValidationErrorHandler.class)); + verify(alertRequestMock, times(1)).post(alertMock); + verify(alertRequestMock, times(1)).setContentType("application/alert+xml"); + verify(alertRequestMock, times(1)).setAccept("application/alert+xml"); + + assertNotNull(retrieved); + assertSame(retrieved, alertOperationResultMock); + } + + @Test(testName = "update") + public void should_return_prepared_op_result_when_jrs_version_is_4_7_0() { + + // Given + mockStatic(JerseyRequest.class); + when(JerseyRequest.buildRequest(eq(sessionStorageMock), eq(ClientReportAlert.class), eq(new String[]{"alerts", expectedAlertId}), any(AlertValidationErrorHandler.class))).thenReturn(alertRequestMock); + when(alertRequestMock.post(alertMock)).thenReturn(alertOperationResultMock); + when(sessionStorageMock.getConfiguration()).thenReturn(configurationMock); + when(configurationMock.getJrsVersion()).thenReturn(JRSVersion.v4_7_0); + when(configurationMock.getContentMimeType()).thenReturn(MimeType.JSON); + when(configurationMock.getAcceptMimeType()).thenReturn(MimeType.JSON); + + // When + SingleAlertOperationsAdapter adapter = spy(new SingleAlertOperationsAdapter(sessionStorageMock, expectedAlertId)); + OperationResult retrieved = adapter.update(alertMock); + + // Then + verifyStatic(JerseyRequest.class, times(1)); + JerseyRequest.buildRequest(eq(sessionStorageMock), eq(ClientReportAlert.class), eq(new String[]{"alerts", expectedAlertId}), any(AlertValidationErrorHandler.class)); + verify(alertRequestMock, times(1)).post(alertMock); + verify(alertRequestMock, times(1)).setContentType("application/alert+json"); + verify(alertRequestMock, times(1)).setAccept("application/alert+json"); + + assertNotNull(retrieved); + assertSame(retrieved, alertOperationResultMock); + } + + @Test(testName = "delete") + public void should_proper_op_result_instance_for_deleting_operation() { + + // Given + mockStatic(JerseyRequest.class); + when(JerseyRequest.buildRequest(eq(sessionStorageMock), eq(Object.class), + eq(new String[]{"alerts", expectedAlertId}))).thenReturn(objectJerseyRequestMock); + when(objectJerseyRequestMock.delete()).thenReturn(operationResultMock); + + // When + SingleAlertOperationsAdapter adapter = spy(new SingleAlertOperationsAdapter(sessionStorageMock, expectedAlertId)); + OperationResult retrieved = adapter.delete(); + + // Then + verifyStatic(JerseyRequest.class, times(1)); + JerseyRequest.buildRequest(eq(sessionStorageMock), eq(Object.class), eq(new String[]{"alerts", expectedAlertId})); + verify(objectJerseyRequestMock, times(1)).delete(); + assertSame(retrieved, operationResultMock); + } + + @Test + public void should_return_RequestExecution_when_environment_is_specified() throws Exception { + + // Given + final AtomicInteger newThreadId = new AtomicInteger(); + final int currentThreadId = (int) Thread.currentThread().getId(); + + PowerMockito.mockStatic(JerseyRequest.class); + PowerMockito.when(JerseyRequest.buildRequest( + eq(sessionStorageMock), + eq(ClientReportAlert.class), + eq(new String[]{"alerts", "123435326"}), + any(AlertValidationErrorHandler.class))).thenReturn(alertRequestMock); + + SingleAlertOperationsAdapter adapterSpy = PowerMockito.spy(new SingleAlertOperationsAdapter(sessionStorageMock, "123435326")); + Callback, Void> callback = PowerMockito.spy(new Callback, Void>() { + public Void execute(OperationResult data) { + newThreadId.set((int) Thread.currentThread().getId()); + synchronized (this) { + this.notify(); + } + return null; + } + }); + + PowerMockito.doReturn(alertOperationResultMock).when(alertRequestMock).post(alertMock); + PowerMockito.doNothing().when(callback).execute(alertOperationResultMock); + PowerMockito.doReturn(configurationMock).when(sessionStorageMock).getConfiguration(); + PowerMockito.doReturn(JRSVersion.v5_1_0).when(configurationMock).getJrsVersion(); + + InOrder inOrder = Mockito.inOrder(alertRequestMock); + + // When + RequestExecution retrieved = adapterSpy.asyncUpdate(alertMock, callback); + + // Wait + synchronized (callback) { + callback.wait(1000); + } + + // Then + Assert.assertNotNull(retrieved); + Assert.assertNotSame(currentThreadId, newThreadId.get()); + Mockito.verify(callback, times(1)).execute(alertOperationResultMock); + + inOrder.verify(alertRequestMock, times(1)).setContentType("application/alert+json"); + inOrder.verify(alertRequestMock, times(1)).setAccept("application/alert+json"); + inOrder.verify(alertRequestMock, times(1)).post(alertMock); + } + + @Test + public void should_return_RequestExecution_when_environment_is_specified_with_5_6_1_jrs_server_version() throws Exception { + + // Given + final AtomicInteger newThreadId = new AtomicInteger(); + final int currentThreadId = (int) Thread.currentThread().getId(); + + PowerMockito.mockStatic(JerseyRequest.class); + PowerMockito.when(JerseyRequest.buildRequest( + eq(sessionStorageMock), + eq(ClientReportAlert.class), + eq(new String[]{"alerts", "123435326"}), + any(AlertValidationErrorHandler.class))).thenReturn(alertRequestMock); + + SingleAlertOperationsAdapter adapterSpy = PowerMockito.spy(new SingleAlertOperationsAdapter(sessionStorageMock, "123435326")); + Callback, Void> callback = PowerMockito.spy(new Callback, Void>() { + public Void execute(OperationResult data) { + newThreadId.set((int) Thread.currentThread().getId()); + synchronized (this) { + this.notify(); + } + return null; + } + }); + + PowerMockito.doReturn(alertOperationResultMock).when(alertRequestMock).post(alertMock); + PowerMockito.doNothing().when(callback).execute(alertOperationResultMock); + PowerMockito.doReturn(configurationMock).when(sessionStorageMock).getConfiguration(); + PowerMockito.doReturn(JRSVersion.v5_6_1).when(configurationMock).getJrsVersion(); + + InOrder inOrder = Mockito.inOrder(alertRequestMock); + + // When + RequestExecution retrieved = adapterSpy.asyncUpdate(alertMock, callback); + + // Wait + synchronized (callback) { + callback.wait(1000); + } + + // Then + Assert.assertNotNull(retrieved); + Assert.assertNotSame(currentThreadId, newThreadId.get()); + Mockito.verify(callback, times(1)).execute(alertOperationResultMock); + + inOrder.verify(alertRequestMock).setContentType("application/alert+xml"); + inOrder.verify(alertRequestMock).setAccept("application/alert+xml"); + inOrder.verify(alertRequestMock, times(1)).post(alertMock); + } + + @Test + public void should_invoke_asynchronous_method_get_and_return_RequestExecution_with_Future() throws InterruptedException { + + // Given + final AtomicInteger newThreadId = new AtomicInteger(); + final int currentThreadId = (int) Thread.currentThread().getId(); + + PowerMockito.mockStatic(JerseyRequest.class); + PowerMockito.when(JerseyRequest.buildRequest( + eq(sessionStorageMock), + eq(ClientReportAlert.class), + eq(new String[]{"alerts", "123435326"}))) + .thenReturn(alertRequestMock); + + SingleAlertOperationsAdapter adapterSpy = PowerMockito.spy(new SingleAlertOperationsAdapter(sessionStorageMock, "123435326")); + Callback, Void> callback = PowerMockito.spy(new Callback, Void>() { + public Void execute(OperationResult data) { + newThreadId.set((int) Thread.currentThread().getId()); + synchronized (this) { + this.notify(); + } + return null; + } + }); + + PowerMockito.doReturn(alertOperationResultMock).when(alertRequestMock).get(); + PowerMockito.doNothing().when(callback).execute(alertOperationResultMock); + PowerMockito.doReturn(configurationMock).when(sessionStorageMock).getConfiguration(); + PowerMockito.doReturn(JRSVersion.v5_6_1).when(configurationMock).getJrsVersion(); + PowerMockito.doReturn(MimeType.JSON).when(configurationMock).getAcceptMimeType(); + + InOrder inOrder = Mockito.inOrder(alertRequestMock); + + // When + RequestExecution retrieved = adapterSpy.asyncGetJob(callback); + + // Wait + synchronized (callback) { + callback.wait(1000); + } + + // Then + Assert.assertNotNull(retrieved); + Assert.assertNotSame(currentThreadId, newThreadId.get()); + Mockito.verify(callback, times(1)).execute(alertOperationResultMock); + + inOrder.verify(alertRequestMock).setAccept("application/alert+json"); + inOrder.verify(alertRequestMock, times(1)).get(); + } + + + @Test + public void should_invoke_asynchronous_method_get_and_return_RequestExecution_with_Future_when_jrs_version_is_lower_Then_5_5_0() throws InterruptedException { + + // Given + final AtomicInteger newThreadId = new AtomicInteger(); + final int currentThreadId = (int) Thread.currentThread().getId(); + + PowerMockito.mockStatic(JerseyRequest.class); + PowerMockito.when(JerseyRequest.buildRequest( + eq(sessionStorageMock), + eq(ClientReportAlert.class), + eq(new String[]{"alerts", "123435326"}))) + .thenReturn(alertRequestMock); + + SingleAlertOperationsAdapter adapterSpy = PowerMockito.spy(new SingleAlertOperationsAdapter(sessionStorageMock, "123435326")); + Callback, Void> callback = PowerMockito.spy(new Callback, Void>() { + public Void execute(OperationResult data) { + newThreadId.set((int) Thread.currentThread().getId()); + synchronized (this) { + this.notify(); + } + return null; + } + }); + + PowerMockito.doReturn(alertOperationResultMock).when(alertRequestMock).get(); + PowerMockito.doNothing().when(callback).execute(alertOperationResultMock); + PowerMockito.doReturn(configurationMock).when(sessionStorageMock).getConfiguration(); + PowerMockito.doReturn(JRSVersion.v4_7_0).when(configurationMock).getJrsVersion(); + + InOrder inOrder = Mockito.inOrder(alertRequestMock); + + // When + RequestExecution retrieved = adapterSpy.asyncGetJob(callback); + + // Wait + synchronized (callback) { + callback.wait(1000); + } + + // Then + Assert.assertNotNull(retrieved); + Assert.assertNotSame(currentThreadId, newThreadId.get()); + Mockito.verify(callback, times(1)).execute(alertOperationResultMock); + + inOrder.verify(alertRequestMock).setAccept("application/alert+json"); + inOrder.verify(alertRequestMock, times(1)).get(); + } + + + @Test + public void should_delete_job_asynchronously() throws InterruptedException { + + // Given + final AtomicInteger newThreadId = new AtomicInteger(); + final int currentThreadId = (int) Thread.currentThread().getId(); + + PowerMockito.mockStatic(JerseyRequest.class); + PowerMockito.when(JerseyRequest.buildRequest( + eq(sessionStorageMock), + eq(Object.class), + eq(new String[]{"alerts", "123435326"}))) + .thenReturn(objectJerseyRequestMock); + + SingleAlertOperationsAdapter adapterSpy = PowerMockito.spy(new SingleAlertOperationsAdapter(sessionStorageMock, "123435326")); + Callback callback = PowerMockito.spy(new Callback() { + public Void execute(OperationResult data) { + newThreadId.set((int) Thread.currentThread().getId()); + synchronized (this) { + this.notify(); + } + return null; + } + }); + + PowerMockito.doReturn(operationResultMock).when(objectJerseyRequestMock).delete(); + PowerMockito.doNothing().when(callback).execute(operationResultMock); + + // When + RequestExecution retrieved = adapterSpy.asyncDelete(callback); + + // Wait + synchronized (callback) { + callback.wait(1000); + } + + // Then + Assert.assertNotNull(retrieved); + Assert.assertNotSame(currentThreadId, newThreadId.get()); + Mockito.verify(objectJerseyRequestMock, times(1)).delete(); + Mockito.verify(callback, times(1)).execute(operationResultMock); + } + + @Test + public void should_retrieve_state_asynchronously() throws InterruptedException { + + // Given + final AtomicInteger newThreadId = new AtomicInteger(); + final int currentThreadId = (int) Thread.currentThread().getId(); + + PowerMockito.mockStatic(JerseyRequest.class); + PowerMockito.when(JerseyRequest.buildRequest( + eq(sessionStorageMock), + eq(ClientAlertingState.class), + eq(new String[]{"alerts", "123435326", "state"}))) + .thenReturn(alertStateJerseyRequestMock); + + SingleAlertOperationsAdapter adapterSpy = PowerMockito.spy(new SingleAlertOperationsAdapter(sessionStorageMock, "123435326")); + Callback, Void> callback = PowerMockito.spy(new Callback, Void>() { + public Void execute(OperationResult data) { + newThreadId.set((int) Thread.currentThread().getId()); + synchronized (this) { + this.notify(); + } + return null; + } + }); + + PowerMockito.doReturn(alertStateOperationResultMock).when(alertStateJerseyRequestMock).get(); + PowerMockito.doNothing().when(callback).execute(alertStateOperationResultMock); + + // When + RequestExecution retrieved = adapterSpy.asyncJobState(callback); + + // Wait + synchronized (callback) { + callback.wait(1000); + } + + // Then + Assert.assertNotNull(retrieved); + Assert.assertNotSame(currentThreadId, newThreadId.get()); + Mockito.verify(alertStateJerseyRequestMock, times(1)).get(); + Mockito.verify(callback, times(1)).execute(alertStateOperationResultMock); + } + + @AfterMethod + public void after() { + reset(sessionStorageMock, alertRequestMock, alertOperationResultMock, configurationMock); + } +} \ No newline at end of file diff --git a/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/trigger/CalendarTriggerTest.java b/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/trigger/CalendarTriggerTest.java new file mode 100644 index 00000000..3a64f511 --- /dev/null +++ b/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/trigger/CalendarTriggerTest.java @@ -0,0 +1,51 @@ +package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alertingNew.trigger; + +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertCalendarTrigger; +import com.jaspersoft.jasperserver.dto.alerting.ClientCalendarDaysType; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.Date; +import java.util.TreeSet; + +import static java.util.Arrays.asList; +import static org.testng.Assert.assertEquals; + +/** + *

+ *

+ * + * @author tetiana.iefimenko + * @version $Id$ + * @see + */ +public class CalendarTriggerTest { + + private ClientAlertCalendarTrigger calendarTrigger = new ClientAlertCalendarTrigger(); + + @BeforeMethod + public void before() { + + calendarTrigger. + setMinutes("1-10"). + setHours("8-16"). + setDaysType(ClientCalendarDaysType.ALL). + setWeekDays(new TreeSet(asList((byte) 10, (byte) 21, (byte) 12))). + setMonthDays("1,3,5-22"). + setMonths(new TreeSet(asList((byte)1, (byte)11, (byte)12))). + setCalendarName("Calendar name"). + setEndDate(new Date()). + setId(Long.valueOf(1000)). + setMisfireInstruction(0). + setStartDate(new Date()). + setStartType(1). + setTimezone("Some time zone"). + setVersion(1); + } + + @Test + public void should_clone_object() { + ClientAlertCalendarTrigger cloned = (ClientAlertCalendarTrigger) calendarTrigger.deepClone(); + assertEquals(cloned, calendarTrigger); + } +} \ No newline at end of file diff --git a/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/trigger/SimpleTriggerTest.java b/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/trigger/SimpleTriggerTest.java new file mode 100644 index 00000000..7b978389 --- /dev/null +++ b/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/alertingNew/trigger/SimpleTriggerTest.java @@ -0,0 +1,49 @@ +package com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alertingNew.trigger; + +import com.jaspersoft.jasperserver.dto.alerting.ClientAlertSimpleTrigger; +import com.jaspersoft.jasperserver.dto.alerting.ClientIntervalUnitType; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.Date; + +import static org.testng.Assert.assertEquals; + + +/** + *

+ *

+ * + * @author tetiana.iefimenko + * @version $Id$ + * @see + */ +public class SimpleTriggerTest { + + private ClientAlertSimpleTrigger simpleTrigger = new ClientAlertSimpleTrigger(); + + @BeforeMethod + public void before() { + + simpleTrigger. + setOccurrenceCount(10). + setRecurrenceInterval(20). + setRecurrenceIntervalUnit(ClientIntervalUnitType.DAY). + setCalendarName("Calendar name"). + setEndDate(new Date()). + setId(Long.valueOf(1000)). + setMisfireInstruction(10). + setStartDate(new Date()). + setStartType(1). + setTimezone("Some time zone"). + setVersion(1); + } + + @Test + public void should_clone_object() { + ClientAlertSimpleTrigger cloned = (ClientAlertSimpleTrigger) simpleTrigger.deepClone(); + + assertEquals(cloned, simpleTrigger); + } + +} \ No newline at end of file diff --git a/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/core/SessionTest.java b/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/core/SessionTest.java index 189396c7..f684f8d8 100644 --- a/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/core/SessionTest.java +++ b/src/test/java/com/jaspersoft/jasperserver/jaxrs/client/core/SessionTest.java @@ -2,6 +2,7 @@ import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.AbstractAdapter; import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.adhoc.queryexecution.QueryExecutionService; +import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.alerting.AlertsService; import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.attributes.AttributesService; import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.authority.organizations.OrganizationsService; import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.authority.roles.RolesService; @@ -227,7 +228,6 @@ public void should_return_not_null_ExportService_instance() { public void should_return_not_null_ImportService_instance() { // When Session sessionSpy = Mockito.spy(new Session(storageMock)); - ; ImportService retrieved = sessionSpy.importService(); // Then assertNotNull(retrieved); @@ -264,6 +264,15 @@ public void should_return_not_null_JobsService_instance() { verify(sessionSpy, times(1)).getService(JobsService.class); } + @Test + public void should_return_not_null_AlertsService_instance() { + // When + Session sessionSpy = Mockito.spy(new Session(storageMock)); + AlertsService retrieved = sessionSpy.alertsService(); + // Then + assertNotNull(retrieved); + verify(sessionSpy, times(1)).getService(AlertsService.class); + } @Test public void should_return_not_null_DomainMetadataService_instance() { // When