Browse Source

updated java rest client with missing methods

pull/13962/head
dashevchenko 9 months ago
parent
commit
4152cd9555
  1. 78
      msa/black-box-tests/src/test/java/org/thingsboard/server/msa/connectivity/RestClientTest.java
  2. 265
      rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java

78
msa/black-box-tests/src/test/java/org/thingsboard/server/msa/connectivity/RestClientTest.java

@ -0,0 +1,78 @@
package org.thingsboard.server.msa.connectivity;
import org.springframework.web.client.RestTemplate;
import org.testcontainers.shaded.org.apache.commons.lang3.RandomStringUtils;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.thingsboard.rest.client.RestClient;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.alarm.Alarm;
import org.thingsboard.server.common.data.alarm.AlarmInfo;
import org.thingsboard.server.common.data.alarm.AlarmSearchStatus;
import org.thingsboard.server.common.data.alarm.AlarmSeverity;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.TimePageLink;
import org.thingsboard.server.msa.AbstractContainerTest;
import org.thingsboard.server.msa.TestProperties;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.thingsboard.server.msa.prototypes.DevicePrototypes.defaultDevicePrototype;
public class RestClientTest extends AbstractContainerTest {
private static final RestClient restClient = new RestClient(new RestTemplate(), TestProperties.getBaseUrl());
@BeforeMethod
public void setUp() throws Exception {
restClient.login("tenant@thingsboard.org", "tenant");
}
@AfterMethod
public void tearDown() {
}
@Test
public void testGetAlarmsV2() {
Device device = restClient.saveDevice(defaultDevicePrototype(RandomStringUtils.randomAlphabetic(5)));
assertThat(device).isNotNull();
String type = "High temp" + RandomStringUtils.randomAlphabetic(5);
Alarm alarm = Alarm.builder()
.originator(device.getId())
.severity(AlarmSeverity.CRITICAL)
.type(type)
.build();
restClient.saveAlarm(alarm);
// get /api/v2/alarm
PageData<AlarmInfo> alarmsV2 = restClient.getAlarmsV2(device.getId(), null, null, List.of(type), null, new TimePageLink(10, 0));
assertThat(alarmsV2.getData()).hasSize(1);
PageData<AlarmInfo> activeAlarms = restClient.getAlarmsV2(device.getId(), List.of(AlarmSearchStatus.ACTIVE), null, List.of(type), null, new TimePageLink(10, 0));
assertThat(activeAlarms.getData()).hasSize(1);
PageData<AlarmInfo> cleared = restClient.getAlarmsV2(device.getId(), List.of(AlarmSearchStatus.CLEARED), null, List.of(type), null, new TimePageLink(10, 0));
assertThat(cleared.getData()).hasSize(0);
PageData<AlarmInfo> activeAndClearedAlarms = restClient.getAlarmsV2(device.getId(), List.of(AlarmSearchStatus.CLEARED, AlarmSearchStatus.ACTIVE), null, null, null, new TimePageLink(10, 0));
assertThat(activeAndClearedAlarms.getData()).hasSize(1);
// get /api/v2/alarms
PageData<AlarmInfo> allAlarmsV2 = restClient.getAllAlarmsV2(List.of(AlarmSearchStatus.ACTIVE), null, List.of(type), null, new TimePageLink(10, 0));
assertThat(allAlarmsV2.getData()).hasSize(1);
PageData<AlarmInfo> allClearedAlarmsV2 = restClient.getAllAlarmsV2(List.of(AlarmSearchStatus.CLEARED), null, List.of(type), null, new TimePageLink(10, 0));
assertThat(allClearedAlarmsV2.getData()).hasSize(0);
// get /api/alarms
PageData<AlarmInfo> allAlarms = restClient.getAllAlarms(AlarmSearchStatus.ACTIVE, null, new TimePageLink(10, 0), null);
assertThat(allAlarms.getData()).hasSize(1);
PageData<AlarmInfo> allClearedAlarms = restClient.getAllAlarms(AlarmSearchStatus.CLEARED, null, new TimePageLink(10, 0), null);
assertThat(allClearedAlarms.getData()).hasSize(0);
}
}

265
rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java

@ -113,6 +113,8 @@ import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.EntityViewId;
import org.thingsboard.server.common.data.id.MobileAppBundleId;
import org.thingsboard.server.common.data.id.MobileAppId;
import org.thingsboard.server.common.data.id.NotificationId;
import org.thingsboard.server.common.data.id.NotificationRequestId;
import org.thingsboard.server.common.data.id.OAuth2ClientId;
import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationTemplateId;
import org.thingsboard.server.common.data.id.OtaPackageId;
@ -132,6 +134,13 @@ import org.thingsboard.server.common.data.kv.TsKvEntry;
import org.thingsboard.server.common.data.mobile.app.MobileApp;
import org.thingsboard.server.common.data.mobile.bundle.MobileAppBundle;
import org.thingsboard.server.common.data.mobile.bundle.MobileAppBundleInfo;
import org.thingsboard.server.common.data.notification.Notification;
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod;
import org.thingsboard.server.common.data.notification.NotificationRequest;
import org.thingsboard.server.common.data.notification.NotificationRequestInfo;
import org.thingsboard.server.common.data.notification.NotificationRequestPreview;
import org.thingsboard.server.common.data.notification.settings.NotificationSettings;
import org.thingsboard.server.common.data.notification.settings.UserNotificationSettings;
import org.thingsboard.server.common.data.oauth2.OAuth2Client;
import org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo;
import org.thingsboard.server.common.data.oauth2.OAuth2ClientLoginInfo;
@ -509,6 +518,99 @@ public class RestClient implements Closeable {
params).getBody();
}
public PageData<AlarmInfo> getAllAlarms(AlarmSearchStatus searchStatus, AlarmStatus status, TimePageLink pageLink, Boolean fetchOriginator) {
String urlSecondPart = "/api/alarms?";
Map<String, String> params = new HashMap<>();
if (fetchOriginator != null) {
params.put("fetchOriginator", String.valueOf(fetchOriginator));
urlSecondPart += "&fetchOriginator={fetchOriginator}";
}
if (searchStatus != null) {
params.put("searchStatus", searchStatus.name());
urlSecondPart += "&searchStatus={searchStatus}";
}
if (status != null) {
params.put("status", status.name());
urlSecondPart += "&status={status}";
}
addTimePageLinkToParam(params, pageLink);
return restTemplate.exchange(
baseURL + urlSecondPart + "&" + getTimeUrlParams(pageLink),
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<PageData<AlarmInfo>>() {
},
params).getBody();
}
public PageData<AlarmInfo> getAlarmsV2(EntityId entityId, List<AlarmSearchStatus> statusList, List<AlarmSeverity> severityList,
List<String> typeList, String assignedId, TimePageLink pageLink) {
String urlSecondPart = "/api/v2/alarm/{entityType}/{entityId}?";
Map<String, String> params = new HashMap<>();
params.put("entityType", entityId.getEntityType().name());
params.put("entityId", entityId.getId().toString());
if (!CollectionUtils.isEmpty(statusList)) {
params.put("statusList", listEnumToString(statusList));
urlSecondPart += "&statusList={statusList}";
}
if (!CollectionUtils.isEmpty(severityList)) {
params.put("severityList", listEnumToString(severityList));
urlSecondPart += "&severityList={severityList}";
}
if (!CollectionUtils.isEmpty(typeList)) {
params.put("typeList", String.join(",", typeList));
urlSecondPart += "&typeList={typeList}";
}
if (assignedId != null) {
params.put("assignedId", assignedId);
urlSecondPart += "&assignedId={assignedId}";
}
addTimePageLinkToParam(params, pageLink);
return restTemplate.exchange(
baseURL + urlSecondPart + "&" + getTimeUrlParams(pageLink),
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<PageData<AlarmInfo>>() {
},
params).getBody();
}
public PageData<AlarmInfo> getAllAlarmsV2(List<AlarmSearchStatus> statusList, List<AlarmSeverity> severityList,
List<String> typeList, String assignedId, TimePageLink pageLink) {
String urlSecondPart = "/api/v2/alarms?";
Map<String, String> params = new HashMap<>();
if (!CollectionUtils.isEmpty(statusList)) {
params.put("statusList", listEnumToString(statusList));
urlSecondPart += "&statusList={statusList}";
}
if (!CollectionUtils.isEmpty(severityList)) {
params.put("severityList", listEnumToString(severityList));
urlSecondPart += "&severityList={severityList}";
}
if (!CollectionUtils.isEmpty(typeList)) {
params.put("typeList", String.join(",", typeList));
urlSecondPart += "&typeList={typeList}";
}
if (assignedId != null) {
params.put("assignedId", assignedId);
urlSecondPart += "&assignedId={assignedId}";
}
addTimePageLinkToParam(params, pageLink);
return restTemplate.exchange(
baseURL + urlSecondPart + "&" + getTimeUrlParams(pageLink),
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<PageData<AlarmInfo>>() {
},
params).getBody();
}
public Optional<AlarmSeverity> getHighestAlarmSeverity(EntityId entityId, AlarmSearchStatus searchStatus, AlarmStatus status) {
Map<String, String> params = new HashMap<>();
params.put("entityType", entityId.getEntityType().name());
@ -1710,6 +1812,14 @@ public class RestClient implements Closeable {
}).getBody();
}
public JsonNode findEntityTimeseriesAndAttributesKeysByQuery(EntityDataQuery query) {
return restTemplate.exchange(
baseURL + "/api/entitiesQuery/find/keys",
HttpMethod.POST, new HttpEntity<>(query),
new ParameterizedTypeReference<JsonNode>() {
}).getBody();
}
public PageData<AlarmData> findAlarmDataByQuery(AlarmDataQuery query) {
return restTemplate.exchange(
baseURL + "/api/alarmsQuery/find",
@ -2158,9 +2268,9 @@ public class RestClient implements Closeable {
restTemplate.delete(baseURL + "/api/oauth2/client/{id}", oAuth2ClientId.getId());
}
public PageData<DomainInfo> getTenantDomainInfos() {
public PageData<DomainInfo> getTenantDomainInfos(PageLink pageLink) {
return restTemplate.exchange(
baseURL + "/api/domain/infos",
baseURL + "/api/domain/infos?" + getUrlParams(pageLink),
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<PageData<DomainInfo>>() {
@ -2192,9 +2302,9 @@ public class RestClient implements Closeable {
restTemplate.postForLocation(baseURL + "/api/domain/{id}/oauth2Clients", oauth2ClientIds, domainId.getId());
}
public PageData<MobileApp> getTenantMobileApps() {
public PageData<MobileApp> getTenantMobileApps(PageLink pageLink) {
return restTemplate.exchange(
baseURL + "/api/mobile/app",
baseURL + "/api/mobile/app?" + getUrlParams(pageLink),
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<PageData<MobileApp>>() {
@ -2222,9 +2332,9 @@ public class RestClient implements Closeable {
restTemplate.delete(baseURL + "/api/mobile/app/{id}", mobileAppId.getId());
}
public PageData<MobileAppBundleInfo> getTenantMobileBundleInfos() {
public PageData<MobileAppBundleInfo> getTenantMobileBundleInfos(PageLink pageLink) {
return restTemplate.exchange(
baseURL + "/api/mobile/bundle/infos",
baseURL + "/api/mobile/bundle/infos?" + getUrlParams(pageLink),
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<PageData<MobileAppBundleInfo>>() {
@ -2846,6 +2956,17 @@ public class RestClient implements Closeable {
}, params).getBody();
}
public PageData<UserEmailInfo> getUsersByQuery(PageLink pageLink) {
Map<String, String> params = new HashMap<>();
addPageLinkToParam(params, pageLink);
return restTemplate.exchange(
baseURL + "/api/users/info?" + getUrlParams(pageLink),
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<PageData<UserEmailInfo>>() {
}, params).getBody();
}
public PageData<User> getTenantAdmins(TenantId tenantId, PageLink pageLink) {
Map<String, String> params = new HashMap<>();
params.put("tenantId", tenantId.getId().toString());
@ -4144,6 +4265,138 @@ public class RestClient implements Closeable {
}
}
public PageData<Notification> getNotifications(PageLink pageLink) {
Map<String, String> params = new HashMap<>();
addPageLinkToParam(params, pageLink);
return restTemplate.exchange(
baseURL + "/api/notifications?" + getUrlParams(pageLink),
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<PageData<Notification>>() {
}, params).getBody();
}
public Integer getUnreadNotificationsCount(NotificationDeliveryMethod deliveryMethod) {
String uri = "/api/notifications/unread/count?";
Map<String, String> params = new HashMap<>();
if (deliveryMethod != null) {
params.put("deliveryMethod", deliveryMethod.name());
uri += "&deliveryMethod={deliveryMethod}";
}
return restTemplate.exchange(
baseURL + uri,
HttpMethod.GET,
HttpEntity.EMPTY,
Integer.class, params).getBody();
}
public void markNotificationAsRead(NotificationId notificationId) {
restTemplate.exchange(
baseURL + "/api/notification/{id}/read",
HttpMethod.PUT,
HttpEntity.EMPTY,
Void.class,
notificationId.getId());
}
public void markAllNotificationsAsRead(NotificationDeliveryMethod deliveryMethod) {
String uri = "/api/notifications/read?";
Map<String, String> params = new HashMap<>();
if (deliveryMethod != null) {
params.put("deliveryMethod", deliveryMethod.name());
uri += "&deliveryMethod={deliveryMethod}";
}
restTemplate.exchange(
baseURL + uri,
HttpMethod.PUT,
HttpEntity.EMPTY,
Void.class);
}
public void deleteNotification(NotificationId notificationId) {
restTemplate.delete(baseURL + "/api/notification/{id}", notificationId.getId());
}
public NotificationRequest createNotificationRequest(NotificationRequest notificationRequest) {
return restTemplate.postForEntity(baseURL + "/api/notification/request", notificationRequest, NotificationRequest.class).getBody();
}
public NotificationRequestPreview getNotificationRequestPreview(NotificationRequest notificationRequest, int recipientsPreviewSize) {
return restTemplate.postForEntity(baseURL + "/api/notification/request/preview?recipientsPreviewSize={recipientsPreviewSize}", notificationRequest, NotificationRequestPreview.class, recipientsPreviewSize).getBody();
}
public Optional<NotificationRequestInfo> getNotificationRequestById(NotificationRequestId notificationRequestId) {
try {
ResponseEntity<NotificationRequestInfo> notificationRequest = restTemplate.getForEntity(baseURL + "/api/notification/request/{id}", NotificationRequestInfo.class, notificationRequestId.getId());
return Optional.ofNullable(notificationRequest.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public PageData<NotificationRequestInfo> getNotificationRequests(PageLink pageLink) {
Map<String, String> params = new HashMap<>();
addPageLinkToParam(params, pageLink);
return restTemplate.exchange(
baseURL + "/api/notification/requests?" + getUrlParams(pageLink),
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<PageData<NotificationRequestInfo>>() {
}, params).getBody();
}
public void deleteNotificationRequest(NotificationRequestId notificationRequestId) {
restTemplate.delete(baseURL + "/api/notification/request/{id}", notificationRequestId.getId());
}
public NotificationSettings saveNotificationSettings(NotificationSettings notificationSettings) {
return restTemplate.postForEntity(baseURL + "/api/notification/settings", notificationSettings, NotificationSettings.class).getBody();
}
public Optional<NotificationSettings> getNotificationSettings() {
try {
ResponseEntity<NotificationSettings> notificationSettings = restTemplate.getForEntity(baseURL + "/api/notification/settings", NotificationSettings.class);
return Optional.ofNullable(notificationSettings.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public List<NotificationDeliveryMethod> getAvailableDeliveryMethods() {
return restTemplate.exchange(URI.create(
baseURL + "/api/notification/deliveryMethods"),
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<List<NotificationDeliveryMethod>>() {
}).getBody();
}
public UserNotificationSettings saveUserNotificationSettings(UserNotificationSettings userNotificationSettings) {
return restTemplate.postForEntity(baseURL + "/api/notification/settings/user", userNotificationSettings, UserNotificationSettings.class).getBody();
}
public Optional<UserNotificationSettings> getUserNotificationSettings() {
try {
ResponseEntity<UserNotificationSettings> userNotificationSettings = restTemplate.getForEntity(baseURL + "/api/notification/settings/user", UserNotificationSettings.class);
return Optional.ofNullable(userNotificationSettings.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
private String getTimeUrlParams(TimePageLink pageLink) {
String urlParams = getUrlParams(pageLink);
if (pageLink.getStartTime() != null) {

Loading…
Cancel
Save