Skip to content

Instantly share code, notes, and snippets.

View Romeh's full-sized avatar

MRomeh Romeh

View GitHub Profile
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AlertManagerApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("INTEGRATION_TEST")
public class AlertManagerApplicationIT {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate template;
private URL base;
@Before
@RunWith(MockitoJUnitRunner.class)
public class IgniteAlertsSoreTest {
@Mock
private Ignite ignite;
@Mock
Cache<String, List<AlertEntry>> cache;
@Mock
IgniteCache IgniteCache;
@InjectMocks
private IgniteAlertsStore igniteAlertsStore;
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-core</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring</artifactId>
<version>${ignite.version}</version>
</dependency>
@Component
public class IgniteAlertConfigStore implements AlertsConfigStore {
private static final Logger logger = LoggerFactory.getLogger(IgniteAlertConfigStore.class);
// here it will be injected as a spring bean
@Autowired
private Ignite ignite;
@Override
public AlertConfigEntry getConfigForServiceIdCodeId(String serviceId, String codeId) {
// durable file memory persistence
if(enableFilePersistence){
PersistentStoreConfiguration persistentStoreConfiguration = new PersistentStoreConfiguration();
persistentStoreConfiguration.setPersistentStorePath("./data/store");
persistentStoreConfiguration.setWalArchivePath("./data/walArchive");
persistentStoreConfiguration.setWalStorePath("./data/walStore");
igniteConfiguration.setPersistentStoreConfiguration(persistentStoreConfiguration);
}
@Override
public List<AlertEntry> getAlertForServiceId(String serviceId) {
final String sql = "serviceId = ?";
// create the sql query object with entity type of the value part of the key value cache
SqlQuery<String, AlertEntry> query = new SqlQuery<>(AlertEntry.class, sql);
// set the query params
query.setArgs(serviceId);
//then execute it over the cache
return Optional.ofNullable(getAlertsCache().query(query).getAll().stream().map(stringAlertEntryEntry -> stringAlertEntryEntry.getValue()).collect(Collectors.toList()))
.orElseThrow(() -> new ResourceNotFoundException(String.format("Alert for %s not found", serviceId)));
@Override
public void updateAlertEntry(String serviceId, String serviceCode, AlertEntry alertEntry) {
final IgniteCache<String, AlertEntry> alertsCache = getAlertsCache();
// update the alert entry via cache invoke for atomicity
alertsCache.invoke(alertEntry.getAlertId(), (mutableEntry, objects) -> {
if (mutableEntry.exists() && mutableEntry.getValue() != null) {
logger.debug("updating alert entry into the cache store invoke: {},{}", serviceId, serviceCode);
mutableEntry.setValue(alertEntry);
} else {
throw new ResourceNotFoundException(String.format("Alert for %s with %s not found", serviceId, serviceCode));
@Configuration
public class AlertManagerConfiguration {
@Value("${mail.service.baseUrl}")
private String baseUrl;
@Value("${mail.service.user}")
private String user;
@Value("${mail.service.password}")
private String password;
@Value("${enableFilePersistence}")
@Builder
@Getter
@Setter
@ToString
@EqualsAndHashCode
public class AlertEntry implements Serializable {
@ApiModelProperty(notes = "the key value alert content for error description required to be entered by user into REST API ", required = true)
@NotNull
private Map<String,String> alertContent;
@ApiModelProperty(notes = "alert error code required to be entered by user into REST API ", required = true)
import org.apache.ignite.*;
import org.apache.ignite.events.DiscoveryEvent;
import org.apache.ignite.events.EventType;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
public class NodeApp {