Class for storing `GeoPoint` in GCP Datastore (Java) - google-cloud-platform

I am trying to store a geo_point type data in datastore via GCP Java client library. I figured out how to do for a Date type data, but could not get a clue which GeoPoint class I use for this.
import com.google.datastore.v1.Entity;
import static com.google.datastore.v1.client.DatastoreHelper.makeValue;
import java.util.Date;
...
public class WriteToDatastoreFromTwitter {
private static Value dValue(Date k) {
return makeValue(k).setExcludeFromIndexes(true).build();
}
public static void main(String[] args) throws TwitterException {
final Builder builder = Entity.newBuilder().
setKey(key).
putProperties("timestamp", dValue(tweet.getCreatedAt()));
// How can I add a `geo_point` data?
I am simply not sure if I should use classes outside of the datastore package, such as this: https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/search/GeoPoint

I figured out by myself. There is a class LatLng in a dependent package com.google.type to the datastore package and I could use this to successfully store geo_point data. Here's how you initialize the object:
import com.google.type.LatLng;
...
LatLng x = LatLng.
newBuilder().
setLatitude(loc.getLatitude()).
setLongitude(loc.getLongitude()).
build();
and in my case, I stored it by doing
private static Value gValue(LatLng k) {
return makeValue(k).setExcludeFromIndexes(true).build();
}
builder.putProperties("geo_point", gValue(x));

Related

Load bayesian network based on BIF-file WEKA

I want to load a bayesian network that is stored in a BIF-file in WEKA to validate it against a specific test-set.
However it keeps crashing with a null-pointer because m_MissinValuesFilter is null.
How do I set this filter correctly?
My current code looks as follows:
BayesNet network = new BayesNet()
BIFReader reader = new BIFReader()
network reader.processFile(path)
Evaluation eval = new Evaluation(testData)
eval.evaluatemodel(network,testData)
Despite loading the network from a BIF XML file, you still need to call the buildClassifier method to initialize all the data structures.
Here is a minimal code example:
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.BayesNet;
import weka.classifiers.bayes.net.search.fixed.FromFile;
import weka.core.Instances;
import weka.core.converters.ConverterUtils;
public class Bif {
public static void main(String[] args) throws Exception {
// load data
Instances data = ConverterUtils.DataSource.read("/somewhere/data.arff");
data.setClassIndex(data.numAttributes() - 1);
// configure classifier
FromFile fromFile = new FromFile();
fromFile.setBIFFile("/somewhere/model.bif");
BayesNet bayesNet = new BayesNet();
bayesNet.setSearchAlgorithm(fromFile);
// build classifier to initialize data structures
// NB: this won't change the model itself, as it is fixed
bayesNet.buildClassifier(data);
// evaluate it
Evaluation eval = new Evaluation(data);
eval.evaluateModel(bayesNet, data);
System.out.println(eval.toSummaryString());
// you can output the BIF XML like this
//System.out.println(bayesNet.graph());
}
}
This approach will, however, only use the structure of the network stored in the BIF file and not the CPDs. The training process will reinitialize the CPDs.
If you also want to use the CPDs from the BIF file, you need to use this slightly hacky approach:
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.net.BIFReader;
import weka.core.Instances;
import weka.core.converters.ConverterUtils;
public class Bif {
public static void main(String[] args) throws Exception {
Instances test = ConverterUtils.DataSource.read("/somewhere/data.arff");
test.setClassIndex(test.numAttributes() - 1);
String bifFile = "/somewhere/model.bif";
BIFReader reader = new BIFReader();
// initialize internal filters etc
reader.buildClassifier(test);
// override data structures using BIF file
reader.processFile(bifFile);
// output graph to confirm correct model
//System.out.println(reader.graph());
// evaluate the model against the test set
Evaluation eval = new Evaluation(test);
eval.evaluateModel(reader, test);
System.out.println(eval.toSummaryString());
}
}

Using AssetManager class from Android NDK in a Flutter app

I've been trying to use the Android NDK's AssetManager class in my Flutter app, that works with Google Oboe, to access to audio files. Following this example in the Oboe repository, I learned that they obtain the AssetManager from Java like this:
JNIEXPORT void JNICALL
Java_com_google_oboe_sample_rhythmgame_MainActivity_native_1onStart(JNIEnv *env, jobject instance,
jobject jAssetManager) {
AAssetManager *assetManager = AAssetManager_fromJava(env, jAssetManager);
if (assetManager == nullptr) {
LOGE("Could not obtain the AAssetManager");
return;
}
game = std::make_unique<Game>(*assetManager);
game->start();
}
Basically with the argument jAssetManager they pass from Java to the C++ functions, through the JNI interface. Now I'm not working with JNI because I'm using Flutter and Dart, and the way in Dart for communicating with C++ functions is through dart:ffi, but since the only way I can create an AssetManager is with AAssetManager_fromJava(env, jAssetManager), I need those two arguments that I can't find a way to replace with Flutter and Dart.
I did some research and when I created the Flutter FFI plugin, apparently the Dart code communicates with Kotlin code, which then calls the native C++ functions.
Here's my C++ function:
EXTERNC void *engine_create(void) {
AAssetManager *assetManager = AAssetManager_fromJava(env, jAssetManager); // ERROR: How do I get these?
if (assetManager == nullptr) {
LOGE("Could not obtain the AAssetManager");
return nullptr;
}
return new DSPAudioEngine(*assetManager);
}
Here's the Dart wrapper for that function:
import 'dart:ffi';
import 'dart:typed_data';
import 'package:ffi/ffi.dart';
import 'package:flutter/services.dart';
typedef oboe_engine_init = Pointer<Void> Function();
typedef OboeEngineInit = Pointer<Void> Function();
class FfiGoogleOboe {
static const MethodChannel _channel =
const MethodChannel('ffi_google_oboe');
static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
static FfiGoogleOboe _instance;
factory FfiGoogleOboe() {
if (_instance == null) {
_instance = FfiGoogleOboe._();
}
return _instance;
}
OboeEngineInit _engineInit;
FfiGoogleOboe._() {
final oboeLib = DynamicLibrary.open('libffi_google_oboe.so');
_engineInit = oboeLib
.lookup<NativeFunction<oboe_engine_init>>('engine_create')
.asFunction();
}
}
And here's the Kotlin code I found in the FFI plugin implementation:
package g1_assd_2020.ffi_google_oboe
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
import android.content.res.AssetManager
/** FfiGoogleOboePlugin */
public class FfiGoogleOboePlugin: FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private lateinit var channel : MethodChannel
override fun onAttachedToEngine(#NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "ffi_google_oboe")
channel.setMethodCallHandler(this);
}
// This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
// plugin registration via this function while apps migrate to use the new Android APIs
// post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
//
// It is encouraged to share logic between onAttachedToEngine and registerWith to keep
// them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
// depending on the user's project. onAttachedToEngine or registerWith must both be defined
// in the same class.
companion object {
#JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "ffi_google_oboe")
channel.setMethodCallHandler(FfiGoogleOboePlugin())
}
}
override fun onMethodCall(#NonNull call: MethodCall, #NonNull result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else {
result.notImplemented()
}
}
override fun onDetachedFromEngine(#NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
}
Finally, here's how the people from Oboe handle it using JNI and Java:
package com.google.oboe.sample.rhythmgame;
import android.content.Context;
import android.content.res.AssetManager;
import androidx.appcompat.app.AppCompatActivity;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setDefaultStreamValues(this);
}
protected void onResume(){
super.onResume();
native_onStart(getAssets());
}
protected void onPause(){
super.onPause();
native_onStop();
}
static void setDefaultStreamValues(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
AudioManager myAudioMgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
String sampleRateStr = myAudioMgr.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
int defaultSampleRate = Integer.parseInt(sampleRateStr);
String framesPerBurstStr = myAudioMgr.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
int defaultFramesPerBurst = Integer.parseInt(framesPerBurstStr);
native_setDefaultStreamValues(defaultSampleRate, defaultFramesPerBurst);
}
}
private native void native_onStart(AssetManager assetManager);
private native void native_onStop();
private static native void native_setDefaultStreamValues(int defaultSampleRate,
int defaultFramesPerBurst);
}
Basically, you need to pass the AssetManager reference from your plugin's Kotlin file to the C++ library. This answer explains how to make the Kotlin file call C++ code: Android: How to call ndk function from Kotlin?
You'll want to use a methodChannel call to trigger this. You can get the AssetManager reference in the onAttachedToEngine method from flutterPluginBinding.applicationContext.assets.
Here's an example Flutter plugin that reads an asset in a C++ library:
https://github.com/mikeperri/ndk_asset_manager_example/commit/533d28b33c1d22f89028f89691f78e907bf19db3

How powermockit mocks a private method

Here, I want to understand the internals of Powermockito. How does it mock a private method?
I thought that it uses a new class loader to create a different copy of the class, but when I tried to do the same, I got a runtime exception showing that the 2 classes (one originally created by System class loader) and the one I passed are of different versions.
package com.concretepage.lang;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
public class MainClass {
public static void main(String[] args) throws InstantiationException, IllegalAccessException,
NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, MalformedURLException {
CustomClassLoaderDemo loader = new CustomClassLoaderDemo();
Class<?> c = loader.findClass("com.concretepage.lang.Test");
Object ob = c.newInstance();
Method md = c.getMethod("show");
md.invoke(ob);
System.out.println(c.getClassLoader());
System.out.println(ob.getClass().getClassLoader());
((com.concretepage.lang.Test)ob).show();
Test c2 = new Test();
System.out.println(c2.getClass().getClassLoader());
Class<?> cClass = Class.forName("com.concretepage.lang.Test");
System.out.println(cClass.getClassLoader());
java.net.URLClassLoader urlLoader = java.net.URLClassLoader.newInstance(new URL[] {new URL("File://c//Users//dell//eclipse-workspace//DeemTest//src")}, loader);
Class<?> testRemoteClazz = urlLoader.loadClass("com.concretepage.lang.Test");
Object obUrl = testRemoteClazz.newInstance();
Method mdUrl = testRemoteClazz.getMethod("show");
mdUrl.invoke(obUrl);
}
}
If your unit test is une the same package path, you may try to pass your methods in protected.
Example :
app package => myapp.services.impl
test package => myapp.serviceS.impl
Then with the same package name, the method protected will be reachable for the test.
If you can't make that, you should pass the method to public, but i don't think that's your objective... Proceted is in general a good solution.

Unable to Mock behavior using Mockito for testing Struts2 action class method

Well this is the setup that I have:
WelcomeUser.java
package org.user.www.action;
import org.user.www.dao.ApplicationDAOfactory;
import org.user.www.dao.UserPojoDao;
import org.user.www.pojo.UserPojo;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class WelcomeUser extends ActionSupport implements ModelDriven<UserPojo> {
private static final long serialVersionUID = 1L;
private UserPojo userPojo = new UserPojo();
private ApplicationDAOfactory appFactory = new ApplicationDAOfactory();
private UserPojoDao appDAOObj /*= ApplicationDAOfactory.getUserPojoDaoInstance()*/;
public WelcomeUser(){
}
public WelcomeUser(UserPojoDao appDAOObj, ApplicationDAOfactory appFactory){
this.appDAOObj = appDAOObj;
this.appFactory = appFactory;
}
public String execute(){
return SUCCESS;
}
#Override
public UserPojo getModel() {
// TODO Auto-generated method stub
return userPojo;
}
public String welcome(){
appDAOObj = appFactory.getUserPojoDaoInstance();
appDAOObj.persistUser(userPojo);
return SUCCESS;
}
}
ApplicationDAOfactory.java
package org.user.www.dao;
public class ApplicationDAOfactory {
public UserPojoDao getUserPojoDaoInstance(){
return new UserPojoDaoImpl();
}
}
WelcomeUserTest.java
package org.user.www.junit;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.Map;
import org.apache.struts2.StrutsJUnit4TestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.user.www.action.WelcomeUser;
import org.user.www.dao.ApplicationDAOfactory;
import org.user.www.dao.UserPojoDao;
import org.user.www.dao.UserPojoDaoImpl;
import org.user.www.pojo.UserPojo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionProxy;
#RunWith(MockitoJUnitRunner.class)
public class WelcomeUserTest extends StrutsJUnit4TestCase<WelcomeUser>{
#Mock private UserPojoDao appDAOObj;
#Mock private ApplicationDAOfactory appFactory;
#Mock private UserPojo userPojo;
#Before
public void setUpBeforeClass() throws Exception {
appDAOObj = mock(UserPojoDaoImpl.class);
appFactory = mock(ApplicationDAOfactory.class);
//MockitoAnnotations.initMocks(WelcomeUser.class);
new WelcomeUser(appDAOObj, appFactory);
}
#Test
public void testWelcome() throws Exception{
when(appFactory.getUserPojoDaoInstance()).thenReturn(appDAOObj);
when(appDAOObj.persistUser(userPojo)).thenReturn(userPojo);
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("name" ,"user");
paramMap.put("email" ,"user#user.com");
ActionProxy proxy = getActionProxy("/submitUser");
ActionContext actionContext= proxy.getInvocation().getInvocationContext();
actionContext.setParameters(paramMap);
String result= proxy.execute();
assertEquals("success", result);
}
}
My doubt is that although I am trying to mock the behavior upon which my welcome method in the struts action class depends but it doesn't seems to be working. I have browsed through various sources but it has all gone in vain. As far as I have been able to comprehend this situation my best guess is that when I call the execute method upon the proxy instance in my test method the control does execute the welcome method as per the mapping in my struts.xml file but when execution arrives at the point where I am expecting my mocks to work it doesn't works and hits my database which I don't want. I do understand there is certainly a gap in my understanding but any help shall be appreciated. Cheers!!
I had similar issue but my case was bit different as I was using setter Injector through spring framework and my action class already had setter method for Dao.
I had to get the action from proxy and Inject mocked Dao object into my action.
I did following before executing proxy :
WelcomeUser actionObj = (WelcomeUser) proxy.getAction();
actionObj.setDaoObj(appDAOObj);
you have to figure out a way to inject mocked objects into actionObj .
Update :
Check if following works for you
WelcomeUser actionObj = (WelcomeUser) proxy.getAction();
ReflectionTestUtils.setField(actionObj, "appDAOObj", appDAOObj);
ReflectionTestUtils.setField(actionObj, "appFactory", appFactory);
String result= proxy.execute();
assertEquals("success", result);
Make sure you have spring-test-x.x.x.RELEASE.jar and spring-core-x.x.x.RELEASE.jar (Including any other dependency) in your classpath.

Enum conversion to String not working on #Indexed unique field

I'm a Neo4j/Spring-data newbie so apologies if this is something obvious but I looked here and there and can't quite figure out if it's a bug or feature.
I'm using SDN 3.1.0 and Neo4j 2.0.4, running in memory for now (for testing).
I have a super simple POJO that I try to save into Neo4j using SDN. It looks like so:
#NodeEntity
public class Weekday {
#GraphId
private Long id;
#Indexed(unique = true)
public DayOfWeek weekdayCode;
}
Everything works beautifully when I make it non-uniquely indexed, or not indexed at all. It works fine with unique constraint when I make it a String as well. (Well, sort of, I'm aware that it doesn't throw an exception but silently updates existing one - this is not perfect but I found JIRA issue related to that). Unfortunately the moment I try to save it as enum with unique constraint I get an exception:
org.springframework.dao.InvalidDataAccessResourceUsageException: Error executing statement MERGE (n:`Weekday` {`weekdayCode`: {value}}) ON CREATE SET n={props} return n; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: Error executing statement MERGE (n:`Weekday` {`weekdayCode`: {value}}) ON CREATE SET n={props} return n; nested exception is java.lang.IllegalArgumentException: [MONDAY:java.time.DayOfWeek] is not a supported property value
at org.springframework.data.neo4j.support.query.CypherQueryEngineImpl.query(CypherQueryEngineImpl.java:61)
at org.springframework.data.neo4j.support.schema.SchemaIndexProvider.merge(SchemaIndexProvider.java:114)
at [...]
Caused by: org.springframework.dao.InvalidDataAccessResourceUsageException: Error executing statement MERGE (n:`Weekday` {`weekdayCode`: {value}}) ON CREATE SET n={props} return n; nested exception is java.lang.IllegalArgumentException: [MONDAY:java.time.DayOfWeek] is not a supported property value
at org.springframework.data.neo4j.support.query.CypherQueryEngineImpl.parseAndExecuteQuery(CypherQueryEngineImpl.java:72)
at org.springframework.data.neo4j.support.query.CypherQueryEngineImpl.query(CypherQueryEngineImpl.java:58)
... 63 more
Caused by: java.lang.IllegalArgumentException: [MONDAY:java.time.DayOfWeek] is not a supported property value
at org.neo4j.kernel.api.properties.PropertyConversion.convertProperty(PropertyConversion.java:107)
at org.neo4j.kernel.api.properties.Property.property(Property.java:51)
at [...]
This, as far as I can see, is because the unique field is put into a map "props", and the contents of the map is not automatically converted so it sends an enum to Neo4j, which obviously it doesn't like.
Is this expected or should I raise a bug with SDN?
If that's expected behaviour, do I have any alternatives other than making the field a String?
I'm not sure if it is a real bug, however I had a similar problem using my own Enum class.
So, you could try something like this:
Create the converters
Register them in neo4j
First create your converters like:
#Component
public class StringToDayOfWeekConverter implements Converter<String, DayOfWeek> {
#Override
public DayOfWeek convert(String source) {
return DayOfWeek.valueOf(source);
}
}
#Component
public class DayOfWeekToStringConverter implements Converter<DayOfWeek, String> {
#Override
public String convert(DayOfWeek source) {
return source.name();
}
}
Then you register the converters, so neo4j can use them:
#Configuration
#EnableNeo4jRepositories("my.repository.package")
#EnableTransactionManagement
public class MyNeo4jConfiguration extends Neo4jConfiguration {
#Autowired
private StringToDayOfWeekConverter stringToDayOfWeekConverter;
#Autowired
private DayOfWeekToStringConverter dayOfWeekToStringConverter;
#Override
protected ConversionService neo4jConversionService() throws Exception {
ConverterRegistry converterRegistry = (ConverterRegistry) super.neo4jConversionService();
converterRegistry.addConverter(stringToDayOfWeekConverter);
converterRegistry.addConverter(dayOfWeekToStringConverter);
return (ConversionService) converterRegistry;
}
}
create an converter extends EnumStringConverter such like this:
package com.noopu.pyramid.domain.model.converter;
import com..pyramid.common.UserType;
import org.neo4j.ogm.typeconversion.EnumStringConverter;
public class UserTypeStringConverter extends EnumStringConverter {
public UserTypeStringConverter ( ) {
super ( UserType.class );
}
}
and used #Converter annotation like under:
package com.noopu.pyramid.domain.model;
import com.noopu.pyramid.common.UserType;
import com.noopu.pyramid.domain.model.converter.UserTypeStringConverter;
import lombok.Data;
import org.neo4j.ogm.annotation.GraphId;
import org.neo4j.ogm.annotation.Index;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.typeconversion.Convert;
import java.io.Serializable;
#NodeEntity
#Data
public class User implements Serializable {
private static final long serialVersionUID = 8979348201709416439L;
#GraphId
private Long graphId;
#Index
protected Long id;
#Index(unique = true)
private Long uid;
#Index(unique = true)
private String phone;
#Index
#Convert(UserTypeStringConverter.class)
private UserType type;
}