use of com.redhat.cloud.notifications.routers.models.SettingsValues in project notifications-backend by RedHatInsights.
the class UserConfigResource method getSettingsSchema.
@GET
@Path("/notification-preference")
@Produces(APPLICATION_JSON)
@Operation(hidden = true)
public Response getSettingsSchema(@Context SecurityContext sec, @QueryParam("bundleName") String bundleName) {
final RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
final String account = principal.getAccount();
final String name = principal.getName();
SettingsValues settingsValues = getSettingsValueForUser(account, name, bundleName);
String jsonFormString = settingsValuesToJsonForm(settingsValues);
Response.ResponseBuilder builder;
builder = Response.ok(jsonFormString);
EntityTag etag = new EntityTag(String.valueOf(jsonFormString.hashCode()));
builder.header("ETag", etag);
return builder.build();
}
use of com.redhat.cloud.notifications.routers.models.SettingsValues in project notifications-backend by RedHatInsights.
the class LifecycleITest method subscribeUserPreferences.
private void subscribeUserPreferences(Header identityHeader, String bundleName, String appName) {
SettingsValues values = new SettingsValues();
SettingsValues.ApplicationSettingsValue applicationSettingsValue = new SettingsValues.ApplicationSettingsValue();
applicationSettingsValue.notifications.put(EmailSubscriptionType.INSTANT, true);
SettingsValues.BundleSettingsValue bundleSettingsValue = new SettingsValues.BundleSettingsValue();
bundleSettingsValue.applications.put(appName, applicationSettingsValue);
values.bundles.put(bundleName, bundleSettingsValue);
given().basePath(API_NOTIFICATIONS_V_1_0).header(identityHeader).contentType(JSON).body(Json.encode(values)).when().post("/user-config/notification-preference").then().statusCode(200).contentType(TEXT);
}
use of com.redhat.cloud.notifications.routers.models.SettingsValues in project notifications-backend by RedHatInsights.
the class UserConfigResourceTest method testSettings.
@Test
void testSettings() {
String tenant = "empty";
String orgId = "empty";
String username = "user";
String identityHeaderValue = TestHelpers.encodeRHIdentityInfo(tenant, orgId, username);
Header identityHeader = TestHelpers.createRHIdentityHeader(identityHeaderValue);
MockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerConfig.RbacAccess.FULL_ACCESS);
String bundle = "rhel";
String application = "policies";
when(templateEngineClient.isSubscriptionTypeSupported(bundle, application, INSTANT)).thenReturn(TRUE);
when(templateEngineClient.isSubscriptionTypeSupported(bundle, application, DAILY)).thenReturn(TRUE);
SettingsValueJsonForm jsonForm = given().header(identityHeader).queryParam("bundleName", bundle).when().get("/user-config/notification-preference").then().statusCode(200).contentType(JSON).extract().body().as(SettingsValueJsonForm.class);
Field rhelPolicy = rhelPolicyForm(jsonForm);
assertNotNull(rhelPolicy, "RHEL policies not found");
SettingsValues settingsValues = createSettingsValue(bundle, application, false, false);
given().header(identityHeader).when().contentType(JSON).body(Json.encode(settingsValues)).post("/user-config/notification-preference").then().statusCode(200).contentType(TEXT);
jsonForm = given().header(identityHeader).when().get("/user-config/notification-preference?bundleName=rhel").then().statusCode(200).contentType(JSON).extract().body().as(SettingsValueJsonForm.class);
rhelPolicy = rhelPolicyForm(jsonForm);
assertNotNull(rhelPolicy, "RHEL policies not found");
Map<EmailSubscriptionType, Boolean> initialValues = extractNotificationValues(rhelPolicy, bundle, application);
assertEquals(initialValues, settingsValues.bundles.get(bundle).applications.get(application).notifications);
UserConfigPreferences preferences = given().header(identityHeader).when().get(String.format("/user-config/notification-preference/%s/%s", bundle, application)).then().statusCode(200).contentType(JSON).extract().body().as(UserConfigPreferences.class);
assertEquals(false, preferences.getDailyEmail());
assertEquals(false, preferences.getInstantEmail());
// Daily to true
settingsValues = createSettingsValue(bundle, application, true, false);
given().header(identityHeader).when().contentType(JSON).body(Json.encode(settingsValues)).post("/user-config/notification-preference").then().statusCode(200).contentType(TEXT);
jsonForm = given().header(identityHeader).when().get("/user-config/notification-preference?bundleName=rhel").then().statusCode(200).contentType(JSON).extract().body().as(SettingsValueJsonForm.class);
rhelPolicy = rhelPolicyForm(jsonForm);
assertNotNull(rhelPolicy, "RHEL policies not found");
initialValues = extractNotificationValues(rhelPolicy, bundle, application);
assertEquals(initialValues, settingsValues.bundles.get(bundle).applications.get(application).notifications);
preferences = given().header(identityHeader).when().get(String.format("/user-config/notification-preference/%s/%s", bundle, application)).then().statusCode(200).contentType(JSON).extract().body().as(UserConfigPreferences.class);
assertEquals(true, preferences.getDailyEmail());
assertEquals(false, preferences.getInstantEmail());
// Instant to true
settingsValues = createSettingsValue(bundle, application, false, true);
given().header(identityHeader).when().contentType(JSON).body(Json.encode(settingsValues)).post("/user-config/notification-preference").then().statusCode(200).contentType(TEXT);
jsonForm = given().header(identityHeader).when().get("/user-config/notification-preference?bundleName=rhel").then().statusCode(200).contentType(JSON).extract().body().as(SettingsValueJsonForm.class);
rhelPolicy = rhelPolicyForm(jsonForm);
assertNotNull(rhelPolicy, "RHEL policies not found");
initialValues = extractNotificationValues(rhelPolicy, bundle, application);
assertEquals(initialValues, settingsValues.bundles.get(bundle).applications.get(application).notifications);
preferences = given().header(identityHeader).when().get(String.format("/user-config/notification-preference/%s/%s", bundle, application)).then().statusCode(200).contentType(JSON).extract().body().as(UserConfigPreferences.class);
assertEquals(false, preferences.getDailyEmail());
assertEquals(true, preferences.getInstantEmail());
// Both to true
settingsValues = createSettingsValue(bundle, application, true, true);
given().header(identityHeader).when().contentType(JSON).body(Json.encode(settingsValues)).post("/user-config/notification-preference").then().statusCode(200).contentType(TEXT);
jsonForm = given().header(identityHeader).when().get("/user-config/notification-preference?bundleName=rhel").then().statusCode(200).contentType(JSON).extract().body().as(SettingsValueJsonForm.class);
rhelPolicy = rhelPolicyForm(jsonForm);
assertNotNull(rhelPolicy, "RHEL policies not found");
initialValues = extractNotificationValues(rhelPolicy, bundle, application);
assertEquals(initialValues, settingsValues.bundles.get(bundle).applications.get(application).notifications);
preferences = given().header(identityHeader).when().get(String.format("/user-config/notification-preference/%s/%s", bundle, application)).then().statusCode(200).contentType(JSON).extract().body().as(UserConfigPreferences.class);
assertEquals(true, preferences.getDailyEmail());
assertEquals(true, preferences.getInstantEmail());
// does not fail if we have unknown apps in our bundle's settings
emailSubscriptionRepository.subscribe(tenant, username, bundle, "not-found-app", DAILY);
given().header(identityHeader).when().queryParam("bundleName", bundle).get("/user-config/notification-preference").then().statusCode(200).contentType(JSON);
emailSubscriptionRepository.unsubscribe(tenant, username, "not-found-bundle", "not-found-app", DAILY);
// Fails if we don't specify the bundleName
given().header(identityHeader).when().get("/user-config/notification-preference").then().statusCode(400).contentType(JSON);
// does not add if we try to create unknown bundle/apps
SettingsValues settingsValue = createSettingsValue("not-found-bundle-2", "not-found-app-2", true, true);
given().header(identityHeader).when().contentType(JSON).body(Json.encode(settingsValue)).post("/user-config/notification-preference").then().statusCode(200).contentType(TEXT);
assertNull(emailSubscriptionRepository.getEmailSubscription(tenant, username, "not-found-bundle-2", "not-found-app-2", DAILY));
assertNull(emailSubscriptionRepository.getEmailSubscription(tenant, username, "not-found-bundle", "not-found-app", INSTANT));
// Does not add event type if is not supported by the templates
when(templateEngineClient.isSubscriptionTypeSupported(bundle, application, DAILY)).thenReturn(FALSE);
SettingsValueJsonForm settingsValueJsonForm = given().header(identityHeader).when().get("/user-config/notification-preference?bundleName=rhel").then().statusCode(200).contentType(JSON).extract().body().as(SettingsValueJsonForm.class);
Field rhelPolicy2 = rhelPolicyForm(settingsValueJsonForm);
assertNotNull(rhelPolicy2, "RHEL policies not found");
assertEquals(1, rhelPolicy2.fields.get(0).fields.size());
assertEquals("bundles[rhel].applications[policies].notifications[INSTANT]", rhelPolicy2.fields.get(0).fields.get(0).name);
}
use of com.redhat.cloud.notifications.routers.models.SettingsValues in project notifications-backend by RedHatInsights.
the class UserConfigResource method saveSettings.
@POST
@Path("/notification-preference")
@Consumes(APPLICATION_JSON)
@Produces(TEXT_PLAIN)
@Operation(hidden = true)
@Transactional
public Response saveSettings(@Context SecurityContext sec, @Valid SettingsValues values) {
final RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
final String account = principal.getAccount();
final String name = principal.getName();
final List<Boolean> subscriptionRequests = new ArrayList<>();
values.bundles.forEach((bundleName, bundleSettingsValue) -> bundleSettingsValue.applications.forEach((applicationName, applicationSettingsValue) -> {
Application app = applicationRepository.getApplication(bundleName, applicationName);
applicationSettingsValue.notifications.forEach((emailSubscriptionType, subscribed) -> {
if (subscribed) {
if (app != null) {
subscriptionRequests.add(emailSubscriptionRepository.subscribe(account, name, bundleName, applicationName, emailSubscriptionType));
}
} else {
subscriptionRequests.add(emailSubscriptionRepository.unsubscribe(account, name, bundleName, applicationName, emailSubscriptionType));
}
});
}));
boolean allisSuccess = subscriptionRequests.stream().allMatch(Boolean.TRUE::equals);
Response.ResponseBuilder builder;
if (allisSuccess) {
builder = Response.ok();
} else {
// Prevent from saving
builder = Response.serverError().entity("Storing of settings Failed.");
builder.type("text/plain");
}
return builder.build();
}
use of com.redhat.cloud.notifications.routers.models.SettingsValues in project notifications-backend by RedHatInsights.
the class UserConfigResource method getSettingsValueForUser.
/**
* Pulls the user settings values of an user across all the know applications of a bundle
*/
private SettingsValues getSettingsValueForUser(String account, String username, String bundleName) {
if (bundleName == null || bundleName.equals("")) {
throw new BadRequestException("bundleName must have a value");
}
SettingsValues settingsValues = new SettingsValues();
Bundle bundle = bundleRepository.getBundle(bundleName);
if (bundle == null) {
throw new BadRequestException("Unknown bundleName: " + bundleName);
} else {
BundleSettingsValue bundleSettingsValue = new BundleSettingsValue();
bundleSettingsValue.displayName = bundle.getDisplayName();
settingsValues.bundles.put(bundle.getName(), bundleSettingsValue);
for (Application application : applicationRepository.getApplications(bundle.getName())) {
ApplicationSettingsValue applicationSettingsValue = new ApplicationSettingsValue();
applicationSettingsValue.displayName = application.getDisplayName();
settingsValues.bundles.get(bundle.getName()).applications.put(application.getName(), applicationSettingsValue);
for (EmailSubscriptionType emailSubscriptionType : EmailSubscriptionType.values()) {
// TODO NOTIF-450 How do we deal with a failure here? What kind of response should be sent to the UI when the engine is down?
boolean supported = templateEngineClient.isSubscriptionTypeSupported(bundle.getName(), application.getName(), emailSubscriptionType);
if (supported) {
settingsValues.bundles.get(bundle.getName()).applications.get(application.getName()).notifications.put(emailSubscriptionType, false);
}
}
}
List<EmailSubscription> emailSubscriptions = emailSubscriptionRepository.getEmailSubscriptionsForUser(account, username);
for (EmailSubscription emailSubscription : emailSubscriptions) {
if (settingsValues.bundles.containsKey(emailSubscription.getApplication().getBundle().getName())) {
BundleSettingsValue bundleSettings = settingsValues.bundles.get(emailSubscription.getApplication().getBundle().getName());
if (bundleSettings.applications.containsKey(emailSubscription.getApplication().getName())) {
ApplicationSettingsValue applicationSettingsValue = bundleSettings.applications.get(emailSubscription.getApplication().getName());
if (applicationSettingsValue.notifications.containsKey(emailSubscription.getType())) {
bundleSettings.applications.get(emailSubscription.getApplication().getName()).notifications.put(emailSubscription.getType(), true);
}
}
}
}
return settingsValues;
}
}
Aggregations