7de35b5c1319b9fab69bcd8b4342fcf76e852f99
[so.git] / asdc-controller / src / test / java / org / onap / asdc / activity / ActivitySpecsActionsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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 package org.onap.asdc.activity;
21
22 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
23 import static com.github.tomakehurst.wiremock.client.WireMock.post;
24 import static com.github.tomakehurst.wiremock.client.WireMock.put;
25 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
26 import static org.junit.Assert.*;
27 import org.junit.Test;
28 import org.onap.so.asdc.BaseTest;
29 import org.onap.so.asdc.activity.ActivitySpecsActions;
30 import org.onap.so.asdc.activity.beans.ActivitySpec;
31 import org.onap.so.asdc.activity.beans.ActivitySpecCreateResponse;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34
35 public class ActivitySpecsActionsTest extends BaseTest {
36     @Autowired
37     ActivitySpecsActions activitySpecsActions;
38
39     @Test
40     public void CreateActivitySpec_Test() throws Exception {
41         String HOSTNAME = createURLWithPort("");
42
43         ActivitySpec activitySpec = new ActivitySpec();
44         activitySpec.setName("testActivitySpec");
45         activitySpec.setDescription("Test Activity Spec");
46         ActivitySpecCreateResponse activitySpecCreateResponse = new ActivitySpecCreateResponse();
47         activitySpecCreateResponse.setId("testActivityId");
48         ObjectMapper mapper = new ObjectMapper();
49         String body = mapper.writeValueAsString(activitySpecCreateResponse);
50         wireMockServer.stubFor(post(urlPathMatching("/v1.0/activity-spec"))
51                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
52                         .withStatus(org.springframework.http.HttpStatus.OK.value()).withBody(body)));
53
54         String activitySpecId = activitySpecsActions.createActivitySpec(HOSTNAME, activitySpec);
55         assertEquals("testActivityId", activitySpecId);
56     }
57
58     @Test
59     public void CreateActivitySpecReturnsCreated_Test() throws Exception {
60         String HOSTNAME = createURLWithPort("");
61
62         ActivitySpec activitySpec = new ActivitySpec();
63         activitySpec.setName("testActivitySpec");
64         activitySpec.setDescription("Test Activity Spec");
65         ActivitySpecCreateResponse activitySpecCreateResponse = new ActivitySpecCreateResponse();
66         activitySpecCreateResponse.setId("testActivityId");
67         ObjectMapper mapper = new ObjectMapper();
68         String body = mapper.writeValueAsString(activitySpecCreateResponse);
69         wireMockServer.stubFor(post(urlPathMatching("/v1.0/activity-spec"))
70                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
71                         .withStatus(org.springframework.http.HttpStatus.CREATED.value()).withBody(body)));
72
73         String activitySpecId = activitySpecsActions.createActivitySpec(HOSTNAME, activitySpec);
74         assertEquals("testActivityId", activitySpecId);
75     }
76
77     @Test
78     public void CreateActivitySpecReturnsExists_Test() throws Exception {
79         String HOSTNAME = createURLWithPort("");
80
81         ActivitySpec activitySpec = new ActivitySpec();
82         activitySpec.setName("testActivitySpec");
83         activitySpec.setDescription("Test Activity Spec");
84         ActivitySpecCreateResponse activitySpecCreateResponse = new ActivitySpecCreateResponse();
85         activitySpecCreateResponse.setId("testActivityId");
86         ObjectMapper mapper = new ObjectMapper();
87         String body = mapper.writeValueAsString(activitySpecCreateResponse);
88         wireMockServer.stubFor(post(urlPathMatching("/v1.0/activity-spec"))
89                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
90                         .withStatus(org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY.value()).withBody(body)));
91
92         String activitySpecId = activitySpecsActions.createActivitySpec(HOSTNAME, activitySpec);
93         assertEquals(null, activitySpecId);
94     }
95
96     @Test
97     public void CertifyActivitySpec_Test() throws Exception {
98         String HOSTNAME = createURLWithPort("");
99
100         String activitySpecId = "testActivitySpec";
101         String urlPath = "/v1.0/activity-spec/testActivitySpec/versions/latest/actions";
102
103         wireMockServer.stubFor(
104                 put(urlPathMatching(urlPath)).willReturn(aResponse().withHeader("Content-Type", "application/json")
105                         .withStatus(org.springframework.http.HttpStatus.OK.value())));
106
107         boolean certificationResult = activitySpecsActions.certifyActivitySpec(HOSTNAME, activitySpecId);
108         assertTrue(certificationResult);
109     }
110
111     @Test
112     public void CertifyActivitySpecReturnsExists_Test() throws Exception {
113         String HOSTNAME = createURLWithPort("");
114
115         String activitySpecId = "testActivitySpec";
116         String urlPath = "/v1.0/activity-spec/testActivitySpec/versions/latest/actions";
117
118         wireMockServer.stubFor(
119                 put(urlPathMatching(urlPath)).willReturn(aResponse().withHeader("Content-Type", "application/json")
120                         .withStatus(org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY.value())));
121
122         boolean certificationResult = activitySpecsActions.certifyActivitySpec(HOSTNAME, activitySpecId);
123         assertFalse(certificationResult);
124     }
125
126     private String createURLWithPort(String uri) {
127         return "http://localhost:" + wireMockPort + uri;
128     }
129 }