Commit 65b0472bcfcf4f098d780d9c6a44baa12910726f

Authored by Pierrick
1 parent 067182a5

add SkillGlobal, GlobalEvaluationCache, SkillGlobalGetTest

skilldr-rs/src/main/java/eu/totoandco/rs/SkillCalculatorEndPoint.java
... ... @@ -7,7 +7,7 @@ import javax.ejb.Stateless;
7 7 import javax.servlet.http.HttpServletRequest;
8 8 import javax.ws.rs.FormParam;
9 9 import javax.ws.rs.GET;
10   -import javax.ws.rs.POST;
  10 +import javax.ws.rs.PUT;
11 11 import javax.ws.rs.Path;
12 12 import javax.ws.rs.PathParam;
13 13 import javax.ws.rs.core.Context;
... ... @@ -73,7 +73,7 @@ public class SkillCalculatorEndPoint extends EndPoint {
73 73 * @param value
74 74 * New value to set
75 75 */
76   - @POST
  76 + @PUT
77 77 @Path("/p/setBaseExperienceSkill/")
78 78 public void setBaseExperienceSkill(@FormParam("value") double value,
79 79 @Context HttpServletRequest request) {
... ... @@ -94,10 +94,10 @@ public class SkillCalculatorEndPoint extends EndPoint {
94 94 * @param period
95 95 * New period to affect
96 96 */
97   - @POST
98   - @Path("/p/setBaseExperienceSkill/")
  97 + @PUT
  98 + @Path("/p/setBaseExperienceSkill/{idExperienceType}")
99 99 public void setBaseExperienceSkill(
100   - @FormParam("idExperienceType") int idExperienceType,
  100 + @PathParam("idExperienceType") int idExperienceType,
101 101 @FormParam("period") int period, @Context HttpServletRequest request) {
102 102  
103 103 if (idExperienceType <= 0) {
... ...
skilldr-rs/src/main/java/eu/totoandco/rs/SkillGlobalEndPoint.java 0 โ†’ 100644
  1 +package eu.totoandco.rs;
  2 +
  3 +import java.util.List;
  4 +
  5 +import javax.ejb.EJB;
  6 +import javax.ejb.Stateless;
  7 +import javax.servlet.http.HttpServletRequest;
  8 +import javax.ws.rs.GET;
  9 +import javax.ws.rs.Path;
  10 +import javax.ws.rs.PathParam;
  11 +import javax.ws.rs.core.Context;
  12 +
  13 +import eu.totoandco.business.SkillEvaluation;
  14 +import eu.totoandco.business.SkillGlobal;
  15 +import eu.totoandco.check.RestParameterCheck;
  16 +import eu.totoandco.model.User;
  17 +
  18 +@Path("/")
  19 +@Stateless
  20 +public class SkillGlobalEndPoint extends EndPoint {
  21 +
  22 + @EJB
  23 + SkillGlobal skillGlobal;
  24 +
  25 + /**
  26 + * Get an evaluation for a precise skill. The evaluation is based on global data
  27 + * @param idSkill The skill's id to evaluate
  28 + * @param request User's context
  29 + * @return an evaluation
  30 + */
  31 + @GET
  32 + @Path("/p/globalEvaluation/{idSkill}")
  33 + public SkillEvaluation getEvaluationById(@PathParam("idSkill") int idSkill,
  34 + @Context HttpServletRequest request) {
  35 + RestParameterCheck.checkNotNegativeOrZero(idSkill);
  36 + return skillGlobal.getEvaluationById(idSkill);
  37 + }
  38 +
  39 + /**
  40 + * Get a list of evaluation for a list of id. The evaluation is based on global data
  41 + * @param listIdSkill The list of skill's id to evaluate
  42 + * @param request User's context
  43 + * @return a list of evaluations
  44 + */
  45 + @GET
  46 + @Path("/p/globalEvaluation/{listIdSkill}")
  47 + public List<SkillEvaluation> getEvaluationMultiId(@PathParam("listIdSkill") List<Integer> listIdSkill,
  48 + @Context HttpServletRequest request) {
  49 + RestParameterCheck.checkNotEmpty(listIdSkill);
  50 + return skillGlobal.getEvaluationMultiId(listIdSkill);
  51 + }
  52 +
  53 + /**
  54 + * Get an evaluation for all skill for the current user. The evaluation is based on global data
  55 + * @param request User's context
  56 + * @return a list of evaluations
  57 + */
  58 + @GET
  59 + @Path("/p/globalEvaluationByUser/")
  60 + public List<SkillEvaluation> getEvaluationsByUser(@Context HttpServletRequest request) {
  61 + return skillGlobal.getEvaluationsByUser((User) request.getAttribute(USERSESSION));
  62 + }
  63 +}
... ...
skilldr-service/src/main/java/eu/totoandco/business/SkillCalculator.java
... ... @@ -118,8 +118,10 @@ public class SkillCalculator {
118 118 * Make global evaluations for all skill mastery
119 119 * @return A list of different evaluations
120 120 */
121   - public List<SkillEvaluation> getGlobalEvaluation(){
122   - return makeAllEvaluations(modelUserSkillAccess.getAll(UserSkill.class));
  121 + public List<SkillEvaluation> makeGlobalEvaluation(){
  122 + List<UserSkill> list = modelUserSkillAccess.getAll(UserSkill.class);
  123 + System.err.println("SIZE " + list.size());
  124 + return makeAllEvaluations(list);
123 125 }
124 126  
125 127 /**
... ...
skilldr-service/src/main/java/eu/totoandco/business/SkillGlobal.java 0 โ†’ 100644
  1 +package eu.totoandco.business;
  2 +
  3 +import java.util.ArrayList;
  4 +import java.util.List;
  5 +
  6 +import javax.ejb.EJB;
  7 +import javax.ejb.LocalBean;
  8 +import javax.ejb.Stateless;
  9 +
  10 +import eu.totoandco.cache.GlobalEvaluationCache;
  11 +import eu.totoandco.check.ParameterCheck;
  12 +import eu.totoandco.entitymanager.UserSkillAccess;
  13 +import eu.totoandco.model.User;
  14 +import eu.totoandco.model.UserSkill;
  15 +
  16 +@Stateless
  17 +@LocalBean
  18 +public class SkillGlobal {
  19 +
  20 + @EJB
  21 + UserSkillAccess userSkillModel;
  22 +
  23 + @EJB
  24 + GlobalEvaluationCache globalEvaluationCache;
  25 +
  26 + /**
  27 + * Get an evaluation for a precise skill. The evaluation is based on global
  28 + * data
  29 + *
  30 + * @param idSkill
  31 + * The skill's id to evaluate
  32 + * @return an evaluation
  33 + */
  34 + public SkillEvaluation getEvaluationById(int idSkill) {
  35 + SkillEvaluation skillEvaluation = null;
  36 + System.out.println("Passage1");
  37 + if (ParameterCheck
  38 + .checkNotEmpty(globalEvaluationCache.getEvaluations()) == true) {
  39 + System.out.println("Passage2");
  40 + for (SkillEvaluation se : globalEvaluationCache.getEvaluations()) {
  41 + System.out.println("Passage3 " +se.getId());
  42 + if (se.getId() == idSkill) {
  43 + System.out.println("Passage4");
  44 + skillEvaluation = se;
  45 + break;
  46 + }
  47 + }
  48 + System.out.println("Passage5");
  49 + }
  50 + System.out.println("Passage6");
  51 + return skillEvaluation;
  52 + }
  53 +
  54 + /**
  55 + * Get a list of evaluation for a list of id. The evaluation is based on
  56 + * global data
  57 + *
  58 + * @param listIdSkill
  59 + * The list of skill's id to evaluate
  60 + * @return a list of evaluations
  61 + */
  62 + public List<SkillEvaluation> getEvaluationMultiId(List<Integer> listIdSkill) {
  63 + List<SkillEvaluation> listSkillEvaluation = new ArrayList<>();
  64 + for (Integer idSkill : listIdSkill) {
  65 + listSkillEvaluation.add(getEvaluationById(idSkill));
  66 + }
  67 + return listSkillEvaluation;
  68 + }
  69 +
  70 + /**
  71 + * Get an evaluation for all skill for the current user. The evaluation is
  72 + * based on global data
  73 + *
  74 + * @return a list of evaluations
  75 + */
  76 + public List<SkillEvaluation> getEvaluationsByUser(User userSession) {
  77 + List<UserSkill> listUserSkill = userSkillModel
  78 + .getAllUserSkillsByUser(userSession.getId());
  79 + List<Integer> listIdSkill = new ArrayList<>();
  80 + for (UserSkill us : listUserSkill) {
  81 + listIdSkill.add(us.getSkill().getId());
  82 + }
  83 +
  84 + return getEvaluationMultiId(listIdSkill);
  85 + }
  86 +}
... ...
skilldr-service/src/main/java/eu/totoandco/cache/GlobalEvaluationCache.java
... ... @@ -9,10 +9,11 @@ import javax.ejb.Startup;
9 9  
10 10 import eu.totoandco.business.SkillCalculator;
11 11 import eu.totoandco.business.SkillEvaluation;
  12 +import eu.totoandco.entitymanager.UserSkillAccess;
12 13  
13 14 /**
14   - * @author Ronan Presle
15   - * This class is a singleton reserved for Skilldr parameters stored in database. It is loaded at startup
  15 + * @author Ronan Presle This class is a singleton reserved for Skilldr
  16 + * parameters stored in database. It is loaded at startup
16 17 */
17 18 @Singleton
18 19 @Startup
... ... @@ -22,22 +23,38 @@ public class GlobalEvaluationCache {
22 23 * Map storing skilldr evaluations
23 24 */
24 25 private List<SkillEvaluation> evaluations;
25   -
  26 +
26 27 @EJB
27 28 SkillCalculator skillCalculator;
28 29  
  30 + @EJB
  31 + UserSkillAccess userSkillModel;
  32 +
29 33 /**
30 34 * Initialize the parameter Cache
31 35 */
32 36 @PostConstruct
33 37 public void init() {
34   - evaluations = skillCalculator.getGlobalEvaluation();
  38 + evaluations = skillCalculator.makeGlobalEvaluation();
35 39 }
  40 +
  41 + /**
  42 + * Get the global evaluation's list
  43 + * @return list of SkillEvaluation
  44 + */
36 45 public List<SkillEvaluation> getEvaluations() {
37 46 return evaluations;
38 47 }
39 48  
  49 + /**
  50 + * Set the global evaluation's list
  51 + * @param evaluations new list of SkillEvaluation
  52 + */
40 53 public void setEvaluations(List<SkillEvaluation> evaluations) {
41 54 this.evaluations = evaluations;
42 55 }
  56 +
  57 + public void resetEvaluation(){
  58 + evaluations = skillCalculator.makeGlobalEvaluation();
  59 + }
43 60 }
... ...
skilldr-service/src/main/java/eu/totoandco/timer/EvaluationTimer.java
... ... @@ -30,7 +30,7 @@ public class EvaluationTimer {
30 30 */
31 31 @Schedule(second="0", minute="0",hour="0", dayOfWeek="*", persistent=false)
32 32 public void getEvaluationsTimer() {
33   - globalEvaluationCache.getEvaluations();
  33 + globalEvaluationCache.resetEvaluation();
34 34 LOG.debug("REFRESH GLOBAL EVALUATION");
35 35 }
36 36 }
... ...
skilldr-service/src/test/java/eu/totoandco/business/SkillCalculatorGetSetTest.java
... ... @@ -87,7 +87,7 @@ public class SkillCalculatorGetSetTest {
87 87 Skill skill1, skill2;
88 88 User user;
89 89 ExperienceType experienceType, experienceType2;
90   - UserSkill userSkill1, userSkill2, userSkill3;
  90 + UserSkill userSkill1, userSkill2;
91 91 final static int PERIOD = 40;
92 92 final static double COEFF = 4.0;
93 93 final static double BASE_EXPERIENCE = 230.0;
... ... @@ -131,10 +131,9 @@ public class SkillCalculatorGetSetTest {
131 131 Assert.assertTrue("No skills found for this user", !evals.isEmpty());
132 132  
133 133 // getGlobalEvaluation
134   - List<SkillEvaluation> globalEvals = skillCalculator.getGlobalEvaluation();
  134 + List<SkillEvaluation> globalEvals = skillCalculator.makeGlobalEvaluation();
135 135 Assert.assertNotNull("List SkillEvaluation is null", globalEvals);
136 136 Assert.assertTrue("No skills found for this user", !globalEvals.isEmpty());
137   -
138 137 }
139 138  
140 139 @Test
... ... @@ -153,6 +152,14 @@ public class SkillCalculatorGetSetTest {
153 152 oldBaseExperienceSkill != newBaseExperienceSkill);
154 153 }
155 154  
  155 + @Test
  156 + public void getMakeGlobalEvaluationTest(){
  157 + // makeGlobalEvaluation
  158 + List<SkillEvaluation> listSkillEvaluation = skillCalculator.makeGlobalEvaluation();
  159 + Assert.assertNotNull("List global SkillEvaluation is null", listSkillEvaluation);
  160 + Assert.assertTrue("No skills found", !listSkillEvaluation.isEmpty());
  161 + }
  162 +
156 163 @After
157 164 public void afterTest(){
158 165 userSkillModel.remove(userSkill1);
... ...
skilldr-service/src/test/java/eu/totoandco/business/SkillGlobalGetTest.java 0 โ†’ 100644
  1 +package eu.totoandco.business;
  2 +
  3 +import java.util.ArrayList;
  4 +import java.util.List;
  5 +
  6 +import javax.ejb.EJB;
  7 +
  8 +import junit.framework.Assert;
  9 +
  10 +import org.jboss.arquillian.container.test.api.Deployment;
  11 +import org.jboss.arquillian.junit.Arquillian;
  12 +import org.jboss.shrinkwrap.api.ShrinkWrap;
  13 +import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset;
  14 +import org.jboss.shrinkwrap.api.spec.WebArchive;
  15 +import org.junit.After;
  16 +import org.junit.Before;
  17 +import org.junit.Test;
  18 +import org.junit.runner.RunWith;
  19 +
  20 +import eu.totoandco.cache.GlobalEvaluationCache;
  21 +import eu.totoandco.cache.ParameterCache;
  22 +import eu.totoandco.check.ParameterCheck;
  23 +import eu.totoandco.data.Role;
  24 +import eu.totoandco.data.parameter.Parameter;
  25 +import eu.totoandco.entitymanager.ModelAccess;
  26 +import eu.totoandco.entitymanager.UserSkillAccess;
  27 +import eu.totoandco.exception.MessageException;
  28 +import eu.totoandco.model.Domain;
  29 +import eu.totoandco.model.ExperienceType;
  30 +import eu.totoandco.model.Job;
  31 +import eu.totoandco.model.Skill;
  32 +import eu.totoandco.model.User;
  33 +import eu.totoandco.model.UserSkill;
  34 +
  35 +/**
  36 + *
  37 + * @author Pierrick Knecht
  38 + *
  39 + */
  40 +@RunWith(Arquillian.class)
  41 +public class SkillGlobalGetTest {
  42 + @Deployment
  43 + public static WebArchive createTestArchive() {
  44 + WebArchive war = ShrinkWrap
  45 + .create(WebArchive.class, "test.war")
  46 + .addClasses(ModelAccess.class,
  47 + UserSkillAccess.class,
  48 + SkillCalculator.class,
  49 + SkillGlobal.class,
  50 + SkillEvaluation.class)
  51 + .addPackage(MessageException.class.getPackage())
  52 + .addPackage(Parameter.class.getPackage())
  53 + .addPackage(ParameterCheck.class.getPackage())
  54 + .addPackage(ParameterCache.class.getPackage())
  55 + .addPackage(ParameterCheck.class.getPackage())
  56 + .addPackage(Domain.class.getPackage())
  57 + .addPackage(Role.class.getPackage())
  58 + .addAsResource(
  59 + new ClassLoaderAsset("META-INF/persistence.xml"),
  60 + "META-INF/persistence.xml");
  61 + return war;
  62 + }
  63 +
  64 + @EJB
  65 + ModelAccess<User> userModel;
  66 +
  67 + @EJB
  68 + ModelAccess<Domain> domainModel;
  69 +
  70 + @EJB
  71 + ModelAccess<Job> jobModel;
  72 +
  73 + @EJB
  74 + ModelAccess<Skill>skillModel;
  75 +
  76 + @EJB
  77 + SkillGlobal skillGlobal;
  78 +
  79 + @EJB
  80 + ModelAccess<ExperienceType> experienceTypeModel;
  81 +
  82 + @EJB
  83 + UserSkillAccess userSkillModel;
  84 +
  85 + @EJB
  86 + GlobalEvaluationCache globalEvaluationCache;
  87 +
  88 + User user;
  89 + Domain domain;
  90 + Job job;
  91 + Skill skill1, skill2;
  92 + List<Integer> listIdSkill;
  93 + ExperienceType experienceType;
  94 + UserSkill userSkill1, userSkill2;
  95 + final static int PERIOD = 40;
  96 + final static double COEFF = 4.0;
  97 +
  98 + @Before
  99 + public void beforeTest(){
  100 + user = new User();
  101 + userModel.save(user);
  102 + domain = new Domain("TestDomain");
  103 + domainModel.save(domain);
  104 + job = new Job("TestJob", domain);
  105 + jobModel.save(job);
  106 + skill1 = new Skill("TestSkill1", job);
  107 + skillModel.save(skill1);
  108 + skill2 = new Skill("TestSkill2", job);
  109 + skillModel.save(skill2);
  110 + listIdSkill = new ArrayList<>();
  111 + listIdSkill.add(skill1.getId());
  112 + listIdSkill.add(skill2.getId());
  113 + experienceType = new ExperienceType("TestExperienceType", COEFF);
  114 + experienceTypeModel.save(experienceType);
  115 + userSkill1 = new UserSkill(user, skill1, PERIOD, experienceType);
  116 + userSkillModel.save(userSkill1);
  117 + userSkill2 = new UserSkill(user, skill2, PERIOD, experienceType);
  118 + userSkillModel.save(userSkill2);
  119 + globalEvaluationCache.resetEvaluation();
  120 + }
  121 +
  122 + @Test
  123 + public void getEvaluationTest(){
  124 + // getEvaluationById
  125 + System.out.println("SKILLID " + skill1.getId());
  126 + System.out.println(skillModel.get(skill1.getId(), Skill.class));
  127 + SkillEvaluation skillEvaluation = skillGlobal.getEvaluationById(skill1.getId());
  128 + Assert.assertNotNull("SkillEvaluation by id is null", skillEvaluation);
  129 + Assert.assertNotNull("SkillEvaluation's name is null", skillEvaluation.getName());
  130 + Assert.assertTrue("idSkill equals 0", skillEvaluation.getId() != 0);
  131 + Assert.assertTrue("Evaluation equals 0", skillEvaluation.getEvaluation() != 0);
  132 +
  133 + // getEvaluationMultiId
  134 + List<SkillEvaluation> evals = skillGlobal.getEvaluationMultiId(listIdSkill);
  135 + Assert.assertNotNull("List SkillEvaluation is null", evals);
  136 + Assert.assertTrue("No skills found for this user", !evals.isEmpty());
  137 +
  138 + //getEvaluationByUser
  139 + List<SkillEvaluation> evalsByUser = skillGlobal.getEvaluationsByUser(user);
  140 + Assert.assertNotNull("List SkillEvaluation is null", evalsByUser);
  141 + Assert.assertTrue("No skills found for this user", !evalsByUser.isEmpty());
  142 + }
  143 +
  144 + @After
  145 + public void afterTest(){
  146 + userSkillModel.remove(userSkill1);
  147 + userSkillModel.remove(userSkill2);
  148 + userModel.remove(user);
  149 + skillModel.remove(skill1);
  150 + skillModel.remove(skill2);
  151 + jobModel.remove(job);
  152 + domainModel.remove(domain);
  153 + experienceTypeModel.remove(experienceType);
  154 + }
  155 +}
... ...