How do I Mock Rest Template for Post method - java

I want one JSONObject response after passing URI through RESTTemplate
Test case is passing but the code coverage is still 0%
I have to return accountDetails object in JSON format
how do we Pass URI which takes account ID and given response entity in JSON Format this is what I have to figure out.
Test method:
void scheduleOnDemand() throws Exception {
AccountDTO accountDTO = new AccountDTO();
accountDTO.setId(1);
accountDTO.setTimeZone("Asia/Kolkata");
accountDTO.setPlatformType("AZURE");
accountDTO.setEnvironmentName("test");
accountDTO.setName("azureAccount");
accountDTO.setNextScheduleDate("2021-09-13");
accountDTO.setEnvironmentId(1);
HashMap<String, Object> accountDetails = new HashMap<>();
accountDetails.put("account_Id", "1");
accountDetails.put("TimeZone", "Asia/Kolkata");
accountDetails.put("AgentStatus", "Initiated");
accountDetails.put("Account_Platform", "AZURE");
accountDetails.put("Schedule_Time", "13:30:50.000");
accountDetails.put("Environment_Name", "test");
accountDetails.put("Account_Name", "azureAccount");
accountDetails.put("History_Id", "109");
accountDetails.put("Schedule_Date", "2021-09-13");
accountDetails.put("Environment_Id", "1");
Mockito.when(restTemplate.postForEntity("configure/accountDetails?Account_Id=1",null, JSONObject.class))
.thenReturn(new ResponseEntity((accountDetails), HttpStatus.OK));
}
Actual Method:
#Override
public JSONObject scheduleOnDemand(String accountId) throws Exception {
JSONObject object = null;
// PlatformHistoryDetails phistory = null;
HashMap<String, Object> accountDetails = new HashMap<>();
accountDetails = utilService.getAccountDetails(Integer.parseInt(accountId));
if (((String) accountDetails.get("scantype")).equalsIgnoreCase("infra")||((String) accountDetails.get("scantype")).equalsIgnoreCase("all")) {
URI postUri = UriComponentsBuilder.fromPath("/").pathSegment("api/scheduleOnDemand")
.queryParam("requestId", MDC.get("requestId")).queryParam("service", "scan")
.queryParam("Account_Id", accountId).build().toUri();
PlatformHistoryDetails phistory = modelMapper.map(apiClient.postOperation(postUri, Object.class),
PlatformHistoryDetails.class);
phistory.getHistory().setUser("admin");
object = utilService.processOneAccount(phistory);
} else {
throw new Exception("Account is not of type INFRA or ALL but of type " + accountDetails.get("scantype"));
}
return object;
}
accountDetails Implementation:
#Override
#SuppressWarnings({ "unchecked", "rawtypes" })
public HashMap<String, Object> getAccountDetails(int accountId) {
URI getUri = UriComponentsBuilder.fromPath("/").pathSegment("configure/accountDetails")
.queryParam("Account_Id", accountId).build().toUri();
HashMap<String, Object> account = (LinkedHashMap) apiClient.getAccountDetails(getUri, Object.class);
return account;
}

Related

How to publish mqtt message to kafka in spring

Develop mqtt connector for Kafka using spring.
Using the mqtt library provided by spring, messages are collected as follows.
message handler
#Bean
#ServiceActivator(inputChannel = "mqttInputChannel")
public MessageHandler handler() {
return new MessageHandler() {
#Override
public void handleMessage(Message<?> message) throws MessagingException {
String topic = message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC).toString();
if(topic.equals("myTopic")) {
System.out.println("Mqtt data pub");
}
System.out.println(message.getPayload());
if(topic==null) {
topic = "mqttdata";
}
String tag = "test/vib";
String name = null;
if(name==null) {
name = KafkaMessageService.MQTT_PRODUCER;
}
HashMap<String, Object> datalist = new HashMap<String, Object>();
try {
datalist =convertJSONstringToMap(message.getPayload().toString());
System.out.println(datalist.get("mac"));
counts = kafkaMessageService.publish(topic, name, tag, (HashMap<String,Object>[] datalist);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public static HashMap<String,Object> convertJSONstringToMap(String json) throws Exception {
ObjectMapper mapper = new ObjectMapper();
HashMap<String, Object> map = new HashMap<String, Object>();
map = mapper.readValue(json, new TypeReference<HashMap<String, Object>>() {});
return map;
}
publish method
public int publish(String topic,String producerName,String tag,HashMap<String,Object>[] datalist) throws NotMatchedProducerException,KafkaPubFailureException{
KafkaProducerAdaptor adaptor = searchProducerAdaptor(producerName);
if(adaptor==null) {
throw new NotMatchedProducerException();
}
KafkaTemplate<String,Object> kafkaTemplate = adaptor.getKafkaTemplate();
LocalDateTime currentDateTime = LocalDateTime.now();
String receivedTime = currentDateTime.toString();
ObjectMapper objectMapper = new ObjectMapper();
String key = adaptor.getName();
int counts = 0;
for(HashMap<String,Object> data : datalist) {
Map<String,Object> messagePacket = new HashMap<String,Object>();
messagePacket.put("tag", tag);
messagePacket.put("data", data);
messagePacket.put("receivedtime", receivedTime);
try {
kafkaTemplate.send(topic,key,objectMapper.valueToTree(messagePacket)).get();
logger.info("Sent message : topic=["+topic+"],key=["+key+"] value=["+messagePacket+"]");
} catch(Exception e) {
logger.info("Unable to send message : topic=["+topic+"],key=["+key+"] message=["+messagePacket+"] / due to : "+e.getMessage());
throw new KafkaPubFailureException(e);
}
counts++;
}
return counts;
}
I don't know how to declare a hashmap <String, object> [] as an instance and how to use it.
The above source was taken from spring support as it is, and some modifications were made.

How can I mock two Uri components in one method

I have two url calls in one method that is in addnewMap() - one is the buildGetSubtenantsURL and the other is buildGetAssetsURL
public void addNewMap(MapDTO mapDTO) {
log.info("going to add the map data into db");
if (mapRepository.existsMapWithMapName(mapDTO.getMapName()))
throw new BadRequestException("Map with map name " + mapDTO.getMapName()
+ " already exists. Please provide a different map name.");
Map<String, String> subtenantInfoMap = new HashMap<>();
Maps mapEntity = new Maps();
String iottenant = mapDTO.getTenant();
String subtenantsURL = buildGetSubtenantsURL(null);
String subTenantsResponse = getSubtenants(subtenantsURL,iottenant);
JSONObject subTenant = getSubtenantName(subTenantsResponse);
checkForMultiplePagesSubtenants(subTenantsResponse, subtenantInfoMap,iottenant);
if(subtenantInfoMap.get(mapDTO.getSubtenantName()) != null) {
mapEntity = Maps.builder().subtenant(subtenantInfoMap.get(mapDTO.getSubtenantName()).toString()).build();
}
else {
throw new DataNotFoundException(SUBTENANT_DOESNT_EXIST);
}
String SubtenantId = subtenantInfoMap.get(mapDTO.getSubtenantName());
UriComponents assetsURL = buildGetAssetsURL(iottenant,SubtenantId);
String assetsResponse = getAssets(assetsURL, iottenant);
String mindsphereAssetId = getAssetId(assetsResponse);
if(mindsphereAssetId.isEmpty()) {
throw new DataNotFoundException(ASSET_ID_DOESNT_EXIST);
}
else {
mapEntity = Maps.builder().mindsphereAssetId(mindsphereAssetId).build();
}
mapEntity = Maps.builder().mapName(mapDTO.getMapName()).displayName(getDisplayName(mapDTO))
.description(Objects.nonNull(mapDTO.getDescription()) ? mapDTO.getDescription() : null)
.tenant(getTenantNameForMapDTO(mapDTO)).mindsphereAssetId(mindsphereAssetId).subtenant(subtenantInfoMap.get(mapDTO.getSubtenantName()).toString())
.mapLocation(mapDTO.getMapLocation()).operator(mapDTO.getOperator()).recipeName(mapDTO.getRecipeName()).subtenantName(mapDTO.getSubtenantName())
.createdBy(getUserEmail()).createdAt(new Timestamp(System.currentTimeMillis()))
.build();
Maps createdMap = mapRepository.saveAndFlush(mapEntity);
addStationsMappingforNewMap(createdMap);
}
I have written the test case for the above method as:
#Test
public void addNewMap() {
map = Maps.builder().mapId(1l).mapName("testMap").displayName("Map Test").mindsphereAssetId("a0609ebf2eb7400da8a5fd707e7f68b7").mapLocation("hyd").operator("operator").recipeName("recipe").subtenantName("NSTI").tenant("ctlbrdev").subtenant("9b04027dde5fbd047073805ab8c1c87c")
.tenant(Tenant).build();
maps = Arrays.asList(map);
mapDTO = MapDTO.builder().mapId(1l).mapName("testMap").displayName("Map Test").subtenantName("NSTI").mapLocation("hyd").recipeName("recipe").operator("operator").description("description")
.tenant("ctlbrdev").build();
ReflectionTestUtils.setField(mapService, "mindsphereBaseURL", MindsphereBaseURL);
ReflectionTestUtils.setField(mapService, "mindsphereSubtenantsURL", mindsphereSubtenantsURL);
ReflectionTestUtils.setField(mapService, "mindsphereAssetsURL", mindsphereAssetsURL);
when(restTemplate.exchange(ArgumentMatchers.anyString(), ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(HttpEntity.class),
ArgumentMatchers.<Class<String>>any()))
.thenReturn(new ResponseEntity<String>(entityDtoCreaters.getSubtenant(),HttpStatus.OK));
when(tokenCaching.retrieveHeadersContainingTechToken("ctblrdev")).thenReturn(new HttpHeaders());
when(mapRepository.existsMapWithMapName(any())).thenReturn(false);
//doReturn(Tenant).when(mapService).getTenantName();
doReturn(EMAIL).when(mapService).getUserEmail();
when(mapRepository.saveAndFlush(any())).thenReturn(map);
when(restTemplate.exchange(ArgumentMatchers.anyString(), ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(HttpEntity.class),
ArgumentMatchers.<Class<String>>any()))
.thenReturn(new ResponseEntity<String>(entityDtoCreaters.getSubtenant(),HttpStatus.OK));
Map<String, String> subtenantInfoMap = new HashMap<>();
subtenantInfoMap.get(mapDTO.getSubtenantName());
mapService.addNewMap(mapDTO);
It is not covering the getAssets() method, hence not covering the whole method. how can I achieve this?

initiallize the java bean using multiple values for ElasticSearch indexing

Am trying to create a java class where i want to create indexing in ElasticSearch. Actual data are available from REST API, but for testing my indexing code i have written a logic.
But now, i want to test my indexing code with few dummy data. For that i have created a bean class and using setter/getter i want to replicate the actual scenario for indexing documents in elasticsearch.
Please find my java code below :
public static void main(String args[]) throws IOException
{
System.out.println("Indexing via Java Code ....");
Product prod1=new Product("1001", 123172l, "Product", "VG3000");
Product prod2=new Product("1002", 123172l, "Series", "Valves, VG3000");
Product prod3=new Product("1003", 123172l, "Series", "Activa RoofTop, VG3000");
Product prod4=new Product("1004", 123172l, "Product", "Activa RoofTop VG3000, 3000");
Product prod=new Product();
Map<String, Object> jsonMap ;
for(int i=1;i<4;i++)
{
jsonMap = new HashMap<String, Object>();
jsonMap.put("id", prod.getId());
jsonMap.put("catalog_id", prod.getCatalog_id());
jsonMap.put("catalog_type", prod.getCatalog_type());
jsonMap.put("values", prod.getValues());
IndexRequest request = new IndexRequest(INDEX_NAME, "doc", prod.getId() )
.source(jsonMap);
try {
IndexResponse response = SearchEngineClient.getInstance3().index(request); // increased timeout
} catch(ElasticsearchException e) {
if (e.status() == RestStatus.CONFLICT) {
}
e.printStackTrace();
}
}
System.out.println("Indexing done....");
}
Please find my bean class :
public class Product {
public Product(String id, long catalog_id, String Catalog_type, String values)
{
this.id=id;
this.catalog_id=catalog_id;
this.catalog_type=catalog_type;
this.values=values;
}
public Product()
{
}
private String id;
private long catalog_id;
private String catalog_type;
private String values;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Long getCatalog_id() {
return catalog_id;
}
public void setCatalog_id(Long catalog_id) {
this.catalog_id = catalog_id;
}
public String getCatalog_type() {
return catalog_type;
}
public void setCatalog_type(String catalog_type) {
this.catalog_type = catalog_type;
}
public String getValues() {
return values;
}
public void setValues(String values) {
this.values = values;
}
}
But, while indexing am getting the value from bean class which all the data coming as null.
**Update 1 :
I have modified the code in the below way :
public static void main(String args[]) throws IOException
{
System.out.println("Indexing via Java Code ....");
Product prod1=new Product("1001", 123172l, "Product", "VG3000");
Product prod2=new Product("1002", 123172l, "Series", "Valves, VG3000");
Product prod3=new Product("1003", 3536633, "Series", "Activa RoofTop, VG3000 abcd");
Product prod4=new Product("1004", 123172l, "Product", "Activa RoofTop VG3000, 3000");
Product prod=new Product();
IndexRequest request;
Map<String, Object> jsonMap ;
jsonMap = new HashMap<String, Object>();
jsonMap.put("id", prod1.getId());
jsonMap.put("catalog_id", prod1.getCatalog_id());
jsonMap.put("catalog_type", prod1.getCatalog_type());
jsonMap.put("values", prod1.getValues());
request = new IndexRequest(INDEX_NAME, "doc", prod1.getId() )
.source(jsonMap);
IndexResponse response1 = SearchEngineClient.getInstance3().index(request);
jsonMap = new HashMap<String, Object>();
jsonMap.put("id", prod2.getId());
jsonMap.put("catalog_id", prod2.getCatalog_id());
jsonMap.put("catalog_type", prod2.getCatalog_type());
jsonMap.put("values", prod2.getValues());
request = new IndexRequest(INDEX_NAME, "doc", prod2.getId() )
.source(jsonMap);
IndexResponse response2 = SearchEngineClient.getInstance3().index(request);
jsonMap = new HashMap<String, Object>();
jsonMap.put("id", prod3.getId());
jsonMap.put("catalog_id", prod3.getCatalog_id());
jsonMap.put("catalog_type", prod3.getCatalog_type());
jsonMap.put("values", prod3.getValues());
request = new IndexRequest(INDEX_NAME, "doc", prod3.getId() )
.source(jsonMap);
IndexResponse response3 = SearchEngineClient.getInstance3().index(request);
jsonMap = new HashMap<String, Object>();
jsonMap.put("id", prod4.getId());
jsonMap.put("catalog_id", prod4.getCatalog_id());
jsonMap.put("catalog_type", prod4.getCatalog_type());
jsonMap.put("values", prod4.getValues());
request = new IndexRequest(INDEX_NAME, "doc", prod4.getId() )
.source(jsonMap);
IndexResponse response4 = SearchEngineClient.getInstance3().index(request);
System.out.println("Indexing done....");
}
Is there any other way to simplify the same.?

#RequestBody Map<String, Object> input getting int value

#RequestMapping(path = "/registrationuser", method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE, consumes=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> getUserDetails(#RequestBody Map<String, Object> input) throws commonException {
Map<String, Object> retMap = new HashMap<String, Object>();
String email=(String) input.get("email");
long id=(Long) input.get("userid");
String password=(String) input.get("password");
String rollid="1";
User user = new User();
user.setEmail(email);
user.setId(id);
user.setPassword(bCryptPasswordEncoder.encode(password));
userRepository.save(user);
ResponseEntity<Map<String, Object>> retValue = new ResponseEntity<Map<String,Object>>(retMap, HttpStatus.OK);
return retValue;
}
===
$scope.saveUserFunction = function(myVar) {
console.log($scope.edituserdetails.email);
console.log($scope.edituserdetails.email);
console.log($scope.edituserdetails.username);
console.log($scope.edituserdetails.password);
console.log($scope.edituserdetails.id);
console.log($scope.token);
//$scope.user = {};
// calling our submit function.
$http({
method : "POST",
url : "/registrationuser",
data : {
"email" : $scope.edituserdetails.email,
"username": $scope.edituserdetails.username,
"password" : $scope.edituserdetails.password,
"userid": $scope.edituserdetails.id
}
})
.success(function(data) {
if (data.errors) {
} else {
// $scope.message = data.message;
}
});
}
});
Getting below error for user ID. Why it is come as integer. I need to cast it as long as well?
You could use your entity directly in spring-rest like:
#RequestBody User user
Afterwards the password then also could be reset.
Also the return value shouldn't be a Map.
return new ResponseEntity<User>(user, HttpStatus.OK);

spring mvc controller error java.lang.IllegalStateException: No suitable resolver for argument [0]

Code gives an error
java.lang.IllegalStateException: No suitable resolver for argument
[0][type=org.jopenclass.form.Course]
It sends a JSON response to an ajax call. I use hibernate to persist the objects.
#RequestMapping(value = "/savecourse", method = RequestMethod.POST)
public #ResponseBody
Object saveLecturer(#Valid #ModelAttribute(value = "course") Course course,
BindingResult result) {
Map<String, Object> response = new HashMap<String, Object>();
if (result.hasErrors()) {
List<ObjectError> results = result.getAllErrors();
for (ObjectError objectError : results) {
System.out.println(objectError.getDefaultMessage());
}
response.put("message", "Could not add the Course to the system.");
} else {
try {
course.setId(courseDao.saveCourse(course));//returns the id
response.put("course", course);
} catch (Exception e) {
System.out.println(e);
}
}
return response;
}
But when I create a new object and copy the parameters to the other object, it works fine. The second method(Not a good method of course) works well. All the parameters in the request object are set to the cse object as well.
#RequestMapping(value = "/savecourse", method = RequestMethod.POST)
public #ResponseBody
Object saveLecturer(#Valid #ModelAttribute(value = "course") Course course,
BindingResult result) {
Map<String, Object> response = new HashMap<String, Object>();
if (result.hasErrors()) {
List<ObjectError> results = result.getAllErrors();
for (ObjectError objectError : results) {
System.out.println(objectError.getDefaultMessage());
}
response.put("message", "Could not add the Course to the system.");
} else {
try {
course.setId(courseDao.saveCourse(course));//returns the id
Course cse = new Course();
cse.setId(course.getId());
cse.setCourseName(course.getCourseName());
cse.setFee(course.getFee());
Lecturer lec = new Lecturer();
lec.setId(course.getLecturer().getId());
lec.setFirstName(course.getLecturer().getFirstName());
lec.setLastName(course.getLecturer().getLastName());
cse.setLecturer(lec);
cse.setGrade(course.getGrade());
response.put("course", cse);
} catch (Exception e) {
System.out.println(e);
}
}
return response;
}
What is wrong in the first method?
In the first case Jackson is not able to deserialize your response. I would suggest changing your return type to Map<String, ? extends object>
Let us know if the problem persists

Categories

Resources