Skip to content

Commit 7c93d83

Browse files
authored
NMS-17749: Zenith Connect UI, connect with Registration Rest API (#7718)
* NMS-17749: Zenith Connect UI, connect with Registration Rest API * NMS-17749: Remove some nested if blocks
1 parent fb0b51a commit 7c93d83

File tree

12 files changed

+570
-173
lines changed

12 files changed

+570
-173
lines changed

features/zenith-connect/persistence/src/main/java/org/opennms/features/zenithconnect/persistence/api/ZenithConnectPersistenceException.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,27 @@
2222
package org.opennms.features.zenithconnect.persistence.api;
2323

2424
public class ZenithConnectPersistenceException extends Exception {
25+
private boolean attemptedToAddDuplicate;
26+
2527
public ZenithConnectPersistenceException() {
2628
super();
2729
}
2830

31+
public ZenithConnectPersistenceException(boolean attemptedToAddDuplicate) {
32+
super();
33+
34+
this.attemptedToAddDuplicate = attemptedToAddDuplicate;
35+
}
36+
2937
public ZenithConnectPersistenceException(String message) {
3038
super(message);
3139
}
3240

3341
public ZenithConnectPersistenceException(String message, Throwable e) {
3442
super(message, e);
3543
}
44+
45+
public boolean isAttemptedToAddDuplicate() {
46+
return attemptedToAddDuplicate;
47+
}
3648
}

features/zenith-connect/persistence/src/main/java/org/opennms/features/zenithconnect/persistence/api/ZenithConnectPersistenceService.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,22 @@ public interface ZenithConnectPersistenceService {
3232

3333
/**
3434
* Add a new registration.
35-
* Currently we only support a single registration.
35+
* Currently we only support a single registration, so this will replace any registration that
36+
* already exists.
3637
* Returns the added registration, including an id and createTimeMs.
38+
* @param preventDuplicates If true, will check to see if the given registration appears to be
39+
* a duplicate of an existing registration (same systemId and same accessToken or refreshToken).
40+
* If so, will throw an exception. This is to prevent e.g. the UI from sending multiple
41+
* duplicate add requests.
3742
*/
38-
ZenithConnectRegistration addRegistration(ZenithConnectRegistration registration) throws ZenithConnectPersistenceException;
43+
ZenithConnectRegistration addRegistration(ZenithConnectRegistration registration, boolean preventDuplicates)
44+
throws ZenithConnectPersistenceException;
45+
46+
/**
47+
* Add a new registration, ignoring preventDuplicates.
48+
*/
49+
ZenithConnectRegistration addRegistration(ZenithConnectRegistration registration)
50+
throws ZenithConnectPersistenceException;
3951

4052
/**
4153
* Update an existing registration. The given id and registration.id must match an existing registration.

features/zenith-connect/persistence/src/main/java/org/opennms/features/zenithconnect/persistence/impl/ZenithConnectPersistenceServiceImpl.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ public ZenithConnectRegistrations getRegistrations() throws ZenithConnectPersist
5959
}
6060

6161
/** {@inheritDoc} */
62-
public ZenithConnectRegistration addRegistration(ZenithConnectRegistration registration) throws ZenithConnectPersistenceException {
62+
public ZenithConnectRegistration addRegistration(ZenithConnectRegistration registration, boolean preventDuplicates)
63+
throws ZenithConnectPersistenceException {
64+
if (preventDuplicates && isDuplicateRegistration(registration)) {
65+
throw new ZenithConnectPersistenceException(true);
66+
}
67+
6368
registration.id = UUID.randomUUID().toString();
6469
registration.createTimeMs = Instant.now().toEpochMilli();
6570

@@ -69,6 +74,11 @@ public ZenithConnectRegistration addRegistration(ZenithConnectRegistration regis
6974
return registration;
7075
}
7176

77+
public ZenithConnectRegistration addRegistration(ZenithConnectRegistration registration)
78+
throws ZenithConnectPersistenceException {
79+
return addRegistration(registration, false);
80+
}
81+
7282
/** {@inheritDoc} */
7383
public void updateRegistration(String id, ZenithConnectRegistration registration) throws ZenithConnectPersistenceException {
7484
if (Strings.isNullOrEmpty(id) ||
@@ -136,4 +146,31 @@ private void setRegistrations(ZenithConnectRegistrations registrations) throws Z
136146
throw new ZenithConnectPersistenceException("Could not serialize Zenith Connect registrations", e);
137147
}
138148
}
149+
150+
private boolean isDuplicateRegistration(ZenithConnectRegistration registration)
151+
throws ZenithConnectPersistenceException {
152+
ZenithConnectRegistrations existingRegistrations = getRegistrationsImpl();
153+
ZenithConnectRegistration existingRegistration = existingRegistrations.first();
154+
155+
if (existingRegistration == null) {
156+
return false;
157+
}
158+
159+
boolean systemIdsMatch = registration.systemId != null && existingRegistration.systemId != null &&
160+
registration.systemId.equals(existingRegistration.systemId);
161+
162+
if (systemIdsMatch) {
163+
boolean accessTokenMatches = registration.accessToken != null &&
164+
existingRegistration.accessToken != null &&
165+
registration.accessToken.equals(existingRegistration.accessToken);
166+
167+
boolean refreshTokenMatches = registration.refreshToken != null &&
168+
existingRegistration.refreshToken != null &&
169+
registration.refreshToken.equals(existingRegistration.refreshToken);
170+
171+
return accessTokenMatches || refreshTokenMatches;
172+
}
173+
174+
return false;
175+
}
139176
}

features/zenith-connect/persistence/src/test/java/org/opennms/features/zenithconnect/persistence/impl/ZenithConnectPersistenceServiceImplIT.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,38 @@ public void testAddAndGetRegistrations() throws ZenithConnectPersistenceExceptio
8383
assertThat(createdRegistration.createTimeMs, greaterThanOrEqualTo(currentTime));
8484
}
8585

86+
@Test
87+
public void testAddDuplicateRegistrations() throws ZenithConnectPersistenceException {
88+
// add a registration
89+
var registration = createDefaultRegistration();
90+
long currentTime = Instant.now().toEpochMilli();
91+
persistenceService.addRegistration(registration);
92+
93+
// make sure it was added correctly
94+
ZenithConnectRegistrations registrations = persistenceService.getRegistrations();
95+
assertNotNull(registrations);
96+
97+
ZenithConnectRegistration createdRegistration = registrations.first();
98+
assertRegistrationFieldsEqual(registration, createdRegistration);
99+
100+
assertThat(createdRegistration.id, not(emptyString()));
101+
assertThat(createdRegistration.id, matchesPattern(UUID_REGEX));
102+
assertThat(createdRegistration.createTimeMs, greaterThanOrEqualTo(currentTime));
103+
104+
// Now add it again, should fail with specific exception and flag
105+
var duplicate = createDefaultRegistration();
106+
ZenithConnectPersistenceException exception = null;
107+
108+
try {
109+
persistenceService.addRegistration(duplicate, true);
110+
} catch (ZenithConnectPersistenceException e) {
111+
exception = e;
112+
}
113+
114+
assertNotNull("Adding duplicate to persistenceService.addRegistration should throw a ZenithConnectPersistenceException.", exception);
115+
assertTrue("Adding duplicate to persistenceService.addRegistration should set attemptedToAddDuplicate.", exception.isAttemptedToAddDuplicate());
116+
}
117+
86118
@Test
87119
public void testUpdateRegistration() throws ZenithConnectPersistenceException {
88120
// add a registration

features/zenith-connect/rest/src/main/java/org/opennms/features/zenithconnect/rest/impl/DefaultZenithConnectRestService.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public DefaultZenithConnectRestService(ZenithConnectPersistenceService persisten
4646
this.persistenceService = persistenceService;
4747
}
4848

49-
/** {@inheritDoc} */
49+
/**
50+
* Get all registrations.
51+
* Currently, there is only one registration at a time, so this will return a
52+
* ZenithConnectRegistrations object with a single object in the registrations list.
53+
*/
5054
@Override
5155
public Response getRegistrations() {
5256
try {
@@ -58,19 +62,31 @@ public Response getRegistrations() {
5862
}
5963
}
6064

61-
/** {@inheritDoc} */
65+
/**
66+
* Add the given registration.
67+
* This will throw a 400 Bad Request if the request is a duplicate of an existing registration.
68+
* Throws a 500 Server Error if the request is malformed, or there was an error updating the database.
69+
*/
6270
@Override
6371
public Response addRegistration(ZenithConnectRegistration registration) {
6472
try {
65-
var newRegistration = persistenceService.addRegistration(registration);
73+
var newRegistration = persistenceService.addRegistration(registration, true);
6674
return Response.status(Response.Status.CREATED).entity(newRegistration).build();
6775
} catch (ZenithConnectPersistenceException e) {
68-
LOG.error("Could not add registration: {}.", e.getMessage(), e);
76+
LOG.error("Could not add registration: {}. Attempted to add duplicate: {}",
77+
e.getMessage(), e.isAttemptedToAddDuplicate(), e);
78+
79+
if (e.isAttemptedToAddDuplicate()) {
80+
return Response.status(Response.Status.BAD_REQUEST).build();
81+
}
82+
6983
return Response.serverError().build();
7084
}
7185
}
7286

73-
/** {@inheritDoc} */
87+
/**
88+
* Update an existing registration. The id and registration.id must match an existing registration.
89+
*/
7490
@Override
7591
public Response updateRegistration(String id, ZenithConnectRegistration registration) {
7692
try {

0 commit comments

Comments
 (0)