implement model for message responses

This commit is contained in:
Stefan Kalscheuer 2019-11-04 17:15:14 +01:00
parent 2fd8d2a87c
commit 4f3d8694e1
3 changed files with 319 additions and 1 deletions
pom.xml
src
main/java/de/stklcode/pubtrans/ura/model
test/java/de/stklcode/pubtrans/ura/model

@ -6,7 +6,7 @@
<groupId>de.stklcode.pubtrans</groupId>
<artifactId>juraclient</artifactId>
<version>1.2.1-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

@ -0,0 +1,165 @@
package de.stklcode.pubtrans.ura.model;
import java.io.IOException;
import java.util.List;
/**
* Entity for a message.
*
* @author Stefan Kalscheuer
* @since 1.3
*/
public class Message implements Model {
private static final int MSG_UUID = 7;
private static final int MSG_TYPE = 8;
private static final int MSG_PRIORITY = 9;
private static final int MSG_TEXT = 10;
private static final int NUM_OF_FIELDS = 11;
private final Stop stop;
private final String uuid;
private final Integer type;
private final Integer priority;
private final String text;
/**
* Construct Message object from complete set of data.
*
* @param stopID Stop ID.
* @param stopName Stop name.
* @param stopIndicator Stop Indicator.
* @param stopState Stop state.
* @param stopLatitude Stop geolocation latitude.
* @param stopLongitude Stop geolocation latitude.
* @param msgUUID Message UUID.
* @param msgType Message type.
* @param msgPriority Message priority.
* @param msgText Message text.
*/
public Message(final String stopID,
final String stopName,
final String stopIndicator,
final Integer stopState,
final Double stopLatitude,
final Double stopLongitude,
final String msgUUID,
final Integer msgType,
final Integer msgPriority,
final String msgText) {
this(new Stop(stopID,
stopName,
stopIndicator,
stopState,
stopLatitude,
stopLongitude),
msgUUID,
msgType,
msgPriority,
msgText);
}
/**
* Construct Message object from Stop model and set of additional data.
*
* @param stop Stop model
* @param msgUUID Message UUID.
* @param msgType Message type.
* @param msgPriority Message priority.
* @param msgText Message text.
*/
public Message(final Stop stop,
final String msgUUID,
final Integer msgType,
final Integer msgPriority,
final String msgText) {
this.stop = stop;
this.uuid = msgUUID;
this.type = msgType;
this.priority = msgPriority;
this.text = msgText;
}
/**
* Construct Message object from raw list of attributes parsed from JSON.
*
* @param raw List of attributes from JSON line
* @throws IOException Thrown on invalid line format.
*/
public Message(final List raw) throws IOException {
this(raw, null);
}
/**
* Construct Message object from raw list of attributes parsed from JSON with explicitly specified version.
*
* @param raw List of attributes from JSON line
* @param version API version
* @throws IOException Thrown on invalid line format.
*/
public Message(final List raw, final String version) throws IOException {
if (raw == null || raw.size() < NUM_OF_FIELDS) {
throw new IOException("Invalid number of fields");
}
stop = new Stop(raw);
if (raw.get(MSG_UUID) instanceof String) {
uuid = (String) raw.get(MSG_UUID);
} else {
throw Model.typeErrorString(MSG_UUID, raw.get(MSG_UUID).getClass());
}
if (raw.get(MSG_TYPE) instanceof Integer) {
type = (Integer) raw.get(MSG_TYPE);
} else {
throw Model.typeError(MSG_TYPE, raw.get(MSG_TYPE).getClass(), "Integer");
}
if (raw.get(MSG_PRIORITY) instanceof Integer) {
priority = (Integer) raw.get(MSG_PRIORITY);
} else {
throw Model.typeError(MSG_PRIORITY, raw.get(MSG_PRIORITY).getClass(), "Integer");
}
if (raw.get(MSG_TEXT) instanceof String) {
text = (String) raw.get(MSG_TEXT);
} else {
throw Model.typeErrorString(MSG_TEXT, raw.get(MSG_TEXT).getClass());
}
}
/**
* @return The affected stop.
*/
public Stop getStop() {
return stop;
}
/**
* @return Message's unique identifier.
*/
public String getUuid() {
return uuid;
}
/**
* @return Message type.
*/
public Integer getType() {
return type;
}
/**
* @return Message priority. Lower value equals higher priority.
*/
public Integer getPriority() {
return priority;
}
/**
* @return Message text.
*/
public String getText() {
return text;
}
}

@ -0,0 +1,153 @@
/*
* Copyright 2016-2019 Stefan Kalscheuer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.stklcode.pubtrans.ura.model;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.fail;
/**
* Unit test for the {@link Message} metamodel.
*
* @author Stefan Kalscheuer
*/
public class MessageTest {
@Test
public void basicConstructorTest() {
Message message = new Message("sid",
"name",
"indicator",
1,
2.345,
6.789,
"msg_uuid",
1,
3,
"message text");
assertThat(message.getStop().getId(), is("sid"));
assertThat(message.getStop().getName(), is("name"));
assertThat(message.getStop().getIndicator(), is("indicator"));
assertThat(message.getStop().getState(), is(1));
assertThat(message.getStop().getLatitude(), is(2.345));
assertThat(message.getStop().getLongitude(), is(6.789));
assertThat(message.getUuid(), is("msg_uuid"));
assertThat(message.getType(), is(1));
assertThat(message.getPriority(), is(3));
assertThat(message.getText(), is("message text"));
}
@Test
public void listConstructorTest() {
/* Create valid raw data list */
List<Object> raw = new ArrayList<>();
raw.add(1);
raw.add("stopName");
raw.add("stopId");
raw.add("stopIndicator");
raw.add(9);
raw.add(8.765);
raw.add(43.21);
raw.add("msg_uuid");
raw.add(1);
raw.add(3);
raw.add("message text");
try {
Message message = new Message(raw);
assertThat(message.getStop().getId(), is("stopId"));
assertThat(message.getStop().getName(), is("stopName"));
assertThat(message.getStop().getIndicator(), is("stopIndicator"));
assertThat(message.getStop().getState(), is(9));
assertThat(message.getStop().getLatitude(), is(8.765));
assertThat(message.getStop().getLongitude(), is(43.21));
assertThat(message.getUuid(), is("msg_uuid"));
assertThat(message.getType(), is(1));
assertThat(message.getPriority(), is(3));
assertThat(message.getText(), is("message text"));
} catch (IOException e) {
fail("Creation of Message from valid list failed: " + e.getMessage());
}
/* Excess elements should be ignored */
raw.add("foo");
try {
Message message = new Message(raw);
assertThat(message, is(notNullValue()));
raw.remove(11);
} catch (IOException e) {
fail("Creation of Message from valid list failed: " + e.getMessage());
}
/* Test exceptions on invalid data */
List<Object> invalid = new ArrayList<>(raw);
invalid.remove(7);
invalid.add(7, 123L);
try {
new Message(invalid);
fail("Creation of Message with invalid UUID field successful");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(8);
invalid.add(8, "abc");
try {
new Message(invalid);
fail("Creation of Message with invalid type field successful");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(9);
invalid.add(9, "xyz");
try {
new Message(invalid);
fail("Creation of Message with invalid priority field successful");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(10);
invalid.add(10, 1.23);
try {
new Message(invalid);
fail("Creation of Message with invalid text field successful");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
invalid = new ArrayList<>(raw);
invalid.remove(10);
try {
new Message(invalid);
fail("Creation of Message with too short list successful");
} catch (Exception e) {
assertThat(e, is(instanceOf(IOException.class)));
}
}
}