Distributing Blueprint to DCAE Dashboard Issue-ID: DCAEGEN2-2385>
[dcaegen2/platform.git] / mod2 / catalog-service / src / test / java / org / onap / dcaegen2 / platform / mod / web / MsRequestValidationTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  org.onap.dcae
4  *  ================================================================================
5  *  Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.platform.mod.web;
22
23 import org.onap.dcaegen2.platform.mod.model.restapi.MicroserviceCreateRequest;
24 import org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother;
25 import org.assertj.core.api.Assertions;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28
29 import javax.validation.ConstraintViolation;
30 import javax.validation.Validation;
31 import javax.validation.Validator;
32 import java.util.Set;
33
34 public class MsRequestValidationTest {
35
36     public Validator validator;
37     private MicroserviceCreateRequest request;
38
39     @BeforeEach
40     public void setup(){
41         validator = Validation.buildDefaultValidatorFactory().getValidator();
42         request = BaseMsObjectMother.createMockMsRequest();
43     }
44
45     @Test
46     void test_msNameShouldNotBeBlank(){
47         request.setName("  ");
48         Set<ConstraintViolation<MicroserviceCreateRequest>> violations = validator.validate(request);
49         Assertions.assertThat(violations.size()).isEqualTo(1);
50     }
51
52     @Test
53     void test_msTagShouldNotBeNull(){
54         request.setTag(null);
55         Set<ConstraintViolation<MicroserviceCreateRequest>> violations = validator.validate(request);
56         Assertions.assertThat(violations.size()).isEqualTo(1);
57
58     }
59
60     @Test
61     void test_msTagShouldFollowRegex() throws Exception{
62         request.setTag("ms-1");
63         Set<ConstraintViolation<MicroserviceCreateRequest>> violations = validator.validate(request);
64         Assertions.assertThat(violations.size()).isEqualTo(1);
65     }
66
67     @Test
68     void test_msTagSizeValidation() throws Exception {
69         request.setTag("core-name-should-not-exceed-fifty-chars-core-name-should-not-exceed-fifty-chars");
70         Set<ConstraintViolation<MicroserviceCreateRequest>> violations = validator.validate(request);
71         Assertions.assertThat(violations.size()).isEqualTo(1);
72     }
73
74     @Test
75     void test_msServiceNameShouldFollowRegex() throws Exception{
76         request.setServiceName("ms-1");
77         Set<ConstraintViolation<MicroserviceCreateRequest>> violations = validator.validate(request);
78         Assertions.assertThat(violations.size()).isEqualTo(1);
79     }
80
81     @Test
82     void test_userShouldNotBeBlank(){
83         request.setUser("  ");
84         Set<ConstraintViolation<MicroserviceCreateRequest>> violations = validator.validate(request);
85         Assertions.assertThat(violations.size()).isEqualTo(1);
86     }
87 }