blob: 099bac16908879c07ee5012e6e386990434b3f42 [file] [log] [blame]
package org.eclipse.basyx.examples.snippets.undoc.vab.connection;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.basyx.aas.api.webserviceclient.WebServiceJSONClient;
import org.eclipse.basyx.aas.backend.connector.http.HTTPConnectorProvider;
import org.eclipse.basyx.components.servlet.submodel.EmptyVABLambdaElementServlet;
import org.eclipse.basyx.examples.contexts.BaSyxExamplesContext_1MemoryAASServer_1SQLDirectory;
import org.eclipse.basyx.examples.deployment.BaSyxDeployment;
import org.eclipse.basyx.examples.support.directory.ExamplesPreconfiguredDirectory;
import org.eclipse.basyx.vab.core.VABConnectionManager;
import org.eclipse.basyx.vab.core.proxy.VABElementProxy;
import org.junit.ClassRule;
import org.junit.Test;
/**
* Illustrate the use of AAS operations using HTTP REST calls
*
* @author kuhn
*
*/
public class RunAASManualHTTPOperationsSnippet {
/**
* VAB connection manager backend
*/
protected VABConnectionManager connManager = new VABConnectionManager(
new ExamplesPreconfiguredDirectory()
// Add example specific mappings
.addMapping("urn:de.FHG:devices.es.iese:statusSM:1.0:3:x-509#003", "http://localhost:8080/basys.examples/Testsuite/components/BaSys/1.0/devicestatusVAB/"),
new HTTPConnectorProvider());
/**
* Instantiate and start context elements for this example. BaSyxDeployment contexts instantiate all
* components on the IP address of the host. Therefore, all components use the same IP address.
*/
@ClassRule
public static BaSyxDeployment context = new BaSyxDeployment(
// Simulated servlets
// - BaSys topology with one AAS Server and one SQL directory
new BaSyxExamplesContext_1MemoryAASServer_1SQLDirectory().
// Deploy example specific servlets to Tomcat server in this context
addServletMapping("/Testsuite/components/BaSys/1.0/devicestatusVAB/*", new EmptyVABLambdaElementServlet())
);
/**
* Test basic queries
*/
@Test @SuppressWarnings("unchecked")
public void snippet() throws Exception {
// Server connections
// - Connect to device (VAB object)
VABElementProxy connSubModel1 = this.connManager.connectToVABElement("urn:de.FHG:devices.es.iese:statusSM:1.0:3:x-509#003");
// Create properties on AAS using connection
connSubModel1.createElement("properties", new HashMap<String, Object>());
connSubModel1.createElement("properties/prop1", 7);
connSubModel1.createElement("properties/prop2", "myStr");
// Web service client accesses AAS using HTTP REST calls
WebServiceJSONClient jsonClient = new WebServiceJSONClient();
// Read property values
// - Use WebServiceJSONClient class. Returned property contains meta data. The actual value is stored in property "entity"
assertTrue((int) ((Map<String, Object>) jsonClient.get("http://localhost:8080/basys.examples/Testsuite/components/BaSys/1.0/devicestatusVAB/properties/prop1")).get("entity") == 7);
assertTrue(((Map<String, Object>) jsonClient.get("http://localhost:8080/basys.examples/Testsuite/components/BaSys/1.0/devicestatusVAB/properties/prop2")).get("entity").equals("myStr"));
}
}