use of io.hops.hopsworks.persistence.entity.alertmanager.AlertReceiver in project hopsworks by logicalclocks.
the class TestAlertManagerConfigTimer method setUp.
@Before
public void setUp() throws ServiceDiscoveryException, AlertManagerConfigFileNotFoundException, AlertManagerConfigReadException, JsonProcessingException {
String alertmanagerConfigPath = TestAlertManagerConfigTimer.class.getResource("/alertmanager.yml").getPath();
alertManagerConfigDBPath = TestAlertManagerConfigTimer.class.getResource("/alertmanagerDB.yml").getPath();
client = Mockito.mock(AlertManagerClient.class);
alertManagerConfigController = new AlertManagerConfigController.Builder().withConfigPath(alertmanagerConfigPath).withClient(client).build();
alertManagerConfigFacade = Mockito.mock(AlertManagerConfigFacade.class);
Mockito.doAnswer((Answer<Void>) invocation -> {
Object[] args = invocation.getArguments();
LOGGER.log(Level.INFO, "Save to database: {0}.", args);
return null;
}).when(alertManagerConfigFacade).save(Mockito.any());
Mockito.doAnswer((Answer<Void>) invocation -> {
Object[] args = invocation.getArguments();
LOGGER.log(Level.INFO, "Update database: {0}.", args);
return null;
}).when(alertManagerConfigFacade).update(Mockito.any());
AlertManagerConfig alertManagerConfig = read(new File(alertManagerConfigDBPath));
AlertManagerConfigEntity alertManagerConfigEntity = new AlertManagerConfigEntity();
alertManagerConfigEntity.setId(1);
alertManagerConfigEntity.setCreated(new Date());
alertManagerConfigEntity.setContent(toJson(alertManagerConfig));
Mockito.when(alertManagerConfigFacade.getLatest()).thenReturn(Optional.of(alertManagerConfigEntity));
alertReceiverFacade = Mockito.mock(AlertReceiverFacade.class);
List<AlertReceiver> receivers = createReceivers();
Mockito.when(alertReceiverFacade.findAll()).thenReturn(receivers);
Mockito.doAnswer((Answer<Optional<AlertReceiver>>) invocation -> {
Object[] args = invocation.getArguments();
Optional<AlertReceiver> alertReceiver = receivers.stream().filter(receiver -> receiver.getName().equals(args[0])).findFirst();
return alertReceiver;
}).when(alertReceiverFacade).findByName(Mockito.any());
alertManagerConfiguration = new AlertManagerConfiguration(client, alertManagerConfigController, alertManagerConfigFacade, alertReceiverFacade);
alertManagerConfigBackup = alertManagerConfigController.read();
alertManagerConfigBackupDB = read(new File(alertManagerConfigDBPath));
}
use of io.hops.hopsworks.persistence.entity.alertmanager.AlertReceiver in project hopsworks by logicalclocks.
the class AlertManagerConfiguration method saveReceiverToDatabase.
private void saveReceiverToDatabase(String name, Receiver receiver) throws AlertManagerConfigUpdateException {
try {
ObjectMapper objectMapper = new ObjectMapper();
JSONObject jsonObject = new JSONObject(objectMapper.writeValueAsString(receiver));
AlertReceiver alertReceiver;
Optional<AlertReceiver> optionalAlertReceiver = alertReceiverFacade.findByName(name);
if (optionalAlertReceiver.isPresent()) {
alertReceiver = optionalAlertReceiver.get();
alertReceiver.setName(receiver.getName());
alertReceiver.setConfig(jsonObject);
alertReceiverFacade.update(alertReceiver);
} else {
alertReceiver = new AlertReceiver(receiver.getName(), jsonObject);
alertReceiverFacade.save(alertReceiver);
}
} catch (JsonProcessingException e) {
throw new AlertManagerConfigUpdateException("Failed to save receiver to database. Failed to parse receiver to json. " + e.getMessage(), e);
}
}
Aggregations