Search in sources :

Example 16 with Project

use of org.ligoj.app.model.Project in project ligoj-api by ligoj.

the class TestAbstractConfiguredServicePlugin method prepareMock.

@SuppressWarnings("unchecked")
@BeforeEach
public void prepareMock() {
    resource = new AbstractConfiguredServicePlugin<>() {

        @Override
        public String getKey() {
            return "key";
        }

        @Override
        public Object getConfiguration(int subscription) {
            return configuration;
        }
    };
    resource.subscriptionRepository = Mockito.mock(SubscriptionRepository.class);
    resource.projectRepository = Mockito.mock(ProjectRepository.class);
    resource.securityHelper = Mockito.mock(SecurityHelper.class);
    repository = Mockito.mock(RestRepository.class);
    configuration = Mockito.mock(PluginConfiguration.class);
    configurable = Mockito.mock(NamedConfigurable.class);
    subscription = new Subscription();
    subscription.setId(33);
    project = new Project();
    project.setId(44);
    subscription.setProject(project);
    Mockito.when(resource.securityHelper.getLogin()).thenReturn("junit");
    Mockito.when(configurable.getConfiguration()).thenReturn(configuration);
    Mockito.when(configurable.getId()).thenReturn(1);
    Mockito.when(configurable.getName()).thenReturn("my-name");
    Mockito.when(configuration.getSubscription()).thenReturn(subscription);
    Mockito.when(resource.subscriptionRepository.findOneExpected(33)).thenReturn(subscription);
    Mockito.when(resource.projectRepository.findOneVisible(44, "junit")).thenReturn(project);
    Mockito.when(repository.findOneExpected(1)).thenReturn(configurable);
    Mockito.when(repository.findAllBy("configuration.subscription.id", subscription.getId(), new String[] { "name" }, "my-name")).thenReturn(Collections.singletonList(configurable));
}
Also used : SubscriptionRepository(org.ligoj.app.dao.SubscriptionRepository) ProjectRepository(org.ligoj.app.dao.ProjectRepository) Project(org.ligoj.app.model.Project) PluginConfiguration(org.ligoj.app.model.PluginConfiguration) Subscription(org.ligoj.app.model.Subscription) RestRepository(org.ligoj.bootstrap.core.dao.RestRepository) SecurityHelper(org.ligoj.bootstrap.core.security.SecurityHelper) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 17 with Project

use of org.ligoj.app.model.Project in project ligoj-api by ligoj.

the class ToVoConverterTest method applyEmpty.

@Test
public void applyEmpty() {
    final ToVoConverter converter = new ToVoConverter(s -> null, new ArrayList<>(), new HashMap<>());
    final Project entity = new Project();
    entity.setSubscriptions(Collections.emptyList());
    final ProjectVo vo = converter.apply(entity);
    Assertions.assertNull(vo.getName());
    Assertions.assertNull(vo.getPkey());
    Assertions.assertNull(vo.getCreatedBy());
    Assertions.assertNull(vo.getCreatedDate());
    Assertions.assertNull(vo.getLastModifiedBy());
    Assertions.assertNull(vo.getLastModifiedDate());
    Assertions.assertNull(vo.getTeamLeader());
    Assertions.assertNull(vo.getId());
    Assertions.assertTrue(vo.getSubscriptions().isEmpty());
}
Also used : Project(org.ligoj.app.model.Project) Test(org.junit.jupiter.api.Test)

Example 18 with Project

use of org.ligoj.app.model.Project in project ligoj-api by ligoj.

the class ProjectResource method update.

/**
 * Update project. Should be protected with RBAC.
 *
 * @param vo
 *            the object to save.
 */
@PUT
public void update(final ProjectEditionVo vo) {
    // pkey can't be updated if there is at least subscription.
    final Project project = repository.findOneExpected(vo.getId());
    final long nbSubscriptions = subscriptionRepository.countByProject(vo.getId());
    if (nbSubscriptions == 0) {
        project.setPkey(vo.getPkey());
    }
    DescribedBean.copy(vo, project);
    project.setTeamLeader(vo.getTeamLeader());
    repository.saveAndFlush(project);
}
Also used : Project(org.ligoj.app.model.Project) PUT(javax.ws.rs.PUT)

Example 19 with Project

use of org.ligoj.app.model.Project in project ligoj-api by ligoj.

the class ProjectResource method delete.

/**
 * Delete entity. Should be protected with RBAC.
 *
 * @param id
 *            The entity identifier.
 */
@DELETE
@Path("{id:\\d+}")
public void delete(@PathParam("id") final int id) throws Exception {
    final Project project = findOneVisible(id, Function.identity());
    for (final Subscription subscription : project.getSubscriptions()) {
        subscriptionResource.delete(subscription.getId());
    }
    repository.delete(project);
}
Also used : Project(org.ligoj.app.model.Project) Subscription(org.ligoj.app.model.Subscription) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 20 with Project

use of org.ligoj.app.model.Project in project ligoj-api by ligoj.

the class SubscriptionResource method create.

/**
 * Create subscription.
 *
 * @param vo
 *            the subscription.
 * @return the created {@link Subscription}.
 * @throws Exception
 *             When the creat fails. Managed at JAX-RS level.
 */
@POST
public int create(final SubscriptionEditionVo vo) throws Exception {
    // Validate entities
    final Project project = checkVisibleProject(vo.getProject());
    checkManagedProject(vo.getProject());
    final Node node = checkManagedNodeForSubscription(vo.getNode());
    final List<Parameter> acceptedParameters = checkInputParameters(vo);
    // Create subscription and parameters that would be removed in case of
    // roll-back because of invalid parameters
    final Subscription entity = toEntity(vo, project, node);
    // Expose the real entity for plug-in since we have loaded it
    entity.setProject(project);
    // Save this subscription in the transaction
    repository.saveAndFlush(entity);
    parameterValueResource.create(vo.getParameters(), entity);
    // Delegate to the related plug-in the next process
    delegateToPlugin(vo, entity);
    // Check again the parameters in the final state
    checkMandatoryParameters(vo.getParameters(), acceptedParameters, SubscriptionMode.CREATE);
    log.info("Subscription of project {} to service {}", vo.getProject(), vo.getNode());
    return entity.getId();
}
Also used : Project(org.ligoj.app.model.Project) Node(org.ligoj.app.model.Node) Parameter(org.ligoj.app.model.Parameter) Subscription(org.ligoj.app.model.Subscription) POST(javax.ws.rs.POST)

Aggregations

Project (org.ligoj.app.model.Project)21 Test (org.junit.jupiter.api.Test)12 AbstractOrgTest (org.ligoj.app.resource.AbstractOrgTest)9 Subscription (org.ligoj.app.model.Subscription)6 SubscriptionVo (org.ligoj.app.resource.subscription.SubscriptionVo)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Node (org.ligoj.app.model.Node)2 Parameter (org.ligoj.app.model.Parameter)2 ParameterValue (org.ligoj.app.model.ParameterValue)2 EventVo (org.ligoj.app.resource.node.EventVo)2 Comparator (java.util.Comparator)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 DELETE (javax.ws.rs.DELETE)1