Commit c68a9652 authored by sharma072072's avatar sharma072072

end point created with repository test case

parent d019eb8f
Pipeline #2291 failed with stages
in 1 minute and 15 seconds
package com.altimetrik.ee.demo.bean;
import java.util.Date;
import java.util.Objects;
import javax.persistence.*;
@Entity
@Table(name = "stocks")
public class Stocks {
@Id
@Column(name = "stockQuote")
String stockQuote;
@Column(name = "companyName")
String companyName;
@Column(name = "date")
Date date;
@Column(name = "price")
Double price;
@Column(name = "currency")
String currency;
public Stocks(String stockQuote, String companyName, Date date, Double price,
String currency) {
this.stockQuote = stockQuote;
this.companyName = companyName;
this.date = date;
this.price = price;
this.currency = currency;
}
public Stocks() {
}
public String getStockQuote() {
return stockQuote;
}
public String getCompanyName() {
return companyName;
}
public Date getDate() {
return date;
}
public Double getPrice() {
return price;
}
public String getCurrency() {
return currency;
}
public void setStockQuote(String stockQuote) {
this.stockQuote = stockQuote;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public void setDate(Date date) {
this.date = date;
}
public void setPrice(Double price) {
this.price = price;
}
public void setCurrency(String currency) {
this.currency = currency;
}
@Override
public String toString() {
return "Stocks{" +
"stockQuote='" + stockQuote + '\'' +
", companyName='" + companyName + '\'' +
", date=" + date +
", price=" + price +
", currency='" + currency + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Stocks stocks = (Stocks) o;
return Objects.equals(stockQuote, stocks.stockQuote) && Objects.equals(
companyName, stocks.companyName) && Objects.equals(date, stocks.date)
&& Objects.equals(price, stocks.price) && Objects.equals(currency,
stocks.currency);
}
@Override
public int hashCode() {
return Objects.hash(stockQuote, companyName, date, price, currency);
}
}
package com.altimetrik.ee.demo.controller;
import com.altimetrik.ee.demo.bean.Stocks;
import com.altimetrik.ee.demo.service.StocksService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/stocks")
public class StocksController {
@Autowired
StocksService stocksService;
@GetMapping(value = "/stockQuote", produces = "application/json")
public ResponseEntity<List<Stocks>>
getSpecificStocks(@RequestParam(value="symbols", required = false) List<String> symbols) {
try {
List<Stocks> stocksList = new ArrayList<Stocks>();
if (symbols == null)
stocksService.getStocks().forEach(stocksList::add);
else if (symbols.size()<=10) {
stocksService.getStocksByIds(symbols).forEach(stocksList::add);
}
else
return new ResponseEntity<>(HttpStatus.PAYLOAD_TOO_LARGE);
if (stocksList.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(stocksList, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
package com.altimetrik.ee.demo.repository;
import com.altimetrik.ee.demo.bean.Stocks;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface StocksRepository extends JpaRepository<Stocks, String> {
String QUERY = "SELECT * FROM stocks WHERE stockQuote IN (?1)";
@Query(value = QUERY, nativeQuery = true)
List<Stocks> findByStockQuotes(List<String> ids);
}
package com.altimetrik.ee.demo.service;
import com.altimetrik.ee.demo.bean.Stocks;
import com.altimetrik.ee.demo.repository.StocksRepository;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StocksService {
@Autowired
private StocksRepository repository;
public List<Stocks> getStocksByIds(List<String> ids) {
return repository.findByStockQuotes(ids);
}
public List<Stocks> getStocks() {
return repository.findAll();
}
}
package com.altimetrik.ee.demo.tests;
import static org.assertj.core.api.Assertions.assertThat;
import com.altimetrik.ee.demo.bean.Stocks;
import com.altimetrik.ee.demo.repository.StocksRepository;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@DataJpaTest
public class StocksJPATest {
@Autowired
private StocksRepository stocksRepository;
@Test
public void shouldCreateNewStock() throws ParseException {
String sDate1="31/12/1998";
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
Stocks s1 = new Stocks("HCL-1", "HCL", date1, 420.0, "INR");
stocksRepository.save(s1);
}
@Test
public void shouldGetStocksByName() throws ParseException {
String sDate1="31/12/1998";
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
Stocks s1 = new Stocks("HCL-1", "HCL", date1, 420.0, "INR");
Stocks s2 = new Stocks("HCL-2", "HCL", date1, 520.0, "USD");
Stocks s3 = new Stocks("HCL-3", "HCL", date1, 620.0, "JNY");
Stocks s4 = new Stocks("HCL-4", "HCL", date1, 720.0, "INR");
List<Stocks> list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
stocksRepository.saveAll(list);
List<String> queries = new ArrayList<>();
queries.add("HCL-1");
queries.add("HCL-2");
queries.add("HCL-3");
Iterable<Stocks> users = stocksRepository.findByStockQuotes(queries);
users.forEach(e -> System.out.println(e.toString()));
assertThat(users).hasSize(3);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment