Implementing Code Review comments

This commit is contained in:
Ankur Gupta 2020-07-30 01:43:57 +05:30
parent b5eef05476
commit 23f1333aaa
5 changed files with 62 additions and 89 deletions

View File

@ -1,48 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-cosmosdb</artifactId> <artifactId>spring-data-cosmosdb</artifactId>
<name>spring-data-cosmos-db</name> <name>spring-data-cosmos-db</name>
<description>tutorial for spring-data-cosmosdb</description> <description>tutorial for spring-data-cosmosdb</description>
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId> <artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath> <relativePath>../../parent-boot-2</relativePath>
</parent> </parent>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
</properties> <cosmodb.version>2.3.0</cosmodb.version>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.microsoft.azure</groupId> <groupId>com.microsoft.azure</groupId>
<artifactId>spring-data-cosmosdb</artifactId> <artifactId>spring-data-cosmosdb</artifactId>
<version>2.3.0</version> <version>${cosmodb.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<dependency> <build>
<groupId>org.springframework.boot</groupId> <plugins>
<artifactId>spring-boot-starter-test</artifactId> <plugin>
<scope>test</scope> <groupId>org.springframework.boot</groupId>
</dependency> <artifactId>spring-boot-maven-plugin</artifactId>
</dependencies> </plugin>
</plugins>
<build> </build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -22,9 +22,13 @@ import java.util.Optional;
@RequestMapping("/products") @RequestMapping("/products")
public class ProductController { public class ProductController {
@Autowired
private ProductService productService; private ProductService productService;
@Autowired
public ProductController(ProductService productService) {
this.productService = productService;
}
@PostMapping @PostMapping
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
public void create(@RequestBody Product product) { public void create(@RequestBody Product product) {
@ -46,8 +50,8 @@ public class ProductController {
return productService.findProductByName(name); return productService.findProductByName(name);
} }
@GetMapping(value = "/category/{category}") @GetMapping(value = "/category")
public List<Product> getByCategory(@PathVariable String category) { public List<Product> getByCategory(@RequestParam String category) {
return productService.getProductsOfCategory(category); return productService.getProductsOfCategory(category);
} }

View File

@ -5,6 +5,11 @@ import com.microsoft.azure.spring.data.cosmosdb.core.mapping.PartitionKey;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Document(collection = "products") @Document(collection = "products")
public class Product { public class Product {
@ -18,41 +23,4 @@ public class Product {
@PartitionKey @PartitionKey
private String productCategory; private String productCategory;
public String getProductid() {
return productid;
}
public void setProductid(String productid) {
this.productid = productid;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductCategory() {
return productCategory;
}
public void setProductCategory(String productCategory) {
this.productCategory = productCategory;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Product [productid=" + productid + ", productName=" + productName + ", price=" + price + ", productCategory=" + productCategory + "]";
}
} }

View File

@ -25,8 +25,7 @@ public class ProductService {
} }
public List<Product> getProductsOfCategory(String category) { public List<Product> getProductsOfCategory(String category) {
List<Product> products = repository.findByProductCategory(category); return repository.findByProductCategory(category);
return products;
} }
public void saveProduct(Product product) { public void saveProduct(Product product) {

View File

@ -14,14 +14,14 @@ import java.util.Optional;
//Uncomment this when configured URI and keys for Azure Cosmos DB in application.properties //Uncomment this when configured URI and keys for Azure Cosmos DB in application.properties
//to run the integration test //to run the integration test
//@SpringBootTest //@SpringBootTest
public class AzurecosmodbApplicationIntegrationTest { public class AzurecosmodbApplicationManualTest {
@Autowired @Autowired
ProductRepository productRepository; ProductRepository productRepository;
// Uncomment this when configured URI and keys for Azure Cosmos DB in application.properties // Uncomment this when configured URI and keys for Azure Cosmos DB in application.properties
// to run the integration test // to run the integration test
// @Test //@Test
public void givenProductIsCreated_whenCallFindById_thenProductIsFound() { public void givenProductIsCreated_whenCallFindById_thenProductIsFound() {
Product product = new Product(); Product product = new Product();
product.setProductid("1001"); product.setProductid("1001");
@ -29,11 +29,9 @@ public class AzurecosmodbApplicationIntegrationTest {
product.setPrice(110.0); product.setPrice(110.0);
product.setProductName("Blue Shirt"); product.setProductName("Blue Shirt");
// Uncomment these lines when configured URI and keys for Azure Cosmos DB in application.properties productRepository.save(product);
// to run the integration test Optional<Product> retrievedProduct = productRepository.findById("1001", new PartitionKey("Shirt"));
// productRepository.save(product); Assert.notNull(retrievedProduct, "Retrieved Product is Null");
// Optional<Product> retrievedProduct = productRepository.findById("1001", new PartitionKey("Shirt"));
// Assert.notNull(retrievedProduct, "Retrieved Product is Null");
} }