Can't override node settings in ES integration test - java

I am writing an integration test for elasticsearch 5.3.
public class ProtectedWordsIndexTests extends ESIntegTestCase {
private final WordDelimiterActionListener wordsListener =
WordDelimiterActionListener.getInstance();
private final static String INDEX_NAME = "protected_words";
private final static String TYPE_NAME = "word";
private final static String FILTER_NAME = "my_word_delimiter";
#Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Collections.singleton(WordDelimiterPlugin.class);
}
#Override
protected Settings nodeSettings(int nodeOrdinal) {
return builder()
.put("plugin.types", TYPE_NAME)
.put("plugin.dynamic_word_delimiter.refresh_interval", "500ms")
.put(super.nodeSettings(nodeOrdinal))
.build();
}
public void testAddWordToIndex() throws Exception {
Settings indexSettings = builder()
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
.put("index.analysis.filter.my_word_delimiter.type", "dynamic_word_delimiter")
.build();
TokenFilterFactory filterFactory = filterFactory(indexSettings, FILTER_NAME);
createIndex(INDEX_NAME);
ensureGreen();
client().prepareIndex(INDEX_NAME, TYPE_NAME, "1")
.setSource("word", "1tb")
.execute();
Thread.sleep(TimeValue.timeValueSeconds(1).getMillis());
Set<String> protectedWords = wordsListener.getProtectedWords();
assertTrue(protectedWords.size() == 1);
}
}
When I am running testAddWordToIndex() I am getting the following error:
"java.lang.IllegalArgumentException: unknown setting
[plugin.dynamic_word_delimiter.refresh_interval] please check that any
required plugins are installed, or check the breaking changes
documentation for removed settings"
If I remove the following part and increase the refresh interval to be more than the default, the test passes. So I just can't override this.
.put("plugin.dynamic_word_delimiter.refresh_interval", "500ms")
The default refresh interval is declared here:
public class WordDelimiterRunnable extends AbstractRunnable {
public static final TimeValue REFRESH_INTERVAL = TimeValue.timeValueSeconds(20);
public static final String INDEX_NAME = "protected_words";
public static final String INDEX_TYPE = "word";
public static final int RESULTS_SIZE = 10000;
private volatile boolean running;
private final Client client;
private final String index;
private final long interval;
private final String type;
public WordDelimiterRunnable(Client client, Settings settings) {
this.client = client;
this.index = settings.get("plugin.dynamic_word_delimiter.protected_words_index", INDEX_NAME);
this.type = settings.get("plugin.dynamic_word_delimiter.protected_words_type", INDEX_TYPE);
this.interval = settings.getAsTime("plugin.dynamic_word_delimiter.refresh_interval", REFRESH_INTERVAL).getMillis();
}
// more code here
}

You need to register the setting using the SettingsModule#registerSettings(Setting) method as explain here:
https://www.elastic.co/guide/en/elasticsearch/reference/5.x/breaking_50_settings_changes.html#breaking_50_settings_changes

Related

I am not getting FLAVOR string in BuildConfig , it is Missing in BuildConfig in Android Studio

How to generate the missing one ? public static final String FLAVOR = "";`
Missing FLAVOR in BuildConfig in Android Studio.It should be like this
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.arkam.konk.look";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0.0";
}
But in my case getting like without this one public static final String FLAVOR = "";
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.arkam.konk.look";
public static final String BUILD_TYPE = "debug";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0.0";}
How to generate the missing one ???
use : Build -> Clean Project and then File -> Invalidate Caches / Restart. and then build your App on smartphone or emulator.
EDIT :
if it does't work, create a new project and copy your classes and codes in that!

Spring annotated controller works, but router/handler approach does not appear to retrieve *Mono<>* from *ServerRequest*

Still playing around and trying to understand the "how" of Spring's Webflux and Reactor.
The following successfully adds a new DemoPOJO to the repo when the annotated controller is used (i.e., POST issued at //localhost:8080/v1/DemoPOJO).
However, when issuing the same POST using the router/handler implementation (i.e., //localhost:8080/v2/DemoPOJO), request.bodyToMono(DemoPOJO.class) does not appear to retrieve the DemoPOJO instance from the ServerRequest (i.e., DemoPOJO.printme() is not being invoked).
I'm "working on this", but thought I'd see if anyone can help me "get there faster". For-what-it's-worth, the router/handler implementations (i.e., GET) that don't require getting a DemoPOJO out of ServerRequest are working.
RESTful endpoints using annotation...
#RestController
public class DemoPOJOController {
private Logger logger = LoggerFactory.getLogger(DemoPOJOHandler.class);
#Autowired
DemoPOJOService service;
#RequestMapping(method = POST, value = "/v1/DemoPOJO")
public Mono<Boolean> addDemoPOJO(#RequestBody DemoPOJO demoPOJO) {
logger.debug("DemoPOJOController.addDemoPOJO( {} )", demoPOJO.getId());
return service.add(demoPOJO);
}
}
"Router" part of the corresponding router/handler implementation...
#Configuration
public class DemoPOJORouter {
private Logger logger = LoggerFactory.getLogger(DemoPOJOHandler.class);
#Bean
public RouterFunction<ServerResponse> route(DemoPOJOHandler requestHandler) {
logger.debug("DemoPOJORouter.route( DemoPOJOHandler )");
return nest(path("/v2"),
nest(accept(APPLICATION_JSON),
RouterFunctions.route(RequestPredicates.POST("/DemoPOJO"), requestHandler::add)));
}
}
"Handler" part of the router/handler implementation...
#Component
public class DemoPOJOHandler {
public static final String PATH_VAR_ID = "id";
private Logger logger = LoggerFactory.getLogger(DemoPOJOHandler.class);
#Autowired
private DemoPOJOService service;
public Mono<ServerResponse> add(ServerRequest request) {
logger.debug("DemoPOJOHandler.add( ServerRequest )");
request.bodyToMono(DemoPOJO.class).doOnSuccess(DemoPOJO::printMe);
return ServerResponse.ok().build();
}
}
DemoPOJORepo implementation (hoping to simplify my learning experience by avoiding a "real" repository)...
#Component
public class DemoPOJORepo {
private static final int NUM_OBJS = 15;
private Logger logger = LoggerFactory.getLogger(DemoPOJORepo.class);
private static DemoPOJORepo demoRepo = null;
private Map<Integer, DemoPOJO> demoPOJOMap;
private DemoPOJORepo() {
logger.debug("DemoPOJORepo.DemoPOJORepo()");
initMap();
}
public boolean add(DemoPOJO demoPOJO) {
logger.debug("DemoPOJORepo.add( DemoPOJO )");
boolean pojoAdded = false;
if (!demoPOJOMap.containsKey(demoPOJO.getId())) {
logger.debug("DemoPOJORepo.add( DemoPOJO ) -> adding for id {}", demoPOJO.getId());
demoPOJOMap.put(demoPOJO.getId(), demoPOJO);
pojoAdded = true;
}
return pojoAdded;
}
private void initMap() {
logger.debug("DemoPOJORepo.initMap()");
demoPOJOMap = new TreeMap<Integer, DemoPOJO>();
for (int ndx = 1; ndx < (NUM_OBJS + 1); ndx++) {
demoPOJOMap.put(ndx, new DemoPOJO(ndx, "foo_" + ndx, ndx + 100));
}
}
}
The objects being manipulated...
public class DemoPOJO {
private Logger logger = LoggerFactory.getLogger(DemoPOJOHandler.class);
public static final String DEF_NAME = "DEFAULT NAME";
public static final int DEF_VALUE = 99;
private int id;
private String name;
private int value;
public DemoPOJO(int id) {
this(id, DEF_NAME, DEF_VALUE);
}
public DemoPOJO(#JsonProperty("id") int id, #JsonProperty("name") String name, #JsonProperty("value") int value) {
logger.debug("DemoPOJO.DemoPOJO( {}, {}, {} )", id, name, value);
this.id = id;
this.name = name;
this.value = value;
}
// getters and setters go here
public void printMe() {
logger.debug("DemoPOJO.printMe()");
System.out.printf("id->%d, name->%s, value->%d%n", id, name, value);
}
}
i am guesstimating here since i am writing from mobile. But i think this is your problem.
request.bodyToMono(DemoPOJO.class).doOnSuccess(DemoPOJO::printMe);
return ServerResponse.ok().build();
You are thinking imperative, that first row will be executed then the second which is not the case in webflux. You have to think events-callbacks.
return request.bodyToMono(DemoPOJO.class)
.doOnSuccess(DemoPOJO::printMe)
.thenReturn(ServerResponse.ok().build());
I think this is it but i could be wrong.

Why I am getting this errors?

I am trying to access to a method named sendHttpPost(final AsyncReponseHttpSending delegate, final String urlString, final Object data), which is in another class, and this class has a private constructor. However when I get the method like HttpSending postMethod = HttpSending.sendHttpPost() and I start to pass the same parameters than in the original class I get the error "delegate is always null" , "data is always null", delegate is an instance of the AsyncResponseHttpSending interface which have only one method void onHttpResult(int httpCode, String httpMessage, String body);what am I doing wrong?
Class from where I get the method
public class HttpSending {
private static final String TAG = "HttpSending: ";
private static final int TIMEOUT_CONNECTION = (int) (30 * UnitsConstants.SECOND_TO_MILISECOND);
private static final int TIMEOUT_READ = (int) (60 * UnitsConstants.SECOND_TO_MILISECOND);
private HttpSending() {
}
public static void sendHttpPost(final AsyncReponseHttpSending delegate, final String urlString, final Object data) {
new Thread(TAG) {
#Override
public void run() {
//BUNCH OF CODE
}
}.start();
}
Class where I do my request
public class HttpPost {
AsyncReponseHttpSending delegate = new AsyncReponseHttpSending() {
#Override
public void onHttpResult(int httpCode, String httpMessage, String body) {
}
};
final String url = "https://postman-echo.com/post";
final Object data = null;
HttpSending postMethod = HttpSending.sendHttpPost(delegate,url,data );
}
This line:
HttpSending postMethod = HttpSending.sendHttpPost(delegate,url,data );
should not compile because HttpSending.sendHttpPost is a void method.
Because you never instantiate delegate that's why you get the warnings for its null.

Mockito mock with when returns null

I mock articleElementSelector.getTag() to return a string and use InjectMocks annotation to inject the mock into the constructor. In the debugger, I see that articleElementSelector is mocked (because of the CGLib stuff), but when getTag() is invoked, it returns null.
HomePageScraperTest:
public class HomePageScraperTest extends UnitTest {
private static final String ARTICLE_TAG = "article";
private static final String URL_HOME_ARTICLE_1 = "http://www.home1.com";
private static final String URL_HOME_ARTICLE_2 = "http://www.home2.com";
#InjectMocks
private HomePageScraper homePageScraper;
#Mock
private JsoupParser jsoupParser;
#Mock
private ArticleScraper articleScraper;
#Mock
private ArticleElementSelector articleElementSelector;
#Mock
private Document homeDocument;
#Mock
private Element element1;
#Mock
private Element element2;
#Mock
private Elements elements1;
#Mock
private Elements elements2;
private URL homeUrl;
private Elements homeArticleElements = new Elements();
private Article homeArticle1;
private Article homeArticle2;
#Before
public void setUp() throws Exception {
homeUrl = new URL(URL_HOME_ARTICLE_1);
homeArticleElements.addAll(asList(element1, element2));
homeArticle1 = anArticle().withTitle("article1").build();
homeArticle2 = anArticle().withTitle("article2").build();
}
#Test
public void scrape() {
HomePage homePage = new HomePage(homeUrl);
when(articleElementSelector.getTag()).thenReturn(ARTICLE_TAG);
when(jsoupParser.parse(homeUrl)).thenReturn(homeDocument);
when(homeDocument.select(ARTICLE_TAG)).thenReturn(homeArticleElements);
when(element1.select("a")).thenReturn(elements1);
when(elements1.attr("href")).thenReturn(URL_HOME_ARTICLE_1);
when(element2.select("a")).thenReturn(elements2);
when(elements2.attr("href")).thenReturn(URL_HOME_ARTICLE_2);
when(articleScraper.scrape(URL_HOME_ARTICLE_1)).thenReturn(homeArticle1);
when(articleScraper.scrape(URL_HOME_ARTICLE_2)).thenReturn(homeArticle2);
List<Article> articles = homePageScraper.scrape(homePage);
assertThat(articles).containsOnly(homeArticle1, homeArticle2);
}
}
HomePageScraper (only relevant code)
#Component
public class HomePageScraper extends AbstractPageScraper {
private static final int HEADLINER_COUNT = 5;
public HomePageScraper(JsoupParser parser, ArticleElementSelector articleElementSelector, ArticleScraper articleScraper) {
super(parser, articleElementSelector, articleScraper);
}
}
AbstractPageScraper
public abstract class AbstractPageScraper {
private final String ARTICLE_TAG;
private JsoupParser parser;
ArticleScraper articleScraper;
public AbstractPageScraper(JsoupParser parser, ArticleElementSelector articleElementSelector, ArticleScraper articleScraper) {
this.parser = parser;
ARTICLE_TAG = articleElementSelector.getTag(); // here the mock returns null
this.articleScraper = articleScraper;
}
}
The test worked before but after I refactored it, pulling up duplicate code in the abstract class, I bumped on this one.

java.lang.ExceptionInInitializerError using MVC

I get this error when I try to compile my code. I am not sure what is wrong with it and I don't understand what is the error saying. Here is the error messaged followed by the lines where it says it errors:
Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
at g52gui.C_login.<clinit>(C_login.java:9)
at g52gui.V_login.<init>(V_login.java:6)
at g52gui.V_login$4.run(V_login.java:181)
...
and the lines where it errors:
line 9 C_login (controller): private static final C_home controller_home = new C_home();
line 6 V_login (view): private final C_login controller_login = new C_login();
line 181 V_login (view): new V_login().setVisible(true);
It seems like the problem comes from the C_home but there is no compilation errors in there.
EDIT
I think the problem might be here, in C_login:
public class C_login {
private static final V_login view_login = new V_login();
private static final M_login model_login = new M_login();
private final static C_home controller_home = new C_home();
private static final C_registration controller_regi = new C_registration();
private static final MySQLAccess sql_connection = new MySQLAccess();
public static void main(String[] args) throws Exception {
view_login.setVisible(true);
}
public static boolean login_button_clicked(String usrn, String psw){
String login = M_login.login(usrn, psw);
if (login == "ok"){
controller_home.start_view(usrn);
view_login.setVisible(false);
} else if (login == "fail" ) {
view_login.error_login();
return false;
} else {
view_login.error_connection();
return false;
}
return false;
}
I delcare C_home controller_home as static so I can access it later. Would there be another way to go around this and could this be the problem ?
EDIT
here are the Initializer in C_Home:
public class C_home {
private static final V_home view_home = new V_home();
private static final M_home model_home = new M_home();
private String username = "";
/* attributes for 3d model */
private BranchGroup sceneBranchGroup = null;
private RotationInterpolator rotator = null;
private Canvas3D offScreenCanvas3D = null;
private ImageComponent2D imageComponent = null;
private static final int offScreenWidth = 200;
private static final int offScreenHeight = 200;

Categories

Resources