-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
210 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 3 additions & 5 deletions
8
.../src/main/java/net/zscript/javaclient/commandbuilder/ZscriptFieldOutOfRangeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
package net.zscript.javaclient.commandbuilder; | ||
|
||
import static java.lang.String.format; | ||
|
||
public class ZscriptFieldOutOfRangeException extends ZscriptClientException { | ||
|
||
public ZscriptFieldOutOfRangeException(String format, Object... params) { | ||
super(format(format, params)); | ||
super(format, params); | ||
} | ||
|
||
public ZscriptFieldOutOfRangeException(String msg, Exception e) { | ||
super(msg, e); | ||
public ZscriptFieldOutOfRangeException(String format, Exception e, Object... params) { | ||
super(format, e, params); | ||
} | ||
} |
9 changes: 3 additions & 6 deletions
9
...api/src/main/java/net/zscript/javaclient/commandbuilder/ZscriptMissingFieldException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
package net.zscript.javaclient.commandbuilder; | ||
|
||
import static java.lang.String.format; | ||
|
||
public class ZscriptMissingFieldException extends ZscriptClientException { | ||
|
||
public ZscriptMissingFieldException(String format, Object... params) { | ||
super(format(format, params)); | ||
super(format, params); | ||
} | ||
|
||
public ZscriptMissingFieldException(String msg, Exception e) { | ||
super(msg, e); | ||
public ZscriptMissingFieldException(String format, Exception cause, Object... params) { | ||
super(format, cause, params); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...src/main/java/net/zscript/javaclient/commandbuilder/notifications/NotificationHandle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,51 @@ | ||
package net.zscript.javaclient.commandbuilder.notifications; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import net.zscript.javaclient.commandPaths.Response; | ||
import net.zscript.javaclient.commandPaths.ResponseExecutionPath; | ||
import net.zscript.javaclient.commandbuilder.ZscriptClientException; | ||
import net.zscript.javaclient.commandbuilder.ZscriptResponse; | ||
|
||
public abstract class NotificationHandle { | ||
@Nonnull | ||
public abstract <T extends ZscriptResponse> NotificationSection<T> getSection(NotificationSectionId<T> response); | ||
|
||
@Nonnull | ||
@Deprecated | ||
public abstract List<NotificationSection<?>> getSections(); | ||
|
||
/** | ||
* Creates a list of the right types of notification section content | ||
* | ||
* @param responsePath | ||
* @return | ||
*/ | ||
public List<ZscriptResponse> buildNotificationContent(final ResponseExecutionPath responsePath) { | ||
final List<ZscriptResponse> actualResponses = new ArrayList<>(); | ||
|
||
final Iterator<NotificationSection<?>> sectionIt = getSections().iterator(); | ||
final Iterator<Response> respIt = responsePath.iterator(); | ||
|
||
for (int i = 0; sectionIt.hasNext() && respIt.hasNext(); i++) { | ||
final NotificationSection<?> section = sectionIt.next(); | ||
final Response response = respIt.next(); | ||
final ZscriptResponse content = section.parseResponse(response.getFields()); | ||
if (!content.isValid()) { | ||
throw new ZscriptClientException("Invalid notification section [ntf=%s, section#=%s, section=%s, text=%s]", this, i, section, response); | ||
} | ||
|
||
actualResponses.add(content); | ||
} | ||
if (sectionIt.hasNext()) { | ||
throw new IllegalStateException("Notification needs more response sections"); | ||
} | ||
if (respIt.hasNext()) { | ||
throw new IllegalStateException("Too many response sections received for notification"); | ||
} | ||
return actualResponses; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
.../test/java/net/zscript/model/modules/testing/test/JavaCommandBuilderNotificationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package net.zscript.model.modules.testing.test; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import net.zscript.client.modules.test.testing.TestingModule; | ||
import net.zscript.javaclient.addressing.CompleteAddressedResponse; | ||
import net.zscript.javaclient.commandbuilder.ZscriptResponse; | ||
import net.zscript.javaclient.commandbuilder.notifications.NotificationSection; | ||
import net.zscript.javaclient.tokens.ExtendingTokenBuffer; | ||
import net.zscript.tokenizer.TokenBuffer.TokenReader; | ||
import net.zscript.tokenizer.Tokenizer; | ||
|
||
public class JavaCommandBuilderNotificationTest { | ||
final ExtendingTokenBuffer buffer = new ExtendingTokenBuffer(); | ||
final Tokenizer tokenizer = new Tokenizer(buffer.getTokenWriter(), 2); | ||
final TokenReader tokenReader = buffer.getTokenReader(); | ||
|
||
@Test | ||
void shouldDefineNotificationClasses() { | ||
TestingModule.TestNtfANotificationId ntfIdA = TestingModule.TestNtfANotificationId.get(); | ||
assertThat(ntfIdA.getHandleType()).isEqualTo(TestingModule.TestNtfANotificationHandle.class); | ||
|
||
TestingModule.TestNtfANotificationHandle handleA = ntfIdA.newHandle(); | ||
List<NotificationSection<?>> sectionsA = handleA.getSections(); | ||
assertThat(sectionsA).hasSize(1); | ||
assertThat(sectionsA).hasExactlyElementsOfTypes(TestingModule.Expr1NotificationSection.class); | ||
|
||
TestingModule.TestNtfBNotificationHandle handleB = TestingModule.TestNtfBNotificationId.get().newHandle(); | ||
List<NotificationSection<?>> sectionsB = handleB.getSections(); | ||
assertThat(sectionsB).hasSize(2); | ||
assertThat(sectionsB).hasExactlyElementsOfTypes(TestingModule.Expr1NotificationSection.class, TestingModule.Expr2NotificationSection.class); | ||
|
||
assertThat(sectionsB.get(0).getResponseType()).isEqualTo(TestingModule.Expr1NotificationSectionContent.class); | ||
} | ||
|
||
@Test | ||
void shouldCreateNotificationWithRequiredFields() { | ||
"!234 Dab Lcd & Xef\n".chars().forEach(c -> tokenizer.accept((byte) c)); | ||
|
||
final TestingModule.TestNtfBNotificationHandle handle = TestingModule.TestNtfBNotificationId.get().newHandle(); | ||
|
||
final CompleteAddressedResponse car = CompleteAddressedResponse.parse(buffer.getTokenReader()); | ||
assertThat(car.asResponse().hasAddress()).isFalse(); | ||
assertThat(car.getContent().getResponseValue()).isEqualTo(0x234); | ||
|
||
final List<ZscriptResponse> sections = handle.buildNotificationContent(car.getContent().getExecutionPath()); | ||
assertThat(sections) | ||
.hasExactlyElementsOfTypes(TestingModule.Expr1NotificationSectionContent.class, TestingModule.Expr2NotificationSectionContent.class); | ||
|
||
final TestingModule.Expr1NotificationSectionContent sec0 = (TestingModule.Expr1NotificationSectionContent) sections.get(0); | ||
final TestingModule.Expr2NotificationSectionContent sec1 = (TestingModule.Expr2NotificationSectionContent) sections.get(1); | ||
|
||
assertThat(sec0.getTestNtfARespDField1()).isEqualTo(0xab); | ||
assertThat(sec0.getTestNtfARespLField2()).isEqualTo(0xcd); | ||
|
||
assertThat(sec1.getTestNtfBRespXField1()).isEqualTo(0xef); | ||
assertThat(sec1.getTestNtfBRespYField2AsString()).isEqualTo(""); | ||
|
||
assertThat(sec0.getField((byte) 'D')).as("field 'D'").hasValue(0xab); | ||
assertThat(sec0.getField((byte) 'L')).as("field 'L'").hasValue(0xcd); | ||
assertThat(sec1.getField((byte) 'X')).as("field 'X'").hasValue(0xef); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters