Skip to content

Instantly share code, notes, and snippets.

View roshanadh's full-sized avatar
🌅
Working from home

Roshan Adhikari roshanadh

🌅
Working from home
View GitHub Profile
@roshanadh
roshanadh / ApplicationTests.java
Created October 21, 2022 18:16
Testing POST /batteries
@Test
void shouldPersistBattery() throws Exception {
Battery mockBattery =
new Battery("Gold Coast Mc", "9729", 50000);
mockMvc
.perform(
post("/batteries")
.content(objectMapper.writeValueAsString(mockBattery))
.contentType(MediaType.APPLICATION_JSON)
@roshanadh
roshanadh / ApplicationTests.java
Last active October 21, 2022 18:10
Testing GET /batteries
@Test
void shouldReturnAllBatteries() throws Exception {
mockMvc
.perform(get("/batteries/"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$", Matchers.hasSize(H2Bootstrap.mockBatteries.size())));
}
@roshanadh
roshanadh / ApplicationTests.java
Last active October 21, 2022 18:03
Testing GET /batteries/:id
package np.com.roshanadhikary.testdemo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@SpringBootTest
@AutoConfigureMockMvc
class ApplicationTests {
// ..
@roshanadh
roshanadh / ApplicationTests.java
Created October 21, 2022 17:48
Spring Boot test class
package np.com.roshanadhikary.testdemo;
@SpringBootTest
@AutoConfigureMockMvc
class ApplicationTests {
@Autowired
private MockMvc mockMvc;
@Autowired
@roshanadh
roshanadh / application.properties
Created October 21, 2022 14:09
Spring Boot properties
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=user
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
@roshanadh
roshanadh / H2Bootstrap.java
Created October 21, 2022 13:57
Bootstrap H2 database with preloaded records
package np.com.roshanadhikary.testdemo.bootstrap;
/**
* Bootstrap the in-memory H2 database with some Battery resources when
* the application starts
*/
@Configuration
public class H2Bootstrap {
private static final Logger logger = LoggerFactory.getLogger(H2Bootstrap.class);
@roshanadh
roshanadh / BatteryRepository.java
Created October 21, 2022 13:48
Repository interface for Battery entity
package np.com.roshanadhikary.testdemo.repository;
import np.com.roshanadhikary.testdemo.entity.Battery;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BatteryRepository extends JpaRepository<Battery, Integer> {
}
@roshanadh
roshanadh / Battery.java
Created October 21, 2022 13:45
Battery resource POJO
package np.com.roshanadhikary.testdemo.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import javax.persistence.*;
/**
@roshanadh
roshanadh / Denoms.js
Last active August 11, 2021 14:46
Find the distinct denominations required for a transaction
/**
* Find the distinct denominations required for a transaction
*/
// list of denominations
const denoms = [1000, 500, 100, 50, 20, 10, 5, 2, 1];
// return array of denoms smaller than or equal to the passed remainder
const getValidDenoms = (remainder) => {
return denoms.filter((el) => {
return el <= remainder;

This blog post has been pushed to the respository after deployment to Heroku.
Let's hope this works!