This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# 760 | +Shibha | Adding API to get programs in order by regimen temp…
…late configured, Added Regimen and RegimenCategory and corresponding Tables. Added Manage regimen template as a new right. Added apis for regimen get and save
- Loading branch information
1 parent
8c870ed
commit 3210f46
Showing
34 changed files
with
577 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
modules/core/src/main/java/org/openlmis/core/domain/Regimen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.openlmis.core.domain; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class Regimen extends BaseModel { | ||
|
||
private String name; | ||
private String code; | ||
private Long programId; | ||
private Boolean active; | ||
private RegimenCategory category; | ||
private Integer displayOrder; | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
modules/core/src/main/java/org/openlmis/core/domain/RegimenCategory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.openlmis.core.domain; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public class RegimenCategory extends BaseModel { | ||
|
||
private String name; | ||
private String code; | ||
private Integer displayOrder; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
modules/core/src/main/java/org/openlmis/core/repository/RegimenRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.openlmis.core.repository; | ||
|
||
import org.openlmis.core.domain.Regimen; | ||
import org.openlmis.core.repository.mapper.RegimenMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public class RegimenRepository { | ||
|
||
@Autowired | ||
RegimenMapper mapper; | ||
|
||
public void insert(Regimen regimen) { | ||
mapper.insert(regimen); | ||
} | ||
|
||
public void update(Regimen regimen) { | ||
mapper.update(regimen); | ||
} | ||
|
||
public List<Regimen> getByProgram(Long programId) { | ||
return mapper.getByProgram(programId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
modules/core/src/main/java/org/openlmis/core/repository/mapper/RegimenMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.openlmis.core.repository.mapper; | ||
|
||
import org.apache.ibatis.annotations.Insert; | ||
import org.apache.ibatis.annotations.Options; | ||
import org.apache.ibatis.annotations.Select; | ||
import org.apache.ibatis.annotations.Update; | ||
import org.openlmis.core.domain.Regimen; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface RegimenMapper { | ||
|
||
|
||
@Insert({"INSERT INTO regimens (code, name, active, programId, categoryId, displayOrder, createdBy) ", | ||
"VALUES (#{code}, #{name}, #{active}, #{programId}, #{category.id}, #{displayOrder}, #{createdBy})"}) | ||
@Options(useGeneratedKeys = true) | ||
public void insert(Regimen regimen); | ||
|
||
|
||
@Select({"SELECT * FROM regimens r INNER JOIN regimen_categories rc ON r.categoryid = rc.id ", | ||
"WHERE r.programId=#{programId} ORDER BY rc.displayOrder,r.displayOrder"}) | ||
List<Regimen> getByProgram(Long programId); | ||
|
||
@Update("UPDATE regimens set code=#{code}, name=#{name}, active=#{active}, displayOrder = #{displayOrder}") | ||
void update(Regimen regimen); | ||
} |
30 changes: 30 additions & 0 deletions
30
modules/core/src/main/java/org/openlmis/core/service/RegimenService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.openlmis.core.service; | ||
|
||
import org.openlmis.core.domain.Regimen; | ||
import org.openlmis.core.repository.RegimenRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class RegimenService { | ||
|
||
@Autowired | ||
RegimenRepository repository; | ||
|
||
public void save(List<Regimen> regimens) { | ||
for (Regimen regimen : regimens) { | ||
if (regimen.getId() != null) { | ||
repository.update(regimen); | ||
} else { | ||
repository.insert(regimen); | ||
} | ||
} | ||
} | ||
|
||
|
||
public List<Regimen> getByProgram(Long programId) { | ||
return repository.getByProgram(programId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
modules/core/src/test/java/org/openlmis/core/builder/RegimenBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright © 2013 VillageReach. All Rights Reserved. This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. | ||
* | ||
* If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package org.openlmis.core.builder; | ||
|
||
import com.natpryce.makeiteasy.Instantiator; | ||
import com.natpryce.makeiteasy.Property; | ||
import com.natpryce.makeiteasy.PropertyLookup; | ||
import org.openlmis.core.domain.Regimen; | ||
import org.openlmis.core.domain.RegimenCategory; | ||
|
||
import static com.natpryce.makeiteasy.Property.newProperty; | ||
|
||
public class RegimenBuilder { | ||
|
||
|
||
public static final Property<Regimen, String> regimenCode = newProperty(); | ||
public static final Property<Regimen, String> regimenName = newProperty(); | ||
public static final Property<Regimen, Boolean> active = newProperty(); | ||
public static final Property<Regimen, Long> programId = newProperty(); | ||
public static final Property<Regimen, Integer> displayOrder = newProperty(); | ||
public static final Property<Regimen, RegimenCategory> category = newProperty(); | ||
|
||
private static final String REGIMEN_CODE = "3TC/AZT"; | ||
private static final String REGIMEN_NAME = "3TC/AZT + EFV"; | ||
public static final Instantiator<Regimen> defaultRegimen = new Instantiator<Regimen>() { | ||
@Override | ||
public Regimen instantiate(PropertyLookup<Regimen> lookup) { | ||
Regimen regimen = new Regimen(); | ||
regimen.setCode(lookup.valueOf(regimenCode, REGIMEN_CODE)); | ||
regimen.setName(lookup.valueOf(regimenName, REGIMEN_NAME)); | ||
regimen.setActive(lookup.valueOf(active, true)); | ||
regimen.setProgramId(lookup.valueOf(programId, 1L)); | ||
RegimenCategory regimenCategory = new RegimenCategory("ADULTS", "Adults",1); | ||
regimenCategory.setId(1l); | ||
regimen.setCategory(lookup.valueOf(category, regimenCategory)); | ||
regimen.setDisplayOrder(lookup.valueOf(displayOrder, 1)); | ||
return regimen; | ||
} | ||
}; | ||
} |
59 changes: 59 additions & 0 deletions
59
modules/core/src/test/java/org/openlmis/core/repository/RegimenRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.openlmis.core.repository; | ||
|
||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
import org.openlmis.core.domain.Regimen; | ||
import org.openlmis.core.repository.mapper.RegimenMapper; | ||
import org.openlmis.db.categories.UnitTests; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Matchers.isNotNull; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
|
||
@Category(UnitTests.class) | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class RegimenRepositoryTest { | ||
|
||
@Mock | ||
RegimenMapper mapper; | ||
|
||
@InjectMocks | ||
RegimenRepository repository; | ||
|
||
|
||
@Test | ||
public void shouldInsertARegimen() { | ||
Regimen regimen = new Regimen(); | ||
repository.insert(regimen); | ||
verify(mapper).insert(regimen); | ||
} | ||
|
||
@Test | ||
public void shouldUpdateARegimen() { | ||
Regimen regimen = new Regimen(); | ||
repository.update(regimen); | ||
verify(mapper).update(regimen); | ||
} | ||
|
||
@Test | ||
public void shouldGetRegimenByProgram() { | ||
List<Regimen> expectedRegimens = new ArrayList<>(); | ||
Long programId = 1l; | ||
when(mapper.getByProgram(programId)).thenReturn(expectedRegimens); | ||
|
||
List<Regimen> regimenList = repository.getByProgram(programId); | ||
|
||
assertThat(regimenList,is(expectedRegimens)); | ||
verify(mapper).getByProgram(programId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
modules/core/src/test/java/org/openlmis/core/repository/mapper/RegimenMapperIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package org.openlmis.core.repository.mapper; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.junit.runner.RunWith; | ||
import org.openlmis.core.domain.Regimen; | ||
import org.openlmis.core.domain.RegimenCategory; | ||
import org.openlmis.db.categories.IntegrationTests; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.test.context.transaction.TransactionConfiguration; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
import static com.natpryce.makeiteasy.MakeItEasy.a; | ||
import static com.natpryce.makeiteasy.MakeItEasy.make; | ||
import static com.natpryce.makeiteasy.MakeItEasy.with; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertThat; | ||
import static org.openlmis.core.builder.RegimenBuilder.*; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(locations = "classpath:test-applicationContext-core.xml") | ||
@TransactionConfiguration(defaultRollback = true, transactionManager = "openLmisTransactionManager") | ||
@Transactional | ||
@Category(IntegrationTests.class) | ||
public class RegimenMapperIT { | ||
|
||
@Autowired | ||
RegimenMapper mapper; | ||
|
||
Regimen regimen; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
regimen = make(a(defaultRegimen)); | ||
} | ||
|
||
@Test | ||
public void shouldInsertARegimen() throws Exception { | ||
|
||
mapper.insert(regimen); | ||
|
||
assertNotNull(regimen.getId()); | ||
} | ||
|
||
@Test | ||
public void shouldGetAllRegimenForAProgramOrderByCategoryAndDisplayOrder(){ | ||
Long progId = 1l; | ||
Regimen adultRegimen1 = make(a(defaultRegimen, with(regimenCode,"CODE_1"), with(displayOrder, 1), with(programId, progId))); | ||
mapper.insert(adultRegimen1); | ||
Regimen adultRegimen2 = make(a(defaultRegimen, with(regimenCode,"CODE_2"), with(displayOrder, 2), with(programId, progId))); | ||
mapper.insert(adultRegimen2); | ||
RegimenCategory regimenCategory = new RegimenCategory("PAEDIATRICS", "Paediatrics", 2); | ||
regimenCategory.setId(2l); | ||
Regimen paediatricsRegimen1 = make(a(defaultRegimen, with(regimenCode,"CODE_4"), with(displayOrder, 1), with(category, regimenCategory), with(programId, progId))); | ||
mapper.insert(paediatricsRegimen1); | ||
Regimen paediatricsRegimen2 = make(a(defaultRegimen, with(regimenCode,"CODE_3"), with(displayOrder, 2), with(category, regimenCategory), with(programId, progId))); | ||
mapper.insert(paediatricsRegimen2); | ||
|
||
|
||
List<Regimen> regimens = mapper.getByProgram(progId); | ||
|
||
assertThat(regimens.size(), is(4)); | ||
assertThat(regimens.get(0).getCode(), is("CODE_1")); | ||
assertThat(regimens.get(1).getCode(), is("CODE_2")); | ||
assertThat(regimens.get(2).getCode(), is("CODE_4")); | ||
assertThat(regimens.get(3).getCode(), is("CODE_3")); | ||
} | ||
|
||
@Test | ||
public void shouldUpdateARegimen(){ | ||
mapper.insert(regimen); | ||
regimen.setName("Regimen Updated Name"); | ||
regimen.setCode("Regimen Updated Code"); | ||
regimen.setActive(true); | ||
regimen.setDisplayOrder(2); | ||
|
||
mapper.update(regimen); | ||
|
||
List<Regimen> updatedRegimenList = mapper.getByProgram(regimen.getProgramId()); | ||
assertThat(updatedRegimenList.get(0).getName(),is("Regimen Updated Name")); | ||
assertThat(updatedRegimenList.get(0).getCode(),is("Regimen Updated Code")); | ||
assertThat(updatedRegimenList.get(0).getActive(),is(true)); | ||
assertThat(updatedRegimenList.get(0).getDisplayOrder(),is(2)); | ||
} | ||
|
||
} |
Oops, something went wrong.