/* * * Copyright (C) 2012-2014 R T Huitema. All Rights Reserved. * Web: www.42.co.nz * Email: robert@42.co.nz * Author: R T Huitema * * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * 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 nz.co.fortytwo.signalk.server; import static nz.co.fortytwo.signalk.util.SignalKConstants.dot; import static nz.co.fortytwo.signalk.util.SignalKConstants.nav_courseOverGroundMagnetic; import static nz.co.fortytwo.signalk.util.SignalKConstants.nav_courseOverGroundTrue; import static nz.co.fortytwo.signalk.util.SignalKConstants.nav_position; import static nz.co.fortytwo.signalk.util.SignalKConstants.nav_position_altitude; import static nz.co.fortytwo.signalk.util.SignalKConstants.nav_position_latitude; import static nz.co.fortytwo.signalk.util.SignalKConstants.nav_position_longitude; import static nz.co.fortytwo.signalk.util.SignalKConstants.sourceRef; import static nz.co.fortytwo.signalk.util.SignalKConstants.vessels_dot_self_dot; import java.util.UUID; import java.util.concurrent.TimeUnit; import mjson.Json; import nz.co.fortytwo.signalk.model.SignalKModel; import nz.co.fortytwo.signalk.model.impl.SignalKModelFactory; import nz.co.fortytwo.signalk.util.SignalKConstants; import nz.co.fortytwo.signalk.util.Util; import org.apache.camel.Exchange; import org.apache.camel.ProducerTemplate; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.component.websocket.WebsocketConstants; import org.apache.camel.impl.DefaultProducerTemplate; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class SignalKListOutputTest extends SignalKCamelTestSupport { static final String DIRECT_INPUT = "seda:input"; static Logger logger = LogManager.getLogger(SignalKListOutputTest.class); MockEndpoint output = null; //@Produce(uri = RouteManager.SEDA_INPUT) protected ProducerTemplate template; String jsonString = null; @BeforeClass public static void setClass() throws Exception { Util.getConfig(); Util.setSelf("motu"); } @Before public void before() throws Exception { //generate json SignalKModel temp = SignalKModelFactory.getCleanInstance(); //add data temp.putValue(vessels_dot_self_dot+nav_courseOverGroundTrue, 11.96d); temp.putValue(vessels_dot_self_dot+nav_courseOverGroundMagnetic, 93.00d); temp.putPosition(vessels_dot_self_dot+nav_position, -41.2936935424d,11.96d,0.0,"test",Util.getIsoTimeString()); jsonString = ser.write(temp); } public void init() throws Exception{ template= new DefaultProducerTemplate(routeManager.getContext()); template.setDefaultEndpointUri(DIRECT_INPUT); template.start(); } @Test public void shouldOutputListMessage() throws Exception { init(); String wsSession = UUID.randomUUID().toString(); assertNotNull(template); output.reset(); output.expectedMessageCount(1); template.sendBody(DIRECT_INPUT,jsonString); latch.await(2,TimeUnit.SECONDS); logger.debug("SignalKModel:"+signalkModel); assertEquals(11.96d,(double)signalkModel.getValue(vessels_dot_self_dot + nav_courseOverGroundTrue),0.00001); logger.debug("Lat :"+signalkModel.getValue(vessels_dot_self_dot + nav_position_latitude)); //request list Json sub = getList("vessels." + SignalKConstants.self,"navigation.position.*"); template.sendBodyAndHeader(DIRECT_INPUT, sub.toString(),WebsocketConstants.CONNECTION_KEY, wsSession); latch.await(2,TimeUnit.SECONDS); output.assertIsSatisfied(); Exchange exch = output.getReceivedExchanges().get(0); Json reply = exch.getIn().getBody(Json.class); logger.debug("Reply = "+reply); assertNotNull(reply); assertTrue(reply.at("pathlist").asList().size()==3); } @Override public void configureRouteBuilder(RouteBuilder routeBuilder) { output = (MockEndpoint) routeBuilder.getContext().getEndpoint("mock:output"); try{ SignalkRouteFactory.configureInputRoute(routeBuilder, DIRECT_INPUT); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); fail(); } routeBuilder.from(RouteManager.SEDA_COMMON_OUT).to(output); } }