Skip to content

Commit 5ec377a

Browse files
List companies (#13)
* change Query parameter * Add id to CompanyRepresentation * Add delete company
1 parent 552d4aa commit 5ec377a

File tree

9 files changed

+136
-16
lines changed

9 files changed

+136
-16
lines changed

src/main/java/io/github/project/openubl/xsender/idm/CompanyRepresentation.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020

2121
import javax.validation.Valid;
2222
import javax.validation.constraints.NotNull;
23+
import javax.validation.constraints.Pattern;
2324

2425
@RegisterForReflection
2526
public class CompanyRepresentation {
2627

28+
private String id;
29+
30+
@Pattern(regexp = "[a-z0-9]([-a-z0-9]*[a-z0-9])?", message = "label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc')")
2731
@NotNull
2832
private String name;
2933

@@ -35,6 +39,14 @@ public class CompanyRepresentation {
3539
@Valid
3640
private SunatCredentialsRepresentation credentials;
3741

42+
public String getId() {
43+
return id;
44+
}
45+
46+
public void setId(String id) {
47+
this.id = id;
48+
}
49+
3850
public String getName() {
3951
return name;
4052
}

src/main/java/io/github/project/openubl/xsender/managers/CompanyManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public CompanyEntity createCompany(String owner, CompanyRepresentation rep) {
4444
companyEntity.setId(UUID.randomUUID().toString());
4545

4646
companyEntity.setOwner(owner);
47-
companyEntity.setName(rep.getName());
47+
companyEntity.setName(rep.getName().toLowerCase());
4848

4949
if (rep.getWebServices() != null) {
5050
SunatUrlsEntity sunatUrlsEntity = new SunatUrlsEntity();

src/main/java/io/github/project/openubl/xsender/models/utils/EntityToRepresentation.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ private EntityToRepresentation() {
4141
public static CompanyRepresentation toRepresentation(CompanyEntity entity) {
4242
CompanyRepresentation rep = new CompanyRepresentation();
4343

44+
rep.setId(entity.getId());
4445
rep.setName(entity.getName());
4546

4647
if (entity.getSunatUrls() != null) {

src/main/java/io/github/project/openubl/xsender/resources/CompanyResource.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ CompanyRepresentation updateCompany(
5050
@NotNull @Valid CompanyRepresentation rep
5151
);
5252

53+
@DELETE
54+
@Path("/{company}")
55+
void deleteCompany(@PathParam("company") @NotNull String company);
5356

5457
/**
5558
* Change SUNAT credentials of a company

src/main/java/io/github/project/openubl/xsender/resources/CurrentUserResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public interface CurrentUserResource {
4141
@GET
4242
@Path("/companies")
4343
PageRepresentation<CompanyRepresentation> getCompanies(
44-
@QueryParam("name") String name,
44+
@QueryParam("filterText") String filterText,
4545
@QueryParam("offset") @DefaultValue("0") Integer offset,
4646
@QueryParam("limit") @DefaultValue("10") Integer limit,
47-
@QueryParam("sort_by") @DefaultValue("name") List<String> sortBy
47+
@QueryParam("sort_by") List<String> sortBy
4848
);
4949
}
5050

src/main/java/io/github/project/openubl/xsender/resources/DefaultCompanyResource.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,23 @@ public class DefaultCompanyResource implements CompanyResource {
6767
@Inject
6868
CompanyManager companyManager;
6969

70-
public CompanyRepresentation getCompany(String org) {
71-
CompanyEntity organizationEntity = companyRepository.findByName(org).orElseThrow(NotFoundException::new);
70+
public CompanyRepresentation getCompany(String company) {
71+
CompanyEntity organizationEntity = companyRepository.findByName(company).orElseThrow(NotFoundException::new);
7272
return EntityToRepresentation.toRepresentation(organizationEntity);
7373
}
7474

75-
public CompanyRepresentation updateCompany(String org, CompanyRepresentation rep) {
76-
CompanyEntity organizationEntity = companyRepository.findByName(org).orElseThrow(NoClassDefFoundError::new);
75+
public CompanyRepresentation updateCompany(String company, CompanyRepresentation rep) {
76+
CompanyEntity organizationEntity = companyRepository.findByName(company).orElseThrow(NotFoundException::new);
7777
organizationEntity = companyManager.updateCompany(rep, organizationEntity);
7878
return EntityToRepresentation.toRepresentation(organizationEntity);
7979
}
8080

81+
@Override
82+
public void deleteCompany(@NotNull String company) {
83+
CompanyEntity organizationEntity = companyRepository.findByName(company).orElseThrow(NotFoundException::new);
84+
companyRepository.deleteById(organizationEntity.getId());
85+
}
86+
8187
public void updateCompanySUNATCredentials(String org, SunatCredentialsRepresentation rep) {
8288
CompanyEntity organizationEntity = companyRepository.findByName(org).orElseThrow(NoClassDefFoundError::new);
8389
companyManager.updateCorporateCredentials(rep, organizationEntity);

src/main/java/io/github/project/openubl/xsender/resources/DefaultCurrentUserResource.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Response createCompany(CompanyRepresentation rep) {
7373
}
7474

7575
public PageRepresentation<CompanyRepresentation> getCompanies(
76-
String name,
76+
String filterText,
7777
Integer offset,
7878
Integer limit,
7979
List<String> sortBy
@@ -87,15 +87,15 @@ public PageRepresentation<CompanyRepresentation> getCompanies(
8787
List<SortBean> sortBeans = ResourceUtils.getSortBeans(sortBy, CompanyRepository.SORT_BY_FIELDS);
8888

8989
PageModel<CompanyEntity> pageModel;
90-
if (name != null && !name.trim().isEmpty()) {
91-
pageModel = CompanyRepository.list(contextBean.getUsername(), name, pageBean, sortBeans);
90+
if (filterText != null && !filterText.trim().isEmpty()) {
91+
pageModel = CompanyRepository.list(contextBean.getUsername(), filterText, pageBean, sortBeans);
9292
} else {
9393
pageModel = CompanyRepository.list(contextBean.getUsername(), pageBean, sortBeans);
9494
}
9595

9696
List<NameValuePair> queryParameters = ResourceUtils.buildNameValuePairs(offset, limit, sortBeans);
97-
if (name != null) {
98-
queryParameters.add(new BasicNameValuePair("name", name));
97+
if (filterText != null) {
98+
queryParameters.add(new BasicNameValuePair("name", filterText));
9999
}
100100

101101
try {
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Eclipse Public License - v 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.eclipse.org/legal/epl-2.0/
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.github.project.openubl.xsender.basic.resources.basic;
18+
19+
import com.fasterxml.jackson.core.JsonProcessingException;
20+
import com.fasterxml.jackson.databind.ObjectMapper;
21+
import io.github.project.openubl.xsender.idm.CompanyRepresentation;
22+
import io.github.project.openubl.xsender.idm.SunatCredentialsRepresentation;
23+
import io.github.project.openubl.xsender.idm.SunatUrlsRepresentation;
24+
import io.github.project.openubl.xsender.models.jpa.CompanyRepository;
25+
import io.github.project.openubl.xsender.models.jpa.entities.CompanyEntity;
26+
import io.github.project.openubl.xsender.models.jpa.entities.SunatCredentialsEntity;
27+
import io.github.project.openubl.xsender.models.jpa.entities.SunatUrlsEntity;
28+
import io.github.project.openubl.xsender.security.UserIdentity;
29+
import io.quarkus.test.junit.QuarkusTest;
30+
import org.junit.jupiter.api.AfterEach;
31+
import org.junit.jupiter.api.Test;
32+
33+
import javax.inject.Inject;
34+
import java.util.Optional;
35+
import java.util.UUID;
36+
37+
import static io.restassured.RestAssured.given;
38+
import static org.hamcrest.CoreMatchers.*;
39+
import static org.junit.jupiter.api.Assertions.*;
40+
41+
@QuarkusTest
42+
public class DefaultCompanyResourceTest {
43+
44+
@Inject
45+
CompanyRepository companyRepository;
46+
47+
@AfterEach
48+
public void afterEach() {
49+
companyRepository.deleteAll();
50+
}
51+
52+
@Test
53+
public void deleteCompany() throws JsonProcessingException {
54+
// Given
55+
final String COMPANY_NAME = "myCompany";
56+
57+
CompanyRepresentation company = CompanyRepresentation.Builder.aCompanyRepresentation()
58+
.withName(COMPANY_NAME)
59+
.withWebServices(SunatUrlsRepresentation.Builder.aSunatUrlsRepresentation()
60+
.withFactura("http://url1.com")
61+
.withGuia("http://url2.com")
62+
.withRetenciones("http://url3.com")
63+
.build()
64+
)
65+
.withCredentials(SunatCredentialsRepresentation.Builder.aSunatCredentialsRepresentation()
66+
.withUsername("myUsername")
67+
.withPassword("myPassword")
68+
.build()
69+
)
70+
.build();
71+
72+
String body = new ObjectMapper().writeValueAsString(company);
73+
74+
// When
75+
given()
76+
.body(body)
77+
.header("Content-Type", "application/json")
78+
.when()
79+
.post("/api/user/companies")
80+
.then()
81+
.statusCode(200)
82+
.body("name", is(company.getName().toLowerCase()));
83+
84+
// Then
85+
Optional<CompanyEntity> companyOptional = companyRepository.findByName(COMPANY_NAME.toLowerCase());
86+
assertTrue(companyOptional.isPresent());
87+
88+
CompanyEntity companyDB = companyOptional.get();
89+
assertEquals(companyDB.getName(), COMPANY_NAME.toLowerCase());
90+
assertEquals(companyDB.getOwner(), UserIdentity.DEFAULT_USERNAME);
91+
assertEquals(companyDB.getSunatUrls().getSunatUrlFactura(), "http://url1.com");
92+
assertEquals(companyDB.getSunatUrls().getSunatUrlGuiaRemision(), "http://url2.com");
93+
assertEquals(companyDB.getSunatUrls().getSunatUrlPercepcionRetencion(), "http://url3.com");
94+
assertEquals(companyDB.getSunatCredentials().getSunatUsername(), "myUsername");
95+
assertEquals(companyDB.getSunatCredentials().getSunatPassword(), "myPassword");
96+
}
97+
98+
}

src/test/java/io/github/project/openubl/xsender/basic/resources/basic/DefaultCurrentUserResourceTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void afterEach() {
5151
}
5252

5353
@Test
54-
public void createCompany() throws JsonProcessingException {
54+
public void createCompanyLowerCasingName() throws JsonProcessingException {
5555
// Given
5656
final String COMPANY_NAME = "myCompany";
5757

@@ -80,14 +80,14 @@ public void createCompany() throws JsonProcessingException {
8080
.post("/api/user/companies")
8181
.then()
8282
.statusCode(200)
83-
.body("name", is(company.getName()));
83+
.body("name", is(company.getName().toLowerCase()));
8484

8585
// Then
86-
Optional<CompanyEntity> companyOptional = companyRepository.findByName(COMPANY_NAME);
86+
Optional<CompanyEntity> companyOptional = companyRepository.findByName(COMPANY_NAME.toLowerCase());
8787
assertTrue(companyOptional.isPresent());
8888

8989
CompanyEntity companyDB = companyOptional.get();
90-
assertEquals(companyDB.getName(), COMPANY_NAME);
90+
assertEquals(companyDB.getName(), COMPANY_NAME.toLowerCase());
9191
assertEquals(companyDB.getOwner(), UserIdentity.DEFAULT_USERNAME);
9292
assertEquals(companyDB.getSunatUrls().getSunatUrlFactura(), "http://url1.com");
9393
assertEquals(companyDB.getSunatUrls().getSunatUrlGuiaRemision(), "http://url2.com");

0 commit comments

Comments
 (0)