From 503df4dcb4accccef159cb250276f60220915c44 Mon Sep 17 00:00:00 2001 From: SimoneFiorani Date: Thu, 10 Oct 2024 12:26:21 +0200 Subject: [PATCH 1/2] feat: added gnssType to position rest service Signed-off-by: SimoneFiorani --- kura/distrib/config/kura.build.properties | 2 +- .../META-INF/MANIFEST.MF | 2 +- .../pom.xml | 2 +- .../rest/position/PositionRestService.java | 29 +++++- .../kura/rest/position/api/PositionDTO.java | 16 +++- .../test/PositionRestServiceTest.java | 92 +++++++++++-------- 6 files changed, 98 insertions(+), 45 deletions(-) diff --git a/kura/distrib/config/kura.build.properties b/kura/distrib/config/kura.build.properties index dcc71216639..0c93af489b4 100644 --- a/kura/distrib/config/kura.build.properties +++ b/kura/distrib/config/kura.build.properties @@ -114,7 +114,7 @@ org.eclipse.kura.rest.network.configuration.provider.version=2.0.0-SNAPSHOT org.eclipse.kura.rest.inventory.provider.version=2.0.0-SNAPSHOT org.eclipse.kura.rest.command.provider.version=2.0.0-SNAPSHOT org.eclipse.kura.rest.packages.provider.version=2.0.0-SNAPSHOT -org.eclipse.kura.rest.position.provider.version=2.0.0-SNAPSHOT +org.eclipse.kura.rest.position.provider.version=2.1.0-SNAPSHOT org.eclipse.kura.rest.security.provider.version=2.0.0-SNAPSHOT org.eclipse.kura.rest.identity.provider.version=2.0.0-SNAPSHOT org.eclipse.kura.rest.service.listing.provider.version=2.0.0-SNAPSHOT diff --git a/kura/org.eclipse.kura.rest.position.provider/META-INF/MANIFEST.MF b/kura/org.eclipse.kura.rest.position.provider/META-INF/MANIFEST.MF index 5f36d89eb1a..7107d5008cd 100644 --- a/kura/org.eclipse.kura.rest.position.provider/META-INF/MANIFEST.MF +++ b/kura/org.eclipse.kura.rest.position.provider/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: org.eclipse.kura.rest.position.provider Bundle-SymbolicName: org.eclipse.kura.rest.position.provider;singleton:=true -Bundle-Version: 2.0.0.qualifier +Bundle-Version: 2.1.0.qualifier Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))" Bundle-ClassPath: . Bundle-ActivationPolicy: lazy diff --git a/kura/org.eclipse.kura.rest.position.provider/pom.xml b/kura/org.eclipse.kura.rest.position.provider/pom.xml index 3cc099970df..0a6b6096ad5 100644 --- a/kura/org.eclipse.kura.rest.position.provider/pom.xml +++ b/kura/org.eclipse.kura.rest.position.provider/pom.xml @@ -30,5 +30,5 @@ org.eclipse.kura.rest.position.provider eclipse-plugin - 2.0.0-SNAPSHOT + 2.1.0-SNAPSHOT diff --git a/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/internal/rest/position/PositionRestService.java b/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/internal/rest/position/PositionRestService.java index 28b88b468bd..721789ecb96 100644 --- a/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/internal/rest/position/PositionRestService.java +++ b/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/internal/rest/position/PositionRestService.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2023 Eurotech and/or its affiliates and others + * Copyright (c) 2023, 2024 Eurotech and/or its affiliates and others * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 @@ -12,6 +12,10 @@ *******************************************************************************/ package org.eclipse.kura.internal.rest.position; +import java.util.ArrayList; +import java.util.Objects; +import java.util.Set; + import javax.annotation.security.RolesAllowed; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -22,6 +26,7 @@ import org.eclipse.kura.KuraException; import org.eclipse.kura.cloudconnection.request.RequestHandler; import org.eclipse.kura.cloudconnection.request.RequestHandlerRegistry; +import org.eclipse.kura.position.GNSSType; import org.eclipse.kura.position.PositionService; import org.eclipse.kura.request.handler.jaxrs.DefaultExceptionHandler; import org.eclipse.kura.request.handler.jaxrs.JaxRsRequestHandlerProxy; @@ -84,7 +89,8 @@ public void unsetRequestHandlerRegistry(final RequestHandlerRegistry registry) { @Produces(MediaType.APPLICATION_JSON) public PositionDTO getPosition() { if (positionServiceImpl.isLocked()) { - return new PositionDTO(positionServiceImpl.getPosition()); + return new PositionDTO(positionServiceImpl.getPosition(), + getGnssTypeFromSet(positionServiceImpl.getGnssType())); } throw DefaultExceptionHandler.toWebApplicationException( @@ -128,4 +134,23 @@ public IsLockedDTO getIsLocked() { throw DefaultExceptionHandler.toWebApplicationException(e); } } + + /* + * Utils + */ + + private String getGnssTypeFromSet(Set gnssTypeSet) { + + if (Objects.isNull(gnssTypeSet)) { + return null; + } + + if (gnssTypeSet.isEmpty()) { + return GNSSType.UNKNOWN.getValue(); + } else if (gnssTypeSet.size() == 1) { + return new ArrayList<>(gnssTypeSet).get(0).getValue(); + } else { + return "MixedGNSSTypes"; + } + } } \ No newline at end of file diff --git a/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/rest/position/api/PositionDTO.java b/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/rest/position/api/PositionDTO.java index 7a0aaeea9f9..198f314191d 100644 --- a/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/rest/position/api/PositionDTO.java +++ b/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/rest/position/api/PositionDTO.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2023 Eurotech and/or its affiliates and others + * Copyright (c) 2023, 2024 Eurotech and/or its affiliates and others * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 @@ -21,8 +21,9 @@ public class PositionDTO { private Double altitude; private Double speed; private Double track; + private String gnssType; - public PositionDTO(Position position) { + public PositionDTO(Position position, String gnssType) { if (position.getLongitude() != null) { this.longitude = Math.toDegrees(position.getLongitude().getValue()); } @@ -42,6 +43,10 @@ public PositionDTO(Position position) { if (position.getTrack() != null) { this.track = Math.toDegrees(position.getTrack().getValue()); } + + if (gnssType != null) { + this.gnssType = gnssType; + } } /** @@ -78,4 +83,11 @@ public Double getSpeed() { public Double getTrack() { return track; } + + /* + * Returns the GNSS Type used to retrieve position information + */ + public String getGnssType() { + return gnssType; + } } diff --git a/kura/test/org.eclipse.kura.rest.position.provider.test/src/main/java/org/eclipse/kura/rest/position/provider/test/PositionRestServiceTest.java b/kura/test/org.eclipse.kura.rest.position.provider.test/src/main/java/org/eclipse/kura/rest/position/provider/test/PositionRestServiceTest.java index 1876cedfad5..4704df633c6 100644 --- a/kura/test/org.eclipse.kura.rest.position.provider.test/src/main/java/org/eclipse/kura/rest/position/provider/test/PositionRestServiceTest.java +++ b/kura/test/org.eclipse.kura.rest.position.provider.test/src/main/java/org/eclipse/kura/rest/position/provider/test/PositionRestServiceTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2023 Eurotech and/or its affiliates and others + * Copyright (c) 2023, 2024 Eurotech and/or its affiliates and others * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 @@ -13,34 +13,27 @@ package org.eclipse.kura.rest.position.provider.test; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.time.LocalDateTime; import java.util.Arrays; -import java.util.List; +import java.util.HashSet; +import java.util.Set; import javax.ws.rs.WebApplicationException; -import java.time.ZonedDateTime; -import java.time.LocalDateTime; -import org.osgi.util.position.Position; -import org.osgi.util.measurement.Measurement; -import org.osgi.util.measurement.Unit; -import org.eclipse.kura.KuraException; -import org.eclipse.kura.cloudconnection.message.KuraMessage; -import org.eclipse.kura.message.KuraPayload; -import org.eclipse.kura.message.KuraResponsePayload; -import org.eclipse.kura.position.PositionService; + import org.eclipse.kura.internal.rest.position.PositionRestService; -import org.eclipse.kura.rest.position.api.IsLockedDTO; +import org.eclipse.kura.position.GNSSType; +import org.eclipse.kura.position.PositionService; import org.eclipse.kura.rest.position.api.DateTimeDTO; +import org.eclipse.kura.rest.position.api.IsLockedDTO; import org.eclipse.kura.rest.position.api.PositionDTO; import org.junit.Test; -import org.mockito.ArgumentCaptor; +import org.osgi.util.measurement.Measurement; +import org.osgi.util.measurement.Unit; +import org.osgi.util.position.Position; public class PositionRestServiceTest { @@ -57,11 +50,11 @@ public class PositionRestServiceTest { public void getPositionTest() { givenMockedPositionService(); givenIsLocked(true); - givenPosition(0.1, 0.2, 4.5, null, null); + givenPosition(0.1, 0.2, 4.5, null, null, null); whenGetPosition(); - thenPositionIs(0.1, 0.2, 4.5, null, null); + thenPositionIs(0.1, 0.2, 4.5, null, null, null); thenNoExceptionIsThrown(); } @@ -69,11 +62,23 @@ public void getPositionTest() { public void getPositionTestWithFullParameters() { givenMockedPositionService(); givenIsLocked(true); - givenPosition(0.1, 0.2, 4.5, 50.6, 9.8); + givenPosition(0.1, 0.2, 4.5, 50.6, 9.8, new HashSet<>(Arrays.asList(GNSSType.GPS))); whenGetPosition(); - thenPositionIs(0.1, 0.2, 4.5, 50.6, 9.8); + thenPositionIs(0.1, 0.2, 4.5, 50.6, 9.8, "Gps"); + thenNoExceptionIsThrown(); + } + + @Test + public void getPositionTestWithFullParametersAndMultipleGNSSTypes() { + givenMockedPositionService(); + givenIsLocked(true); + givenPosition(0.1, 0.2, 4.5, 50.6, 9.8, new HashSet<>(Arrays.asList(GNSSType.GPS, GNSSType.GLONASS))); + + whenGetPosition(); + + thenPositionIs(0.1, 0.2, 4.5, 50.6, 9.8, "MixedGNSSTypes"); thenNoExceptionIsThrown(); } @@ -112,7 +117,7 @@ public void getIsLockedTestFalse() { } @Test - public void getPositionNoLockTest(){ + public void getPositionNoLockTest() { givenMockedPositionService(); givenIsLocked(false); @@ -122,7 +127,7 @@ public void getPositionNoLockTest(){ } @Test - public void getLocalDateTimeNoLockTest(){ + public void getLocalDateTimeNoLockTest() { givenMockedPositionService(); givenIsLocked(false); @@ -131,22 +136,25 @@ public void getLocalDateTimeNoLockTest(){ thenExceptionIsThrown(); } - - private void givenMockedPositionService(){ + private void givenMockedPositionService() { positionRestService.setPositionServiceImpl(positionService); } - private void givenPosition(Double longitude, Double latitude, Double altitude, Double speed, Double track) { + private void givenPosition(Double longitude, Double latitude, Double altitude, Double speed, Double track, + Set gnssTypeSet) { Measurement latitudeMesurment = latitude != null ? new Measurement(Math.toRadians(latitude), Unit.rad) : null; - Measurement longitudeMesurment = longitude != null ? new Measurement(Math.toRadians(longitude), Unit.rad) : null; + Measurement longitudeMesurment = longitude != null ? new Measurement(Math.toRadians(longitude), Unit.rad) + : null; Measurement altitudeMesurment = altitude != null ? new Measurement(altitude, Unit.m) : null; Measurement speedMesurment = speed != null ? new Measurement(speed, Unit.m_s) : null; Measurement trackMesurment = track != null ? new Measurement(Math.toRadians(track), Unit.rad) : null; - Position testPosition = new Position(latitudeMesurment, longitudeMesurment, altitudeMesurment, speedMesurment, trackMesurment); + Position testPosition = new Position(latitudeMesurment, longitudeMesurment, altitudeMesurment, speedMesurment, + trackMesurment); when(positionService.getPosition()).thenReturn(testPosition); + when(positionService.getGnssType()).thenReturn(gnssTypeSet); } private void givenLocalDateTime(String zonedDateTime) { @@ -154,7 +162,7 @@ private void givenLocalDateTime(String zonedDateTime) { when(positionService.getDateTime()).thenReturn(testLocalDateTime); } - private void givenIsLocked(boolean isLocked){ + private void givenIsLocked(boolean isLocked) { when(positionService.isLocked()).thenReturn(isLocked); } @@ -167,7 +175,7 @@ private void whenGetPosition() { } private void whenGetLocalDateTime() { - try{ + try { localDateTimeDTO = positionRestService.getLocalDateTime(); } catch (Exception e) { testException = e; @@ -175,43 +183,51 @@ private void whenGetLocalDateTime() { } private void whenGetIsLocked() { - try{ + try { isLockedDTO = positionRestService.getIsLocked(); } catch (Exception e) { testException = e; } } - private void thenPositionIs(Double longitude, Double latitude, Double altitude, Double speed, Double track) { - if (longitude != null){ + private void thenPositionIs(Double longitude, Double latitude, Double altitude, Double speed, Double track, + String gnssType) { + + if (longitude != null) { assertEquals(longitude, positionDTO.getLongitude(), 0.0); } else { assertNull(positionDTO.getLongitude()); } - if (latitude != null){ + if (latitude != null) { assertEquals(latitude, positionDTO.getLatitude(), 0.0); } else { assertNull(positionDTO.getLatitude()); } - if (altitude != null){ + if (altitude != null) { assertEquals(altitude, positionDTO.getAltitude(), 0.0); } else { assertNull(positionDTO.getAltitude()); } - if (speed != null){ + if (speed != null) { assertEquals(speed, positionDTO.getSpeed(), 0.0); } else { assertNull(positionDTO.getSpeed()); } - if (track != null){ + if (track != null) { assertEquals(track, positionDTO.getTrack(), 0.0); } else { assertNull(positionDTO.getTrack()); } + + if (gnssType != null) { + assertEquals(gnssType, positionDTO.getGnssType()); + } else { + assertNull(positionDTO.getGnssType()); + } } private void thenLocalDateTimeIs(String zonedDateTime) { From c01d8448509662b49621d3e21c0f4bfa0af69c28 Mon Sep 17 00:00:00 2001 From: SimoneFiorani Date: Fri, 11 Oct 2024 15:44:40 +0200 Subject: [PATCH 2/2] refactor: modified how the gnsstype info is retrieved Signed-off-by: SimoneFiorani --- .../rest/position/PositionRestService.java | 27 +------------------ .../kura/rest/position/api/PositionDTO.java | 17 ++++++++---- .../test/PositionRestServiceTest.java | 10 +++---- 3 files changed, 18 insertions(+), 36 deletions(-) diff --git a/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/internal/rest/position/PositionRestService.java b/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/internal/rest/position/PositionRestService.java index 721789ecb96..6d4ac29e7a5 100644 --- a/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/internal/rest/position/PositionRestService.java +++ b/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/internal/rest/position/PositionRestService.java @@ -12,10 +12,6 @@ *******************************************************************************/ package org.eclipse.kura.internal.rest.position; -import java.util.ArrayList; -import java.util.Objects; -import java.util.Set; - import javax.annotation.security.RolesAllowed; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -26,7 +22,6 @@ import org.eclipse.kura.KuraException; import org.eclipse.kura.cloudconnection.request.RequestHandler; import org.eclipse.kura.cloudconnection.request.RequestHandlerRegistry; -import org.eclipse.kura.position.GNSSType; import org.eclipse.kura.position.PositionService; import org.eclipse.kura.request.handler.jaxrs.DefaultExceptionHandler; import org.eclipse.kura.request.handler.jaxrs.JaxRsRequestHandlerProxy; @@ -89,8 +84,7 @@ public void unsetRequestHandlerRegistry(final RequestHandlerRegistry registry) { @Produces(MediaType.APPLICATION_JSON) public PositionDTO getPosition() { if (positionServiceImpl.isLocked()) { - return new PositionDTO(positionServiceImpl.getPosition(), - getGnssTypeFromSet(positionServiceImpl.getGnssType())); + return new PositionDTO(positionServiceImpl.getPosition(), positionServiceImpl.getGnssType()); } throw DefaultExceptionHandler.toWebApplicationException( @@ -134,23 +128,4 @@ public IsLockedDTO getIsLocked() { throw DefaultExceptionHandler.toWebApplicationException(e); } } - - /* - * Utils - */ - - private String getGnssTypeFromSet(Set gnssTypeSet) { - - if (Objects.isNull(gnssTypeSet)) { - return null; - } - - if (gnssTypeSet.isEmpty()) { - return GNSSType.UNKNOWN.getValue(); - } else if (gnssTypeSet.size() == 1) { - return new ArrayList<>(gnssTypeSet).get(0).getValue(); - } else { - return "MixedGNSSTypes"; - } - } } \ No newline at end of file diff --git a/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/rest/position/api/PositionDTO.java b/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/rest/position/api/PositionDTO.java index 198f314191d..8259b4d4bb7 100644 --- a/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/rest/position/api/PositionDTO.java +++ b/kura/org.eclipse.kura.rest.position.provider/src/main/java/org/eclipse/kura/rest/position/api/PositionDTO.java @@ -12,6 +12,10 @@ *******************************************************************************/ package org.eclipse.kura.rest.position.api; +import java.util.HashSet; +import java.util.Set; + +import org.eclipse.kura.position.GNSSType; import org.osgi.util.position.Position; public class PositionDTO { @@ -21,9 +25,9 @@ public class PositionDTO { private Double altitude; private Double speed; private Double track; - private String gnssType; + private Set gnssType; - public PositionDTO(Position position, String gnssType) { + public PositionDTO(Position position, Set gnssTypeSet) { if (position.getLongitude() != null) { this.longitude = Math.toDegrees(position.getLongitude().getValue()); } @@ -44,8 +48,11 @@ public PositionDTO(Position position, String gnssType) { this.track = Math.toDegrees(position.getTrack().getValue()); } - if (gnssType != null) { - this.gnssType = gnssType; + if (gnssTypeSet != null) { + this.gnssType = new HashSet<>(); + for (GNSSType type : gnssTypeSet) { + this.gnssType.add(type.getValue()); + } } } @@ -87,7 +94,7 @@ public Double getTrack() { /* * Returns the GNSS Type used to retrieve position information */ - public String getGnssType() { + public Set getGnssType() { return gnssType; } } diff --git a/kura/test/org.eclipse.kura.rest.position.provider.test/src/main/java/org/eclipse/kura/rest/position/provider/test/PositionRestServiceTest.java b/kura/test/org.eclipse.kura.rest.position.provider.test/src/main/java/org/eclipse/kura/rest/position/provider/test/PositionRestServiceTest.java index 4704df633c6..e18070f6d93 100644 --- a/kura/test/org.eclipse.kura.rest.position.provider.test/src/main/java/org/eclipse/kura/rest/position/provider/test/PositionRestServiceTest.java +++ b/kura/test/org.eclipse.kura.rest.position.provider.test/src/main/java/org/eclipse/kura/rest/position/provider/test/PositionRestServiceTest.java @@ -66,7 +66,7 @@ public void getPositionTestWithFullParameters() { whenGetPosition(); - thenPositionIs(0.1, 0.2, 4.5, 50.6, 9.8, "Gps"); + thenPositionIs(0.1, 0.2, 4.5, 50.6, 9.8, new HashSet<>(Arrays.asList("Gps"))); thenNoExceptionIsThrown(); } @@ -78,7 +78,7 @@ public void getPositionTestWithFullParametersAndMultipleGNSSTypes() { whenGetPosition(); - thenPositionIs(0.1, 0.2, 4.5, 50.6, 9.8, "MixedGNSSTypes"); + thenPositionIs(0.1, 0.2, 4.5, 50.6, 9.8, new HashSet<>(Arrays.asList("Gps", "Glonass"))); thenNoExceptionIsThrown(); } @@ -191,7 +191,7 @@ private void whenGetIsLocked() { } private void thenPositionIs(Double longitude, Double latitude, Double altitude, Double speed, Double track, - String gnssType) { + Set gnssTypeSet) { if (longitude != null) { assertEquals(longitude, positionDTO.getLongitude(), 0.0); @@ -223,8 +223,8 @@ private void thenPositionIs(Double longitude, Double latitude, Double altitude, assertNull(positionDTO.getTrack()); } - if (gnssType != null) { - assertEquals(gnssType, positionDTO.getGnssType()); + if (gnssTypeSet != null) { + assertEquals(gnssTypeSet, positionDTO.getGnssType()); } else { assertNull(positionDTO.getGnssType()); }