failed to connect to 192.168.95.2 on port 80 - java

I'm trying to run my android app on real android device and i put the IPv4 address as URL , but i get an error that says failed to connect to 192.168.95.2 on port 80 , i tried all things in previous questions but didn't work out also checked my permissions and make sure that i added internet permission :/
here is the code :
public class Constants {
public static final String BASE_URL = "http://192.168.95.2/";
public static final String REGISTER_OPERATION = "register";
public static final String LOGIN_OPERATION = "login";
public static final String CHANGE_PASSWORD_OPERATION = "chgPass";
public static final String SUCCESS = "success";
public static final String FAILURE = "failure";
public static final String IS_LOGGED_IN = "isLoggedIn";
public static final String NAME = "name";
public static final String EMAIL = "email";
public static final String UNIQUE_ID = "unique_id";
public static final String TAG = "Learn2Crack";

Related

How to get AppSearch private api key in integration test

I am using EnterpriseElasticSearch (AppSearch) and ElasticSearch for my web application. In integration test I use testcontainer to setup real database. To call api of AppSearch you need to provide AppSearch api private key. My question is how to get private api key in my integration test?
Below is my config for AppSearch & ElasticSearch by using testcontainer
/**
* The base class for all integration test classes.
*/
public abstract class AbstractTest {
private static final Logger log = LoggerFactory.getLogger(AbstractTest.class);
// ElasticSearch
public static final String ELASTICSEARCH_VERSION = "7.12.0";
public static final String ELASTICSEARCH_USERNAME = "elastic";
private static final String ELASTICSEARCH_PASSWORD = "elasticsearch";
private static final String ELASTICSEARCH_HOSTNAME = "elasticsearch";
private static final int ELASTICSEARCH_PORT = 9200;
private static final Duration ELASTICSEARCH_STARTUP_TIMEOUT = Duration.ofMinutes(10);
private static final DockerImageName ELASTICSEARCH_IMAGE = DockerImageName
.parse("docker.elastic.co/elasticsearch/elasticsearch")
.withTag(ELASTICSEARCH_VERSION);
protected static final ElasticsearchContainer ELASTICSEARCH =
new ElasticsearchContainer(ELASTICSEARCH_IMAGE);
// EnterpriseSearch
private static final String APPSEARCH_HOSTNAME = "enterprisesearch";
private static final Duration APPSEARCH_STARTUP_TIMEOUT = Duration.ofMinutes(10);
private static final DockerImageName APPSEARCH_IMAGE = DockerImageName
.parse("docker.elastic.co/enterprise-search/enterprise-search")
.withTag(ELASTICSEARCH_VERSION);
protected static final GenericContainer APPSEARCH =
new GenericContainer(APPSEARCH_IMAGE);
private static void startElasticSearch() {
ELASTICSEARCH.withPassword(ELASTICSEARCH_PASSWORD)
.withNetworkAliases(ELASTICSEARCH_HOSTNAME)
.withStartupTimeout(ELASTICSEARCH_STARTUP_TIMEOUT)
.start();
}
private static void startAppSearch() {
APPSEARCH.withNetworkAliases(APPSEARCH_HOSTNAME)
.withEnv("elasticsearch.host", String.format("http://%s:%s", ELASTICSEARCH_HOSTNAME, ELASTICSEARCH_PORT))
.withEnv("ent_search.auth.source", "standard")
.withEnv("elasticsearch.username", ELASTICSEARCH_USERNAME)
.withEnv("elasticsearch.password", ELASTICSEARCH_PASSWORD)
.withEnv("JAVA_OPTS", " $JAVA_OPTS -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8")
.withEnv("ENT_SEARCH_DEFAULT_PASSWORD", ELASTICSEARCH_PASSWORD)
.withEnv("allow_es_settings_modification", "true")
.withStartupTimeout(APPSEARCH_STARTUP_TIMEOUT)
.start();
}
}

Required, request body is missing: public when I try loading Swagger

I am trying to access the swagger URL from my Spring-Boot application using the post request. But getting a 400-Bad request and it says the request body is missing. But the same request works fine in Postman.
Controller:
#RestController
public class IdVController {
#Autowired
private IdService idService;
#Autowired
protected FileUtility util;
/** The Constant STATUS. */
private static final String STATUS = "status";
/** The Constant SUCCESS. */
private static final String SUCCESS = "success";
/** The Constant SYSTEM. */
private static final String SYSTEM = "SYSTEM";
#RequestMapping(value ="/idApi")
public ResponseEntity<MarketPlaceResponse>
validateIdData(#RequestBody VerifyIdDTO verifyIdDTO) throws Exception {
JSONObject rawResponse = idService.validateId(verifyIdDTO);
IdVerifyEntity DTOResponse = (IdVerifyEntity)util.convertToEntity(rawResponse.toString(), IdVerifyEntity.class);
if (rawResponse.get(STATUS).equals(SUCCESS)) {
DTOResponse.setCreated_by(SYSTEM);
DTOResponse.setCreated_date(new
java.sql.Date(System.currentTimeMillis()).toString());
}
//return ResponseBuilder.buildResponse(DTOResponse);
MarketPlaceResponse response = new MarketPlaceResponse();
response.setResponse(DTOResponse);
return new ResponseEntity<MarketPlaceResponse>(response,HttpStatus.OK);
}
}
Request DTO:
public class VerifyIdDTO {
#NotBlank(message="Owner ID should not be empty")
private String id;
private String citizenship;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCitizenship() {
return citizenship;
}
public void setCitizenship(String citizenship) {
this.citizenship = citizenship;
}
}
I am contacting an external service by building URL and using GET from this POST method
this is the swagger URL
localhost:8080/idvalidationservice/swagger-ui.html
You can see the error message in this screenshot.

Java error "cannot find symbol" when using a constructor

This is the class and it's constructor that I made. No problems while compiling.
public class Usuario
{
private String usuario="a";
private String contrasena="a";
private String nombre="a";
private String apellido="a";
private int dni=0;
private int edad=0;
public Usuario (String usuario, String contrasena, String nombre, String apellido, int dni, int edad)
{
this.usuario=usuario;
this.contrasena=contrasena;
this.nombre=nombre;
this.apellido=apellido;
this.dni=dni;
this.edad=edad;
}
And this is my main code where I use the constructor.
public class userTest
{
public static void main (String args[])
{
Usuario philip;
philip=new Usuario (user987, pass123, Philip, Fry, 11000111, 21);
}
}
When compiling this part, the javac shows that error.
philip=new Usuario (user987, pass123, Philip, Fry, 11000111, 21);
The first 4 parameters are not defined.
If you are attempting to pass String values to the constructor, then the code should be:
philip= new Usuario ("user987", "pass123", "Philip", "Fry', 11000111, 21);

Junit mokito util.properties show null after moking

I write a email testing using greenmail and JUNIT with mokito but
when I use
when(emailproperties.getUsername()).thenReturn("abc#gmail.com");
show null
Here is my code
public class EmailServiceImplTest {
#InjectMocks
EmailServiceImpl emailServiceImpl = new EmailServiceImpl();
#Mock
private MessageTemplateService messageTemplateService;
#Mock
private EmailProperties emailProperties;
//private static final Logger LOGGER = Logger.getLogger(EmailServiceImplTest.class);
private GreenMail greenMail;
private static final String USER_NAME = "hascode";
private static final String EMAIL_USER_ADDRESS = "hascode#localhost";
private static final String EMAIL_TO = "someone#localhost.com";
private static final String EMAIL_SUBJECT = "Test E-Mail";
private static final String EMAIL_TEXT = "This is a test e-mail.";
private static final String LOCALHOST = "localhost";
private static final String USER_PASSWORD = "abcdef123";
#Before
public void testSmtpInit() {
MockitoAnnotations.initMocks(this);
greenMail = new GreenMail(ServerSetupTest.SMTP);
greenMail.start();
greenMail.setUser(EMAIL_USER_ADDRESS, USER_NAME, USER_PASSWORD);
}
#Test
public void sendContactEmail() {
MessageTemplate mock_template=new MessageTemplate();
mock_template.setId(2);
mock_template.setBody("Hi ${name} want to contact");
mock_template.setSubject("Contact EMAIL");
ContactDTO mock_Dto=new ContactDTO();
mock_Dto.setFirstName("abc");
mock_Dto.setLastName("xyz");
Properties mock_props = System.getProperties();
mock_props.put("mail.smtp.host", LOCALHOST);
mock_props.put("mail.smtp.auth", "true");
mock_props.put("mail.smtp.port", ServerSetupTest.SMTP.getPort());
mock_props.put("mail.debug", "true");
when(messageTemplateService.getMessageTemplateById("2")).thenReturn(mock_template);
emailProperties.setAdminTo("abc#mail.com");
when(emailProperties.getAdminTo()).thenReturn("abc#mail.com");
when(emailProperties.getContactMsgKey()).thenReturn("2");
when(emailProperties.getProps()).thenReturn(mock_props);
when(emailProperties.getSenderEmail()).thenReturn(EMAIL_USER_ADDRESS);
when(emailProperties.getSender()).thenReturn(USER_NAME);
when(emailProperties.getHost()).thenReturn(LOCALHOST);
when(emailProperties.getUserName()).thenReturn(EMAIL_USER_ADDRESS);
when(emailProperties.getPassword()).thenReturn(USER_PASSWORD);
emailServiceImpl.sendContactEmail(mock_Dto);
MimeMessage[] messages = greenMail.getReceivedMessages();
assertNotNull(messages);
assertEquals(1, messages.length);
}
when I debug it show null what I am doing wronge
Yes Done.Change to #spy
#Mock
private EmailProperties emailProperties;
and set the value as it is.
15 days effort end to day.

ContentProvider.Query - How to select the table I want using URIs?

I'm currently facing a database query issue. I have two tables, items and details with the following schema definition:
items(_id, title, type) <--- _id is primary key
details(_id, _itemid, name, quantity, dosis) <--- _id is primary key. _itemid is foreign key
Furthermore I access my database via. my own contentProvider. In my activity I want to query the details table to get all tuples, however Im not sure how to "get" the table using URI's. My query is:
Uri subItems = Uri.parse("content://tod.dosetracker.provider.Food");
Cursor c = getContentResolver().query(
subItems,
new String[] {"*"},
"_itemid = " + _id,
null,
null);
In my ContentProvider class I have the following (extract):
public static final String PROVIDER_NAME = "tod.dosetracker.provider.Food";
public static final Uri CONTENT_URI_ITEMS = Uri.parse("content://" + PROVIDER_NAME + "/items");
public static final Uri CONTENT_URI_DETAILS = Uri.parse("content://" + PROVIDER_NAME + "/details");
public static final String _ID = "_id";
public static final String _ITEMID = "_itemid";
public static final String TITLE = "title";
public static final String TYPE = "type";
public static final String NAME = "name";
public static final String DOSIS = "dosis";
public static final String QUANTITY = "quantity";
private static final int ITEM = 1;
private static final int ITEM_ID = 2;
private static final int DETAILS = 3;
private static final int DETAILS_ID = 4;
// URI resource matchers
private static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "items", ITEM);
uriMatcher.addURI(PROVIDER_NAME, "items/#", ITEM_ID);
uriMatcher.addURI(PROVIDER_NAME, "details", DETAILS);
uriMatcher.addURI(PROVIDER_NAME, "details/#", DETAILS_ID);
}
// Database reference name
private SQLiteDatabase foodDB;
You are using the wrong content uri. Use the one you defined for your items instead(if you want items)
FoodContentProvider.CONTENT_URI_ITEMS
So your query would look like this (oh BTW just put null if you want all columns);
Uri subItems = Uri.parse("content://tod.dosetracker.provider.Food");
Cursor c = getContentResolver().query(
CONTENT_URI_ITEMS,
null,
"_itemid = " + _id,
null,
null);

Categories