Calling CreateIndex with settings without using ImmutableSettings - java

In the Jest readme there is an example of creating index with settings but it uses the ImmutableSettings class from ElasticSearch library.
Unfortunately due to some conflicts, we can't include ElasticSearch library in our project. Is there a way to use CreateIndex.Builder to create index with settings without using ImmutableSettings class from ElasticSearch library?
String settings = "\"settings\" : {\n" +
" \"number_of_shards\" : 5,\n" +
" \"number_of_replicas\" : 1\n" +
" }\n";
client.execute(new CreateIndex.Builder("articles").settings(ImmutableSettings.builder().loadFromSource(settings).build().getAsMap()).build());

See CreateIndexIntegrationTest which has live examples of both (with and without Elasticsearch builders) usage styles and please do read the README which explicitly recommends the integration tests for actual-use examples.

Related

updating index settings through elasticsearch java client

I am using Jest client libraries to connect and interface with my ES cluster. I was able to injest documents to an index sucessfully and specify settings.
Settings.Builder settings = Settings.settingsBuilder();
settings.put("number_of_shards", 5);
settings.put("number_of_replicas", 1);
I am having issues with updating the settings that I have specified after indexing documents. The documentation doesn't seem to be clear on how to achieve this.
After an extensive google search, I found a solution. This can be achieved using the updatesettings class.
String body = "{ \"index\" : { " +
"\"number_of_replicas\" :50" + ","+
"} }";
UpdateSettings updateSettings = new UpdateSettings.Builder(body).addIndex(index).build();

How to install chef extension via azure sdk

I need to create deployment with role that contains Chef extension via Java Azure SDK. I have next code for setting extension parameters, but it doesn't work.
As an example I use next material in Python http://blogs.msdn.com/b/shwetasblogs/archive/2015/02/19/creating-deployment-amp-customizing-linux-vms-with-python-amp-chef-part-2.aspx
private void withExtension(Role role) {
ArrayList<ResourceExtensionReference> references = new ArrayList<ResourceExtensionReference>();
ResourceExtensionReference reference = new ResourceExtensionReference();
reference.setReferenceName("LinuxChefClient");
reference.setVersion("11.*");
reference.setPublisher("Chef.Bootstrap.WindowsAzure");
reference.setName("LinuxChefClient");
ResourceExtensionParameterValue publicConfig = new ResourceExtensionParameterValue();
publicConfig.setType("Public");
publicConfig.setKey("PublicParams");
String value = "{\n" +
" \"client_rb\": \"chef_server_url \\\"https://<chefServerUri>\\\"\\nnode_name \\\"<vmName>\\\"\\nvalidation_key \\\"/etc/chef/validation.pem\\\"\\nlog_location \\\"/etc/chef/client.log\\\"\\nlog_level :info\\njson_attribs \\\"/etc/chef/role.json\\\"\",\n" +
" \"run_list\": [\"role[base]\"]\n" +
"}";
publicConfig.setValue(value);
ResourceExtensionParameterValue privateConfig = new ResourceExtensionParameterValue();
privateConfig.setType("Private");
privateConfig.setKey("PrivateParams");
privateConfig.setValue("{ \"validation_key\": \"" + VALIDATION_PEM + "\" }");
ArrayList<ResourceExtensionParameterValue> parameterValues = new ArrayList<ResourceExtensionParameterValue>();
parameterValues.add(publicConfig);
parameterValues.add(privateConfig);
reference.setResourceExtensionParameterValues(parameterValues);
references.add(reference);
role.setResourceExtensionReferences(references);
}
It seems that you do not encrypt the value of privateConfig and publicConfig. As this official document description(Request body part), before setting value, we need encrypt those values:
<PublicConfiguration>
base-64-encoded-xsd-for-extension-public-configuration
</PublicConfiguration>
<PrivateConfiguration>
base-64-encoded-xsd-for-extension-private-configuration
</PrivateConfiguration>
Please try to add this code into your project:
value=Base64.encode(value.getBytes(Charset.forName("UTF-8")));
By the way, please share more error information if the above code don't work.
I am not able to add the comment, so asking my questions in Reply -
What is the error that you see?
Is your VM getting created successfully?
George, please answer these questions so that I can help you better. By the way, you can check the blog - http://blogs.msdn.com/b/azureossds/archive/2015/09/08/accessing-vm-extensions-like-chef-using-azure-sdk-for-java-programmatically.aspx
for using Azure sdk for Java to access the extensions. You will have to modify the code to set the extension reference in order to create a new one.
Hope this helps !!

Use protractor with Java

I want to use Protractor on Java and not on Node.js. Is it possible to use Protractor with Java or Python? We do not want to add another technology for testing and want to use existing technologies.
Unfortunately you don't have much of a choice in the matter, as Protractor is a JavaScript Testing framework for AngularJS, it is distributed via Node.js.
We do not want to add another technology for testing and want to use existing technologies.
Protractor is customized for angularJS applications. So if your application was created using AngularJS, Protractor will help as it has inbuilt support for AngularJS page load and actions.
If your application is not built on top of Angular, you can use Selenium WebDriver on top of any other languages you prefer.
Selenium provides users with documentation on using Python as a medium to write tests, read more about this here.
Protractor is a JS library so you can't run it in Java and testing Angular apps without Protractor is difficult because your tests code needs to wait for Angular processes to complete before interactions like clicking occur.
Fortunately, Angular has made it easy to identify when it's done processing.
There is a JS function that takes a callback and will notify you once the Angular is ready.
angular.getTestability("body").whenStable(callback);
NOTE: That this works with Angular 1.4.8. Some other versions of Angular have a different method that is similar.
You can invoke the testability method from your Java test code using the following simple method or something similar.
private void waitForAngular() {
final String script = "var callback = arguments[arguments.length - 1];\n" +
"var rootSelector = \'body\';\n" +
"var el = document.querySelector(rootSelector);\n" +
"\n" +
"try {\n" +
" if (angular) {\n" +
" window.angular.getTestability(el).whenStable(callback);\n" +
" }\n" +
" else {\n" +
" callback();\n" +
" }\n" +
"} catch (err) {\n" +
" callback(err.message);\n" +
"}";
((JavascriptExecutor) driver).executeAsyncScript(script, new Object[0]);
}
Call waitForAngular() before interacting with the driver with a method like click.
You may want a different rootSelector from 'body' and you may want to throw an error if angular doesn't exist but this works well for my needs.
Protractor provides other selectors which may make testing an Angular app easier but personally I use ID and Class selectors so I don't need them.
There is already a library in Java for automating Angular stuffs. Its built based on Protractor called "ngWebDriver"
As of 2017, I have found these Protractor libraries for Java:
jProtractor - Development is somewhat inactive but I have tested it to work. Click here for more details.
ngWebDriver - Developed by Paul Hammant (co-creator of Selenium). Currently in active development with good documentation.
Code Snippet:
<input type="text" ng-model="startBalance" placeholder="Enter your current balance" class="ng-pristine ng-valid">
// jProtractor
WebElement startBalanceField = driver.findElement(NgBy.model("startBalance"));
// ngWebDriver
WebElement startBalanceField = driver.findElement(ByAngular.model("startBalance"));
To add something to Tom's answer above, you can even test non-angular based apps/websites with Protractor. But there is still no way, you can write Protractor tests using Java or Python as Protractor's core is built on Javascript(node.js) and is purely Javascript. Hope it helps.
The best way to use protractor is , have protractor tests separately written in javascript, and call those tests from java/python when ever required. Thats what we are currently doing!

Eclipse RCP: How to programmatically get Problems View records

On Eclipse Luna, I need to programmatically build java projects and then retrieve the Problems View's records. I use the following code
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IResource resource = workspace.getRoot();
IMarker[] markers = resource.findMarkers(IMarker.MARKER, true, IResource.DEPTH_INFINITE);
for (IMarker m : markers) {
System.out.println("Id: " + m.getId());
System.out.println("Message: " + m.getAttribute(IMarker.MESSAGE));
System.out.println("Source ID: " + m.getAttribute(IMarker.SOURCE_ID));
System.out.println("Location: " + m.getAttribute(IMarker.LOCATION));
System.out.println("Line Number: " + m.getAttribute(IMarker.LINE_NUMBER));
System.out.println("Marker: " + m.getAttribute(IMarker.MARKER));
}
The message and line number are printed correctly. But IMarker.SOURCE_ID returns "JDT" and IMarker.LOCATION is always null.
Anybody knows how can I get the data shown as "Resource" and "Path" on the Problems View? I cannot create any custom Marker view using MarkerSupportView. I need to access the existing Problems View in a programmatic way. Thank you for any suggestion.
Got it. Use getResource() instead of getAttribute().
The markers API is pretty flexible, you should read the documentation.
Long story short, there will be other attributes that you're not looking at. Try calling getAttributes and dumping them out.

Test my js file loaded or not using phantom in play framework

I am using java play framework. How can I implement phantomJS to check whether my external js file is loading or not?
Since you are asking about phantomjs, I suspect this is about running tests on your application.
You would need to write a phantomjs script. There are event listeners that you can use to detect if resources like referenced javascript files are loaded:
Use page.onResourceError when a resource for whatever reason cannot be loaded.
Use page.onResourceTimeout when you suspect a timeout.
Example:
page.onResourceError = function(resourceError) {
if (resourceError.url.match(/\.js/)) {
// only javascript
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
}
};

Categories